Line data Source code
1 : #include "test/common.h"
2 :
3 : #include "artdaq-database/ConfigurationDB/common.h"
4 : #include "artdaq-database/ConfigurationDB/conftoolifc.h"
5 :
6 : #include "artdaq-database/BasicTypes/basictypes.h"
7 : #include "artdaq-database/JsonDocument/JSONDocument.h"
8 : #include "artdaq-database/JsonDocument/JSONDocumentBuilder.h"
9 : #include "artdaq-database/JsonDocument/JSONDocumentMigrator.h"
10 :
11 : #include "artdaq-database/DataFormats/Json/json_reader.h"
12 :
13 : #include <fstream>
14 : #include <regex>
15 :
16 : namespace db = artdaq::database;
17 : namespace bpo = boost::program_options;
18 :
19 : using artdaq::database::docrecord::JSONDocument;
20 : using artdaq::database::docrecord::JSONDocumentBuilder;
21 : using artdaq::database::docrecord::JSONDocumentMigrator;
22 :
23 : using test_case = bool (*)(const std::string&, const std::string&, const std::string&);
24 :
25 : bool test_migratev1v2(std::string const& /*source*/, std::string const& /*compare*/, std::string const& /*source_filename*/);
26 :
27 : // Helper function to extract test number from filename
28 : // e.g., "test001.src.json" -> "001"
29 0 : std::string extract_test_number(const std::string& filename) {
30 0 : std::regex pattern(R"(test(\d{3}))");
31 0 : std::smatch matches;
32 :
33 0 : if (std::regex_search(filename, matches, pattern)) {
34 0 : return matches[1].str();
35 : }
36 :
37 0 : return "unknown";
38 0 : }
39 :
40 : // Helper function to write content to a file
41 0 : bool write_to_file(const std::string& filename, const std::string& content) {
42 0 : std::ofstream outfile(filename);
43 :
44 0 : if (!outfile.is_open()) {
45 0 : std::cerr << "ERROR: Failed to open file for writing: " << filename << "\n";
46 0 : return false;
47 : }
48 :
49 0 : outfile << content;
50 0 : outfile.close();
51 :
52 0 : std::cout << "Debug file written: " << filename << "\n";
53 0 : return true;
54 0 : }
55 :
56 1 : int main(int argc, char* argv[]) try {
57 : #if 0
58 :
59 : impl::enable_trace();
60 :
61 : debug::registerUngracefullExitHandlers();
62 : #endif
63 1 : artdaq::database::useFakeTime(true);
64 :
65 1 : artdaq::database::docrecord::debug::JSONDocumentMigrator();
66 :
67 : // Get the input parameters via the boost::program_options library,
68 : // designed to make it relatively simple to define arguments and
69 : // issue errors if argument list is supplied incorrectly
70 :
71 1 : std::ostringstream descstr;
72 1 : descstr << argv[0] << " <-s <source-file>> <-x <compare-with-file>>";
73 :
74 1 : bpo::options_description desc = descstr.str();
75 :
76 1 : desc.add_options()("source,s", bpo::value<std::string>(), "Input source file.")("compare,x", bpo::value<std::string>(),
77 1 : "Expected result of convertion.")("help,h", "produce help message");
78 :
79 1 : bpo::variables_map vm;
80 :
81 : try {
82 1 : bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
83 1 : bpo::notify(vm);
84 0 : } catch (bpo::error const& e) {
85 0 : std::cerr << "Exception from command line processing in " << argv[0] << ": " << e.what() << "\n";
86 0 : return process_exit_code::INVALID_ARGUMENT;
87 0 : }
88 :
89 2 : if (vm.count("help") != 0u) {
90 0 : std::cout << desc << std::endl;
91 0 : return process_exit_code::HELP;
92 : }
93 :
94 2 : if (vm.count("source") == 0u) {
95 : std::cerr << "Exception from command line processing in " << argv[0] << ": no source file given.\n"
96 : << "For usage and an options list, please do '" << argv[0] << " --help"
97 0 : << "'.\n";
98 0 : return process_exit_code::INVALID_ARGUMENT | 1;
99 : }
100 :
101 2 : if (vm.count("compare") == 0u) {
102 : std::cerr << "Exception from command line processing in " << argv[0] << ": no compare file given.\n"
103 : << "For usage and an options list, please do '" << argv[0] << " --help"
104 0 : << "'.\n";
105 0 : return process_exit_code::INVALID_ARGUMENT | 2;
106 : }
107 :
108 2 : auto source_name = vm["source"].as<std::string>();
109 2 : auto compare_name = vm["compare"].as<std::string>();
110 1 : auto test_name = std::string{"migratev1v2"};
111 :
112 1 : auto source_buffer = std::string{};
113 :
114 1 : if (!db::read_buffer_from_file(source_buffer, source_name)) {
115 0 : std::cerr << "Unable to read the source file " << source_name;
116 0 : return process_exit_code::INVALID_ARGUMENT | 1;
117 : }
118 :
119 1 : auto compare_buffer = std::string{};
120 :
121 1 : if (!db::read_buffer_from_file(compare_buffer, compare_name)) {
122 0 : std::cerr << "Unable to read the compare file " << compare_name;
123 0 : return process_exit_code::INVALID_ARGUMENT | 2;
124 : }
125 :
126 1 : auto runTest = [](std::string const& name) {
127 3 : auto tests = std::map<std::string, test_case>{{"migratev1v2", test_migratev1v2}};
128 :
129 1 : std::cout << "Running test:<" << name << ">\n";
130 :
131 2 : return tests.at(name);
132 2 : };
133 :
134 1 : auto testResult = runTest(test_name)(source_buffer, compare_buffer, source_name);
135 :
136 1 : return static_cast<int>(!testResult);
137 1 : } catch (...) {
138 0 : std::cout << "Process exited with error: " << ::debug::current_exception_diagnostic_information();
139 0 : return process_exit_code::UNCAUGHT_EXCEPTION;
140 0 : }
141 :
142 1 : bool test_migratev1v2(std::string const& source, std::string const& compare, std::string const& source_filename) {
143 1 : confirm(!source.empty());
144 1 : confirm(!compare.empty());
145 :
146 1 : auto jsondoc = JSONDocument{source};
147 1 : JSONDocument rdoc = JSONDocumentMigrator{jsondoc};
148 1 : JSONDocument cdoc{compare};
149 :
150 1 : JSONDocumentBuilder returned{rdoc};
151 1 : JSONDocumentBuilder expected{cdoc};
152 :
153 : using namespace artdaq::database::overlay;
154 :
155 1 : auto result = returned == expected;
156 :
157 1 : if (result.first) {
158 1 : return true;
159 : }
160 :
161 0 : std::cout << "Error returned!=expected.\n";
162 0 : std::cerr << "returned:\n" << returned << "\n";
163 0 : std::cerr << "expected:\n" << expected << "\n";
164 0 : std::cerr << "error:\n" << result.second << "\n";
165 :
166 : // Write debug files
167 0 : std::string test_number = extract_test_number(source_filename);
168 0 : std::ostringstream returned_stream, expected_stream;
169 0 : returned_stream << returned;
170 0 : expected_stream << expected;
171 0 : write_to_file("test" + test_number + ".output.json", returned_stream.str());
172 0 : write_to_file("test" + test_number + ".expected.json", expected_stream.str());
173 :
174 0 : return false;
175 1 : }
|