]> granicus.if.org Git - icinga2/blob - test/base-dictionary.cpp
Merge pull request #5559 from rhopfer/fix/disk-exclude-configfs
[icinga2] / test / base-dictionary.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.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 #include <boost/tuple/tuple.hpp>
25
26 using namespace icinga;
27
28 BOOST_AUTO_TEST_SUITE(base_dictionary)
29
30 BOOST_AUTO_TEST_CASE(construct)
31 {
32         Dictionary::Ptr dictionary = new Dictionary();
33         BOOST_CHECK(dictionary);
34 }
35
36 BOOST_AUTO_TEST_CASE(get1)
37 {
38         Dictionary::Ptr dictionary = new Dictionary();
39         dictionary->Set("test1", 7);
40         dictionary->Set("test2", "hello world");
41
42         BOOST_CHECK(dictionary->GetLength() == 2);
43
44         Value test1;
45         test1 = dictionary->Get("test1");
46         BOOST_CHECK(test1 == 7);
47
48         Value test2;
49         test2 = dictionary->Get("test2");
50         BOOST_CHECK(test2 == "hello world");
51
52         String key3 = "test3";
53         Value test3;
54         test3 = dictionary->Get(key3);
55         BOOST_CHECK(test3.IsEmpty());
56 }
57
58 BOOST_AUTO_TEST_CASE(get2)
59 {
60         Dictionary::Ptr dictionary = new Dictionary();
61         Dictionary::Ptr other = new Dictionary();
62
63         dictionary->Set("test1", other);
64
65         BOOST_CHECK(dictionary->GetLength() == 1);
66
67         Dictionary::Ptr test1 = dictionary->Get("test1");
68         BOOST_CHECK(other == test1);
69
70         Dictionary::Ptr test2 = dictionary->Get("test2");
71         BOOST_CHECK(!test2);
72 }
73
74 BOOST_AUTO_TEST_CASE(foreach)
75 {
76         Dictionary::Ptr dictionary = new Dictionary();
77         dictionary->Set("test1", 7);
78         dictionary->Set("test2", "hello world");
79
80         ObjectLock olock(dictionary);
81
82         bool seen_test1 = false, seen_test2 = false;
83
84         for (const Dictionary::Pair& kv : dictionary) {
85                 BOOST_CHECK(kv.first == "test1" || kv.first == "test2");
86
87                 if (kv.first == "test1") {
88                         BOOST_CHECK(!seen_test1);
89                         seen_test1 = true;
90
91                         BOOST_CHECK(kv.second == 7);
92
93                         continue;
94                 } else if (kv.first == "test2") {
95                         BOOST_CHECK(!seen_test2);
96                         seen_test2 = true;
97
98                         BOOST_CHECK(kv.second == "hello world");
99                 }
100         }
101
102         BOOST_CHECK(seen_test1);
103         BOOST_CHECK(seen_test2);
104 }
105
106 BOOST_AUTO_TEST_CASE(remove)
107 {
108         Dictionary::Ptr dictionary = new Dictionary();
109
110         dictionary->Set("test1", 7);
111         dictionary->Set("test2", "hello world");
112
113         BOOST_CHECK(dictionary->Contains("test1"));
114         BOOST_CHECK(dictionary->GetLength() == 2);
115
116         dictionary->Set("test1", Empty);
117
118         BOOST_CHECK(dictionary->Contains("test1"));
119         BOOST_CHECK(dictionary->GetLength() == 2);
120
121         dictionary->Remove("test1");
122
123         BOOST_CHECK(!dictionary->Contains("test1"));
124         BOOST_CHECK(dictionary->GetLength() == 1);
125
126         dictionary->Remove("test2");
127
128         BOOST_CHECK(!dictionary->Contains("test2"));
129         BOOST_CHECK(dictionary->GetLength() == 0);
130
131         dictionary->Set("test1", 7);
132         dictionary->Set("test2", "hello world");
133
134         {
135                 ObjectLock olock(dictionary);
136
137                 Dictionary::Iterator it = dictionary->Begin();
138                 dictionary->Remove(it);
139         }
140
141         BOOST_CHECK(dictionary->GetLength() == 1);
142 }
143
144 BOOST_AUTO_TEST_CASE(clone)
145 {
146         Dictionary::Ptr dictionary = new Dictionary();
147
148         dictionary->Set("test1", 7);
149         dictionary->Set("test2", "hello world");
150
151         Dictionary::Ptr clone = dictionary->ShallowClone();
152
153         BOOST_CHECK(dictionary != clone);
154
155         BOOST_CHECK(clone->GetLength() == 2);
156         BOOST_CHECK(clone->Get("test1") == 7);
157         BOOST_CHECK(clone->Get("test2") == "hello world");
158
159         clone->Set("test3", 5);
160         BOOST_CHECK(!dictionary->Contains("test3"));
161         BOOST_CHECK(dictionary->GetLength() == 2);
162
163         clone->Set("test2", "test");
164         BOOST_CHECK(dictionary->Get("test2") == "hello world");
165 }
166
167 BOOST_AUTO_TEST_CASE(json)
168 {
169         Dictionary::Ptr dictionary = new Dictionary();
170
171         dictionary->Set("test1", 7);
172         dictionary->Set("test2", "hello world");
173
174         String json = JsonEncode(dictionary);
175         BOOST_CHECK(json.GetLength() > 0);
176         Dictionary::Ptr deserialized = JsonDecode(json);
177         BOOST_CHECK(deserialized->GetLength() == 2);
178         BOOST_CHECK(deserialized->Get("test1") == 7);
179         BOOST_CHECK(deserialized->Get("test2") == "hello world");
180 }
181
182 BOOST_AUTO_TEST_SUITE_END()