lordgamez commented on code in PR #1980: URL: https://github.com/apache/nifi-minifi-cpp/pull/1980#discussion_r2351807014
########## extensions/standard-processors/tests/unit/SplitJsonTests.cpp: ########## @@ -0,0 +1,150 @@ +/** + * 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/SplitJson.h" +#include "unit/TestUtils.h" +#include "unit/ProcessorUtils.h" + +namespace org::apache::nifi::minifi::test { + +class SplitJsonTestFixture { + public: + SplitJsonTestFixture() : + controller_(utils::make_processor<processors::SplitJson>("SplitJson")), + split_json_processor_(controller_.getProcessor()) { + REQUIRE(split_json_processor_); + LogTestController::getInstance().setTrace<processors::SplitJson>(); + } + + protected: + void verifySuccessfulSplit(const std::string& input_json_content, const std::string& json_path_expression, const std::vector<std::string>& expected_split_contents) { + REQUIRE(controller_.plan->setProperty(split_json_processor_, processors::SplitJson::JsonPathExpression, json_path_expression)); + + auto result = controller_.trigger({{.content = input_json_content, .attributes = {{"filename", "unique_file_name"}}}}); + + CHECK(result.at(processors::SplitJson::Failure).empty()); + REQUIRE(result.at(processors::SplitJson::Original).size() == 1); + auto original_flow_file = result.at(processors::SplitJson::Original).at(0); + CHECK(controller_.plan->getContent(original_flow_file) == input_json_content); + auto original_fragment_id = original_flow_file->getAttribute(processors::SplitJson::FragmentIdentifier.name); + CHECK_FALSE(original_fragment_id->empty()); + auto fragment_count = original_flow_file->getAttribute(processors::SplitJson::FragmentCount.name); + CHECK(fragment_count.value() == std::to_string(expected_split_contents.size())); + + REQUIRE(result.at(processors::SplitJson::Split).size() == expected_split_contents.size()); + for (size_t i = 0; i < expected_split_contents.size(); ++i) { + auto flow_file = result.at(processors::SplitJson::Split).at(i); + CHECK(controller_.plan->getContent(flow_file) == expected_split_contents[i]); + auto fragment_id = flow_file->getAttribute(processors::SplitJson::FragmentIdentifier.name); + CHECK(fragment_id.value() == original_fragment_id.value()); + CHECK(flow_file->getAttribute(processors::SplitJson::FragmentCount.name).value() == std::to_string(expected_split_contents.size())); + CHECK(flow_file->getAttribute(processors::SplitJson::FragmentIndex.name).value() == std::to_string(i)); + CHECK(flow_file->getAttribute(processors::SplitJson::SegmentOriginalFilename.name).value() == "unique_file_name"); + CHECK_FALSE(flow_file->getAttribute(core::SpecialFlowAttribute::FILENAME).value() == "unique_file_name"); + } + } + + SingleProcessorTestController controller_; + core::Processor* split_json_processor_; +}; + +TEST_CASE_METHOD(SplitJsonTestFixture, "Query fails with parsing issues", "[SplitJsonTests]") { + ProcessorTriggerResult result; + std::string error_log; + REQUIRE(controller_.plan->setProperty(split_json_processor_, processors::SplitJson::JsonPathExpression, "invalid json path")); + 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"; + } + + SECTION("Json Path expression is invalid") { + result = controller_.trigger({{.content = "{}"}}); + error_log = "Invalid JSON path expression 'invalid json path' set in the 'JsonPath Expression' property:"; + } + + CHECK(result.at(processors::SplitJson::Original).empty()); + CHECK(result.at(processors::SplitJson::Split).empty()); + CHECK(result.at(processors::SplitJson::Failure).size() == 1); + CHECK(utils::verifyLogLinePresenceInPollTime(1s, error_log)); +} + +TEST_CASE_METHOD(SplitJsonTestFixture, "Query does not match input JSON content", "[SplitJsonTests]") { + REQUIRE(controller_.plan->setProperty(split_json_processor_, processors::SplitJson::JsonPathExpression, "$.email")); + + std::string input_json; + SECTION("Flow file content does not contain the specified path") { + input_json = R"({"name": "John"})"; + } + + SECTION("Flow file content null") { + input_json = "null"; + } + + auto result = controller_.trigger({{.content = input_json}}); + + CHECK(result.at(processors::SplitJson::Original).empty()); + CHECK(result.at(processors::SplitJson::Split).empty()); + CHECK(result.at(processors::SplitJson::Failure).size() == 1); + CHECK(utils::verifyLogLinePresenceInPollTime(1s, "JSON Path expression '$.email' did not match the input flow file content, transferring to Failure relationship")); Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/1980/commits/0aa867f69a964c3d3f0a1562750bd81218fc6434 ########## extensions/standard-processors/processors/SplitJson.cpp: ########## @@ -0,0 +1,133 @@ +/** + * 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 "SplitJson.h" + +#include <unordered_map> + +#include "core/ProcessSession.h" +#include "core/ProcessContext.h" +#include "core/Resource.h" +#include "utils/ProcessorConfigUtils.h" +#include "utils/Id.h" + +#include "jsoncons_ext/jsonpath/jsonpath.hpp" + +namespace org::apache::nifi::minifi::processors { + +void SplitJson::initialize() { + setSupportedProperties(Properties); + setSupportedRelationships(Relationships); +} + +void SplitJson::onSchedule(core::ProcessContext& context, core::ProcessSessionFactory&) { + json_path_expression_ = utils::parseProperty(context, SplitJson::JsonPathExpression); + null_value_representation_ = utils::parseEnumProperty<split_json::NullValueRepresentationOption>(context, SplitJson::NullValueRepresentation); +} + +std::optional<jsoncons::json> SplitJson::queryArrayUsingJsonPath(core::ProcessSession& session, const std::shared_ptr<core::FlowFile>& flow_file) const { + const auto json_string = to_string(session.readBuffer(flow_file)); + if (json_string.empty()) { + logger_->log_error("FlowFile content is empty, transferring to Failure relationship"); + return std::nullopt; + } + + jsoncons::json json_object; + try { + json_object = jsoncons::json::parse(json_string); + } catch (const jsoncons::json_exception& e) { + logger_->log_error("FlowFile content is not a valid JSON document, transferring to Failure relationship: {}", e.what()); + return std::nullopt; + } + + jsoncons::json query_result; + try { + query_result = jsoncons::jsonpath::json_query(json_object, json_path_expression_); + } catch (const jsoncons::jsonpath::jsonpath_error& e) { + logger_->log_error("Invalid JSON path expression '{}' set in the 'JsonPath Expression' property: {}", json_path_expression_, e.what()); + return std::nullopt; + } + + return query_result; +} + +std::string SplitJson::jsonValueToString(const jsoncons::json& json_value) const { + if (json_value.is_null()) { + return null_value_representation_ == split_json::NullValueRepresentationOption::EmptyString ? "" : "null"; + } + + if (json_value.is_string()) { + return json_value.as<std::string>(); + } + + return json_value.to_string(); +} + +void SplitJson::onTrigger(core::ProcessContext& context, core::ProcessSession& session) { + auto flow_file = session.get(); + if (!flow_file) { + context.yield(); + return; + } + + auto query_result = queryArrayUsingJsonPath(session, flow_file); + if (!query_result) { + session.transfer(flow_file, Failure); + return; + } + + gsl_Assert(query_result->is_array()); + if (query_result->empty()) { + logger_->log_error("JSON Path expression '{}' did not match the input flow file content, transferring to Failure relationship", json_path_expression_); + session.transfer(flow_file, Failure); + return; + } + + if (query_result->size() == 1 && !query_result.value()[0].is_array()) { + logger_->log_error("JSON Path expression '{}' did not return an array, transferring to Failure relationship", json_path_expression_); Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/1980/commits/0aa867f69a964c3d3f0a1562750bd81218fc6434 ########## extensions/standard-processors/processors/SplitJson.cpp: ########## @@ -0,0 +1,133 @@ +/** + * 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 "SplitJson.h" + +#include <unordered_map> + +#include "core/ProcessSession.h" +#include "core/ProcessContext.h" +#include "core/Resource.h" +#include "utils/ProcessorConfigUtils.h" +#include "utils/Id.h" + +#include "jsoncons_ext/jsonpath/jsonpath.hpp" + +namespace org::apache::nifi::minifi::processors { + +void SplitJson::initialize() { + setSupportedProperties(Properties); + setSupportedRelationships(Relationships); +} + +void SplitJson::onSchedule(core::ProcessContext& context, core::ProcessSessionFactory&) { + json_path_expression_ = utils::parseProperty(context, SplitJson::JsonPathExpression); + null_value_representation_ = utils::parseEnumProperty<split_json::NullValueRepresentationOption>(context, SplitJson::NullValueRepresentation); +} + +std::optional<jsoncons::json> SplitJson::queryArrayUsingJsonPath(core::ProcessSession& session, const std::shared_ptr<core::FlowFile>& flow_file) const { + const auto json_string = to_string(session.readBuffer(flow_file)); + if (json_string.empty()) { + logger_->log_error("FlowFile content is empty, transferring to Failure relationship"); Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/1980/commits/0aa867f69a964c3d3f0a1562750bd81218fc6434 ########## extensions/standard-processors/processors/SplitJson.cpp: ########## @@ -0,0 +1,133 @@ +/** + * 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 "SplitJson.h" + +#include <unordered_map> + +#include "core/ProcessSession.h" +#include "core/ProcessContext.h" +#include "core/Resource.h" +#include "utils/ProcessorConfigUtils.h" +#include "utils/Id.h" + +#include "jsoncons_ext/jsonpath/jsonpath.hpp" + +namespace org::apache::nifi::minifi::processors { + +void SplitJson::initialize() { + setSupportedProperties(Properties); + setSupportedRelationships(Relationships); +} + +void SplitJson::onSchedule(core::ProcessContext& context, core::ProcessSessionFactory&) { + json_path_expression_ = utils::parseProperty(context, SplitJson::JsonPathExpression); + null_value_representation_ = utils::parseEnumProperty<split_json::NullValueRepresentationOption>(context, SplitJson::NullValueRepresentation); +} + +std::optional<jsoncons::json> SplitJson::queryArrayUsingJsonPath(core::ProcessSession& session, const std::shared_ptr<core::FlowFile>& flow_file) const { + const auto json_string = to_string(session.readBuffer(flow_file)); + if (json_string.empty()) { + logger_->log_error("FlowFile content is empty, transferring to Failure relationship"); + return std::nullopt; + } + + jsoncons::json json_object; + try { + json_object = jsoncons::json::parse(json_string); + } catch (const jsoncons::json_exception& e) { + logger_->log_error("FlowFile content is not a valid JSON document, transferring to Failure relationship: {}", e.what()); Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/1980/commits/0aa867f69a964c3d3f0a1562750bd81218fc6434 ########## extensions/standard-processors/tests/unit/SplitJsonTests.cpp: ########## @@ -0,0 +1,150 @@ +/** + * 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/SplitJson.h" +#include "unit/TestUtils.h" +#include "unit/ProcessorUtils.h" + +namespace org::apache::nifi::minifi::test { + +class SplitJsonTestFixture { + public: + SplitJsonTestFixture() : + controller_(utils::make_processor<processors::SplitJson>("SplitJson")), + split_json_processor_(controller_.getProcessor()) { + REQUIRE(split_json_processor_); + LogTestController::getInstance().setTrace<processors::SplitJson>(); + } + + protected: + void verifySuccessfulSplit(const std::string& input_json_content, const std::string& json_path_expression, const std::vector<std::string>& expected_split_contents) { + REQUIRE(controller_.plan->setProperty(split_json_processor_, processors::SplitJson::JsonPathExpression, json_path_expression)); + + auto result = controller_.trigger({{.content = input_json_content, .attributes = {{"filename", "unique_file_name"}}}}); + + CHECK(result.at(processors::SplitJson::Failure).empty()); + REQUIRE(result.at(processors::SplitJson::Original).size() == 1); + auto original_flow_file = result.at(processors::SplitJson::Original).at(0); + CHECK(controller_.plan->getContent(original_flow_file) == input_json_content); + auto original_fragment_id = original_flow_file->getAttribute(processors::SplitJson::FragmentIdentifier.name); + CHECK_FALSE(original_fragment_id->empty()); + auto fragment_count = original_flow_file->getAttribute(processors::SplitJson::FragmentCount.name); + CHECK(fragment_count.value() == std::to_string(expected_split_contents.size())); + + REQUIRE(result.at(processors::SplitJson::Split).size() == expected_split_contents.size()); + for (size_t i = 0; i < expected_split_contents.size(); ++i) { + auto flow_file = result.at(processors::SplitJson::Split).at(i); + CHECK(controller_.plan->getContent(flow_file) == expected_split_contents[i]); + auto fragment_id = flow_file->getAttribute(processors::SplitJson::FragmentIdentifier.name); + CHECK(fragment_id.value() == original_fragment_id.value()); + CHECK(flow_file->getAttribute(processors::SplitJson::FragmentCount.name).value() == std::to_string(expected_split_contents.size())); + CHECK(flow_file->getAttribute(processors::SplitJson::FragmentIndex.name).value() == std::to_string(i)); + CHECK(flow_file->getAttribute(processors::SplitJson::SegmentOriginalFilename.name).value() == "unique_file_name"); + CHECK_FALSE(flow_file->getAttribute(core::SpecialFlowAttribute::FILENAME).value() == "unique_file_name"); + } + } + + SingleProcessorTestController controller_; + core::Processor* split_json_processor_; +}; + +TEST_CASE_METHOD(SplitJsonTestFixture, "Query fails with parsing issues", "[SplitJsonTests]") { + ProcessorTriggerResult result; + std::string error_log; + REQUIRE(controller_.plan->setProperty(split_json_processor_, processors::SplitJson::JsonPathExpression, "invalid json path")); + SECTION("Flow file content is empty") { + result = controller_.trigger({{.content = ""}}); + error_log = "FlowFile content is empty, transferring to Failure relationship"; Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/1980/commits/0aa867f69a964c3d3f0a1562750bd81218fc6434 ########## extensions/standard-processors/tests/unit/SplitJsonTests.cpp: ########## @@ -0,0 +1,150 @@ +/** + * 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/SplitJson.h" +#include "unit/TestUtils.h" +#include "unit/ProcessorUtils.h" + +namespace org::apache::nifi::minifi::test { + +class SplitJsonTestFixture { + public: + SplitJsonTestFixture() : + controller_(utils::make_processor<processors::SplitJson>("SplitJson")), + split_json_processor_(controller_.getProcessor()) { + REQUIRE(split_json_processor_); + LogTestController::getInstance().setTrace<processors::SplitJson>(); + } + + protected: + void verifySuccessfulSplit(const std::string& input_json_content, const std::string& json_path_expression, const std::vector<std::string>& expected_split_contents) { + REQUIRE(controller_.plan->setProperty(split_json_processor_, processors::SplitJson::JsonPathExpression, json_path_expression)); + + auto result = controller_.trigger({{.content = input_json_content, .attributes = {{"filename", "unique_file_name"}}}}); + + CHECK(result.at(processors::SplitJson::Failure).empty()); + REQUIRE(result.at(processors::SplitJson::Original).size() == 1); + auto original_flow_file = result.at(processors::SplitJson::Original).at(0); + CHECK(controller_.plan->getContent(original_flow_file) == input_json_content); + auto original_fragment_id = original_flow_file->getAttribute(processors::SplitJson::FragmentIdentifier.name); + CHECK_FALSE(original_fragment_id->empty()); + auto fragment_count = original_flow_file->getAttribute(processors::SplitJson::FragmentCount.name); + CHECK(fragment_count.value() == std::to_string(expected_split_contents.size())); + + REQUIRE(result.at(processors::SplitJson::Split).size() == expected_split_contents.size()); + for (size_t i = 0; i < expected_split_contents.size(); ++i) { + auto flow_file = result.at(processors::SplitJson::Split).at(i); + CHECK(controller_.plan->getContent(flow_file) == expected_split_contents[i]); + auto fragment_id = flow_file->getAttribute(processors::SplitJson::FragmentIdentifier.name); + CHECK(fragment_id.value() == original_fragment_id.value()); + CHECK(flow_file->getAttribute(processors::SplitJson::FragmentCount.name).value() == std::to_string(expected_split_contents.size())); + CHECK(flow_file->getAttribute(processors::SplitJson::FragmentIndex.name).value() == std::to_string(i)); + CHECK(flow_file->getAttribute(processors::SplitJson::SegmentOriginalFilename.name).value() == "unique_file_name"); + CHECK_FALSE(flow_file->getAttribute(core::SpecialFlowAttribute::FILENAME).value() == "unique_file_name"); + } + } + + SingleProcessorTestController controller_; + core::Processor* split_json_processor_; +}; + +TEST_CASE_METHOD(SplitJsonTestFixture, "Query fails with parsing issues", "[SplitJsonTests]") { + ProcessorTriggerResult result; + std::string error_log; + REQUIRE(controller_.plan->setProperty(split_json_processor_, processors::SplitJson::JsonPathExpression, "invalid json path")); + 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"; Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/1980/commits/0aa867f69a964c3d3f0a1562750bd81218fc6434 ########## extensions/standard-processors/tests/unit/SplitJsonTests.cpp: ########## @@ -0,0 +1,150 @@ +/** + * 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/SplitJson.h" +#include "unit/TestUtils.h" +#include "unit/ProcessorUtils.h" + +namespace org::apache::nifi::minifi::test { + +class SplitJsonTestFixture { + public: + SplitJsonTestFixture() : + controller_(utils::make_processor<processors::SplitJson>("SplitJson")), + split_json_processor_(controller_.getProcessor()) { + REQUIRE(split_json_processor_); + LogTestController::getInstance().setTrace<processors::SplitJson>(); + } + + protected: + void verifySuccessfulSplit(const std::string& input_json_content, const std::string& json_path_expression, const std::vector<std::string>& expected_split_contents) { + REQUIRE(controller_.plan->setProperty(split_json_processor_, processors::SplitJson::JsonPathExpression, json_path_expression)); + + auto result = controller_.trigger({{.content = input_json_content, .attributes = {{"filename", "unique_file_name"}}}}); + + CHECK(result.at(processors::SplitJson::Failure).empty()); + REQUIRE(result.at(processors::SplitJson::Original).size() == 1); + auto original_flow_file = result.at(processors::SplitJson::Original).at(0); + CHECK(controller_.plan->getContent(original_flow_file) == input_json_content); + auto original_fragment_id = original_flow_file->getAttribute(processors::SplitJson::FragmentIdentifier.name); + CHECK_FALSE(original_fragment_id->empty()); + auto fragment_count = original_flow_file->getAttribute(processors::SplitJson::FragmentCount.name); + CHECK(fragment_count.value() == std::to_string(expected_split_contents.size())); + + REQUIRE(result.at(processors::SplitJson::Split).size() == expected_split_contents.size()); + for (size_t i = 0; i < expected_split_contents.size(); ++i) { + auto flow_file = result.at(processors::SplitJson::Split).at(i); + CHECK(controller_.plan->getContent(flow_file) == expected_split_contents[i]); + auto fragment_id = flow_file->getAttribute(processors::SplitJson::FragmentIdentifier.name); + CHECK(fragment_id.value() == original_fragment_id.value()); + CHECK(flow_file->getAttribute(processors::SplitJson::FragmentCount.name).value() == std::to_string(expected_split_contents.size())); + CHECK(flow_file->getAttribute(processors::SplitJson::FragmentIndex.name).value() == std::to_string(i)); + CHECK(flow_file->getAttribute(processors::SplitJson::SegmentOriginalFilename.name).value() == "unique_file_name"); + CHECK_FALSE(flow_file->getAttribute(core::SpecialFlowAttribute::FILENAME).value() == "unique_file_name"); + } + } + + SingleProcessorTestController controller_; + core::Processor* split_json_processor_; +}; + +TEST_CASE_METHOD(SplitJsonTestFixture, "Query fails with parsing issues", "[SplitJsonTests]") { + ProcessorTriggerResult result; + std::string error_log; + REQUIRE(controller_.plan->setProperty(split_json_processor_, processors::SplitJson::JsonPathExpression, "invalid json path")); + 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"; + } + + SECTION("Json Path expression is invalid") { + result = controller_.trigger({{.content = "{}"}}); + error_log = "Invalid JSON path expression 'invalid json path' set in the 'JsonPath Expression' property:"; + } + + CHECK(result.at(processors::SplitJson::Original).empty()); + CHECK(result.at(processors::SplitJson::Split).empty()); + CHECK(result.at(processors::SplitJson::Failure).size() == 1); + CHECK(utils::verifyLogLinePresenceInPollTime(1s, error_log)); +} + +TEST_CASE_METHOD(SplitJsonTestFixture, "Query does not match input JSON content", "[SplitJsonTests]") { + REQUIRE(controller_.plan->setProperty(split_json_processor_, processors::SplitJson::JsonPathExpression, "$.email")); + + std::string input_json; + SECTION("Flow file content does not contain the specified path") { + input_json = R"({"name": "John"})"; + } + + SECTION("Flow file content null") { + input_json = "null"; + } + + auto result = controller_.trigger({{.content = input_json}}); + + CHECK(result.at(processors::SplitJson::Original).empty()); + CHECK(result.at(processors::SplitJson::Split).empty()); + CHECK(result.at(processors::SplitJson::Failure).size() == 1); + CHECK(utils::verifyLogLinePresenceInPollTime(1s, "JSON Path expression '$.email' did not match the input flow file content, transferring to Failure relationship")); +} + +TEST_CASE_METHOD(SplitJsonTestFixture, "Query returns non-array result", "[SplitJsonTests]") { + REQUIRE(controller_.plan->setProperty(split_json_processor_, processors::SplitJson::JsonPathExpression, "$.name")); + + auto result = controller_.trigger({{.content = R"({"name": "John"})"}}); + + CHECK(result.at(processors::SplitJson::Original).empty()); + CHECK(result.at(processors::SplitJson::Split).empty()); + CHECK(result.at(processors::SplitJson::Failure).size() == 1); + CHECK(utils::verifyLogLinePresenceInPollTime(1s, "JSON Path expression '$.name' did not return an array, transferring to Failure relationship")); Review Comment: Updated in https://github.com/apache/nifi-minifi-cpp/pull/1980/commits/0aa867f69a964c3d3f0a1562750bd81218fc6434 -- 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]
