Line data Source code
1 : #define BOOST_TEST_DYN_LINK
2 : #define BOOST_TEST_MODULE (ErrorHandling test)
3 :
4 : #include <boost/test/unit_test.hpp>
5 :
6 : #include <cstdlib>
7 : #include <ctime>
8 : #include <iostream>
9 : #include <memory>
10 : #include <stdexcept>
11 : #include <string>
12 :
13 : #include "../DatabaseConfigurationInterface.h"
14 :
15 : using namespace ots;
16 :
17 : struct ErrorHandlingTestData {
18 2 : ErrorHandlingTestData() {
19 2 : srand(time(nullptr));
20 2 : baseVersion = rand() % 99999 + 100000;
21 2 : std::cout << "=== ERROR HANDLING TEST SUITE ===\n";
22 2 : std::cout << "Base version: " << baseVersion << "\n\n";
23 2 : }
24 :
25 2 : ~ErrorHandlingTestData() { std::cout << "\n=== TEST SUITE COMPLETE ===\n"; }
26 :
27 : int baseVersion;
28 : };
29 :
30 : ErrorHandlingTestData fixture;
31 :
32 : BOOST_AUTO_TEST_SUITE(error_handling_test)
33 :
34 4 : BOOST_AUTO_TEST_CASE(load_nonexistent_version) {
35 2 : std::cout << "\n=== TEST 1: Load nonexistent version ===\n";
36 :
37 2 : auto ifc = DatabaseConfigurationInterface();
38 :
39 2 : std::shared_ptr<ConfigurationBase> cfg = std::make_shared<TestConfiguration001>();
40 :
41 2 : int nonExistentVersion = 999999999;
42 :
43 2 : auto result = ifc.fill(cfg.get(), nonExistentVersion);
44 2 : std::cout << "fill() returned: " << result << "\n";
45 :
46 2 : if (result == 0) {
47 0 : std::cout << " NOTE: fill() returned success for non-existent version\n";
48 0 : std::cout << " This may be expected if the implementation returns a default config\n";
49 0 : BOOST_WARN_MESSAGE(false, "fill() returns 0 for non-existent version - may need investigation");
50 : } else {
51 2 : std::cout << " fill() correctly returned error for non-existent version\n";
52 : }
53 :
54 2 : BOOST_CHECK(true);
55 2 : std::cout << " Documented: fill() behavior for nonexistent version\n";
56 2 : }
57 :
58 4 : BOOST_AUTO_TEST_CASE(load_nonexistent_composition) {
59 2 : std::cout << "\n=== TEST 2: Load nonexistent composition ===\n";
60 :
61 2 : auto ifc = DatabaseConfigurationInterface();
62 :
63 8 : BOOST_CHECK_THROW(ifc.loadGlobalConfiguration("NonExistentComposition_XYZ123_" + std::to_string(fixture.baseVersion)), std::exception);
64 :
65 2 : std::cout << " Verified: Loading nonexistent composition throws exception\n";
66 2 : }
67 :
68 4 : BOOST_AUTO_TEST_CASE(store_duplicate_version_without_overwrite) {
69 2 : std::cout << "\n=== TEST 3: Store duplicate version without overwrite ===\n";
70 :
71 2 : auto ifc = DatabaseConfigurationInterface();
72 2 : int testVersion = fixture.baseVersion;
73 :
74 2 : std::shared_ptr<ConfigurationBase> cfg1 = std::make_shared<TestConfiguration001>();
75 2 : cfg1->getView().version = testVersion;
76 2 : auto result1 = ifc.saveActiveVersion(cfg1.get());
77 2 : BOOST_REQUIRE_EQUAL(result1, 0);
78 2 : std::cout << "Step 1: Saved version " << testVersion << "\n";
79 :
80 2 : std::shared_ptr<ConfigurationBase> cfg2 = std::make_shared<TestConfiguration001>();
81 2 : cfg2->getView().version = testVersion;
82 2 : auto result2 = ifc.saveActiveVersion(cfg2.get(), false);
83 2 : std::cout << "Step 2: Second save returned: " << result2 << "\n";
84 :
85 2 : BOOST_CHECK_NE(result2, 0);
86 2 : std::cout << " Verified: Duplicate version without overwrite fails\n";
87 2 : }
88 :
89 4 : BOOST_AUTO_TEST_CASE(store_duplicate_composition_without_overwrite) {
90 2 : std::cout << "\n=== TEST 4: Store duplicate composition without overwrite ===\n";
91 :
92 2 : auto ifc = DatabaseConfigurationInterface();
93 2 : int testVersion = fixture.baseVersion + 1;
94 :
95 2 : std::shared_ptr<ConfigurationBase> cfg = std::make_shared<TestConfiguration001>();
96 2 : cfg->getView().version = testVersion;
97 2 : auto saveResult = ifc.saveActiveVersion(cfg.get());
98 2 : BOOST_REQUIRE_EQUAL(saveResult, 0);
99 2 : std::cout << "Step 1: Saved version " << testVersion << "\n";
100 :
101 2 : auto map = DatabaseConfigurationInterface::config_version_map_t{};
102 2 : map[cfg->getConfigurationName()] = testVersion;
103 2 : std::string compName = "DuplicateTest_" + std::to_string(fixture.baseVersion);
104 :
105 2 : BOOST_CHECK_NO_THROW(ifc.storeGlobalConfiguration(map, compName));
106 2 : std::cout << "Step 2: Created composition " << compName << "\n";
107 :
108 2 : std::cout << "Step 3: Attempting to create duplicate composition...\n";
109 4 : BOOST_CHECK_THROW(ifc.storeGlobalConfiguration(map, compName), std::exception);
110 :
111 2 : std::cout << " Verified: Duplicate composition without overwrite throws exception\n";
112 2 : }
113 :
114 4 : BOOST_AUTO_TEST_CASE(find_versions_for_unknown_type) {
115 2 : std::cout << "\n=== TEST 5: Find versions for unknown type ===\n";
116 :
117 2 : auto ifc = DatabaseConfigurationInterface();
118 :
119 2 : std::shared_ptr<ConfigurationBase> cfg = std::make_shared<TestConfiguration001>();
120 :
121 2 : auto versions = ifc.getVersions(cfg.get());
122 :
123 2 : std::cout << "getVersions returned set with " << versions.size() << " elements\n";
124 :
125 2 : BOOST_CHECK_MESSAGE(true, "getVersions returned without throwing");
126 2 : std::cout << " Verified: getVersions for config type returns valid set\n";
127 2 : }
128 :
129 4 : BOOST_AUTO_TEST_CASE(find_compositions_empty_database) {
130 2 : std::cout << "\n=== TEST 6: Find compositions with empty search ===\n";
131 :
132 2 : auto ifc = DatabaseConfigurationInterface();
133 :
134 2 : auto compositions = ifc.findAllGlobalConfigurations("");
135 :
136 2 : std::cout << "findAllGlobalConfigurations(\"\") returned set with " << compositions.size() << " elements\n";
137 :
138 2 : BOOST_CHECK_MESSAGE(true, "findAllGlobalConfigurations returned without throwing");
139 2 : std::cout << " Verified: Empty search returns valid set\n";
140 2 : }
141 :
142 4 : BOOST_AUTO_TEST_CASE(find_compositions_wildcard) {
143 2 : std::cout << "\n=== TEST 7: Find compositions with wildcard ===\n";
144 :
145 2 : auto ifc = DatabaseConfigurationInterface();
146 :
147 2 : auto compositions = ifc.findAllGlobalConfigurations("*");
148 :
149 2 : std::cout << "findAllGlobalConfigurations(\"*\") returned set with " << compositions.size() << " elements\n";
150 :
151 2 : BOOST_CHECK_MESSAGE(true, "findAllGlobalConfigurations with wildcard returned without throwing");
152 2 : std::cout << " Verified: Wildcard search returns valid set\n";
153 2 : }
154 :
155 : BOOST_AUTO_TEST_SUITE_END()
|