1*9cf0838aSVernon Mauery #include <ipmid/entity_map_json.hpp>
202e32376SPatrick Venture #include <ipmid/types.hpp>
302e32376SPatrick Venture #include <nlohmann/json.hpp>
4fbc6c9d7SPatrick Williams
502e32376SPatrick Venture #include <utility>
602e32376SPatrick Venture
702e32376SPatrick Venture #include <gmock/gmock.h>
802e32376SPatrick Venture #include <gtest/gtest.h>
902e32376SPatrick Venture
1002e32376SPatrick Venture namespace ipmi
1102e32376SPatrick Venture {
1202e32376SPatrick Venture namespace sensor
1302e32376SPatrick Venture {
1402e32376SPatrick Venture
1502e32376SPatrick Venture namespace
1602e32376SPatrick Venture {
1702e32376SPatrick Venture using ::testing::IsEmpty;
1802e32376SPatrick Venture
TEST(ValidateJson,FailWithNonArrayReturnsEmpty)1902e32376SPatrick Venture TEST(ValidateJson, FailWithNonArrayReturnsEmpty)
2002e32376SPatrick Venture {
2102e32376SPatrick Venture /* The entity map input json is expected to be an array of objects. */
2202e32376SPatrick Venture auto j = R"(
2302e32376SPatrick Venture {
2402e32376SPatrick Venture "id" : 1,
2502e32376SPatrick Venture "containerEntityId" : 2,
2602e32376SPatrick Venture "containerEntityInstance" : 3,
2702e32376SPatrick Venture "isList" : false,
2802e32376SPatrick Venture "isLinked" : false,
2902e32376SPatrick Venture "entities" : [
3002e32376SPatrick Venture {"id" : 1, "instance" : 2},
3102e32376SPatrick Venture {"id" : 1, "instance" : 3},
3202e32376SPatrick Venture {"id" : 1, "instance" : 4},
3302e32376SPatrick Venture {"id" : 1, "instance" : 5}
3402e32376SPatrick Venture ]
3502e32376SPatrick Venture }
3602e32376SPatrick Venture )"_json;
3702e32376SPatrick Venture
3802e32376SPatrick Venture EXPECT_THAT(buildJsonEntityMap(j), IsEmpty());
3902e32376SPatrick Venture }
4002e32376SPatrick Venture
TEST(ValidateJson,FailWithMissingFieldReturnsEmpty)4102e32376SPatrick Venture TEST(ValidateJson, FailWithMissingFieldReturnsEmpty)
4202e32376SPatrick Venture {
4302e32376SPatrick Venture /* There are many required fields, let's just validate that if one is
4402e32376SPatrick Venture * missing, it returns the empty map.
4502e32376SPatrick Venture */
4602e32376SPatrick Venture auto j = R"(
4702e32376SPatrick Venture [
4802e32376SPatrick Venture {
4902e32376SPatrick Venture "id" : 1,
5002e32376SPatrick Venture "containerEntityId" : 2,
5102e32376SPatrick Venture "containerEntityInstance" : 3,
5202e32376SPatrick Venture "isList" : false,
5302e32376SPatrick Venture "entities" : [
5402e32376SPatrick Venture {"id" : 1, "instance" : 2},
5502e32376SPatrick Venture {"id" : 1, "instance" : 3},
5602e32376SPatrick Venture {"id" : 1, "instance" : 4},
5702e32376SPatrick Venture {"id" : 1, "instance" : 5}
5802e32376SPatrick Venture ]
5902e32376SPatrick Venture }
6002e32376SPatrick Venture ]
6102e32376SPatrick Venture )"_json;
6202e32376SPatrick Venture
6302e32376SPatrick Venture EXPECT_THAT(buildJsonEntityMap(j), IsEmpty());
6402e32376SPatrick Venture }
6502e32376SPatrick Venture
TEST(ValidateJson,AllValidEntryReturnsExpectedMap)6602e32376SPatrick Venture TEST(ValidateJson, AllValidEntryReturnsExpectedMap)
6702e32376SPatrick Venture {
6802e32376SPatrick Venture /* Boring test where we provide completely valid information and expect the
6902e32376SPatrick Venture * resulting map contains that information.
7002e32376SPatrick Venture */
7102e32376SPatrick Venture auto j = R"(
7202e32376SPatrick Venture [
7302e32376SPatrick Venture {
7402e32376SPatrick Venture "id" : 1,
7502e32376SPatrick Venture "containerEntityId" : 2,
7602e32376SPatrick Venture "containerEntityInstance" : 3,
7702e32376SPatrick Venture "isList" : false,
7802e32376SPatrick Venture "isLinked" : false,
7902e32376SPatrick Venture "entities" : [
8002e32376SPatrick Venture {"id" : 1, "instance" : 2},
8102e32376SPatrick Venture {"id" : 1, "instance" : 3},
8202e32376SPatrick Venture {"id" : 1, "instance" : 4},
8302e32376SPatrick Venture {"id" : 1, "instance" : 5}
8402e32376SPatrick Venture ]
8502e32376SPatrick Venture }
8602e32376SPatrick Venture ]
8702e32376SPatrick Venture )"_json;
8802e32376SPatrick Venture
8902e32376SPatrick Venture auto map = buildJsonEntityMap(j);
9002e32376SPatrick Venture EXPECT_FALSE(map.find(1) == map.end());
9102e32376SPatrick Venture auto entry = map.find(1);
9202e32376SPatrick Venture EXPECT_EQ(entry->first, 1);
9302e32376SPatrick Venture
9402e32376SPatrick Venture /* TODO: someone could write an equality operator for this object. */
9502e32376SPatrick Venture EXPECT_EQ(entry->second.containerEntityId, 2);
9602e32376SPatrick Venture EXPECT_EQ(entry->second.containerEntityInstance, 3);
9702e32376SPatrick Venture EXPECT_FALSE(entry->second.isList);
9802e32376SPatrick Venture EXPECT_FALSE(entry->second.isLinked);
9902e32376SPatrick Venture ContainedEntitiesArray expected = {
10002e32376SPatrick Venture std::make_pair(1, 2), std::make_pair(1, 3), std::make_pair(1, 4),
10102e32376SPatrick Venture std::make_pair(1, 5)};
10202e32376SPatrick Venture EXPECT_EQ(entry->second.containedEntities, expected);
10302e32376SPatrick Venture }
10402e32376SPatrick Venture
TEST(ValidateJson,EntryHasInsufficientContainerEntryCountReturnsEmpty)10502e32376SPatrick Venture TEST(ValidateJson, EntryHasInsufficientContainerEntryCountReturnsEmpty)
10602e32376SPatrick Venture {
10702e32376SPatrick Venture /* The container must have four pairs. (I don't know why, and maybe this
10802e32376SPatrick Venture * restriction will change).
10902e32376SPatrick Venture */
11002e32376SPatrick Venture auto j = R"(
11102e32376SPatrick Venture [
11202e32376SPatrick Venture {
11302e32376SPatrick Venture "id" : 1,
11402e32376SPatrick Venture "containerEntityId" : 2,
11502e32376SPatrick Venture "containerEntityInstance" : 3,
11602e32376SPatrick Venture "isList" : false,
11702e32376SPatrick Venture "isLinked" : false,
11802e32376SPatrick Venture "entities" : [
11902e32376SPatrick Venture {"id" : 1, "instance" : 2},
12002e32376SPatrick Venture {"id" : 1, "instance" : 3},
12102e32376SPatrick Venture {"id" : 1, "instance" : 4}
12202e32376SPatrick Venture ]
12302e32376SPatrick Venture }
12402e32376SPatrick Venture ]
12502e32376SPatrick Venture )"_json;
12602e32376SPatrick Venture
12702e32376SPatrick Venture EXPECT_THAT(buildJsonEntityMap(j), IsEmpty());
12802e32376SPatrick Venture }
12902e32376SPatrick Venture
TEST(ValidateJson,ThereAreTwoEntriesOneInvalidReturnsEmpty)13002e32376SPatrick Venture TEST(ValidateJson, ThereAreTwoEntriesOneInvalidReturnsEmpty)
13102e32376SPatrick Venture {
13202e32376SPatrick Venture /* If any entry in the file is corrupt, the file is disregarded. */
13302e32376SPatrick Venture auto j = R"(
13402e32376SPatrick Venture [
13502e32376SPatrick Venture {
13602e32376SPatrick Venture "id" : 1,
13702e32376SPatrick Venture "containerEntityId" : 2,
13802e32376SPatrick Venture "containerEntityInstance" : 3,
13902e32376SPatrick Venture "isList" : false,
14002e32376SPatrick Venture "isLinked" : false,
14102e32376SPatrick Venture "entities" : [
14202e32376SPatrick Venture {"id" : 1, "instance" : 2},
14302e32376SPatrick Venture {"id" : 1, "instance" : 3},
14402e32376SPatrick Venture {"id" : 1, "instance" : 4},
14502e32376SPatrick Venture {"id" : 1, "instance" : 5}
14602e32376SPatrick Venture ]
14702e32376SPatrick Venture },
14802e32376SPatrick Venture {
14902e32376SPatrick Venture "id" : 2,
15002e32376SPatrick Venture "containerEntityId" : 2,
15102e32376SPatrick Venture "containerEntityInstance" : 3,
15202e32376SPatrick Venture "isList" : false,
15302e32376SPatrick Venture "isLinked" : false,
15402e32376SPatrick Venture "entities" : [
15502e32376SPatrick Venture {"id" : 1, "instance" : 2},
15602e32376SPatrick Venture {"id" : 1, "instance" : 3},
15702e32376SPatrick Venture {"id" : 1, "instance" : 4}
15802e32376SPatrick Venture ]
15902e32376SPatrick Venture }
16002e32376SPatrick Venture ]
16102e32376SPatrick Venture )"_json;
16202e32376SPatrick Venture
16302e32376SPatrick Venture EXPECT_THAT(buildJsonEntityMap(j), IsEmpty());
16402e32376SPatrick Venture }
16502e32376SPatrick Venture
TEST(ValidateJson,ThereAreTwoEntriesBothValidReturnsBoth)16602e32376SPatrick Venture TEST(ValidateJson, ThereAreTwoEntriesBothValidReturnsBoth)
16702e32376SPatrick Venture {
16802e32376SPatrick Venture /* The map supports more than one entry, just validate this. */
16902e32376SPatrick Venture auto j = R"(
17002e32376SPatrick Venture [
17102e32376SPatrick Venture {
17202e32376SPatrick Venture "id" : 1,
17302e32376SPatrick Venture "containerEntityId" : 2,
17402e32376SPatrick Venture "containerEntityInstance" : 3,
17502e32376SPatrick Venture "isList" : false,
17602e32376SPatrick Venture "isLinked" : false,
17702e32376SPatrick Venture "entities" : [
17802e32376SPatrick Venture {"id" : 1, "instance" : 2},
17902e32376SPatrick Venture {"id" : 1, "instance" : 3},
18002e32376SPatrick Venture {"id" : 1, "instance" : 4},
18102e32376SPatrick Venture {"id" : 1, "instance" : 5}
18202e32376SPatrick Venture ]
18302e32376SPatrick Venture },
18402e32376SPatrick Venture {
18502e32376SPatrick Venture "id" : 2,
18602e32376SPatrick Venture "containerEntityId" : 2,
18702e32376SPatrick Venture "containerEntityInstance" : 3,
18802e32376SPatrick Venture "isList" : false,
18902e32376SPatrick Venture "isLinked" : false,
19002e32376SPatrick Venture "entities" : [
19102e32376SPatrick Venture {"id" : 1, "instance" : 6},
19202e32376SPatrick Venture {"id" : 1, "instance" : 7},
19302e32376SPatrick Venture {"id" : 1, "instance" : 8},
19402e32376SPatrick Venture {"id" : 1, "instance" : 9}
19502e32376SPatrick Venture ]
19602e32376SPatrick Venture }
19702e32376SPatrick Venture ]
19802e32376SPatrick Venture )"_json;
19902e32376SPatrick Venture
20002e32376SPatrick Venture auto map = buildJsonEntityMap(j);
20102e32376SPatrick Venture EXPECT_FALSE(map.find(1) == map.end());
20202e32376SPatrick Venture EXPECT_FALSE(map.find(2) == map.end());
20302e32376SPatrick Venture
20402e32376SPatrick Venture auto entry = map.find(1);
20502e32376SPatrick Venture EXPECT_EQ(entry->first, 1);
20602e32376SPatrick Venture EXPECT_EQ(entry->second.containerEntityId, 2);
20702e32376SPatrick Venture EXPECT_EQ(entry->second.containerEntityInstance, 3);
20802e32376SPatrick Venture EXPECT_FALSE(entry->second.isList);
20902e32376SPatrick Venture EXPECT_FALSE(entry->second.isLinked);
21002e32376SPatrick Venture ContainedEntitiesArray expected = {
21102e32376SPatrick Venture std::make_pair(1, 2), std::make_pair(1, 3), std::make_pair(1, 4),
21202e32376SPatrick Venture std::make_pair(1, 5)};
21302e32376SPatrick Venture EXPECT_EQ(entry->second.containedEntities, expected);
21402e32376SPatrick Venture
21502e32376SPatrick Venture entry = map.find(2);
21602e32376SPatrick Venture expected = {std::make_pair(1, 6), std::make_pair(1, 7),
21702e32376SPatrick Venture std::make_pair(1, 8), std::make_pair(1, 9)};
21802e32376SPatrick Venture EXPECT_EQ(entry->second.containedEntities, expected);
21902e32376SPatrick Venture }
22002e32376SPatrick Venture
22102e32376SPatrick Venture } // namespace
22202e32376SPatrick Venture } // namespace sensor
22302e32376SPatrick Venture } // namespace ipmi
224