From 88ab18755e414cf8eb927628ed27ed97e14b9373 Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Tue, 26 Jul 2022 17:43:28 +0100
Subject: [PATCH] Add SingleUseSendReport model

---
 .../Models/SingleUseSendReport.swift          | 51 +++++++++++++++++++
 .../Models/SingleUseSendReportTests.swift     | 35 +++++++++++++
 2 files changed, 86 insertions(+)
 create mode 100644 Sources/ElixxirDAppsSDK/Models/SingleUseSendReport.swift
 create mode 100644 Tests/ElixxirDAppsSDKTests/Models/SingleUseSendReportTests.swift

diff --git a/Sources/ElixxirDAppsSDK/Models/SingleUseSendReport.swift b/Sources/ElixxirDAppsSDK/Models/SingleUseSendReport.swift
new file mode 100644
index 00000000..f86f26ce
--- /dev/null
+++ b/Sources/ElixxirDAppsSDK/Models/SingleUseSendReport.swift
@@ -0,0 +1,51 @@
+import Foundation
+
+public struct SingleUseSendReport: Equatable {
+  public init(
+    rounds: [Int],
+    ephId: EphId
+  ) {
+    self.rounds = rounds
+    self.ephId = ephId
+  }
+
+  public var rounds: [Int]
+  public var ephId: EphId
+}
+
+extension SingleUseSendReport {
+  public struct EphId: Equatable {
+    public init(
+      ephId: [Int],
+      source: Data
+    ) {
+      self.ephId = ephId
+      self.source = source
+    }
+
+    public var ephId: [Int]
+    public var source: Data
+  }
+}
+
+extension SingleUseSendReport: Codable {
+  enum CodingKeys: String, CodingKey {
+    case rounds = "Rounds"
+    case ephId = "EphID"
+  }
+
+  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 SingleUseSendReport.EphId: Codable {
+  enum CodingKeys: String, CodingKey {
+    case ephId = "EphId"
+    case source = "Source"
+  }
+}
diff --git a/Tests/ElixxirDAppsSDKTests/Models/SingleUseSendReportTests.swift b/Tests/ElixxirDAppsSDKTests/Models/SingleUseSendReportTests.swift
new file mode 100644
index 00000000..d53c49f3
--- /dev/null
+++ b/Tests/ElixxirDAppsSDKTests/Models/SingleUseSendReportTests.swift
@@ -0,0 +1,35 @@
+import CustomDump
+import XCTest
+@testable import ElixxirDAppsSDK
+
+final class SingleUseSendReportTests: XCTestCase {
+  func testCoding() throws {
+    let rounds: [Int] = [1, 5, 9]
+    let ephId: [Int] = [0, 0, 0, 0, 0, 0, 3, 89]
+    let ephIdSourceB64 = "emV6aW1hAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD"
+    let jsonString = """
+    {
+      "Rounds": [\(rounds.map { "\($0)" }.joined(separator: ", "))],
+      "EphID": {
+        "EphId": [\(ephId.map { "\($0)" }.joined(separator: ", "))],
+        "Source": "\(ephIdSourceB64)"
+      }
+    }
+    """
+    let jsonData = jsonString.data(using: .utf8)!
+    let report = try SingleUseSendReport.decode(jsonData)
+
+    XCTAssertNoDifference(report, SingleUseSendReport(
+      rounds: rounds,
+      ephId: .init(
+        ephId: ephId,
+        source: Data(base64Encoded: ephIdSourceB64)!
+      )
+    ))
+
+    let encodedReport = try report.encode()
+    let decodedReport = try SingleUseSendReport.decode(encodedReport)
+
+    XCTAssertNoDifference(decodedReport, report)
+  }
+}
-- 
GitLab