Skip to content

Commit

Permalink
refactor: fixes tests after changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aritosteles committed Dec 23, 2024
1 parent ec5b93a commit 19ea588
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ TEST(CentralizedConfiguration, ExecuteCommandReturnsFailureOnParseParameters)
{
CentralizedConfiguration centralizedConfiguration;
centralizedConfiguration.SetGroupIdFunction([](const std::vector<std::string>&) { return true; });
centralizedConfiguration.SetDownloadGroupFilesFunction([](const std::string&, const std::string&)
{ return true; });
centralizedConfiguration.SetDownloadGroupFilesFunction(
[](std::string, std::string) -> boost::asio::awaitable<bool> { co_return true; });
centralizedConfiguration.ValidateFileFunction([](const std::filesystem::path&) { return true; });
centralizedConfiguration.ReloadModulesFunction([]() {});

Expand Down Expand Up @@ -123,8 +123,8 @@ TEST(CentralizedConfiguration, ExecuteCommandHandlesRecognizedCommands)
CentralizedConfiguration centralizedConfiguration(std::move(mockFileSystem));
centralizedConfiguration.SetGroupIdFunction([](const std::vector<std::string>&) { return true; });
centralizedConfiguration.GetGroupIdFunction([]() { return std::vector<std::string> {"group1", "group2"}; });
centralizedConfiguration.SetDownloadGroupFilesFunction([](const std::string&, const std::string&)
{ return true; });
centralizedConfiguration.SetDownloadGroupFilesFunction(
[](std::string, std::string) -> boost::asio::awaitable<bool> { co_return true; });
centralizedConfiguration.ValidateFileFunction([](const std::filesystem::path&) { return true; });
centralizedConfiguration.ReloadModulesFunction([]() {});

Expand Down Expand Up @@ -186,12 +186,14 @@ TEST(CentralizedConfiguration, SetFunctionsAreCalledAndReturnsCorrectResultsForS
return true;
});

// NOLINTBEGIN(cppcoreguidelines-avoid-capturing-lambda-coroutines)
centralizedConfiguration.SetDownloadGroupFilesFunction(
[&wasDownloadGroupFilesFunctionCalled](const std::string&, const std::string&)
[&wasDownloadGroupFilesFunctionCalled](std::string, std::string) -> boost::asio::awaitable<bool>
{
wasDownloadGroupFilesFunctionCalled = true;
return wasDownloadGroupFilesFunctionCalled;
co_return wasDownloadGroupFilesFunctionCalled;
});
// NOLINTEND(cppcoreguidelines-avoid-capturing-lambda-coroutines)

centralizedConfiguration.ValidateFileFunction([](const std::filesystem::path&) { return true; });
centralizedConfiguration.ReloadModulesFunction([]() {});
Expand Down Expand Up @@ -244,12 +246,14 @@ TEST(CentralizedConfiguration, SetFunctionsAreCalledAndReturnsCorrectResultsForU
return std::vector<std::string> {"group1", "group2"};
});

// NOLINTBEGIN(cppcoreguidelines-avoid-capturing-lambda-coroutines)
centralizedConfiguration.SetDownloadGroupFilesFunction(
[&wasDownloadGroupFilesFunctionCalled](const std::string&, const std::string&)
[&wasDownloadGroupFilesFunctionCalled](std::string, std::string) -> boost::asio::awaitable<bool>
{
wasDownloadGroupFilesFunctionCalled = true;
return wasDownloadGroupFilesFunctionCalled;
co_return wasDownloadGroupFilesFunctionCalled;
});
// NOLINTEND(cppcoreguidelines-avoid-capturing-lambda-coroutines)

centralizedConfiguration.ValidateFileFunction([](const std::filesystem::path&) { return true; });
centralizedConfiguration.ReloadModulesFunction([]() {});
Expand Down
90 changes: 77 additions & 13 deletions src/agent/communicator/tests/communicator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,51 @@ TEST(CommunicatorTest, GetGroupConfigurationFromManager_Success)

// not really a leak, as its lifetime is managed by the Communicator
testing::Mock::AllowLeak(mockHttpClientPtr);
auto communicatorPtr =
std::make_shared<communicator::Communicator>(std::move(mockHttpClient), "uuid", "key", nullptr, FUNC);

std::string groupName = "group1";
std::string dstFilePath = "/path/to/file";
std::string dstFilePath = "./test-output";

boost::beast::http::response<boost::beast::http::dynamic_body> mockResponse;
mockResponse.result(boost::beast::http::status::ok);

// TODO: Expect call to Co_PerformHttpRequest
// EXPECT_CALL(*mockHttpClientPtr, PerformHttpRequestDownload(_, dstFilePath)).WillOnce(Return(mockResponse));
// NOLINTBEGIN(cppcoreguidelines-avoid-reference-coroutine-parameters)
auto MockCo_PerformHttpRequest =
[](std::shared_ptr<std::string>,
const http_client::HttpRequestParams&,
const GetMessagesFuncType&,
const std::function<void()>&,
[[maybe_unused]] std::time_t connectionRetry,
[[maybe_unused]] size_t batchSize,
[[maybe_unused]] std::function<void(const int, const std::string&)> pOnSuccess,
[[maybe_unused]] std::function<bool()> loopRequestCondition) -> boost::asio::awaitable<void>
{
pOnSuccess(200, "Dummy response"); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
co_return;
};
// NOLINTEND(cppcoreguidelines-avoid-reference-coroutine-parameters)

EXPECT_CALL(*mockHttpClient, Co_PerformHttpRequest(_, _, _, _, _, _, _, _))
.WillOnce(Invoke(MockCo_PerformHttpRequest));

communicator::Communicator communicator(std::move(mockHttpClient), "uuid", "key", nullptr, FUNC);

std::future<bool> result;

auto task = communicator.GetGroupConfigurationFromManager(groupName, dstFilePath);
boost::asio::io_context ioContext;
boost::asio::co_spawn(
ioContext,
[&]() -> boost::asio::awaitable<void>
{
bool value = co_await communicator.GetGroupConfigurationFromManager(groupName, dstFilePath);
std::promise<bool> promise;
promise.set_value(value);
result = promise.get_future();
},
boost::asio::detached);

EXPECT_TRUE(communicatorPtr->GetGroupConfigurationFromManager(groupName, dstFilePath));
ioContext.run();
EXPECT_TRUE(result.get());
}

TEST(CommunicatorTest, GetGroupConfigurationFromManager_Error)
Expand All @@ -237,19 +269,51 @@ TEST(CommunicatorTest, GetGroupConfigurationFromManager_Error)

// not really a leak, as its lifetime is managed by the Communicator
testing::Mock::AllowLeak(mockHttpClientPtr);
auto communicatorPtr =
std::make_shared<communicator::Communicator>(std::move(mockHttpClient), "uuid", "key", nullptr, FUNC);

std::string groupName = "group1";
std::string dstFilePath = "/path/to/file";
std::string dstFilePath = "dummy/non/existing/path";

boost::beast::http::response<boost::beast::http::dynamic_body> mockResponse;
mockResponse.result(boost::beast::http::status::internal_server_error);
mockResponse.result(boost::beast::http::status::ok);

// TODO: Expect call to Co_PerformHttpRequest
// EXPECT_CALL(*mockHttpClientPtr, PerformHttpRequestDownload(_, dstFilePath)).WillOnce(Return(mockResponse));
// NOLINTBEGIN(cppcoreguidelines-avoid-reference-coroutine-parameters)
auto MockCo_PerformHttpRequest =
[](std::shared_ptr<std::string>,
const http_client::HttpRequestParams&,
const GetMessagesFuncType&,
const std::function<void()>&,
[[maybe_unused]] std::time_t connectionRetry,
[[maybe_unused]] size_t batchSize,
[[maybe_unused]] std::function<void(const int, const std::string&)> pOnSuccess,
[[maybe_unused]] std::function<bool()> loopRequestCondition) -> boost::asio::awaitable<void>
{
pOnSuccess(200, "Dummy response"); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
co_return;
};
// NOLINTEND(cppcoreguidelines-avoid-reference-coroutine-parameters)

EXPECT_FALSE(communicatorPtr->GetGroupConfigurationFromManager(groupName, dstFilePath));
EXPECT_CALL(*mockHttpClient, Co_PerformHttpRequest(_, _, _, _, _, _, _, _))
.WillOnce(Invoke(MockCo_PerformHttpRequest));

communicator::Communicator communicator(std::move(mockHttpClient), "uuid", "key", nullptr, FUNC);

std::future<bool> result;

auto task = communicator.GetGroupConfigurationFromManager(groupName, dstFilePath);
boost::asio::io_context ioContext;
boost::asio::co_spawn(
ioContext,
[&]() -> boost::asio::awaitable<void>
{
bool value = co_await communicator.GetGroupConfigurationFromManager(groupName, dstFilePath);
std::promise<bool> promise;
promise.set_value(value);
result = promise.get_future();
},
boost::asio::detached);

ioContext.run();
EXPECT_FALSE(result.get());
}

int main(int argc, char** argv)
Expand Down

0 comments on commit 19ea588

Please sign in to comment.