2 // MockCuttlefish.swift
3 // TrustedPeersHelperUnitTests
5 // Created by Ben Williamson on 5/1/18.
12 case reset((ResetRequest, @escaping (ResetResponse?, Error?) -> Void) -> Void)
13 case establish((EstablishRequest, @escaping (EstablishResponse?, Error?) -> Void) -> Void)
14 case joinWithVoucher((JoinWithVoucherRequest, @escaping (JoinWithVoucherResponse?, Error?) -> Void) -> Void)
15 case updateTrust((UpdateTrustRequest, @escaping (UpdateTrustResponse?, Error?) -> Void) -> Void)
16 case setRecoveryKey((SetRecoveryKeyRequest, @escaping (SetRecoveryKeyResponse?, Error?) -> Void) -> Void)
17 case fetchChanges((FetchChangesRequest, @escaping (FetchChangesResponse?, Error?) -> Void) -> Void)
18 case fetchViableBottles((FetchViableBottlesRequest, @escaping (FetchViableBottlesResponse?, Error?) -> Void) ->Void)
19 case fetchPolicyDocuments((FetchPolicyDocumentsRequest,
20 @escaping (FetchPolicyDocumentsResponse?, Error?) -> Void) -> Void)
23 class MockCuttlefishAPIAsyncClient: CuttlefishAPIAsync {
25 var handlers: [Handler] = []
28 func expect(_ handler: Handler) {
29 self.handlers.append(handler)
32 func next() -> Handler {
33 if index >= handlers.count {
34 XCTFail("MockCuttlefish: Not expecting any more calls.")
38 return handlers[index]
43 for i in 0 ..< handlers.count {
44 print("\(i) \(i == index ? "->" : " ") \(handlers[i])")
49 func reset(_ request: ResetRequest, completion: @escaping (ResetResponse?, Error?) -> Void) {
50 print("MockCuttlefish: reset called")
51 if case .reset(let f) = next() {
53 f(request, completion)
55 XCTFail("Unexpected: reset")
56 print("Unexpected: reset")
62 func establish(_ request: EstablishRequest, completion: @escaping (EstablishResponse?, Error?) -> Void) {
63 print("MockCuttlefish: establish called")
64 if case .establish(let f) = next() {
66 f(request, completion)
68 XCTFail("Unexpected: establish")
69 print("Unexpected: establish")
75 func joinWithVoucher(_ request: JoinWithVoucherRequest,
76 completion: @escaping (JoinWithVoucherResponse?, Error?) -> Void) {
77 print("MockCuttlefish: joinWithVoucher called")
78 if case .joinWithVoucher(let f) = next() {
80 f(request, completion)
82 XCTFail("Unexpected: joinWithVoucher")
83 print("Unexpected: joinWithVoucher")
89 func updateTrust(_ request: UpdateTrustRequest, completion: @escaping (UpdateTrustResponse?, Error?) -> Void) {
90 print("MockCuttlefish: updateTrust called")
91 if case .updateTrust(let f) = next() {
93 f(request, completion)
95 XCTFail("Unexpected: updateTrust")
96 print("Unexpected: updateTrust")
102 func setRecoveryKey(_ request: SetRecoveryKeyRequest,
103 completion: @escaping (SetRecoveryKeyResponse?, Error?) -> Void) {
104 print("MockCuttlefish: setRecoveryKey called")
105 if case .setRecoveryKey(let f) = next() {
107 f(request, completion)
109 XCTFail("Unexpected: setRecoveryKey")
110 print("Unexpected: setRecoveryKey")
116 func fetchChanges(_ request: FetchChangesRequest, completion: @escaping (FetchChangesResponse?, Error?) -> Void) {
117 print("MockCuttlefish: fetchChanges called")
118 if case .fetchChanges(let f) = next() {
120 f(request, completion)
122 XCTFail("Unexpected: fetchChanges")
123 print("Unexpected: fetchChanges")
129 func fetchViableBottles(_ request: FetchViableBottlesRequest, completion: @escaping (FetchViableBottlesResponse?, Error?) -> Void) {
130 print("MockCuttlefish: fetchViableBottles called")
131 if case .fetchViableBottles(let f) = next() {
133 f(request, completion)
135 XCTFail("Unexpected: fetchViableBottles")
141 func fetchPolicyDocuments(_ request: FetchPolicyDocumentsRequest,
142 completion: @escaping (FetchPolicyDocumentsResponse?, Error?) -> Void) {
143 print("MockCuttlefish: fetchPolicyDocuments called")
144 if case .fetchPolicyDocuments(let f) = next() {
146 f(request, completion)
148 XCTFail("Unexpected: fetchPolicyDocuments")
149 print("Unexpected: fetchPolicyDocuments")
155 func validatePeers(_: ValidatePeersRequest, completion: @escaping (ValidatePeersResponse?, Error?) -> Void) {
156 completion(ValidatePeersResponse(), nil)
158 func reportHealth(_: ReportHealthRequest, completion: @escaping (ReportHealthResponse?, Error?) -> Void) {
159 print("MockCuttlefish: reportHealth called")
160 completion(ReportHealthResponse(), nil)
162 func pushHealthInquiry(_: HealthInquiryRequest, completion: @escaping (HealthInquiryResponse?, Error?) -> Void) {
163 completion(HealthInquiryResponse(), nil)
165 func getRepairAction(_: GetRepairActionRequest, completion: @escaping (GetRepairActionResponse?, Error?) -> Void) {
166 completion(GetRepairActionResponse(), nil)