bakaid commented on a change in pull request #605: MINIFICPP-550 - Implement 
RocksDB controller service and component st…
URL: https://github.com/apache/nifi-minifi-cpp/pull/605#discussion_r389759366
 
 

 ##########
 File path: 
libminifi/test/keyvalue-tests/PersistableKeyValueStoreServiceTest.cpp
 ##########
 @@ -0,0 +1,257 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define CATCH_CONFIG_RUNNER
+#include "catch.hpp"
+
+#include <uuid/uuid.h>
+#include <vector>
+#include <memory>
+#include <utility>
+#include <string>
+#include <array>
+#include <unistd.h>
+#include "../TestBase.h"
+#include "../../controller/Controller.h"
+#include "core/controller/ControllerService.h"
+#include "core/ProcessGroup.h"
+#include "core/yaml/YamlConfiguration.h"
+#include "controllers/keyvalue/PersistableKeyValueStoreService.h"
+#include "controllers/keyvalue/AbstractAutoPersistingKeyValueStoreService.h"
+
+static std::string config_yaml; // NOLINT
+
+static inline void configYamlHandler(Catch::ConfigData&, const std::string& 
path) {
+  config_yaml = path;
+}
+
+int main(int argc, char* argv[]) {
+  Catch::Session session;
+
+  Catch::Clara::CommandLine<Catch::ConfigData>& cli = 
const_cast<Catch::Clara::CommandLine<Catch::ConfigData>&>(session.cli());
+  cli["--config-yaml"]
+      .describe("path to the config.yaml containing the 
PersistableKeyValueStoreService controller service configuration")
+      .bind(&configYamlHandler, "path");
+
+  int ret = session.applyCommandLine(argc, argv);
+  if (ret != 0) {
+    return ret;
+  }
+
+  if (config_yaml.empty()) {
+    std::cerr << "Missing --config-yaml <path>. It must contain the path to 
the config.yaml containing the PersistableKeyValueStoreService controller 
service configuration." << std::endl;
+    return -1;
+  }
+
+  return session.run();
+}
+
+class PersistableKeyValueStoreServiceTestsFixture {
+ public:
+  PersistableKeyValueStoreServiceTestsFixture()
+      : state_dir(strdup("/tmp/state.XXXXXX")) {
+    LogTestController::getInstance().setTrace<TestPlan>();
+    
LogTestController::getInstance().setTrace<minifi::controllers::PersistableKeyValueStoreService>();
+    
LogTestController::getInstance().setTrace<minifi::controllers::AbstractAutoPersistingKeyValueStoreService>();
+
+    // Create temporary directories
+    testController.createTempDirectory(state_dir, false /*cleanup*/);
+    REQUIRE(state_dir != nullptr);
+    REQUIRE(0 == chdir(state_dir));
+
+    loadYaml();
+  }
+
+  virtual ~PersistableKeyValueStoreServiceTestsFixture() {
+    free(state_dir);
+    LogTestController::getInstance().reset();
+  }
+
+  void loadYaml() {
+    controller.reset();
+    persistable_key_value_store_service_node.reset();
+
+    process_group.reset();
+    yaml_config.reset();
+
+    stream_factory.reset();
+    content_repo.reset();
+    test_flow_repo.reset();
+    test_repo.reset();
+    configuration.reset();
+
+    configuration = std::make_shared<minifi::Configure>();
+    test_repo = std::make_shared<TestRepository>();
+    test_flow_repo = std::make_shared<TestFlowRepository>();
+
+    configuration->set(minifi::Configure::nifi_flow_configuration_file, 
config_yaml);
+
+    content_repo = 
std::make_shared<core::repository::VolatileContentRepository>();
+    content_repo->initialize(configuration);
+    stream_factory = minifi::io::StreamFactory::getInstance(configuration);
+
+    yaml_config = std::unique_ptr<core::YamlConfiguration>(new 
core::YamlConfiguration(test_repo, test_repo, content_repo, stream_factory, 
configuration, config_yaml));
+
+    process_group = yaml_config->getRoot(config_yaml);
+    persistable_key_value_store_service_node = 
process_group->findControllerService("testcontroller");
+    REQUIRE(persistable_key_value_store_service_node != nullptr);
+    persistable_key_value_store_service_node->enable();
+
+    controller = 
std::dynamic_pointer_cast<minifi::controllers::PersistableKeyValueStoreService>(
+        
persistable_key_value_store_service_node->getControllerServiceImplementation());
+    REQUIRE(controller != nullptr);
+  }
+
+ protected:
+  char *state_dir;
+  std::shared_ptr<minifi::Configure> configuration;
+  std::shared_ptr<core::Repository> test_repo;
+  std::shared_ptr<core::Repository> test_flow_repo;
+  std::shared_ptr<core::ContentRepository> content_repo;
+  std::shared_ptr<minifi::io::StreamFactory> stream_factory;
+
+  std::unique_ptr<core::YamlConfiguration> yaml_config;
+  std::unique_ptr<core::ProcessGroup> process_group;
+
+  std::shared_ptr<core::controller::ControllerServiceNode> 
persistable_key_value_store_service_node;
+  std::shared_ptr<minifi::controllers::PersistableKeyValueStoreService> 
controller;
+
+  TestController testController;
+};
+
+
+TEST_CASE_METHOD(PersistableKeyValueStoreServiceTestsFixture, 
"PersistableKeyValueStoreServiceTestsFixture set and get", "[basic]") {
+  const char* key = "foobar";
+  const char* value = "234";
+  REQUIRE(true == controller->set(key, value));
+
+  SECTION("without persistence") {
+  }
+  SECTION("with persistence") {
+    controller->persist();
+    loadYaml();
+  }
+
+  std::string res;
+  REQUIRE(true == controller->get(key, res));
+  REQUIRE(value == res);
+}
+
+TEST_CASE_METHOD(PersistableKeyValueStoreServiceTestsFixture, 
"PersistableKeyValueStoreServiceTestsFixture special characters", "[basic]") {
+  const char* key = "[]{}()==\\=\n\n";
+  const char* value = ":./'\\=!\n=[]{}()";
+  REQUIRE(true == controller->set(key, value));
+
+  SECTION("without persistence") {
+  }
+  SECTION("with persistence") {
+    controller->persist();
+    loadYaml();
+  }
+
+  std::string res;
+  REQUIRE(true == controller->get(key, res));
+  REQUIRE(value == res);
+}
+
+TEST_CASE_METHOD(PersistableKeyValueStoreServiceTestsFixture, 
"PersistableKeyValueStoreServiceTestsFixture set and get all", "[basic]") {
+  const std::unordered_map<std::string, std::string> kvs = {
+      {"foobar", "234"},
+      {"buzz", "value"},
+  };
+  for (const auto& kv : kvs) {
+    REQUIRE(true == controller->set(kv.first, kv.second));
+  }
+
+  SECTION("without persistence") {
+  }
+  SECTION("with persistence") {
+    controller->persist();
+    loadYaml();
+  }
+
+  std::unordered_map<std::string, std::string> kvs_res;
+  REQUIRE(true == controller->get(kvs_res));
+  REQUIRE(kvs == kvs_res);
+}
+
+TEST_CASE_METHOD(PersistableKeyValueStoreServiceTestsFixture, 
"PersistableKeyValueStoreServiceTestsFixture set and overwrite", "[basic]") {
+  const char* key = "foobar";
+  const char* value = "234";
+  const char* new_value = "baz";
+  REQUIRE(true == controller->set(key, value));
 
 Review comment:
   Yep, but in tests I think it is better readable.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to