// The MIT License // // Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk) // // Copyright (C) 2020 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk) // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies // of the Software, and to permit persons to whom the Software is furnished to do // so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. import Foundation import ErrorData import Combine import DependencyInjection import Logger public extension Publisher { /// Applies fake loading time for mock publishers func mockDelay() -> Publishers.Delay { let randomDelay = Int.random(in: 50...500) return self.delay(for: .milliseconds(randomDelay), scheduler: DispatchQueue.main) } /// Maps potential URLSession errors func mapToServiceError() -> Publishers.MapError { self.mapError { (error) in let mapped: ServiceError let nsError = error as NSError if let error = error as? ServiceError { mapped = error } else if nsError.domain == NSURLErrorDomain { switch nsError.code { case NSURLErrorNetworkConnectionLost, NSURLErrorNotConnectedToInternet, NSURLErrorTimedOut: mapped = .networkError(.missingInternetConnection) case NSURLErrorCancelled: mapped = .networkError(.cancelled) default: mapped = .networkError(.unexpectedError(nsError)) } } else { mapped = .unexpectedError(error) } return mapped } } /// Maps result to Void func mapToVoid() -> Publishers.Map { self.map({ _ in Void() }) } } /// This cannot be an extension on `Publisher` due to assiociated types. public final class MockPublisher { @Inject static private var logger: LoggerProtocol private static func shouldFail() -> Bool { (Double.random(in: 0.0...1.0) <= MockFlags.failRisk.rawValue) && MockFlags.failRisk != .disabled } public static func mockFailable(value: O, error: F) -> AnyPublisher { let publisher: AnyPublisher if shouldFail() { logger.debug("Mock publisher will fail because of random risk failure (\(Int(MockFlags.failRisk.rawValue*100))%)!") publisher = Fail(outputType: O.self, failure: error) .eraseToAnyPublisher() } else { publisher = Just(value) .setFailureType(to: F.self) .eraseToAnyPublisher() } return publisher .mockDelay() .eraseToAnyPublisher() } }