martinzink commented on code in PR #1866: URL: https://github.com/apache/nifi-minifi-cpp/pull/1866#discussion_r1799556788
########## extensions/standard-processors/tests/unit/SplitRecordTests.cpp: ########## @@ -0,0 +1,96 @@ +/** + * + * 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 "processors/SplitRecord.h" +#include "unit/SingleProcessorTestController.h" +#include "utils/StringUtils.h" +#include "rapidjson/document.h" + +namespace org::apache::nifi::minifi::test { + +class SplitRecordTestController : public TestController { + public: + SplitRecordTestController() { + controller_.plan->addController("JsonRecordSetReader", "JsonRecordSetReader"); + controller_.plan->addController("JsonRecordSetWriter", "JsonRecordSetWriter"); + proc_->setProperty(processors::SplitRecord::RecordReader, "JsonRecordSetReader"); + proc_->setProperty(processors::SplitRecord::RecordWriter, "JsonRecordSetWriter"); + } + + void verifyResults(const ProcessorTriggerResult& results, const std::vector<std::string>& expected_contents) const { + REQUIRE(results.at(processors::SplitRecord::Original).size() == 1); + REQUIRE(results.at(processors::SplitRecord::Splits).size() == expected_contents.size()); + auto& split_results = results.at(processors::SplitRecord::Splits); + const auto fragment_identifier = split_results[0]->getAttribute("fragment.identifier").value(); + const auto original_filename = results.at(processors::SplitRecord::Original)[0]->getAttribute("filename").value(); + for (size_t i = 0; i < expected_contents.size(); ++i) { + rapidjson::Document result_document; + result_document.Parse(controller_.plan->getContent(split_results[i]).c_str()); + rapidjson::Document expected_document; + expected_document.Parse(expected_contents[i].c_str()); + CHECK(result_document == expected_document); + CHECK(split_results[i]->getAttribute("record.count").value() == std::to_string(minifi::utils::string::split(expected_contents[i], "},{").size())); + CHECK(split_results[i]->getAttribute("fragment.index").value() == std::to_string(i)); + CHECK(split_results[i]->getAttribute("fragment.count").value() == std::to_string(expected_contents.size())); + CHECK(split_results[i]->getAttribute("fragment.identifier").value() == fragment_identifier); + CHECK(split_results[i]->getAttribute("segment.original.filename").value() == original_filename); + } + } + + protected: + std::shared_ptr<core::Processor> proc_ = std::make_shared<processors::SplitRecord>("SplitRecord"); + SingleProcessorTestController controller_{proc_}; +}; + +TEST_CASE_METHOD(SplitRecordTestController, "Invalid Records Per Split property", "[splitrecord]") { + proc_->setProperty(processors::SplitRecord::RecordsPerSplit, "invalid"); + auto results = controller_.trigger({InputFlowFileData{"{\"name\": \"John\"}\n{\"name\": \"Jill\"}"}}); + REQUIRE(results[processors::SplitRecord::Failure].size() == 1); + REQUIRE(LogTestController::getInstance().contains("Failed to convert Records Per Split property to an integer", 1s)); +} + +TEST_CASE_METHOD(SplitRecordTestController, "Records Per Split property should be greater than zero", "[splitrecord]") { + proc_->setProperty(processors::SplitRecord::RecordsPerSplit, "${id}"); + auto results = controller_.trigger({InputFlowFileData{"{\"name\": \"John\"}\n{\"name\": \"Jill\"}", {{"id", "0"}}}}); + REQUIRE(results[processors::SplitRecord::Failure].size() == 1); + REQUIRE(LogTestController::getInstance().contains("Records per split should be set to a number larger than 0", 1s)); +} Review Comment: I think we might want to consider restructuring tests so we can separate tests which need additional extensions, but that seems out of the scope of this PR. -- 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]
