Line data Source code
1 : #include "artdaq-database/JsonDocument/JSONDocument.h"
2 : #include "test/common.h"
3 :
4 : namespace bpo = boost::program_options;
5 : using artdaq::database::docrecord::JSONDocument;
6 : namespace adb = artdaq::database;
7 :
8 : using test_case = bool (*)(const std::string&);
9 :
10 : bool test_insertChild(std::string const& conf);
11 : bool test_replaceChild(std::string const& conf);
12 : bool test_deleteChild(std::string const& conf);
13 : bool test_findChild(std::string const& conf);
14 : bool test_appendChild(std::string const& conf);
15 : bool test_removeChild(std::string const& conf);
16 :
17 11 : int main(int argc, char* argv[]) try {
18 11 : artdaq::database::docrecord::debug::JSONDocument();
19 11 : artdaq::database::docrecord::debug::JSONDocumentUtils();
20 :
21 11 : debug::registerUngracefullExitHandlers();
22 :
23 11 : std::ostringstream descstr;
24 11 : descstr << argv[0] << " <-c <config-file>> <other-options>";
25 :
26 11 : bpo::options_description desc = descstr.str();
27 :
28 11 : desc.add_options()("config,c", bpo::value<std::string>(), "Configuration file.")("help,h", "produce help message");
29 :
30 11 : bpo::variables_map vm;
31 :
32 : try {
33 11 : bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
34 11 : bpo::notify(vm);
35 0 : } catch (bpo::error const& e) {
36 0 : std::cerr << "Exception from command line processing in " << argv[0] << ": " << e.what() << "\n";
37 0 : return process_exit_code::INVALID_ARGUMENT;
38 0 : }
39 :
40 22 : if (vm.count("help") != 0u) {
41 0 : std::cout << desc << std::endl;
42 0 : return process_exit_code::HELP;
43 : }
44 :
45 22 : if (vm.count("config") == 0u) {
46 : std::cerr << "Exception from command line processing in " << argv[0] << ": no configuration file given.\n"
47 : << "For usage and an options list, please do '" << argv[0] << " --help"
48 0 : << "'.\n";
49 0 : return process_exit_code::INVALID_ARGUMENT | 1;
50 : }
51 :
52 11 : auto file_name = vm["config"].as<std::string>();
53 :
54 11 : std::ifstream is(file_name);
55 :
56 11 : std::string conf((std::istreambuf_iterator<char>(is)), std::istreambuf_iterator<char>());
57 :
58 33 : auto name = [](auto const& conf) { return JSONDocument(conf).value_as<std::string>("operation"); };
59 :
60 11 : auto runTest = [](std::string const& name) {
61 : auto tests =
62 : std::map<std::string, test_case>{{"insertChild", test_insertChild}, {"replaceChild", test_replaceChild}, {"deleteChild", test_deleteChild},
63 88 : {"findChild", test_findChild}, {"appendChild", test_appendChild}, {"removeChild", test_removeChild}};
64 :
65 11 : std::cout << "Running test:<" << name << ">\n";
66 :
67 22 : return tests.at(name);
68 22 : };
69 :
70 11 : auto testResult = runTest(name(conf))(conf);
71 :
72 11 : if (testResult) {
73 11 : return process_exit_code::SUCCESS;
74 : }
75 :
76 0 : return process_exit_code::FAILURE;
77 11 : } catch (...) {
78 0 : std::cerr << "Process exited with error: " << ::debug::current_exception_diagnostic_information();
79 0 : return process_exit_code::UNCAUGHT_EXCEPTION;
80 0 : }
81 :
82 : namespace literal {
83 : constexpr auto operation [[gnu::unused]] = "operation";
84 : constexpr auto path [[gnu::unused]] = "path";
85 : constexpr auto beginstate [[gnu::unused]] = "begin-state";
86 : constexpr auto delta [[gnu::unused]] = "delta";
87 : constexpr auto endstate [[gnu::unused]] = "end-state";
88 : constexpr auto returnedvalue [[gnu::unused]] = "returned-value";
89 : constexpr auto mustsucceed [[gnu::unused]] = "must-succeed";
90 : } // namespace literal
91 :
92 4 : bool test_insertChild(std::string const& conf) {
93 4 : confirm(!conf.empty());
94 :
95 4 : auto opts = JSONDocument(conf);
96 8 : auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
97 8 : auto delta = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::delta));
98 8 : auto path = opts.value_as<std::string>(literal::path);
99 8 : auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
100 8 : auto expected = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::returnedvalue));
101 :
102 4 : auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
103 :
104 : try {
105 4 : auto returned = begin.insertChild(delta, path);
106 :
107 2 : if (begin != end) {
108 0 : std::cout << "Error invalid end state. \n";
109 0 : std::cerr << "begin:\n" << begin << "\n";
110 0 : std::cerr << "end:\n" << end << "\n";
111 0 : return false;
112 : }
113 :
114 2 : if (returned != expected) {
115 0 : std::cout << "Error returned!=expected.\n";
116 0 : std::cerr << "returned:\n" << returned << "\n";
117 0 : std::cerr << "expected:\n" << expected << "\n";
118 0 : return false;
119 : }
120 4 : } catch (adb::exception const& e) {
121 2 : if (mustsucceed) {
122 0 : throw;
123 : }
124 2 : }
125 :
126 4 : return true;
127 4 : }
128 :
129 4 : bool test_replaceChild(std::string const& conf) {
130 4 : confirm(!conf.empty());
131 :
132 4 : auto opts = JSONDocument(conf);
133 8 : auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
134 8 : auto delta = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::delta));
135 8 : auto path = opts.value_as<std::string>(literal::path);
136 8 : auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
137 8 : auto expected = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::returnedvalue));
138 :
139 4 : auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
140 :
141 : try {
142 4 : auto returned = begin.replaceChild(delta, path);
143 :
144 4 : if (begin != end) {
145 0 : std::cout << "Error invalid end state. \n";
146 0 : std::cerr << "begin:\n" << begin << "\n";
147 0 : std::cerr << "end:\n" << end << "\n";
148 0 : return false;
149 : }
150 :
151 4 : if (returned != expected) {
152 0 : std::cout << "Error returned!=expected.\n";
153 0 : std::cerr << "returned:\n" << returned << "\n";
154 0 : std::cerr << "expected:\n" << expected << "\n";
155 0 : return false;
156 : }
157 4 : } catch (adb::exception const& e) {
158 0 : if (mustsucceed) {
159 0 : throw;
160 : }
161 0 : }
162 :
163 4 : return true;
164 4 : }
165 :
166 1 : bool test_deleteChild(std::string const& conf) {
167 1 : confirm(!conf.empty());
168 :
169 1 : auto opts = JSONDocument(conf);
170 2 : auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
171 2 : auto path = opts.value_as<std::string>(literal::path);
172 2 : auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
173 2 : auto expected = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::returnedvalue));
174 :
175 1 : auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
176 :
177 : try {
178 1 : auto returned = begin.deleteChild(path);
179 :
180 1 : if (begin != end) {
181 0 : std::cout << "Error invalid end state. \n";
182 0 : return false;
183 : }
184 :
185 1 : if (returned != expected) {
186 0 : std::cout << "Error returned!=expected.\n";
187 0 : return false;
188 : }
189 1 : } catch (adb::exception const& e) {
190 0 : if (mustsucceed) {
191 0 : throw;
192 : }
193 :
194 : // std::cout << "Caught exception e.what()=" << e.what() << "\n";
195 0 : }
196 :
197 1 : return true;
198 1 : }
199 :
200 0 : bool test_findChild(std::string const& conf) {
201 0 : confirm(!conf.empty());
202 :
203 0 : auto opts = JSONDocument(conf);
204 0 : auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
205 0 : auto path = opts.value_as<std::string>(literal::path);
206 0 : auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
207 0 : auto expected = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::returnedvalue));
208 :
209 0 : auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
210 :
211 : try {
212 0 : auto returned = begin.findChild(path);
213 :
214 0 : if (begin != end) {
215 0 : std::cout << "Error invalid end state. \n";
216 0 : return false;
217 : }
218 :
219 0 : if (returned != expected) {
220 0 : std::cout << "Error returned!=expected.\n";
221 0 : return false;
222 : }
223 0 : } catch (adb::exception const& e) {
224 0 : if (mustsucceed) {
225 0 : throw;
226 : }
227 :
228 0 : std::cout << "Caught exception e.what()=" << e.what() << "\n";
229 0 : }
230 :
231 0 : return true;
232 0 : }
233 :
234 1 : bool test_appendChild(std::string const& conf) {
235 1 : confirm(!conf.empty());
236 :
237 1 : auto opts = JSONDocument(conf);
238 2 : auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
239 2 : auto delta = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::delta));
240 2 : auto path = opts.value_as<std::string>(literal::path);
241 2 : auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
242 2 : auto expected = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::returnedvalue));
243 :
244 1 : auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
245 :
246 : try {
247 1 : auto returned = begin.appendChild(delta, path);
248 :
249 1 : if (begin != end) {
250 0 : std::cout << "Error invalid end state. \n";
251 0 : std::cerr << "begin:\n" << begin << "\n";
252 0 : std::cerr << "end:\n" << end << "\n";
253 0 : return false;
254 : }
255 :
256 1 : if (returned != expected) {
257 0 : std::cout << "Error returned!=expected.\n";
258 0 : std::cerr << "returned:\n" << returned << "\n";
259 0 : std::cerr << "expected:\n" << expected << "\n";
260 0 : return false;
261 : }
262 1 : } catch (adb::exception const& e) {
263 0 : if (mustsucceed) {
264 0 : throw;
265 : }
266 0 : }
267 :
268 1 : return true;
269 1 : }
270 :
271 1 : bool test_removeChild(std::string const& conf) {
272 1 : confirm(!conf.empty());
273 :
274 1 : auto opts = JSONDocument(conf);
275 2 : auto begin = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::beginstate));
276 2 : auto delta = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::delta));
277 2 : auto path = opts.value_as<std::string>(literal::path);
278 2 : auto end = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::endstate));
279 2 : auto expected = JSONDocument::loadFromFile(opts.value_as<std::string>(literal::returnedvalue));
280 :
281 1 : auto mustsucceed = opts.value_as<bool>(literal::mustsucceed);
282 :
283 : try {
284 1 : auto returned = begin.removeChild(delta, path);
285 :
286 1 : if (begin != end) {
287 0 : std::cout << "Error invalid end state. \n";
288 0 : std::cerr << "begin:\n" << begin << "\n";
289 0 : std::cerr << "end:\n" << end << "\n";
290 0 : return false;
291 : }
292 :
293 1 : if (returned != expected) {
294 0 : std::cout << "Error returned!=expected.\n";
295 0 : std::cerr << "returned:\n" << returned << "\n";
296 0 : std::cerr << "expected:\n" << expected << "\n";
297 0 : return false;
298 : }
299 :
300 1 : } catch (adb::exception const& e) {
301 0 : if (mustsucceed) {
302 0 : throw;
303 : }
304 0 : }
305 :
306 1 : return true;
307 1 : }
|