Skip to content

Commit

Permalink
test(inventory): add unit test for SendDeltaEvent method
Browse files Browse the repository at this point in the history
  • Loading branch information
vikman90 committed Dec 11, 2024
1 parent bdb54a9 commit 32d00f2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/modules/inventory/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ cmake_minimum_required(VERSION 3.22)

project(unit_tests)

add_subdirectory(inventory)
add_subdirectory(inventoryImp)
add_subdirectory(invNormalizer)
14 changes: 14 additions & 0 deletions src/modules/inventory/tests/inventory/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
find_package(GTest CONFIG REQUIRED)

add_executable(inventory_unit_test inventory_test.cpp)
configure_target(inventory_unit_test)
target_include_directories(inventory_unit_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include)
target_link_libraries(inventory_unit_test PRIVATE
Inventory
GTest::gtest
GTest::gtest_main
GTest::gmock
GTest::gmock_main)
if(NOT WIN32)
add_test(NAME InventoryTest COMMAND inventory_unit_test)
endif()
51 changes: 51 additions & 0 deletions src/modules/inventory/tests/inventory/inventory_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <cstdio>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "inventory.hpp"

constexpr auto INVENTORY_DB_PATH {"TEMP.db"};

class InventoryTest : public ::testing::Test
{
protected:

void SetUp() override {}
void TearDown() override
{
std::remove(INVENTORY_DB_PATH);
}

Inventory &inventory = Inventory::Instance();
};

TEST_F(InventoryTest, SendUpdateEvent) {
::testing::MockFunction<int(const Message&)> mockPushMessage;

inventory.SetPushMessageFunction(mockPushMessage.AsStdFunction());

EXPECT_CALL(mockPushMessage, Call(::testing::_))
.WillOnce([](const Message& msg)
{
auto expectedData = R"({"key":"value"})";
auto expectedMetadata = R"({"id":"123","module":"inventory","operation":"update","type":"hardware"})";

EXPECT_EQ(msg.data.dump(), expectedData);
EXPECT_EQ(msg.metaData, expectedMetadata);
return 1;
});

auto inputData = R"({
"type": "hardware",
"operation": "update",
"id": "123",
"data": {"key": "value"}
})";

inventory.SendDeltaEvent(inputData);
}

int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 32d00f2

Please sign in to comment.