diff options
author | Nan Zhou <nanzhoumails@gmail.com> | 2022-06-30 03:39:48 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2023-05-26 00:16:13 +0300 |
commit | 185444b1dd5f28702e287c4ce7f7b6558356519b (patch) | |
tree | 18db9e2bbcc354de94f4fe572ab61398885832da /test | |
parent | 4b242749b31b16c663fe2e95ad7dbc6a2f3ca973 (diff) | |
download | bmcweb-185444b1dd5f28702e287c4ce7f7b6558356519b.tar.xz |
json utility: add sort
This commit adds a utility function |sortJsonArrayByKey|. It can sort an
json array by value of a given key of each element.
Use cases includes:
1. sort the MemberCollection by @odata.id
Tested:
1. unit test passed;
Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: Idc175fab3af5c6102a5a3439b712b659ecb76468
Diffstat (limited to 'test')
-rw-r--r-- | test/redfish-core/include/utils/json_utils_test.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/test/redfish-core/include/utils/json_utils_test.cpp b/test/redfish-core/include/utils/json_utils_test.cpp index fa5e39a540..3fca4e72b1 100644 --- a/test/redfish-core/include/utils/json_utils_test.cpp +++ b/test/redfish-core/include/utils/json_utils_test.cpp @@ -354,5 +354,58 @@ TEST(ReadJsonAction, EmptyObjectReturnsTrueResponseOk) EXPECT_THAT(res.jsonValue, IsEmpty()); } +TEST(odataObjectCmp, PositiveCases) +{ + EXPECT_EQ(0, odataObjectCmp(R"({"@odata.id": "/redfish/v1/1"})"_json, + R"({"@odata.id": "/redfish/v1/1"})"_json)); + EXPECT_EQ(0, odataObjectCmp(R"({"@odata.id": ""})"_json, + R"({"@odata.id": ""})"_json)); + EXPECT_EQ(0, odataObjectCmp(R"({"@odata.id": 42})"_json, + R"({"@odata.id": 0})"_json)); + EXPECT_EQ(0, odataObjectCmp(R"({})"_json, R"({})"_json)); + + EXPECT_GT(0, odataObjectCmp(R"({"@odata.id": "/redfish/v1"})"_json, + R"({"@odata.id": "/redfish/v1/1"})"_json)); + EXPECT_LT(0, odataObjectCmp(R"({"@odata.id": "/redfish/v1/1"})"_json, + R"({"@odata.id": "/redfish/v1"})"_json)); + + EXPECT_LT(0, odataObjectCmp(R"({"@odata.id": "/10"})"_json, + R"({"@odata.id": "/1"})"_json)); + EXPECT_GT(0, odataObjectCmp(R"({"@odata.id": "/1"})"_json, + R"({"@odata.id": "/10"})"_json)); + + EXPECT_GT(0, odataObjectCmp(R"({})"_json, R"({"@odata.id": "/1"})"_json)); + EXPECT_LT(0, odataObjectCmp(R"({"@odata.id": "/1"})"_json, R"({})"_json)); + + EXPECT_GT(0, odataObjectCmp(R"({"@odata.id": 4})"_json, + R"({"@odata.id": "/1"})"_json)); + EXPECT_LT(0, odataObjectCmp(R"({"@odata.id": "/1"})"_json, + R"({"@odata.id": 4})"_json)); +} + +TEST(SortJsonArrayByKey, ElementMissingKeyReturnsFalseArrayIsPartlySorted) +{ + nlohmann::json::array_t array = + R"([{"@odata.id" : "/redfish/v1/100"}, {"@odata.id": "/redfish/v1/1"}, {"@odata.id" : "/redfish/v1/20"}])"_json; + sortJsonArrayByOData(array); + // Objects with other keys are always larger than those with the specified + // key. + EXPECT_THAT(array, + ElementsAre(R"({"@odata.id": "/redfish/v1/1"})"_json, + R"({"@odata.id" : "/redfish/v1/20"})"_json, + R"({"@odata.id" : "/redfish/v1/100"})"_json)); +} + +TEST(SortJsonArrayByKey, SortedByStringValueOnSuccessArrayIsSorted) +{ + nlohmann::json::array_t array = + R"([{"@odata.id": "/redfish/v1/20"}, {"@odata.id" : "/redfish/v1"}, {"@odata.id" : "/redfish/v1/100"}])"_json; + sortJsonArrayByOData(array); + EXPECT_THAT(array, + ElementsAre(R"({"@odata.id": "/redfish/v1"})"_json, + R"({"@odata.id" : "/redfish/v1/20"})"_json, + R"({"@odata.id" : "/redfish/v1/100"})"_json)); +} + } // namespace } // namespace redfish::json_util |