From 1ecd925e42173ca1a17b9fa110d674974be0bd26 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Wed, 31 Aug 2022 01:47:36 +0100 Subject: [PATCH] Add NotificationReport model --- .../XXClient/Models/NotificationReport.swift | 47 +++++++++++++++++++ .../Models/NotificationReportTests.swift | 31 ++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 Sources/XXClient/Models/NotificationReport.swift create mode 100644 Tests/XXClientTests/Models/NotificationReportTests.swift diff --git a/Sources/XXClient/Models/NotificationReport.swift b/Sources/XXClient/Models/NotificationReport.swift new file mode 100644 index 00000000..31c106a3 --- /dev/null +++ b/Sources/XXClient/Models/NotificationReport.swift @@ -0,0 +1,47 @@ +import Foundation + +public struct NotificationReport: Equatable { + public enum ReportType: String, Equatable { + case `default` + case request + case reset + case confirm + case silent + case e2e + case group + case endFT + case groupRQ + } + + public init( + forMe: Bool, + type: NotificationReport.ReportType, + source: Data + ) { + self.forMe = forMe + self.type = type + self.source = source + } + + public var forMe: Bool + public var type: ReportType + public var source: Data +} + +extension NotificationReport.ReportType: Codable {} + +extension NotificationReport: Codable { + enum CodingKeys: String, CodingKey { + case forMe = "ForMe" + case type = "Type" + case source = "Source" + } + + 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/XXClientTests/Models/NotificationReportTests.swift b/Tests/XXClientTests/Models/NotificationReportTests.swift new file mode 100644 index 00000000..9e99d825 --- /dev/null +++ b/Tests/XXClientTests/Models/NotificationReportTests.swift @@ -0,0 +1,31 @@ +import CustomDump +import XCTest +@testable import XXClient + +final class NotificationReportTests: XCTestCase { + func testCoding() throws { + let forMe = true + let type = NotificationReport.ReportType.default + let sourceB64 = "dGVzdGVyMTIzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + let jsonString = """ + { + "ForMe": true, + "Type": "\(type.rawValue)", + "Source": "\(sourceB64)" + } + """ + let jsonData = jsonString.data(using: .utf8)! + let model = try NotificationReport.decode(jsonData) + + XCTAssertNoDifference(model, NotificationReport( + forMe: forMe, + type: type, + source: Data(base64Encoded: sourceB64)! + )) + + let encodedModel = try model.encode() + let decodedModel = try NotificationReport.decode(encodedModel) + + XCTAssertNoDifference(decodedModel, model) + } +} -- GitLab