Skip to content

Commit b5c3682

Browse files
committed
feat: added changes to tests
1 parent ae53dee commit b5c3682

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

src/agent/configuration_parser/tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ find_package(GTest CONFIG REQUIRED)
22

33
add_executable(ConfigurationParser_test configuration_parser_test.cpp)
44
configure_target(ConfigurationParser_test)
5-
target_link_libraries(ConfigurationParser_test PUBLIC ConfigurationParser GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main Logger)
5+
target_link_libraries(ConfigurationParser_test PUBLIC ConfigurationParser Config GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main Logger)
66
add_test(NAME ConfigParserTest COMMAND ConfigurationParser_test)

src/agent/configuration_parser/tests/configuration_parser_test.cpp

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <configuration_parser.hpp>
22

3+
#include <config.h>
34
#include <gtest/gtest.h>
45

56
#include <filesystem>
@@ -89,7 +90,7 @@ TEST(ConfigurationParser, GetConfigString)
8990
string_conf: string
9091
)";
9192
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
92-
const auto ret = parserStr->GetConfig<std::string>("agent", "server_url");
93+
const auto ret = parserStr->GetConfig<std::string>("agent", "server_url").value_or("Invalid string");
9394
ASSERT_EQ(ret, "192.168.0.11");
9495
}
9596

@@ -103,7 +104,8 @@ TEST(ConfigurationParser, GetConfigArrayString)
103104
string_conf: string
104105
)";
105106
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
106-
const auto ret = parserStr->GetConfig<std::vector<std::string>>("agent_array", "array_manager_ip");
107+
const auto ret = parserStr->GetConfig<std::vector<std::string>>("agent_array", "array_manager_ip")
108+
.value_or(std::vector<std::string>({"Invalid string1", "Invalid string2"}));
107109
ASSERT_EQ(ret[0], "192.168.0.0");
108110
ASSERT_EQ(ret[1], "192.168.0.1");
109111
}
@@ -118,7 +120,7 @@ TEST(ConfigurationParser, GetConfigInt)
118120
int_conf: 10
119121
)";
120122
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
121-
const auto ret = parserStr->GetConfig<int>("agent_array", "int_conf");
123+
const auto ret = parserStr->GetConfig<int>("agent_array", "int_conf").value_or(1234);
122124
ASSERT_EQ(ret, 10);
123125
}
124126

@@ -132,7 +134,7 @@ TEST(ConfigurationParser, GetConfigFloat)
132134
float_conf: 12.34
133135
)";
134136
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
135-
const auto ret = parserStr->GetConfig<float>("agent_array", "float_conf");
137+
const auto ret = parserStr->GetConfig<float>("agent_array", "float_conf").value_or(1.1f);
136138
EXPECT_FLOAT_EQ(ret, 12.34f);
137139
}
138140

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

152155
TEST(ConfigurationParser, GetConfigIntSubTable)
@@ -161,7 +164,7 @@ TEST(ConfigurationParser, GetConfigIntSubTable)
161164
int_conf: 1234
162165
)";
163166
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
164-
const auto ret = parserStr->GetConfig<int>("agent_array", "sub_table", "int_conf");
167+
const auto ret = parserStr->GetConfig<int>("agent_array", "sub_table", "int_conf").value_or(0);
165168
ASSERT_EQ(ret, 1234);
166169
}
167170

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

@@ -197,7 +200,10 @@ TEST(ConfigurationParser, GetConfigArrayMap)
197200
api_token: api_token2
198201
)";
199202
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
200-
const auto ret = parserStr->GetConfig<std::vector<std::map<std::string, std::string>>>("agent_array", "api_auth");
203+
const auto ret = parserStr->GetConfig<std::vector<std::map<std::string, std::string>>>("agent_array", "api_auth")
204+
.value_or(std::vector<std::map<std::string, std::string>> {
205+
{{"org_name", "default1"}, {"api_token", "default_token1"}},
206+
{{"org_name", "default2"}, {"api_token", "default_token2"}}});
201207
ASSERT_EQ(ret[0].at("org_name"), "dummy1");
202208
ASSERT_EQ(ret[0].at("api_token"), "api_token1");
203209
ASSERT_EQ(ret[1].at("org_name"), "dummy2");
@@ -212,7 +218,9 @@ TEST(ConfigurationParser, GetConfigMap)
212218
string_conf_2: string_2
213219
)";
214220
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
215-
const auto ret = parserStr->GetConfig<std::map<std::string, std::string>>("map_string");
221+
const auto ret = parserStr->GetConfig<std::map<std::string, std::string>>("map_string")
222+
.value_or(std::map<std::string, std::string> {{"string_conf_1", "default_1"},
223+
{"string_conf_2", "default_2"}});
216224
ASSERT_EQ(ret.at("string_conf_1"), "string_1");
217225
ASSERT_EQ(ret.at("string_conf_2"), "string_2");
218226
}
@@ -225,7 +233,10 @@ TEST(ConfigurationParser, GetConfigBadCast)
225233
int_conf: 10
226234
)";
227235
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
228-
EXPECT_ANY_THROW(parserStr->GetConfig<std::vector<std::string>>("bad_cast_array"));
236+
const auto ret = parserStr->GetConfig<std::vector<std::string>>("bad_cast_array")
237+
.value_or(std::vector<std::string> {"dummy", "string"});
238+
ASSERT_EQ(ret[0], "dummy");
239+
ASSERT_EQ(ret[1], "string");
229240
}
230241

231242
TEST(ConfigurationParser, GetConfigMultiNode)
@@ -245,10 +256,12 @@ TEST(ConfigurationParser, GetConfigMultiNode)
245256
file_wait: 500
246257
)";
247258
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
248-
const auto ret = parserStr->GetConfig<std::vector<std::string>>("agent_array", "array_manager_ip");
249-
const auto retEnabled = parserStr->GetConfig<bool>("logcollector", "enabled");
250-
const auto retFileWait = parserStr->GetConfig<int>("logcollector", "file_wait");
251-
const auto retLocalFiles = parserStr->GetConfig<std::vector<std::string>>("logcollector", "localfiles");
259+
const auto ret = parserStr->GetConfig<std::vector<std::string>>("agent_array", "array_manager_ip")
260+
.value_or(std::vector<std::string> {});
261+
const auto retEnabled = parserStr->GetConfig<bool>("logcollector", "enabled").value_or(false);
262+
const auto retFileWait = parserStr->GetConfig<int>("logcollector", "file_wait").value_or(0);
263+
const auto retLocalFiles = parserStr->GetConfig<std::vector<std::string>>("logcollector", "localfiles")
264+
.value_or(std::vector<std::string> {});
252265
ASSERT_EQ(ret[0], "192.168.0.0");
253266
ASSERT_EQ(ret[1], "192.168.0.1");
254267
ASSERT_TRUE(retEnabled);
@@ -273,11 +286,11 @@ TEST_F(ConfigurationParserFileTest, ValidConfigFileLoadsCorrectly)
273286
{
274287
const auto parser = std::make_unique<configuration::ConfigurationParser>(m_tempConfigFilePath);
275288

276-
EXPECT_EQ(parser->GetConfig<std::string>("agent", "server_url"), "https://myserver:28000");
277-
EXPECT_FALSE(parser->GetConfig<bool>("inventory", "enabled"));
278-
EXPECT_EQ(parser->GetConfig<int>("inventory", "interval"), 7200);
279-
EXPECT_FALSE(parser->GetConfig<bool>("logcollector", "enabled"));
280-
EXPECT_EQ(parser->GetConfig<int>("logcollector", "file_wait"), 1000);
289+
EXPECT_EQ(parser->GetConfig<std::string>("agent", "server_url").value_or(""), "https://myserver:28000");
290+
EXPECT_FALSE(parser->GetConfig<bool>("inventory", "enabled").value_or(true));
291+
EXPECT_EQ(parser->GetConfig<int>("inventory", "interval").value_or(0), 7200);
292+
EXPECT_FALSE(parser->GetConfig<bool>("logcollector", "enabled").value_or(true));
293+
EXPECT_EQ(parser->GetConfig<int>("logcollector", "file_wait").value_or(0), 1000);
281294
}
282295
catch (const std::exception& e)
283296
{
@@ -293,11 +306,12 @@ TEST_F(ConfigurationParserInvalidYamlFileTest, InvalidConfigFileLoadsDefault)
293306
{
294307
const auto parser = std::make_unique<configuration::ConfigurationParser>(m_tempConfigFilePath);
295308

296-
EXPECT_EQ(parser->GetConfig<std::string>("agent", "server_url"), "https://localhost:27000");
297-
EXPECT_TRUE(parser->GetConfig<bool>("inventory", "enabled"));
298-
EXPECT_EQ(parser->GetConfig<int>("inventory", "interval"), 3600);
299-
EXPECT_TRUE(parser->GetConfig<bool>("logcollector", "enabled"));
300-
EXPECT_EQ(parser->GetConfig<int>("logcollector", "file_wait"), 500);
309+
EXPECT_EQ(parser->GetConfig<std::string>("agent", "server_url").value_or("https://localhost:27000"),
310+
"https://localhost:27000");
311+
EXPECT_TRUE(parser->GetConfig<bool>("inventory", "enabled").value_or(true));
312+
EXPECT_EQ(parser->GetConfig<int>("inventory", "interval").value_or(3600), 3600);
313+
EXPECT_TRUE(parser->GetConfig<bool>("logcollector", "enabled").value_or(true));
314+
EXPECT_EQ(parser->GetConfig<int>("logcollector", "file_wait").value_or(500), 500);
301315
}
302316
catch (const std::exception& e)
303317
{

0 commit comments

Comments
 (0)