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