martinzink commented on code in PR #1634: URL: https://github.com/apache/nifi-minifi-cpp/pull/1634#discussion_r1331215824
########## extensions/smb/tests/ListSmbTests.cpp: ########## @@ -0,0 +1,146 @@ +/** + * + * 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 "TestBase.h" +#include "Catch.h" +#include "ListSmb.h" +#include "Utils.h" +#include "utils/MockSmbConnectionControllerService.h" +#include "SingleProcessorTestController.h" +#include "range/v3/algorithm/count_if.hpp" +#include "range/v3/algorithm/find_if.hpp" +#include "core/Resource.h" + +namespace org::apache::nifi::minifi::extensions::smb::test { + +REGISTER_RESOURCE(MockSmbConnectionControllerService, ControllerService); + +TEST_CASE("ListSmb invalid network path") { + const auto list_smb = std::make_shared<ListSmb>("ListSmb"); + minifi::test::SingleProcessorTestController controller{list_smb}; + auto smb_connection_node = controller.plan->addController("MockSmbConnectionControllerService", "smb_connection_controller_service"); + REQUIRE(controller.plan->setProperty(smb_connection_node, SmbConnectionControllerService::Hostname, utils::OsUtils::getHostName().value_or("localhost"))); + REQUIRE(controller.plan->setProperty(smb_connection_node, SmbConnectionControllerService::Share, "some_share_that_does_not_exists")); + REQUIRE(controller.plan->setProperty(list_smb, ListSmb::ConnectionControllerService, "smb_connection_controller_service")); + const auto trigger_results = controller.trigger(); + CHECK(trigger_results.at(ListSmb::Success).empty()); + CHECK(list_smb->isYield()); +} + +bool checkForFlowFileWithAttributes(const std::vector<std::shared_ptr<core::FlowFile>>& result, ListSmbExpectedAttributes expected_attributes) { + auto matching_flow_file = ranges::find_if(result, [&](const auto& flow_file) { return flow_file->getAttribute(core::SpecialFlowAttribute::FILENAME) == expected_attributes.expected_filename; }); + if (matching_flow_file == result.end()) { + return false; + } + expected_attributes.checkAttributes(**matching_flow_file); + return true; +} + +TEST_CASE("ListSmb tests") { + const auto list_smb = std::make_shared<ListSmb>("ListSmb"); + minifi::test::SingleProcessorTestController controller{list_smb}; + + auto smb_connection_node = controller.plan->addController("MockSmbConnectionControllerService", "smb_connection_controller_service"); + auto mock_smb_connection_controller_service = std::dynamic_pointer_cast<MockSmbConnectionControllerService>(smb_connection_node->getControllerServiceImplementation()); + REQUIRE(mock_smb_connection_controller_service); + mock_smb_connection_controller_service->setPath(controller.createTempDirectory()); + + auto a_expected_attributes = mock_smb_connection_controller_service->addFile("a.foo", std::string(10_KiB, 'a'), 5min); + auto b_expected_attributes = mock_smb_connection_controller_service->addFile("b.foo", std::string(13_KiB, 'b'), 1h); + auto c_expected_attributes = mock_smb_connection_controller_service->addFile("c.bar", std::string(1_KiB, 'c'), 2h); + auto d_expected_attributes = mock_smb_connection_controller_service->addFile(std::filesystem::path("subdir") / "some" / "d.foo", std::string(100, 'd'), 10min); + auto e_expected_attributes = mock_smb_connection_controller_service->addFile(std::filesystem::path("subdir2") /"e.foo", std::string(1, 'e'), 10s); + auto f_expected_attributes = mock_smb_connection_controller_service->addFile(std::filesystem::path("third") / "f.bar", std::string(50_KiB, 'f'), 30min); + auto g_expected_attributes = mock_smb_connection_controller_service->addFile("g.foo", std::string(50_KiB, 'f'), 30min); + auto hide_file_error = minifi::test::utils::hide_file(mock_smb_connection_controller_service->getPath() / "g.foo"); + REQUIRE_FALSE(hide_file_error); + + REQUIRE((a_expected_attributes && b_expected_attributes && c_expected_attributes && d_expected_attributes && e_expected_attributes && f_expected_attributes && g_expected_attributes)); + + REQUIRE(controller.plan->setProperty(list_smb, ListSmb::ConnectionControllerService, "smb_connection_controller_service")); + + SECTION("FileFilter without subdirs") { + REQUIRE(controller.plan->setProperty(list_smb, ListSmb::FileFilter, ".*\\.foo")); + REQUIRE(controller.plan->setProperty(list_smb, ListSmb::RecurseSubdirectories, "false")); + const auto trigger_results = controller.trigger(); + CHECK(trigger_results.at(ListSmb::Success).size() == 2); + CHECK_FALSE(list_smb->isYield()); + CHECK(checkForFlowFileWithAttributes(trigger_results.at(ListSmb::Success), *a_expected_attributes)); + CHECK(checkForFlowFileWithAttributes(trigger_results.at(ListSmb::Success), *b_expected_attributes)); + } + + SECTION("Input directory") { + REQUIRE(controller.plan->setProperty(list_smb, ListSmb::InputDirectory, "subdir")); + REQUIRE(controller.plan->setProperty(list_smb, ListSmb::RecurseSubdirectories, "true")); + const auto trigger_results = controller.trigger(); + CHECK(trigger_results.at(ListSmb::Success).size() == 1); + CHECK_FALSE(list_smb->isYield()); + CHECK(checkForFlowFileWithAttributes(trigger_results.at(ListSmb::Success), *d_expected_attributes)); + } + + SECTION("PathFilter and FileFilter") { + REQUIRE(controller.plan->setProperty(list_smb, ListSmb::FileFilter, ".*\\.foo")); + REQUIRE(controller.plan->setProperty(list_smb, ListSmb::RecurseSubdirectories, "true")); + REQUIRE(controller.plan->setProperty(list_smb, ListSmb::PathFilter, "subdir.*")); + const auto trigger_results = controller.trigger(); + CHECK(trigger_results.at(ListSmb::Success).size() == 2); + CHECK_FALSE(list_smb->isYield()); + CHECK(checkForFlowFileWithAttributes(trigger_results.at(ListSmb::Success), *d_expected_attributes)); + CHECK(checkForFlowFileWithAttributes(trigger_results.at(ListSmb::Success), *e_expected_attributes)); + } + + SECTION("Subdirs with age restriction") { + REQUIRE(controller.plan->setProperty(list_smb, ListSmb::MinimumFileAge, "3min")); + REQUIRE(controller.plan->setProperty(list_smb, ListSmb::MaximumFileAge, "59min")); + REQUIRE(controller.plan->setProperty(list_smb, ListSmb::RecurseSubdirectories, "true")); + const auto trigger_results = controller.trigger(); + CHECK(trigger_results.at(ListSmb::Success).size() == 3); + CHECK_FALSE(list_smb->isYield()); + CHECK(checkForFlowFileWithAttributes(trigger_results.at(ListSmb::Success), *a_expected_attributes)); + CHECK(checkForFlowFileWithAttributes(trigger_results.at(ListSmb::Success), *d_expected_attributes)); + } Review Comment: From what I've gathered its was only due to the slow CI. I've increased the threshold in https://github.com/apache/nifi-minifi-cpp/pull/1634/commits/40dff807c58f68f2070c54613fb9db73cad3bb72 -- 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: issues-unsubscr...@nifi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org