From d3d244d729cf6893dec6792a9401c87222ecd77c Mon Sep 17 00:00:00 2001 From: Nick Porter <43650450+nporter-adbe@users.noreply.github.com> Date: Wed, 3 Mar 2021 15:01:15 -0700 Subject: [PATCH] Change CustomIdentity to use underscores during encoding (#550) * Fix CustomIdentity encoding keys * Make CustomIdentity init public * Override decode to support decoding incorrect keys for migration * Remove migration keys * remove public modified * Update CustomIdentity.swift --- AEPIdentity/Sources/CustomIdentity.swift | 8 +- AEPIdentity/Tests/CustomIdentityTests.swift | 196 ++++++++++++++++++++ 2 files changed, 200 insertions(+), 4 deletions(-) diff --git a/AEPIdentity/Sources/CustomIdentity.swift b/AEPIdentity/Sources/CustomIdentity.swift index 87c9d7ca1..b5256e3a8 100644 --- a/AEPIdentity/Sources/CustomIdentity.swift +++ b/AEPIdentity/Sources/CustomIdentity.swift @@ -12,7 +12,7 @@ import Foundation /// CustomIdentity contains identifier origin, identifier type, identifier value and authentication state. -public class CustomIdentity: Identifiable, Codable { +class CustomIdentity: Identifiable, Codable { public var origin: String? public var type: String? public var identifier: String? @@ -32,10 +32,10 @@ public class CustomIdentity: Identifiable, Codable { } enum CodingKeys: String, CodingKey { - case origin = "id.origin" - case type = "id.type" + case origin = "id_origin" + case type = "id_type" case identifier = "id" - case authenticationState = "authentication.state" + case authenticationState = "authentication_state" } } diff --git a/AEPIdentity/Tests/CustomIdentityTests.swift b/AEPIdentity/Tests/CustomIdentityTests.swift index 1ec668833..d218d27fc 100644 --- a/AEPIdentity/Tests/CustomIdentityTests.swift +++ b/AEPIdentity/Tests/CustomIdentityTests.swift @@ -14,6 +14,202 @@ import XCTest class CustomIdentityTests: XCTestCase { + // MARK: Codable tests + + func testCustomIdentityEmptyString_DecodingReturnsNil() { + // setup + let customIdentityJson = "" + + // test decoding + let decodedCustomId = try? JSONDecoder().decode(CustomIdentity.self, from: customIdentityJson.data(using: .utf8)!) + + // verify decoding + XCTAssertNil(decodedCustomId) + } + + func testCustomIdentityEmptyJson_DecodingReturnsNil() { + // setup + let customIdentityJson = """ + { + } + """ + + // test decoding + let decodedCustomId = try? JSONDecoder().decode(CustomIdentity.self, from: customIdentityJson.data(using: .utf8)!) + + // verify decoding + XCTAssertNil(decodedCustomId) + } + + func testCustomIdentityInvalidJson_DecodingReturnsNil() { + // setup + let customIdentityJson = """ + { + "not-a-real-key": "some-value" + } + """ + + // test decoding + let decodedCustomId = try? JSONDecoder().decode(CustomIdentity.self, from: customIdentityJson.data(using: .utf8)!) + + // verify decoding + XCTAssertNil(decodedCustomId) + } + + func testCustomIdentityAllProperties_EncodesAndDecodesWithExtraneousKey() { + // setup + let customIdentityJson = """ + { + "id_type" : "test-type", + "id" : "test-id", + "id_origin" : "test-origin", + "authentication_state" : 1, + "not-a-real-key": "some-value" + } + """ + + let expectedCustomIdentityJson = """ + { + "id_type" : "test-type", + "id" : "test-id", + "id_origin" : "test-origin", + "authentication_state" : 1 + } + """ + + // test decoding + let decodedCustomId = try! JSONDecoder().decode(CustomIdentity.self, from: customIdentityJson.data(using: .utf8)!) + + // verify decoding + XCTAssertEqual("test-type", decodedCustomId.type) + XCTAssertEqual("test-id", decodedCustomId.identifier) + XCTAssertEqual("test-origin", decodedCustomId.origin) + XCTAssertEqual(MobileVisitorAuthenticationState.authenticated, decodedCustomId.authenticationState) + + // test encoding + let encoder = JSONEncoder() + encoder.outputFormatting = .prettyPrinted + let encoded = try! encoder.encode(decodedCustomId) + + // verify encoding + XCTAssertEqual(expectedCustomIdentityJson, String(data: encoded, encoding: .utf8)) + } + + func testCustomIdentityAllProperties_EncodesAndDecodes() { + // setup + let customIdentityJson = """ + { + "id_type" : "test-type", + "id" : "test-id", + "id_origin" : "test-origin", + "authentication_state" : 1 + } + """ + + // test decoding + let decodedCustomId = try! JSONDecoder().decode(CustomIdentity.self, from: customIdentityJson.data(using: .utf8)!) + + // verify decoding + XCTAssertEqual("test-type", decodedCustomId.type) + XCTAssertEqual("test-id", decodedCustomId.identifier) + XCTAssertEqual("test-origin", decodedCustomId.origin) + XCTAssertEqual(MobileVisitorAuthenticationState.authenticated, decodedCustomId.authenticationState) + + // test encoding + let encoder = JSONEncoder() + encoder.outputFormatting = .prettyPrinted + let encoded = try! encoder.encode(decodedCustomId) + + // verify encoding + XCTAssertEqual(customIdentityJson, String(data: encoded, encoding: .utf8)) + } + + func testCustomIdentityMissingType_EncodesAndDecodes() { + // setup + let customIdentityJson = """ + { + "id" : "test-id", + "authentication_state" : 1, + "id_origin" : "test-origin" + } + """ + + // test decoding + let decodedCustomId = try! JSONDecoder().decode(CustomIdentity.self, from: customIdentityJson.data(using: .utf8)!) + + // verify decoding + XCTAssertNil(decodedCustomId.type) + XCTAssertEqual("test-id", decodedCustomId.identifier) + XCTAssertEqual("test-origin", decodedCustomId.origin) + XCTAssertEqual(MobileVisitorAuthenticationState.authenticated, decodedCustomId.authenticationState) + + // test encoding + let encoder = JSONEncoder() + encoder.outputFormatting = .prettyPrinted + let encoded = try! encoder.encode(decodedCustomId) + + // verify encoding + XCTAssertEqual(customIdentityJson, String(data: encoded, encoding: .utf8)) + } + + func testCustomIdentityMissingId_EncodesAndDecodes() { + // setup + let customIdentityJson = """ + { + "id_type" : "test-type", + "authentication_state" : 1, + "id_origin" : "test-origin" + } + """ + + // test decoding + let decodedCustomId = try! JSONDecoder().decode(CustomIdentity.self, from: customIdentityJson.data(using: .utf8)!) + + // verify decoding + XCTAssertEqual("test-type", decodedCustomId.type) + XCTAssertNil(decodedCustomId.identifier) + XCTAssertEqual("test-origin", decodedCustomId.origin) + XCTAssertEqual(MobileVisitorAuthenticationState.authenticated, decodedCustomId.authenticationState) + + // test encoding + let encoder = JSONEncoder() + encoder.outputFormatting = .prettyPrinted + let encoded = try! encoder.encode(decodedCustomId) + + // verify encoding + XCTAssertEqual(customIdentityJson, String(data: encoded, encoding: .utf8)) + } + + func testCustomIdentityMissingOrigin_EncodesAndDecodes() { + // setup + let customIdentityJson = """ + { + "id_type" : "test-type", + "id" : "test-id", + "authentication_state" : 1 + } + """ + + // test decoding + let decodedCustomId = try! JSONDecoder().decode(CustomIdentity.self, from: customIdentityJson.data(using: .utf8)!) + + // verify decoding + XCTAssertEqual("test-type", decodedCustomId.type) + XCTAssertEqual("test-id", decodedCustomId.identifier) + XCTAssertNil(decodedCustomId.origin) + XCTAssertEqual(MobileVisitorAuthenticationState.authenticated, decodedCustomId.authenticationState) + + // test encoding + let encoder = JSONEncoder() + encoder.outputFormatting = .prettyPrinted + let encoded = try! encoder.encode(decodedCustomId) + + // verify encoding + XCTAssertEqual(customIdentityJson, String(data: encoded, encoding: .utf8)) + } + + // MARK: Equatable tests + /// CustomIdentity's with same types are considered equal func testCustomIdentityAreEqual() { // setup