martinzink commented on code in PR #1927: URL: https://github.com/apache/nifi-minifi-cpp/pull/1927#discussion_r2077537258
########## extensions/aws/tests/PutKinesisStreamTests.cpp: ########## @@ -0,0 +1,195 @@ +/** + * + * 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 <minifi-cpp/core/FlowFile.h> + +#include "aws/kinesis/model/PutRecordsRequest.h" +#include "core/Resource.h" +#include "processors/PutKinesisStream.h" +#include "unit/Catch.h" +#include "unit/SingleProcessorTestController.h" +#include "unit/TestBase.h" + +namespace org::apache::nifi::minifi::aws::processors::test { + +class MockKinesisClient final : public Aws::Kinesis::KinesisClient { + Aws::Kinesis::Model::PutRecordsOutcome PutRecords(const Aws::Kinesis::Model::PutRecordsRequest& request) const override { + Aws::Kinesis::Model::PutRecordsResult result; + for ([[maybe_unused]] const auto& request_entry : request.GetRecords()) { + Aws::Kinesis::Model::PutRecordsResultEntry result_entry; + result_entry.SetSequenceNumber(fmt::format("sequence_number_{}", ++sequence_number_)); + result_entry.SetShardId("shard_id"); + result.AddRecords(result_entry); + } + return result; + } + + mutable uint32_t sequence_number_ = 0; +}; + +class PutKinesisStreamMocked final : public aws::processors::PutKinesisStream { + public: + static constexpr const char* Description = "PutKinesisStreamMocked"; + + explicit PutKinesisStreamMocked(const std::string& name, const minifi::utils::Identifier& uuid = minifi::utils::Identifier()) + : PutKinesisStream(name, uuid) { + } + + PutKinesisStreamMocked(const PutKinesisStreamMocked&) = delete; + PutKinesisStreamMocked(PutKinesisStreamMocked&&) = delete; + PutKinesisStreamMocked& operator=(const PutKinesisStreamMocked&) = delete; + PutKinesisStreamMocked& operator=(PutKinesisStreamMocked&&) = delete; + + ~PutKinesisStreamMocked() override = default; + + ADD_COMMON_VIRTUAL_FUNCTIONS_FOR_PROCESSORS + + std::unique_ptr<Aws::Kinesis::KinesisClient> getClient(const Aws::Auth::AWSCredentials&) override { + return std::make_unique<MockKinesisClient>(); + } +}; +REGISTER_RESOURCE(PutKinesisStreamMocked, Processor); + +TEST_CASE("PutKinesisStream simple happy path") { + minifi::test::SingleProcessorTestController controller(std::make_unique<PutKinesisStreamMocked>("PutKinesisStream")); + auto put_kinesis_stream = controller.getProcessor(); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::AccessKey, "access_key"); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::SecretKey, "secret_key"); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::AmazonKinesisStreamName, "stream_name"); + + + const auto result = controller.trigger({{.content = "foo"}, {.content = "bar"}}); + CHECK(result.at(PutKinesisStream::Failure).empty()); + CHECK(result.at(PutKinesisStream::Success).size() == 2); + const auto res_ff_1 = result.at(PutKinesisStream::Success).at(0); + const auto res_ff_2 = result.at(PutKinesisStream::Success).at(1); + + CHECK(controller.plan->getContent(res_ff_1) == "foo"); + CHECK(controller.plan->getContent(res_ff_2) == "bar"); + + CHECK(res_ff_1->getAttribute(PutKinesisStream::AwsKinesisSequenceNumber.name) == "sequence_number_1"); + CHECK(res_ff_1->getAttribute(PutKinesisStream::AwsKinesisShardId.name) == "shard_id"); + CHECK(res_ff_2->getAttribute(PutKinesisStream::AwsKinesisSequenceNumber.name) == "sequence_number_2"); + CHECK(res_ff_2->getAttribute(PutKinesisStream::AwsKinesisShardId.name) == "shard_id"); +} + +TEST_CASE("PutKinesisStream smaller batch size than available ffs") { + minifi::test::SingleProcessorTestController controller(std::make_unique<PutKinesisStreamMocked>("PutKinesisStream")); + auto put_kinesis_stream = controller.getProcessor(); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::AccessKey, "access_key"); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::SecretKey, "secret_key"); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::AmazonKinesisStreamName, "stream_name"); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::MessageBatchSize, "10"); + + const auto result = controller.trigger({ + {.content = "Lorem"}, + {.content = "ipsum"}, + {.content = "dolor"}, + {.content = "sit"}, + {.content = "amet"}, + {.content = "consectetur"}, + {.content = "adipiscing"}, + {.content = "elit"}, + {.content = "Morbi"}, + {.content = "dapibus"}, + {.content = "risus"}, + {.content = "a"}, + {.content = "bibendum"}, + {.content = "luctus"}}); + + CHECK(result.at(PutKinesisStream::Success).size() == 10); +} + +TEST_CASE("PutKinesisStream max batch data size fills up") { + minifi::test::SingleProcessorTestController controller(std::make_unique<PutKinesisStreamMocked>("PutKinesisStream")); + auto put_kinesis_stream = controller.getProcessor(); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::AccessKey, "access_key"); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::SecretKey, "secret_key"); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::AmazonKinesisStreamName, "stream_name"); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::MessageBatchSize, "10"); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::MaxBatchDataSize, "12 B"); + + const auto result = controller.trigger({ + {.content = "Lorem"}, + {.content = "ipsum"}, + {.content = "dolor"}, + {.content = "sit"}, + {.content = "amet"}, + {.content = "consectetur"}, + {.content = "adipiscing"}, + {.content = "elit"}, + {.content = "Morbi"}, + {.content = "dapibus"}, + {.content = "risus"}, + {.content = "a"}, + {.content = "bibendum"}, + {.content = "luctus"}}); + + CHECK(result.at(PutKinesisStream::Success).size() == 3); +} + +TEST_CASE("PutKinesisStream max batch data size to different streams") { + minifi::test::SingleProcessorTestController controller(std::make_unique<PutKinesisStreamMocked>("PutKinesisStream")); + auto put_kinesis_stream = controller.getProcessor(); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::AccessKey, "access_key"); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::SecretKey, "secret_key"); + controller.plan->setProperty(put_kinesis_stream, PutKinesisStream::AmazonKinesisStreamName, "stream_name"); Review Comment: 👍 https://github.com/apache/nifi-minifi-cpp/pull/1927/commits/3bf6cb8601b1584cdb9ee1c9a252ee97577699da#diff-9b7be298094693acd6428290b361c7409667d54c39c839a78c6bb833b12194b1L151 -- 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]
