Skip to content

Commit

Permalink
Change CustomIdentity to use underscores during encoding (#550)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nporter-adbe authored Mar 3, 2021
1 parent 924cda6 commit d3d244d
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 4 deletions.
8 changes: 4 additions & 4 deletions AEPIdentity/Sources/CustomIdentity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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"
}
}

Expand Down
196 changes: 196 additions & 0 deletions AEPIdentity/Tests/CustomIdentityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d3d244d

Please sign in to comment.