]> granicus.if.org Git - icinga2/blob - test/base-dictionary.cpp
Merge pull request #6771 from Icinga/feature/tests
[icinga2] / test / base-dictionary.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "base/dictionary.hpp"
21 #include "base/objectlock.hpp"
22 #include "base/json.hpp"
23 #include <BoostTestTargetConfig.h>
24
25 using namespace icinga;
26
27 BOOST_AUTO_TEST_SUITE(base_dictionary)
28
29 BOOST_AUTO_TEST_CASE(construct)
30 {
31         Dictionary::Ptr dictionary = new Dictionary();
32         BOOST_CHECK(dictionary);
33 }
34
35 BOOST_AUTO_TEST_CASE(initializer1)
36 {
37         DictionaryData dict;
38
39         dict.emplace_back("test1", "Gin-o-clock");
40
41         Dictionary::Ptr dictionary = new Dictionary(std::move(dict));
42
43         Value test1;
44         test1 = dictionary->Get("test1");
45         BOOST_CHECK(test1 == "Gin-o-clock");
46 }
47
48 BOOST_AUTO_TEST_CASE(initializer2)
49 {
50         Dictionary::Ptr dictionary = new Dictionary({ {"test1", "Gin-for-the-win"} });
51
52         Value test1;
53         test1 = dictionary->Get("test1");
54         BOOST_CHECK(test1 == "Gin-for-the-win");
55 }
56
57 BOOST_AUTO_TEST_CASE(get1)
58 {
59         Dictionary::Ptr dictionary = new Dictionary();
60         dictionary->Set("test1", 7);
61         dictionary->Set("test2", "hello world");
62
63         BOOST_CHECK(dictionary->GetLength() == 2);
64
65         Value test1;
66         test1 = dictionary->Get("test1");
67         BOOST_CHECK(test1 == 7);
68
69         Value test2;
70         test2 = dictionary->Get("test2");
71         BOOST_CHECK(test2 == "hello world");
72
73         String key3 = "test3";
74         Value test3;
75         test3 = dictionary->Get(key3);
76         BOOST_CHECK(test3.IsEmpty());
77 }
78
79 BOOST_AUTO_TEST_CASE(get2)
80 {
81         Dictionary::Ptr dictionary = new Dictionary();
82         Dictionary::Ptr other = new Dictionary();
83
84         dictionary->Set("test1", other);
85
86         BOOST_CHECK(dictionary->GetLength() == 1);
87
88         Dictionary::Ptr test1 = dictionary->Get("test1");
89         BOOST_CHECK(other == test1);
90
91         Dictionary::Ptr test2 = dictionary->Get("test2");
92         BOOST_CHECK(!test2);
93 }
94
95 BOOST_AUTO_TEST_CASE(foreach)
96 {
97         Dictionary::Ptr dictionary = new Dictionary();
98         dictionary->Set("test1", 7);
99         dictionary->Set("test2", "hello world");
100
101         ObjectLock olock(dictionary);
102
103         bool seen_test1 = false, seen_test2 = false;
104
105         for (const Dictionary::Pair& kv : dictionary) {
106                 BOOST_CHECK(kv.first == "test1" || kv.first == "test2");
107
108                 if (kv.first == "test1") {
109                         BOOST_CHECK(!seen_test1);
110                         seen_test1 = true;
111
112                         BOOST_CHECK(kv.second == 7);
113
114                         continue;
115                 } else if (kv.first == "test2") {
116                         BOOST_CHECK(!seen_test2);
117                         seen_test2 = true;
118
119                         BOOST_CHECK(kv.second == "hello world");
120                 }
121         }
122
123         BOOST_CHECK(seen_test1);
124         BOOST_CHECK(seen_test2);
125 }
126
127 BOOST_AUTO_TEST_CASE(remove)
128 {
129         Dictionary::Ptr dictionary = new Dictionary();
130
131         dictionary->Set("test1", 7);
132         dictionary->Set("test2", "hello world");
133
134         BOOST_CHECK(dictionary->Contains("test1"));
135         BOOST_CHECK(dictionary->GetLength() == 2);
136
137         dictionary->Set("test1", Empty);
138
139         BOOST_CHECK(dictionary->Contains("test1"));
140         BOOST_CHECK(dictionary->GetLength() == 2);
141
142         dictionary->Remove("test1");
143
144         BOOST_CHECK(!dictionary->Contains("test1"));
145         BOOST_CHECK(dictionary->GetLength() == 1);
146
147         dictionary->Remove("test2");
148
149         BOOST_CHECK(!dictionary->Contains("test2"));
150         BOOST_CHECK(dictionary->GetLength() == 0);
151
152         dictionary->Set("test1", 7);
153         dictionary->Set("test2", "hello world");
154
155         {
156                 ObjectLock olock(dictionary);
157
158                 auto it = dictionary->Begin();
159                 dictionary->Remove(it);
160         }
161
162         BOOST_CHECK(dictionary->GetLength() == 1);
163 }
164
165 BOOST_AUTO_TEST_CASE(clone)
166 {
167         Dictionary::Ptr dictionary = new Dictionary();
168
169         dictionary->Set("test1", 7);
170         dictionary->Set("test2", "hello world");
171
172         Dictionary::Ptr clone = dictionary->ShallowClone();
173
174         BOOST_CHECK(dictionary != clone);
175
176         BOOST_CHECK(clone->GetLength() == 2);
177         BOOST_CHECK(clone->Get("test1") == 7);
178         BOOST_CHECK(clone->Get("test2") == "hello world");
179
180         clone->Set("test3", 5);
181         BOOST_CHECK(!dictionary->Contains("test3"));
182         BOOST_CHECK(dictionary->GetLength() == 2);
183
184         clone->Set("test2", "test");
185         BOOST_CHECK(dictionary->Get("test2") == "hello world");
186 }
187
188 BOOST_AUTO_TEST_CASE(json)
189 {
190         Dictionary::Ptr dictionary = new Dictionary();
191
192         dictionary->Set("test1", 7);
193         dictionary->Set("test2", "hello world");
194
195         String json = JsonEncode(dictionary);
196         BOOST_CHECK(json.GetLength() > 0);
197         Dictionary::Ptr deserialized = JsonDecode(json);
198         BOOST_CHECK(deserialized->GetLength() == 2);
199         BOOST_CHECK(deserialized->Get("test1") == 7);
200         BOOST_CHECK(deserialized->Get("test2") == "hello world");
201 }
202
203 BOOST_AUTO_TEST_SUITE_END()