From 2342343e1fb71dcd0cfde63551ca2008150eaaac Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Mon, 8 Aug 2022 12:38:11 +0100 Subject: [PATCH] Add GroupSendReport model --- .../Models/GroupSendReport.swift | 33 +++++++++++++++++++ .../Models/GroupSendReportTests.swift | 31 +++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 Sources/ElixxirDAppsSDK/Models/GroupSendReport.swift create mode 100644 Tests/ElixxirDAppsSDKTests/Models/GroupSendReportTests.swift diff --git a/Sources/ElixxirDAppsSDK/Models/GroupSendReport.swift b/Sources/ElixxirDAppsSDK/Models/GroupSendReport.swift new file mode 100644 index 00000000..c7b3b01d --- /dev/null +++ b/Sources/ElixxirDAppsSDK/Models/GroupSendReport.swift @@ -0,0 +1,33 @@ +import Foundation + +public struct GroupSendReport: Equatable { + public init( + roundId: UInt64, + timestamp: Int64, + messageId: Data + ) { + self.roundId = roundId + self.timestamp = timestamp + self.messageId = messageId + } + + public var roundId: UInt64 + public var timestamp: Int64 + public var messageId: Data +} + +extension GroupSendReport: Codable { + enum CodingKeys: String, CodingKey { + case roundId = "RoundID" + case timestamp = "Timestamp" + case messageId = "MessageID" + } + + public static func decode(_ data: Data) throws -> Self { + try JSONDecoder().decode(Self.self, from: data) + } + + public func encode() throws -> Data { + try JSONEncoder().encode(self) + } +} diff --git a/Tests/ElixxirDAppsSDKTests/Models/GroupSendReportTests.swift b/Tests/ElixxirDAppsSDKTests/Models/GroupSendReportTests.swift new file mode 100644 index 00000000..8d99647c --- /dev/null +++ b/Tests/ElixxirDAppsSDKTests/Models/GroupSendReportTests.swift @@ -0,0 +1,31 @@ +import CustomDump +import XCTest +@testable import ElixxirDAppsSDK + +final class GroupSendReportTests: XCTestCase { + func testCoding() throws { + let roundId: UInt64 = 123 + let timestamp: Int64 = 321 + let messageIdB64 = "EB/70R5HYEw5htZ4Hg9ondrn3+cAc/lH2G0mjQMja3w=" + let jsonString = """ + { + "RoundID": \(roundId), + "Timestamp": \(timestamp), + "MessageID": "\(messageIdB64)" + } + """ + let jsonData = jsonString.data(using: .utf8)! + let model = try GroupSendReport.decode(jsonData) + + XCTAssertNoDifference(model, GroupSendReport( + roundId: roundId, + timestamp: timestamp, + messageId: Data(base64Encoded: messageIdB64)! + )) + + let encodedModel = try model.encode() + let decodedModel = try GroupSendReport.decode(encodedModel) + + XCTAssertNoDifference(decodedModel, model) + } +} -- GitLab