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