VersionEntityTests.cpp (14493B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 4 #include "../../../util/SFSExceptionMatcher.h" 5 #include "../../../util/TestHelper.h" 6 #include "ReportingHandler.h" 7 #include "entity/VersionEntity.h" 8 #include "sfsclient/ContentId.h" 9 10 #include <catch2/catch_test_macros.hpp> 11 #include <nlohmann/json.hpp> 12 13 #define TEST(...) TEST_CASE("[VersionEntityTests] " __VA_ARGS__) 14 15 using namespace SFS; 16 using namespace SFS::details; 17 using namespace SFS::test; 18 using json = nlohmann::json; 19 20 // GenericVersionEntity constants 21 const std::string c_ns = "namespace"; 22 const std::string c_name = "name"; 23 const std::string c_version = "version"; 24 25 // AppVersionEntity constants 26 const std::string c_updateId = "updateId"; 27 28 TEST("Testing VersionEntity::FromJson()") 29 { 30 ReportingHandler handler; 31 handler.SetLoggingCallback(LogCallbackToTest); 32 33 SECTION("Generic Version Entity") 34 { 35 std::unique_ptr<VersionEntity> entity; 36 SECTION("Correct") 37 { 38 const json versionEntity = {{"ContentId", {{"Namespace", c_ns}, {"Name", c_name}, {"Version", c_version}}}}; 39 40 REQUIRE_NOTHROW(entity = VersionEntity::FromJson(versionEntity, handler)); 41 REQUIRE(entity != nullptr); 42 REQUIRE(entity->GetContentType() == ContentType::Generic); 43 REQUIRE(entity->contentId.nameSpace == c_ns); 44 REQUIRE(entity->contentId.name == c_name); 45 REQUIRE(entity->contentId.version == c_version); 46 } 47 48 SECTION("Missing fields") 49 { 50 SECTION("Missing ContentId") 51 { 52 const json versionEntity = {{"Namespace", c_ns}, {"Name", c_name}, {"Version", c_version}}; 53 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 54 ServiceInvalidResponse, 55 "Missing ContentId in response"); 56 } 57 58 SECTION("Missing ContentId.Namespace") 59 { 60 const json versionEntity = {{"ContentId", {{"Name", c_name}, {"Version", c_version}}}}; 61 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 62 ServiceInvalidResponse, 63 "Missing ContentId.Namespace in response"); 64 } 65 66 SECTION("Missing ContentId.Name") 67 { 68 const json versionEntity = {{"ContentId", {{"Namespace", c_ns}, {"Version", c_version}}}}; 69 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 70 ServiceInvalidResponse, 71 "Missing ContentId.Name in response"); 72 } 73 74 SECTION("Missing ContentId.Version") 75 { 76 const json versionEntity = {{"ContentId", {{"Namespace", c_ns}, {"Name", c_name}}}}; 77 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 78 ServiceInvalidResponse, 79 "Missing ContentId.Version in response"); 80 } 81 } 82 83 SECTION("Wrong types") 84 { 85 SECTION("ContentId not an object") 86 { 87 const json versionEntity = json::array({{"Namespace", 1}, {"Name", c_name}, {"Version", c_version}}); 88 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 89 ServiceInvalidResponse, 90 "Response is not a JSON object"); 91 } 92 93 SECTION("ContentId.Namespace") 94 { 95 const json versionEntity = { 96 {"ContentId", {{"Namespace", 1}, {"Name", c_name}, {"Version", c_version}}}}; 97 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 98 ServiceInvalidResponse, 99 "ContentId.Namespace is not a string"); 100 } 101 102 SECTION("ContentId.Name") 103 { 104 const json versionEntity = {{"ContentId", {{"Namespace", c_ns}, {"Name", 1}, {"Version", c_version}}}}; 105 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 106 ServiceInvalidResponse, 107 "ContentId.Name is not a string"); 108 } 109 110 SECTION("ContentId.Version") 111 { 112 const json versionEntity = {{"ContentId", {{"Namespace", c_ns}, {"Name", c_name}, {"Version", 1}}}}; 113 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 114 ServiceInvalidResponse, 115 "ContentId.Version is not a string"); 116 } 117 } 118 } 119 120 SECTION("App Version Entity") 121 { 122 std::unique_ptr<VersionEntity> entity; 123 const json contentId = {{"Namespace", c_ns}, {"Name", c_name}, {"Version", c_version}}; 124 125 SECTION("Correct") 126 { 127 const json versionEntity = {{"ContentId", contentId}, 128 {"UpdateId", c_updateId}, 129 {"Prerequisites", json::array({contentId})}}; 130 131 REQUIRE_NOTHROW(entity = VersionEntity::FromJson(versionEntity, handler)); 132 REQUIRE(entity != nullptr); 133 REQUIRE(entity->GetContentType() == ContentType::App); 134 REQUIRE(entity->contentId.nameSpace == c_ns); 135 REQUIRE(entity->contentId.name == c_name); 136 REQUIRE(entity->contentId.version == c_version); 137 138 AppVersionEntity* appEntity = dynamic_cast<AppVersionEntity*>(entity.get()); 139 REQUIRE(appEntity->updateId == c_updateId); 140 REQUIRE(appEntity->prerequisites.size() == 1); 141 REQUIRE(appEntity->prerequisites[0].contentId.nameSpace == c_ns); 142 REQUIRE(appEntity->prerequisites[0].contentId.name == c_name); 143 REQUIRE(appEntity->prerequisites[0].contentId.version == c_version); 144 } 145 146 SECTION("Missing fields") 147 { 148 SECTION("Missing Prerequisites") 149 { 150 const json versionEntity = {{"ContentId", contentId}, {"UpdateId", c_updateId}}; 151 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 152 ServiceInvalidResponse, 153 "Missing Prerequisites in response"); 154 } 155 156 SECTION("Missing Prerequisite.Namespace") 157 { 158 const json wrongPrerequisite = {{"Name", c_name}, {"Version", c_version}}; 159 const json versionEntity = {{"ContentId", contentId}, 160 {"UpdateId", c_updateId}, 161 {"Prerequisites", json::array({wrongPrerequisite})}}; 162 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 163 ServiceInvalidResponse, 164 "Missing Prerequisite.Namespace in response"); 165 } 166 167 SECTION("Missing Prerequisite.Name") 168 { 169 const json wrongPrerequisite = {{"Namespace", c_ns}, {"Version", c_version}}; 170 const json versionEntity = {{"ContentId", contentId}, 171 {"UpdateId", c_updateId}, 172 {"Prerequisites", json::array({wrongPrerequisite})}}; 173 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 174 ServiceInvalidResponse, 175 "Missing Prerequisite.Name in response"); 176 } 177 178 SECTION("Missing Prerequisite.Version") 179 { 180 const json wrongPrerequisite = {{"Namespace", c_ns}, {"Name", c_name}}; 181 const json versionEntity = {{"ContentId", contentId}, 182 {"UpdateId", c_updateId}, 183 {"Prerequisites", json::array({wrongPrerequisite})}}; 184 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 185 ServiceInvalidResponse, 186 "Missing Prerequisite.Version in response"); 187 } 188 } 189 190 SECTION("Wrong types") 191 { 192 SECTION("UpdateId") 193 { 194 const json versionEntity = {{"ContentId", contentId}, 195 {"UpdateId", 1}, 196 {"Prerequisites", json::array({contentId})}}; 197 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 198 ServiceInvalidResponse, 199 "UpdateId is not a string"); 200 } 201 202 SECTION("Prerequisites") 203 { 204 SECTION("Not an array") 205 { 206 const json versionEntity = {{"ContentId", contentId}, 207 {"UpdateId", c_updateId}, 208 {"Prerequisites", contentId}}; 209 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 210 ServiceInvalidResponse, 211 "Prerequisites is not an array"); 212 } 213 214 SECTION("Element is not an object") 215 { 216 const json versionEntity = {{"ContentId", contentId}, 217 {"UpdateId", c_updateId}, 218 {"Prerequisites", json::array({1})}}; 219 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 220 ServiceInvalidResponse, 221 "Prerequisite element is not a JSON object"); 222 } 223 224 SECTION("Prerequisite.Namespace") 225 { 226 const json wrongPrerequisite = {{"Namespace", 1}, {"Name", c_name}, {"Version", c_version}}; 227 const json versionEntity = {{"ContentId", contentId}, 228 {"UpdateId", c_updateId}, 229 {"Prerequisites", json::array({wrongPrerequisite})}}; 230 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 231 ServiceInvalidResponse, 232 "Prerequisite.Namespace is not a string"); 233 } 234 235 SECTION("Prerequisite.Name") 236 { 237 const json wrongPrerequisite = {{"Namespace", c_ns}, {"Name", 1}, {"Version", c_version}}; 238 const json versionEntity = {{"ContentId", contentId}, 239 {"UpdateId", c_updateId}, 240 {"Prerequisites", json::array({wrongPrerequisite})}}; 241 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 242 ServiceInvalidResponse, 243 "Prerequisite.Name is not a string"); 244 } 245 246 SECTION("Prerequisite.Version") 247 { 248 const json wrongPrerequisite = {{"Namespace", c_ns}, {"Name", c_name}, {"Version", 1}}; 249 const json versionEntity = {{"ContentId", contentId}, 250 {"UpdateId", c_updateId}, 251 {"Prerequisites", json::array({wrongPrerequisite})}}; 252 REQUIRE_THROWS_CODE_MSG(VersionEntity::FromJson(versionEntity, handler), 253 ServiceInvalidResponse, 254 "Prerequisite.Version is not a string"); 255 } 256 } 257 } 258 } 259 } 260 261 TEST("Testing VersionEntity conversions") 262 { 263 ReportingHandler handler; 264 handler.SetLoggingCallback(LogCallbackToTest); 265 266 auto CheckContentId = [&](const ContentId& contentId) { 267 REQUIRE(contentId.GetNameSpace() == c_ns); 268 REQUIRE(contentId.GetName() == c_name); 269 REQUIRE(contentId.GetVersion() == c_version); 270 }; 271 272 SECTION("VersionEntity::ToContentId()") 273 { 274 SECTION("Success with GenericVersionEntity") 275 { 276 std::unique_ptr<VersionEntity> entity = std::make_unique<GenericVersionEntity>(); 277 entity->contentId.nameSpace = c_ns; 278 entity->contentId.name = c_name; 279 entity->contentId.version = c_version; 280 281 auto contentId = VersionEntity::ToContentId(std::move(*entity), handler); 282 CheckContentId(*contentId); 283 } 284 285 SECTION("Success with AppVersionEntity") 286 { 287 std::unique_ptr<VersionEntity> entity = std::make_unique<AppVersionEntity>(); 288 auto appEntity = AppVersionEntity::GetAppVersionEntityPtr(entity, handler); 289 appEntity->contentId.nameSpace = c_ns; 290 appEntity->contentId.name = c_name; 291 appEntity->contentId.version = c_version; 292 appEntity->updateId = c_updateId; 293 294 std::unique_ptr<GenericVersionEntity> prereqEntity = std::make_unique<GenericVersionEntity>(); 295 prereqEntity->contentId.nameSpace = c_ns; 296 prereqEntity->contentId.name = c_name; 297 prereqEntity->contentId.version = c_version; 298 299 appEntity->prerequisites.push_back(std::move(*prereqEntity)); 300 301 auto contentId = VersionEntity::ToContentId(std::move(*entity), handler); 302 CheckContentId(*contentId); 303 } 304 } 305 }