Skip to content

Commit

Permalink
feat: added changes to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aritosteles committed Nov 15, 2024
1 parent d4d8005 commit c53ec86
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/agent/configuration_parser/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ find_package(GTest CONFIG REQUIRED)

add_executable(ConfigurationParser_test configuration_parser_test.cpp)
configure_target(ConfigurationParser_test)
target_link_libraries(ConfigurationParser_test PUBLIC ConfigurationParser GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main Logger)
target_link_libraries(ConfigurationParser_test PUBLIC ConfigurationParser Config GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main Logger)
add_test(NAME ConfigParserTest COMMAND ConfigurationParser_test)
62 changes: 38 additions & 24 deletions src/agent/configuration_parser/tests/configuration_parser_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <configuration_parser.hpp>

#include <config.h>
#include <gtest/gtest.h>

#include <filesystem>
Expand Down Expand Up @@ -89,7 +90,7 @@ TEST(ConfigurationParser, GetConfigString)
string_conf: string
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<std::string>("agent", "server_url");
const auto ret = parserStr->GetConfig<std::string>("agent", "server_url").value_or("Invalid string");
ASSERT_EQ(ret, "192.168.0.11");
}

Expand All @@ -103,7 +104,8 @@ TEST(ConfigurationParser, GetConfigArrayString)
string_conf: string
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<std::vector<std::string>>("agent_array", "array_manager_ip");
const auto ret = parserStr->GetConfig<std::vector<std::string>>("agent_array", "array_manager_ip")
.value_or(std::vector<std::string>({"Invalid string1", "Invalid string2"}));
ASSERT_EQ(ret[0], "192.168.0.0");
ASSERT_EQ(ret[1], "192.168.0.1");
}
Expand All @@ -118,7 +120,7 @@ TEST(ConfigurationParser, GetConfigInt)
int_conf: 10
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<int>("agent_array", "int_conf");
const auto ret = parserStr->GetConfig<int>("agent_array", "int_conf").value_or(1234);
ASSERT_EQ(ret, 10);
}

Expand All @@ -132,7 +134,7 @@ TEST(ConfigurationParser, GetConfigFloat)
float_conf: 12.34
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<float>("agent_array", "float_conf");
const auto ret = parserStr->GetConfig<float>("agent_array", "float_conf").value_or(1.1f);
EXPECT_FLOAT_EQ(ret, 12.34f);
}

Expand All @@ -146,7 +148,8 @@ TEST(ConfigurationParser, GetConfigNoKey)
float_conf: 12.34
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
EXPECT_ANY_THROW(parserStr->GetConfig<float>("agent_array", "no_key"));
const auto ret = parserStr->GetConfig<float>("agent_array", "no_key").value_or(1.1f); // NOLINT
EXPECT_FLOAT_EQ(ret, 1.1f);
}

TEST(ConfigurationParser, GetConfigIntSubTable)
Expand All @@ -161,7 +164,7 @@ TEST(ConfigurationParser, GetConfigIntSubTable)
int_conf: 1234
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<int>("agent_array", "sub_table", "int_conf");
const auto ret = parserStr->GetConfig<int>("agent_array", "sub_table", "int_conf").value_or(0);
ASSERT_EQ(ret, 1234);
}

Expand All @@ -178,7 +181,7 @@ TEST(ConfigurationParser, GetConfigBoolSubTable)
bool_conf: true
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<bool>("agent_array", "sub_table", "bool_conf");
const auto ret = parserStr->GetConfig<bool>("agent_array", "sub_table", "bool_conf").value_or(false);
ASSERT_EQ(ret, true);
}

Expand All @@ -197,7 +200,10 @@ TEST(ConfigurationParser, GetConfigArrayMap)
api_token: api_token2
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<std::vector<std::map<std::string, std::string>>>("agent_array", "api_auth");
const auto ret = parserStr->GetConfig<std::vector<std::map<std::string, std::string>>>("agent_array", "api_auth")
.value_or(std::vector<std::map<std::string, std::string>> {
{{"org_name", "default1"}, {"api_token", "default_token1"}},
{{"org_name", "default2"}, {"api_token", "default_token2"}}});
ASSERT_EQ(ret[0].at("org_name"), "dummy1");
ASSERT_EQ(ret[0].at("api_token"), "api_token1");
ASSERT_EQ(ret[1].at("org_name"), "dummy2");
Expand All @@ -212,7 +218,9 @@ TEST(ConfigurationParser, GetConfigMap)
string_conf_2: string_2
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<std::map<std::string, std::string>>("map_string");
const auto ret = parserStr->GetConfig<std::map<std::string, std::string>>("map_string")
.value_or(std::map<std::string, std::string> {{"string_conf_1", "default_1"},
{"string_conf_2", "default_2"}});
ASSERT_EQ(ret.at("string_conf_1"), "string_1");
ASSERT_EQ(ret.at("string_conf_2"), "string_2");
}
Expand All @@ -225,7 +233,10 @@ TEST(ConfigurationParser, GetConfigBadCast)
int_conf: 10
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
EXPECT_ANY_THROW(parserStr->GetConfig<std::vector<std::string>>("bad_cast_array"));
const auto ret = parserStr->GetConfig<std::vector<std::string>>("bad_cast_array")
.value_or(std::vector<std::string> {"dummy", "string"});
ASSERT_EQ(ret[0], "dummy");
ASSERT_EQ(ret[1], "string");
}

TEST(ConfigurationParser, GetConfigMultiNode)
Expand All @@ -245,10 +256,12 @@ TEST(ConfigurationParser, GetConfigMultiNode)
file_wait: 500
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<std::vector<std::string>>("agent_array", "array_manager_ip");
const auto retEnabled = parserStr->GetConfig<bool>("logcollector", "enabled");
const auto retFileWait = parserStr->GetConfig<int>("logcollector", "file_wait");
const auto retLocalFiles = parserStr->GetConfig<std::vector<std::string>>("logcollector", "localfiles");
const auto ret = parserStr->GetConfig<std::vector<std::string>>("agent_array", "array_manager_ip")
.value_or(std::vector<std::string> {});
const auto retEnabled = parserStr->GetConfig<bool>("logcollector", "enabled").value_or(false);
const auto retFileWait = parserStr->GetConfig<int>("logcollector", "file_wait").value_or(0);
const auto retLocalFiles = parserStr->GetConfig<std::vector<std::string>>("logcollector", "localfiles")
.value_or(std::vector<std::string> {});
ASSERT_EQ(ret[0], "192.168.0.0");
ASSERT_EQ(ret[1], "192.168.0.1");
ASSERT_TRUE(retEnabled);
Expand All @@ -273,11 +286,11 @@ TEST_F(ConfigurationParserFileTest, ValidConfigFileLoadsCorrectly)
{
const auto parser = std::make_unique<configuration::ConfigurationParser>(m_tempConfigFilePath);

EXPECT_EQ(parser->GetConfig<std::string>("agent", "server_url"), "https://myserver:28000");
EXPECT_FALSE(parser->GetConfig<bool>("inventory", "enabled"));
EXPECT_EQ(parser->GetConfig<int>("inventory", "interval"), 7200);
EXPECT_FALSE(parser->GetConfig<bool>("logcollector", "enabled"));
EXPECT_EQ(parser->GetConfig<int>("logcollector", "file_wait"), 1000);
EXPECT_EQ(parser->GetConfig<std::string>("agent", "server_url").value_or(""), "https://myserver:28000");
EXPECT_FALSE(parser->GetConfig<bool>("inventory", "enabled").value_or(true));
EXPECT_EQ(parser->GetConfig<int>("inventory", "interval").value_or(0), 7200);
EXPECT_FALSE(parser->GetConfig<bool>("logcollector", "enabled").value_or(true));
EXPECT_EQ(parser->GetConfig<int>("logcollector", "file_wait").value_or(0), 1000);
}
catch (const std::exception& e)
{
Expand All @@ -293,11 +306,12 @@ TEST_F(ConfigurationParserInvalidYamlFileTest, InvalidConfigFileLoadsDefault)
{
const auto parser = std::make_unique<configuration::ConfigurationParser>(m_tempConfigFilePath);

EXPECT_EQ(parser->GetConfig<std::string>("agent", "server_url"), "https://localhost:27000");
EXPECT_TRUE(parser->GetConfig<bool>("inventory", "enabled"));
EXPECT_EQ(parser->GetConfig<int>("inventory", "interval"), 3600);
EXPECT_TRUE(parser->GetConfig<bool>("logcollector", "enabled"));
EXPECT_EQ(parser->GetConfig<int>("logcollector", "file_wait"), 500);
EXPECT_EQ(parser->GetConfig<std::string>("agent", "server_url").value_or("https://localhost:27000"),
"https://localhost:27000");
EXPECT_TRUE(parser->GetConfig<bool>("inventory", "enabled").value_or(true));
EXPECT_EQ(parser->GetConfig<int>("inventory", "interval").value_or(3600), 3600);
EXPECT_TRUE(parser->GetConfig<bool>("logcollector", "enabled").value_or(true));
EXPECT_EQ(parser->GetConfig<int>("logcollector", "file_wait").value_or(500), 500);
}
catch (const std::exception& e)
{
Expand Down

0 comments on commit c53ec86

Please sign in to comment.