Skip to content

Commit

Permalink
test(logcollector): unit test for SendMessage method
Browse files Browse the repository at this point in the history
  • Loading branch information
vikman90 committed Dec 17, 2024
1 parent e7868e6 commit 66bae5d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/modules/logcollector/tests/unit/logcollector_mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ class LogcollectorMock : public Logcollector {
}

MOCK_METHOD(void, AddReader, (std::shared_ptr<IReader> reader), (override));
MOCK_METHOD(void, SendMessage, (const std::string& location, const std::string& log,
const std::string& collectorType), (override));
MOCK_METHOD(void, EnqueueTask, (Awaitable task), (override));
};

class PushMessageMock {
public:
MOCK_METHOD(int, Call, (Message), ());
};
30 changes: 30 additions & 0 deletions src/modules/logcollector/tests/unit/logcollector_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,33 @@ TEST(Logcollector, SetupFileReader) {
ASSERT_NE(capturedReader1, nullptr);
ASSERT_NE(capturedReader2, nullptr);
}

TEST(Logcollector, SendMessage) {
PushMessageMock mock;
LogcollectorMock logcollector;

logcollector.SetPushMessageFunction([&mock](Message message) {
(void)(message);
return mock.Call(message);
});

Message capturedMessage(MessageType::STATELESS, nlohmann::json::object(), "", "", "");

EXPECT_CALL(mock, Call(::testing::_)).WillOnce(::testing::DoAll(::testing::SaveArg<0>(&capturedMessage), ::testing::Return(0)));

const auto MODULE = "logcollector";
const auto LOCATION = "/test/location";
const auto LOG = "test log";
const auto PROVIDER = "syslog";
const auto METADATA = R"({"module":"logcollector","type":"reader"})";

logcollector.SendMessage(LOCATION, LOG, "reader");

ASSERT_EQ(capturedMessage.type, MessageType::STATELESS);
ASSERT_EQ(capturedMessage.data["log"]["file"]["path"], LOCATION);
ASSERT_EQ(capturedMessage.data["event"]["original"], LOG);
ASSERT_NE(capturedMessage.data["event"]["created"], nullptr);
ASSERT_EQ(capturedMessage.data["event"]["module"], MODULE);
ASSERT_EQ(capturedMessage.data["event"]["provider"], PROVIDER);
ASSERT_EQ(capturedMessage.metaData, METADATA);
}

0 comments on commit 66bae5d

Please sign in to comment.