1
1
#include < configuration_parser.hpp>
2
2
3
+ #include < config.h>
3
4
#include < gtest/gtest.h>
4
5
5
6
#include < filesystem>
@@ -89,7 +90,7 @@ TEST(ConfigurationParser, GetConfigString)
89
90
string_conf: string
90
91
)" ;
91
92
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 " ) ;
93
94
ASSERT_EQ (ret, " 192.168.0.11" );
94
95
}
95
96
@@ -103,7 +104,8 @@ TEST(ConfigurationParser, GetConfigArrayString)
103
104
string_conf: string
104
105
)" ;
105
106
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" }));
107
109
ASSERT_EQ (ret[0 ], " 192.168.0.0" );
108
110
ASSERT_EQ (ret[1 ], " 192.168.0.1" );
109
111
}
@@ -118,7 +120,7 @@ TEST(ConfigurationParser, GetConfigInt)
118
120
int_conf: 10
119
121
)" ;
120
122
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 ) ;
122
124
ASSERT_EQ (ret, 10 );
123
125
}
124
126
@@ -132,7 +134,7 @@ TEST(ConfigurationParser, GetConfigFloat)
132
134
float_conf: 12.34
133
135
)" ;
134
136
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 ) ;
136
138
EXPECT_FLOAT_EQ (ret, 12 .34f );
137
139
}
138
140
@@ -146,7 +148,8 @@ TEST(ConfigurationParser, GetConfigNoKey)
146
148
float_conf: 12.34
147
149
)" ;
148
150
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 );
150
153
}
151
154
152
155
TEST (ConfigurationParser, GetConfigIntSubTable)
@@ -161,7 +164,7 @@ TEST(ConfigurationParser, GetConfigIntSubTable)
161
164
int_conf: 1234
162
165
)" ;
163
166
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 ) ;
165
168
ASSERT_EQ (ret, 1234 );
166
169
}
167
170
@@ -178,7 +181,7 @@ TEST(ConfigurationParser, GetConfigBoolSubTable)
178
181
bool_conf: true
179
182
)" ;
180
183
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 ) ;
182
185
ASSERT_EQ (ret, true );
183
186
}
184
187
@@ -197,7 +200,10 @@ TEST(ConfigurationParser, GetConfigArrayMap)
197
200
api_token: api_token2
198
201
)" ;
199
202
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" }}});
201
207
ASSERT_EQ (ret[0 ].at (" org_name" ), " dummy1" );
202
208
ASSERT_EQ (ret[0 ].at (" api_token" ), " api_token1" );
203
209
ASSERT_EQ (ret[1 ].at (" org_name" ), " dummy2" );
@@ -212,7 +218,9 @@ TEST(ConfigurationParser, GetConfigMap)
212
218
string_conf_2: string_2
213
219
)" ;
214
220
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" }});
216
224
ASSERT_EQ (ret.at (" string_conf_1" ), " string_1" );
217
225
ASSERT_EQ (ret.at (" string_conf_2" ), " string_2" );
218
226
}
@@ -225,7 +233,10 @@ TEST(ConfigurationParser, GetConfigBadCast)
225
233
int_conf: 10
226
234
)" ;
227
235
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" );
229
240
}
230
241
231
242
TEST (ConfigurationParser, GetConfigMultiNode)
@@ -245,10 +256,12 @@ TEST(ConfigurationParser, GetConfigMultiNode)
245
256
file_wait: 500
246
257
)" ;
247
258
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> {});
252
265
ASSERT_EQ (ret[0 ], " 192.168.0.0" );
253
266
ASSERT_EQ (ret[1 ], " 192.168.0.1" );
254
267
ASSERT_TRUE (retEnabled);
@@ -273,11 +286,11 @@ TEST_F(ConfigurationParserFileTest, ValidConfigFileLoadsCorrectly)
273
286
{
274
287
const auto parser = std::make_unique<configuration::ConfigurationParser>(m_tempConfigFilePath);
275
288
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 );
281
294
}
282
295
catch (const std::exception& e)
283
296
{
@@ -293,11 +306,12 @@ TEST_F(ConfigurationParserInvalidYamlFileTest, InvalidConfigFileLoadsDefault)
293
306
{
294
307
const auto parser = std::make_unique<configuration::ConfigurationParser>(m_tempConfigFilePath);
295
308
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 );
301
315
}
302
316
catch (const std::exception& e)
303
317
{
0 commit comments