1 #include <ipmid/entity_map_json.hpp>
2 #include <ipmid/types.hpp>
3 #include <nlohmann/json.hpp>
4
5 #include <utility>
6
7 #include <gmock/gmock.h>
8 #include <gtest/gtest.h>
9
10 namespace ipmi
11 {
12 namespace sensor
13 {
14
15 namespace
16 {
17 using ::testing::IsEmpty;
18
TEST(ValidateJson,FailWithNonArrayReturnsEmpty)19 TEST(ValidateJson, FailWithNonArrayReturnsEmpty)
20 {
21 /* The entity map input json is expected to be an array of objects. */
22 auto j = R"(
23 {
24 "id" : 1,
25 "containerEntityId" : 2,
26 "containerEntityInstance" : 3,
27 "isList" : false,
28 "isLinked" : false,
29 "entities" : [
30 {"id" : 1, "instance" : 2},
31 {"id" : 1, "instance" : 3},
32 {"id" : 1, "instance" : 4},
33 {"id" : 1, "instance" : 5}
34 ]
35 }
36 )"_json;
37
38 EXPECT_THAT(buildJsonEntityMap(j), IsEmpty());
39 }
40
TEST(ValidateJson,FailWithMissingFieldReturnsEmpty)41 TEST(ValidateJson, FailWithMissingFieldReturnsEmpty)
42 {
43 /* There are many required fields, let's just validate that if one is
44 * missing, it returns the empty map.
45 */
46 auto j = R"(
47 [
48 {
49 "id" : 1,
50 "containerEntityId" : 2,
51 "containerEntityInstance" : 3,
52 "isList" : false,
53 "entities" : [
54 {"id" : 1, "instance" : 2},
55 {"id" : 1, "instance" : 3},
56 {"id" : 1, "instance" : 4},
57 {"id" : 1, "instance" : 5}
58 ]
59 }
60 ]
61 )"_json;
62
63 EXPECT_THAT(buildJsonEntityMap(j), IsEmpty());
64 }
65
TEST(ValidateJson,AllValidEntryReturnsExpectedMap)66 TEST(ValidateJson, AllValidEntryReturnsExpectedMap)
67 {
68 /* Boring test where we provide completely valid information and expect the
69 * resulting map contains that information.
70 */
71 auto j = R"(
72 [
73 {
74 "id" : 1,
75 "containerEntityId" : 2,
76 "containerEntityInstance" : 3,
77 "isList" : false,
78 "isLinked" : false,
79 "entities" : [
80 {"id" : 1, "instance" : 2},
81 {"id" : 1, "instance" : 3},
82 {"id" : 1, "instance" : 4},
83 {"id" : 1, "instance" : 5}
84 ]
85 }
86 ]
87 )"_json;
88
89 auto map = buildJsonEntityMap(j);
90 EXPECT_FALSE(map.find(1) == map.end());
91 auto entry = map.find(1);
92 EXPECT_EQ(entry->first, 1);
93
94 /* TODO: someone could write an equality operator for this object. */
95 EXPECT_EQ(entry->second.containerEntityId, 2);
96 EXPECT_EQ(entry->second.containerEntityInstance, 3);
97 EXPECT_FALSE(entry->second.isList);
98 EXPECT_FALSE(entry->second.isLinked);
99 ContainedEntitiesArray expected = {
100 std::make_pair(1, 2), std::make_pair(1, 3), std::make_pair(1, 4),
101 std::make_pair(1, 5)};
102 EXPECT_EQ(entry->second.containedEntities, expected);
103 }
104
TEST(ValidateJson,EntryHasInsufficientContainerEntryCountReturnsEmpty)105 TEST(ValidateJson, EntryHasInsufficientContainerEntryCountReturnsEmpty)
106 {
107 /* The container must have four pairs. (I don't know why, and maybe this
108 * restriction will change).
109 */
110 auto j = R"(
111 [
112 {
113 "id" : 1,
114 "containerEntityId" : 2,
115 "containerEntityInstance" : 3,
116 "isList" : false,
117 "isLinked" : false,
118 "entities" : [
119 {"id" : 1, "instance" : 2},
120 {"id" : 1, "instance" : 3},
121 {"id" : 1, "instance" : 4}
122 ]
123 }
124 ]
125 )"_json;
126
127 EXPECT_THAT(buildJsonEntityMap(j), IsEmpty());
128 }
129
TEST(ValidateJson,ThereAreTwoEntriesOneInvalidReturnsEmpty)130 TEST(ValidateJson, ThereAreTwoEntriesOneInvalidReturnsEmpty)
131 {
132 /* If any entry in the file is corrupt, the file is disregarded. */
133 auto j = R"(
134 [
135 {
136 "id" : 1,
137 "containerEntityId" : 2,
138 "containerEntityInstance" : 3,
139 "isList" : false,
140 "isLinked" : false,
141 "entities" : [
142 {"id" : 1, "instance" : 2},
143 {"id" : 1, "instance" : 3},
144 {"id" : 1, "instance" : 4},
145 {"id" : 1, "instance" : 5}
146 ]
147 },
148 {
149 "id" : 2,
150 "containerEntityId" : 2,
151 "containerEntityInstance" : 3,
152 "isList" : false,
153 "isLinked" : false,
154 "entities" : [
155 {"id" : 1, "instance" : 2},
156 {"id" : 1, "instance" : 3},
157 {"id" : 1, "instance" : 4}
158 ]
159 }
160 ]
161 )"_json;
162
163 EXPECT_THAT(buildJsonEntityMap(j), IsEmpty());
164 }
165
TEST(ValidateJson,ThereAreTwoEntriesBothValidReturnsBoth)166 TEST(ValidateJson, ThereAreTwoEntriesBothValidReturnsBoth)
167 {
168 /* The map supports more than one entry, just validate this. */
169 auto j = R"(
170 [
171 {
172 "id" : 1,
173 "containerEntityId" : 2,
174 "containerEntityInstance" : 3,
175 "isList" : false,
176 "isLinked" : false,
177 "entities" : [
178 {"id" : 1, "instance" : 2},
179 {"id" : 1, "instance" : 3},
180 {"id" : 1, "instance" : 4},
181 {"id" : 1, "instance" : 5}
182 ]
183 },
184 {
185 "id" : 2,
186 "containerEntityId" : 2,
187 "containerEntityInstance" : 3,
188 "isList" : false,
189 "isLinked" : false,
190 "entities" : [
191 {"id" : 1, "instance" : 6},
192 {"id" : 1, "instance" : 7},
193 {"id" : 1, "instance" : 8},
194 {"id" : 1, "instance" : 9}
195 ]
196 }
197 ]
198 )"_json;
199
200 auto map = buildJsonEntityMap(j);
201 EXPECT_FALSE(map.find(1) == map.end());
202 EXPECT_FALSE(map.find(2) == map.end());
203
204 auto entry = map.find(1);
205 EXPECT_EQ(entry->first, 1);
206 EXPECT_EQ(entry->second.containerEntityId, 2);
207 EXPECT_EQ(entry->second.containerEntityInstance, 3);
208 EXPECT_FALSE(entry->second.isList);
209 EXPECT_FALSE(entry->second.isLinked);
210 ContainedEntitiesArray expected = {
211 std::make_pair(1, 2), std::make_pair(1, 3), std::make_pair(1, 4),
212 std::make_pair(1, 5)};
213 EXPECT_EQ(entry->second.containedEntities, expected);
214
215 entry = map.find(2);
216 expected = {std::make_pair(1, 6), std::make_pair(1, 7),
217 std::make_pair(1, 8), std::make_pair(1, 9)};
218 EXPECT_EQ(entry->second.containedEntities, expected);
219 }
220
221 } // namespace
222 } // namespace sensor
223 } // namespace ipmi
224