lordgamez commented on code in PR #1978:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1978#discussion_r2198140043


##########
extensions/standard-processors/tests/unit/EvaluateJsonPathTests.cpp:
##########
@@ -0,0 +1,324 @@
+/**
+ * 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.
+ */
+#include "unit/TestBase.h"
+#include "unit/Catch.h"
+#include "unit/SingleProcessorTestController.h"
+#include "processors/EvaluateJsonPath.h"
+#include "unit/TestUtils.h"
+
+namespace org::apache::nifi::minifi::test {
+
+class EvaluateJsonPathTestFixture {
+ public:
+  EvaluateJsonPathTestFixture() :
+      
controller_(std::make_unique<processors::EvaluateJsonPath>("EvaluateJsonPath")),
+      
evaluate_json_path_processor_(dynamic_cast<processors::EvaluateJsonPath*>(controller_.getProcessor()))
 {
+    REQUIRE(evaluate_json_path_processor_);
+    LogTestController::getInstance().setTrace<processors::EvaluateJsonPath>();
+  }
+
+ protected:
+  SingleProcessorTestController controller_;
+  processors::EvaluateJsonPath* evaluate_json_path_processor_;
+};
+
+TEST_CASE_METHOD(EvaluateJsonPathTestFixture, "At least one dynamic property 
must be specified", "[EvaluateJsonPathTests]") {
+  REQUIRE_THROWS_WITH(controller_.trigger({{.content = "foo"}}), "Process 
Schedule Operation: At least one dynamic property must be specified with a 
valid JSON path expression");
+}
+
+TEST_CASE_METHOD(EvaluateJsonPathTestFixture, "When destination is set to 
flowfile content only one dynamic property is allowed", 
"[EvaluateJsonPathTests]") {
+  controller_.plan->setProperty(evaluate_json_path_processor_, 
processors::EvaluateJsonPath::Destination, "flowfile-content");
+  controller_.plan->setDynamicProperty(evaluate_json_path_processor_, 
"attribute1", "value1");
+  controller_.plan->setDynamicProperty(evaluate_json_path_processor_, 
"attribute2", "value2");
+  REQUIRE_THROWS_WITH(controller_.trigger({{.content = "foo"}}), "Process 
Schedule Operation: Only one dynamic property is allowed for JSON path when 
destination is set to flowfile-content");
+}
+
+TEST_CASE_METHOD(EvaluateJsonPathTestFixture, "Input flowfile has invalid JSON 
as content", "[EvaluateJsonPathTests]") {
+  ProcessorTriggerResult result;
+  std::string error_log;
+  controller_.plan->setDynamicProperty(evaluate_json_path_processor_, 
"attribute1", "value1");
+  SECTION("Flow file content is empty") {
+    result = controller_.trigger({{.content = ""}});
+    error_log = "FlowFile content is empty, transferring to Failure 
relationship";
+  }
+
+  SECTION("Flow file content is invalid json") {
+    result = controller_.trigger({{.content = "invalid json"}});
+    error_log = "FlowFile content is not a valid JSON document, transferring 
to Failure relationship";
+  }
+
+  CHECK(result.at(processors::EvaluateJsonPath::Matched).empty());
+  CHECK(result.at(processors::EvaluateJsonPath::Unmatched).empty());
+  CHECK(result.at(processors::EvaluateJsonPath::Failure).size() == 1);
+  CHECK(utils::verifyLogLinePresenceInPollTime(1s, error_log));
+}
+
+TEST_CASE_METHOD(EvaluateJsonPathTestFixture, "Dynamic property contains 
invalid JSON path expression", "[EvaluateJsonPathTests]") {
+  controller_.plan->setDynamicProperty(evaluate_json_path_processor_, 
"attribute", "1234");
+
+  auto result = controller_.trigger({{.content = "{}"}});
+
+  REQUIRE(result.at(processors::EvaluateJsonPath::Matched).empty());
+  REQUIRE(result.at(processors::EvaluateJsonPath::Unmatched).empty());
+  REQUIRE(result.at(processors::EvaluateJsonPath::Failure).size() == 1);
+
+  const auto result_flow_file = 
result.at(processors::EvaluateJsonPath::Failure).at(0);
+
+  CHECK(controller_.plan->getContent(result_flow_file) == "{}");
+  CHECK(utils::verifyLogLinePresenceInPollTime(0s, "Invalid JSON path 
expression '1234' found for attribute key 'attribute'"));
+}
+
+TEST_CASE_METHOD(EvaluateJsonPathTestFixture, "JSON paths are not found in 
content when destination is set to attribute", "[EvaluateJsonPathTests]") {
+  controller_.plan->setDynamicProperty(evaluate_json_path_processor_, 
"attribute1", "$.firstName");
+  controller_.plan->setDynamicProperty(evaluate_json_path_processor_, 
"attribute2", "$.lastName");
+
+  std::map<std::string, std::string> expected_attributes = {
+    {"attribute1", ""},
+    {"attribute2", ""}
+  };
+
+  bool warn_path_not_found_behavior = false;
+  bool expect_attributes = false;
+
+  SECTION("Ignore path not found behavior") {
+    controller_.plan->setProperty(evaluate_json_path_processor_, 
processors::EvaluateJsonPath::PathNotFoundBehavior, "ignore");
+    expect_attributes = true;
+  }
+
+  SECTION("Skip path not found behavior") {
+    controller_.plan->setProperty(evaluate_json_path_processor_, 
processors::EvaluateJsonPath::PathNotFoundBehavior, "skip");
+  }
+
+  SECTION("Warn path not found behavior") {
+    controller_.plan->setProperty(evaluate_json_path_processor_, 
processors::EvaluateJsonPath::PathNotFoundBehavior, "warn");
+    warn_path_not_found_behavior = true;
+    expect_attributes = true;
+  }
+
+  auto result = controller_.trigger({{.content = "{}"}});
+
+  REQUIRE(result.at(processors::EvaluateJsonPath::Matched).size() == 1);
+  REQUIRE(result.at(processors::EvaluateJsonPath::Unmatched).empty());
+  REQUIRE(result.at(processors::EvaluateJsonPath::Failure).empty());
+
+  const auto result_flow_file = 
result.at(processors::EvaluateJsonPath::Matched).at(0);
+
+  CHECK(controller_.plan->getContent(result_flow_file) == "{}");
+
+  for (const auto& [key, value] : expected_attributes) {
+    if (!expect_attributes) {
+      CHECK_FALSE(result_flow_file->getAttribute(key));
+    } else {
+      CHECK(*result_flow_file->getAttribute(key) == value);

Review Comment:
   Updated in 
https://github.com/apache/nifi-minifi-cpp/pull/1978/commits/b4b85218fe8fa02d08256a1770967ce2d2ff5331



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to