Skip to content
Snippets Groups Projects
Commit 333d0f64 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Add TrackServicesCallbackResult model

parent f84c676c
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!60Update Bindings
import Foundation
public struct TrackServicesCallbackResult: Equatable {
public init(id: Data, services: [MessageService]) {
self.id = id
self.services = services
}
public var id: Data
public var services: [MessageService]
}
extension TrackServicesCallbackResult: Codable {
enum CodingKeys: String, CodingKey {
case id = "Id"
case services = "Services"
}
public static func decode(_ data: Data) throws -> Self {
try JSONDecoder().decode(Self.self, from: data)
}
public func encode() throws -> Data {
try JSONEncoder().encode(self)
}
}
extension Array where Element == TrackServicesCallbackResult {
public static func decode(_ data: Data) throws -> Self {
try JSONDecoder().decode(Self.self, from: data)
}
public func encode() throws -> Data {
try JSONEncoder().encode(self)
}
}
import CustomDump
import XCTest
@testable import XXClient
final class TrackServicesCallbackResultTests: XCTestCase {
func testCoding() throws {
let idB64 = "AAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD"
let serviceIdentifierB64 = "AQID"
let serviceTag = "TestTag 2"
let serviceMetadataB64 = "BAUG"
let jsonString = """
{
"Id": "\(idB64)",
"Services": [
{
"Identifier": "\(serviceIdentifierB64)",
"Tag": "\(serviceTag)",
"Metadata": "\(serviceMetadataB64)"
}
]
}
"""
let jsonData = jsonString.data(using: .utf8)!
let model = try TrackServicesCallbackResult.decode(jsonData)
XCTAssertNoDifference(model, TrackServicesCallbackResult(
id: Data(base64Encoded: idB64)!,
services: [
MessageService(
identifier: Data(base64Encoded: serviceIdentifierB64)!,
tag: serviceTag,
metadata: Data(base64Encoded: serviceMetadataB64)!
)
]
))
let encodedModel = try model.encode()
let decodedModel = try TrackServicesCallbackResult.decode(encodedModel)
XCTAssertNoDifference(decodedModel, model)
}
func testCodingArray() throws {
let models = [
TrackServicesCallbackResult(
id: "id1".data(using: .utf8)!,
services: [
MessageService(
identifier: "service1-id".data(using: .utf8)!,
tag: "service1-tag",
metadata: "service1-metadata".data(using: .utf8)!
),
]
),
TrackServicesCallbackResult(
id: "id2".data(using: .utf8)!,
services: [
MessageService(
identifier: "service2-id".data(using: .utf8)!,
tag: "service2-tag",
metadata: "service2-metadata".data(using: .utf8)!
),
MessageService(
identifier: "service3-id".data(using: .utf8)!,
tag: "service3-tag",
metadata: "service3-metadata".data(using: .utf8)!
),
]
),
]
let encodedModels = try models.encode()
let decodedModels = try [TrackServicesCallbackResult].decode(encodedModels)
XCTAssertNoDifference(models, decodedModels)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment