JsonHelper.cpp (4428B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "TestCommon.h" 5 #include <winget/JsonUtil.h> 6 #include "cpprest/json.h" 7 8 using namespace AppInstaller; 9 10 web::json::value GetTestJsonObject() 11 { 12 web::json::value jsonObject = web::json::value::object(); 13 jsonObject[L"Key1"] = web::json::value::string(L"Value1"); 14 jsonObject[L"Key2"] = web::json::value::string(L"Value2"); 15 jsonObject[L"IntKey"] = 100; 16 17 web::json::value arrayValue = web::json::value::array(); 18 arrayValue[0] = web::json::value::string(L"ArrayValue1"); 19 arrayValue[1] = web::json::value::string(L"ArrayValue2"); 20 arrayValue[2] = web::json::value::string(L"ArrayValue3"); 21 jsonObject[L"Array"] = arrayValue; 22 23 return jsonObject; 24 } 25 26 TEST_CASE("GetUtilityString", "[RestSource]") 27 { 28 REQUIRE(JSON::GetUtilityString("cpprest") == L"cpprest"); 29 REQUIRE(JSON::GetUtilityString(" ") == L" "); 30 } 31 32 TEST_CASE("GetJsonValueFromNode", "[RestSource]") 33 { 34 web::json::value jsonObject = GetTestJsonObject(); 35 std::optional<std::reference_wrapper<const web::json::value>> actual = JSON::GetJsonValueFromNode(jsonObject, L"Key1"); 36 REQUIRE(actual); 37 REQUIRE(actual.value().get().as_string() == L"Value1"); 38 39 std::optional<std::reference_wrapper<const web::json::value>> absentKey = JSON::GetJsonValueFromNode(jsonObject, L"Key3"); 40 REQUIRE(!absentKey); 41 42 web::json::value emptyObject; 43 std::optional<std::reference_wrapper<const web::json::value>> empty = JSON::GetJsonValueFromNode(emptyObject, L"Key1"); 44 REQUIRE(!empty); 45 } 46 47 TEST_CASE("GetRawStringValueFromJsonValue", "[RestSource]") 48 { 49 std::optional<std::string> stringTest = JSON::GetRawStringValueFromJsonValue(web::json::value::string(L"cpprest ")); 50 REQUIRE(stringTest); 51 REQUIRE(stringTest.value() == "cpprest "); 52 53 std::optional<std::string> emptyTest = JSON::GetRawStringValueFromJsonValue(web::json::value::string(L" ")); 54 REQUIRE(emptyTest); 55 REQUIRE(emptyTest.value() == " "); 56 57 web::json::value obj; 58 std::optional<std::string> nullTest = JSON::GetRawStringValueFromJsonValue(obj); 59 REQUIRE(!nullTest); 60 61 web::json::value integer = 100; 62 std::optional<std::string> mismatchFieldTest = JSON::GetRawStringValueFromJsonValue(integer); 63 REQUIRE(!mismatchFieldTest); 64 } 65 66 TEST_CASE("GetRawStringValueFromJsonNode", "[RestSource]") 67 { 68 web::json::value jsonObject = GetTestJsonObject(); 69 70 std::optional<std::string> stringTest = JSON::GetRawStringValueFromJsonNode(jsonObject, L"Key1"); 71 REQUIRE(stringTest); 72 REQUIRE(stringTest.value() == "Value1"); 73 74 std::optional<std::string> emptyTest = JSON::GetRawStringValueFromJsonNode(jsonObject, L"Key3"); 75 REQUIRE(!emptyTest); 76 77 std::optional<std::string> mismatchFieldTest = JSON::GetRawStringValueFromJsonNode(jsonObject, L"IntKey"); 78 REQUIRE(!mismatchFieldTest); 79 } 80 81 TEST_CASE("GetRawIntValueFromJsonValue", "[RestSource]") 82 { 83 web::json::value jsonObject = 100; 84 std::optional<int> expected = JSON::GetRawIntValueFromJsonValue(jsonObject); 85 REQUIRE(expected); 86 REQUIRE(expected.value() == 100); 87 88 std::optional<int> mismatchFieldTest = JSON::GetRawIntValueFromJsonValue(web::json::value::string(L"cpprest")); 89 REQUIRE(!mismatchFieldTest); 90 } 91 92 TEST_CASE("GetRawJsonArrayFromJsonNode", "[RestSource]") 93 { 94 web::json::value jsonObject = GetTestJsonObject(); 95 std::optional<std::reference_wrapper<const web::json::array>> expected = JSON::GetRawJsonArrayFromJsonNode(jsonObject, L"Array"); 96 REQUIRE(expected); 97 REQUIRE(expected.value().get().size() == 3); 98 REQUIRE(expected.value().get().at(0).as_string() == L"ArrayValue1"); 99 100 std::optional<std::reference_wrapper<const web::json::array>> mismatchFieldTest = JSON::GetRawJsonArrayFromJsonNode(jsonObject, L"Keyword"); 101 REQUIRE(!mismatchFieldTest); 102 } 103 104 TEST_CASE("GetRawStringArrayFromJsonNode", "[RestSource]") 105 { 106 web::json::value jsonObject = GetTestJsonObject(); 107 std::vector<std::string> expected = JSON::GetRawStringArrayFromJsonNode(jsonObject, L"Array"); 108 REQUIRE(expected.size() == 3); 109 REQUIRE(expected[0] == "ArrayValue1"); 110 111 std::vector<std::string> mismatchFieldTest = JSON::GetRawStringArrayFromJsonNode(jsonObject, L"Keyword"); 112 REQUIRE(mismatchFieldTest.size() == 0); 113 }