Line data Source code
1 : #define BOOST_TEST_DYN_LINK
2 : #define BOOST_TEST_MODULE (SafeWrappers test)
3 :
4 : #include <boost/test/unit_test.hpp>
5 :
6 : #include <cstdlib>
7 : #include <ctime>
8 : #include <iostream>
9 : #include <memory>
10 : #include <string>
11 :
12 : #include "../DatabaseConfigurationInterface.h"
13 :
14 : using namespace ots;
15 :
16 : struct TestFixture {
17 2 : TestFixture() {
18 2 : std::cout << "SafeWrappers test fixture setup\n";
19 6 : std::cout << "ARTDAQ_DATABASE_URI=" << (getenv("ARTDAQ_DATABASE_URI") != nullptr ? getenv("ARTDAQ_DATABASE_URI") : std::string("notprovided"))
20 4 : << "\n";
21 2 : }
22 :
23 2 : ~TestFixture() { std::cout << "SafeWrappers test fixture teardown\n"; }
24 :
25 : const int _version = (srand(time(nullptr)), rand() % 99999 + 100000);
26 :
27 26 : int version() const { return _version; }
28 : };
29 :
30 : TestFixture fixture;
31 :
32 : BOOST_AUTO_TEST_SUITE(safe_wrappers_test)
33 :
34 4 : BOOST_AUTO_TEST_CASE(getVersions_safe_success) {
35 2 : std::shared_ptr<ConfigurationBase> cfg = std::make_shared<TestConfiguration001>();
36 2 : auto ifc = DatabaseConfigurationInterface();
37 :
38 2 : cfg->getView().version = fixture.version();
39 2 : auto storeResult = ifc.saveActiveVersion(cfg.get());
40 2 : BOOST_CHECK_EQUAL(storeResult, 0);
41 :
42 2 : std::set<int> versions;
43 2 : auto result = ifc.getVersions_safe(cfg.get(), versions);
44 :
45 2 : BOOST_CHECK_EQUAL(result.first, true);
46 2 : BOOST_CHECK_MESSAGE(!versions.empty(), "Versions set should not be empty");
47 :
48 2 : auto found = (versions.find(fixture.version()) != versions.end());
49 2 : BOOST_CHECK_MESSAGE(found, "Stored version should be in the versions set");
50 2 : }
51 :
52 4 : BOOST_AUTO_TEST_CASE(getVersions_safe_multiple_calls) {
53 2 : std::shared_ptr<ConfigurationBase> cfg = std::make_shared<TestConfiguration001>();
54 2 : auto ifc = DatabaseConfigurationInterface();
55 :
56 2 : std::set<int> versions1, versions2;
57 :
58 2 : auto result1 = ifc.getVersions_safe(cfg.get(), versions1);
59 2 : auto result2 = ifc.getVersions_safe(cfg.get(), versions2);
60 :
61 2 : BOOST_CHECK_EQUAL(result1.first, true);
62 2 : BOOST_CHECK_EQUAL(result2.first, true);
63 2 : BOOST_CHECK(versions1 == versions2);
64 2 : }
65 :
66 4 : BOOST_AUTO_TEST_CASE(listConfigurationsTypes_safe_success) {
67 2 : auto ifc = DatabaseConfigurationInterface();
68 :
69 2 : std::set<std::string> collections;
70 2 : auto result = ifc.listConfigurationsTypes_safe(collections);
71 :
72 2 : BOOST_CHECK_EQUAL(result.first, true);
73 2 : std::cout << "Found " << collections.size() << " configuration types\n";
74 4 : for (auto const& c : collections) {
75 2 : std::cout << " - " << c << "\n";
76 : }
77 2 : }
78 :
79 4 : BOOST_AUTO_TEST_CASE(findAllGlobalConfigurations_safe_setup) {
80 2 : std::shared_ptr<ConfigurationBase> cfg1 = std::make_shared<TestConfiguration001>();
81 2 : std::shared_ptr<ConfigurationBase> cfg2 = std::make_shared<TestConfiguration002>();
82 :
83 2 : auto ifc = DatabaseConfigurationInterface();
84 :
85 2 : cfg1->getView().version = fixture.version() + 100;
86 2 : cfg2->getView().version = fixture.version() + 200;
87 :
88 2 : ifc.saveActiveVersion(cfg1.get());
89 2 : ifc.saveActiveVersion(cfg2.get());
90 :
91 2 : auto map = DatabaseConfigurationInterface::config_version_map_t{};
92 2 : map[cfg1->getConfigurationName()] = cfg1->getView().version;
93 2 : map[cfg2->getConfigurationName()] = cfg2->getView().version;
94 :
95 6 : auto configName = std::string{"safewrapper_config_"} + std::to_string(fixture.version());
96 2 : ifc.storeGlobalConfiguration(map, configName);
97 :
98 2 : BOOST_CHECK(true);
99 2 : }
100 :
101 4 : BOOST_AUTO_TEST_CASE(findAllGlobalConfigurations_safe_success) {
102 2 : auto ifc = DatabaseConfigurationInterface();
103 :
104 2 : std::set<std::string> configurations;
105 4 : auto result = ifc.findAllGlobalConfigurations_safe("", configurations);
106 :
107 2 : BOOST_CHECK_EQUAL(result.first, true);
108 2 : BOOST_CHECK_MESSAGE(!configurations.empty(), "Should find at least one global configuration");
109 :
110 2 : std::cout << "Found " << configurations.size() << " global configurations\n";
111 2 : }
112 :
113 4 : BOOST_AUTO_TEST_CASE(findAllGlobalConfigurations_safe_with_filter) {
114 2 : auto ifc = DatabaseConfigurationInterface();
115 6 : auto configName = std::string{"safewrapper_config_"} + std::to_string(fixture.version());
116 :
117 2 : std::set<std::string> configurations;
118 2 : auto result = ifc.findAllGlobalConfigurations_safe(configName, configurations);
119 :
120 2 : BOOST_CHECK_EQUAL(result.first, true);
121 :
122 2 : auto found = (configurations.find(configName) != configurations.end());
123 2 : BOOST_CHECK_MESSAGE(found, "Should find the specific global configuration");
124 2 : }
125 :
126 4 : BOOST_AUTO_TEST_CASE(loadGlobalConfiguration_safe_success) {
127 2 : auto ifc = DatabaseConfigurationInterface();
128 6 : auto configName = std::string{"safewrapper_config_"} + std::to_string(fixture.version());
129 :
130 2 : DatabaseConfigurationInterface::config_version_map_t configMap;
131 2 : auto result = ifc.loadGlobalConfiguration_safe(configName, configMap);
132 :
133 2 : BOOST_CHECK_EQUAL(result.first, true);
134 2 : BOOST_CHECK_EQUAL(configMap.size(), 2);
135 :
136 2 : std::shared_ptr<ConfigurationBase> cfg1 = std::make_shared<TestConfiguration001>();
137 2 : std::shared_ptr<ConfigurationBase> cfg2 = std::make_shared<TestConfiguration002>();
138 :
139 2 : BOOST_CHECK_EQUAL(configMap.at(cfg1->getConfigurationName()), fixture.version() + 100);
140 2 : BOOST_CHECK_EQUAL(configMap.at(cfg2->getConfigurationName()), fixture.version() + 200);
141 2 : }
142 :
143 4 : BOOST_AUTO_TEST_CASE(loadGlobalConfiguration_safe_nonexistent) {
144 2 : auto ifc = DatabaseConfigurationInterface();
145 6 : auto configName = std::string{"nonexistent_config_"} + std::to_string(fixture.version());
146 :
147 2 : DatabaseConfigurationInterface::config_version_map_t configMap;
148 2 : auto result = ifc.loadGlobalConfiguration_safe(configName, configMap);
149 :
150 2 : BOOST_CHECK_EQUAL(result.first, false);
151 2 : BOOST_CHECK_MESSAGE(!result.second.empty(), "Error message should not be empty");
152 2 : BOOST_CHECK_MESSAGE(configMap.empty(), "Config map should be empty on failure");
153 :
154 2 : std::cout << "Expected error for non-existent config: " << result.second << "\n";
155 2 : }
156 :
157 4 : BOOST_AUTO_TEST_CASE(findCompositionsContaining_safe_success) {
158 2 : auto ifc = DatabaseConfigurationInterface();
159 :
160 2 : std::shared_ptr<ConfigurationBase> cfg1 = std::make_shared<TestConfiguration001>();
161 2 : auto versionStr = std::to_string(fixture.version() + 100);
162 :
163 2 : std::set<std::string> compositions;
164 2 : auto result = ifc.findCompositionsContaining_safe(cfg1->getConfigurationName(), versionStr, compositions);
165 :
166 2 : BOOST_CHECK_EQUAL(result.first, true);
167 :
168 6 : auto configName = std::string{"safewrapper_config_"} + std::to_string(fixture.version());
169 2 : auto found = (compositions.find(configName) != compositions.end());
170 2 : BOOST_CHECK_MESSAGE(found, "Should find the composition containing the version");
171 :
172 2 : std::cout << "Found " << compositions.size() << " compositions containing version " << versionStr << "\n";
173 2 : }
174 :
175 4 : BOOST_AUTO_TEST_CASE(findCompositionsContaining_safe_not_found) {
176 2 : auto ifc = DatabaseConfigurationInterface();
177 :
178 2 : std::shared_ptr<ConfigurationBase> cfg1 = std::make_shared<TestConfiguration001>();
179 2 : auto versionStr = std::to_string(999999);
180 :
181 2 : std::set<std::string> compositions;
182 2 : auto result = ifc.findCompositionsContaining_safe(cfg1->getConfigurationName(), versionStr, compositions);
183 :
184 2 : BOOST_CHECK_EQUAL(result.first, true);
185 2 : BOOST_CHECK_MESSAGE(compositions.empty(), "Should not find any compositions for non-existent version");
186 2 : }
187 :
188 4 : BOOST_AUTO_TEST_CASE(compare_safe_and_throwing_getVersions) {
189 2 : std::shared_ptr<ConfigurationBase> cfg = std::make_shared<TestConfiguration001>();
190 2 : auto ifc = DatabaseConfigurationInterface();
191 :
192 2 : std::set<int> throwingVersions;
193 : try {
194 2 : throwingVersions = ifc.getVersions(cfg.get());
195 : } catch (...) {
196 : throwingVersions.clear();
197 : }
198 :
199 2 : std::set<int> safeVersions;
200 2 : auto result = ifc.getVersions_safe(cfg.get(), safeVersions);
201 :
202 2 : BOOST_CHECK_EQUAL(result.first, true);
203 2 : BOOST_CHECK_EQUAL(throwingVersions.size(), safeVersions.size());
204 2 : BOOST_CHECK(throwingVersions == safeVersions);
205 2 : }
206 :
207 4 : BOOST_AUTO_TEST_CASE(compare_safe_and_throwing_findGlobalConfigurations) {
208 2 : auto ifc = DatabaseConfigurationInterface();
209 :
210 2 : std::set<std::string> throwingConfigs;
211 : try {
212 4 : throwingConfigs = ifc.findAllGlobalConfigurations("");
213 0 : } catch (...) {
214 0 : throwingConfigs.clear();
215 0 : }
216 :
217 2 : std::set<std::string> safeConfigs;
218 4 : auto result = ifc.findAllGlobalConfigurations_safe("", safeConfigs);
219 :
220 2 : BOOST_CHECK_EQUAL(result.first, true);
221 2 : BOOST_CHECK_EQUAL(throwingConfigs.size(), safeConfigs.size());
222 2 : BOOST_CHECK(throwingConfigs == safeConfigs);
223 2 : }
224 :
225 4 : BOOST_AUTO_TEST_CASE(compare_safe_and_throwing_loadGlobalConfiguration) {
226 2 : auto ifc = DatabaseConfigurationInterface();
227 6 : auto configName = std::string{"safewrapper_config_"} + std::to_string(fixture.version());
228 :
229 2 : DatabaseConfigurationInterface::config_version_map_t throwingMap;
230 : try {
231 2 : throwingMap = ifc.loadGlobalConfiguration(configName);
232 0 : } catch (...) {
233 0 : throwingMap.clear();
234 0 : }
235 :
236 2 : DatabaseConfigurationInterface::config_version_map_t safeMap;
237 2 : auto result = ifc.loadGlobalConfiguration_safe(configName, safeMap);
238 :
239 2 : BOOST_CHECK_EQUAL(result.first, true);
240 2 : BOOST_CHECK_EQUAL(throwingMap.size(), safeMap.size());
241 2 : BOOST_CHECK(throwingMap == safeMap);
242 2 : }
243 :
244 : BOOST_AUTO_TEST_SUITE_END()
|