// 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 Combine import XCTest import CMSData import ErrorData import HTTPMockHelpers @testable import CMSServices final class ArticlesServiceImplTests: CMSURLSessionMockTestCase { private let articlesService = ArticlesServiceImpl() private let category = [ CMSData.Category( id: 0, name: "Test 1", slug: "test", description: "Description 1", iconUrl: nil, articles: [] ) ] private let recommendedCategories = RecommendedCategories( current: [ CMSData.Category( id: 0, name: "Test 2", slug: "test", description: "Description 2", iconUrl: nil, articles: [] ) ], next: [ CMSData.Category( id: 0, name: "Test 3", slug: "test", description: "Description 3", iconUrl: nil, articles: [] ) ] ) private let searchResults = [ CMSData.SearchResultDTO( id: 123, title: "title", sortPoints: 3, categoryName: "cat", categoryId: 321, categoryIcon: "icon", tagTitle: "tag title", content: "content...", weeks: nil, articleId: nil, articleName: nil ) ] func testFetchArticleCategories() { let url = URL(string: "\(Self.config.cmsBaseUrl)/post")! URLSessionStubResults.resultsForUrls[url] = .dataResponse( data: try! JSONEncoder.iso8601().encode(category), response: URLSessionStubResponses.response(url: url)! ) assertCategoriesPublisher(articlesService.fetchArticleCategories()) } func testFetchArticleCategoriesForWeek() { let url = URL(string: "\(Self.config.cmsBaseUrl)/week-v2/35")! URLSessionStubResults.resultsForUrls[url] = .dataResponse( data: try! JSONEncoder.iso8601().encode(recommendedCategories), response: URLSessionStubResponses.response(url: url)! ) assertRecommendedCategoriesPublisher(articlesService.fetchArticleCategories(for: 35)) } func testSearch() { let url = URL(string: "\(Self.config.cmsBaseUrl)/search-new?q=my%20query")! URLSessionStubResults.resultsForUrls[url] = .dataResponse( data: try! JSONEncoder.iso8601().encode(searchResults), response: URLSessionStubResponses.response(url: url)! ) assertSearchResultPublisher(articlesService.search(query: "my query")) } private func assertCategoriesPublisher(_ publisher: AnyPublisher<[CMSData.Category], ServiceError>) { let expect = XCTestExpectation() publisher .sink( receiveCompletion: { self.handleSubscriberCompletion($0, expectation: expect) }, receiveValue: { categories in XCTAssertEqual(categories.count, 1) } ) .store(in: &cancelBag) wait(for: [expect], timeout: 1.0) } private func assertSearchResultPublisher(_ publisher: AnyPublisher<[SearchResultDomain], ServiceError>) { let expect = XCTestExpectation() publisher .sink( receiveCompletion: { self.handleSubscriberCompletion($0, expectation: expect) }, receiveValue: { results in XCTAssertEqual(results.count, 1) } ) .store(in: &cancelBag) wait(for: [expect], timeout: 10.0) } private func assertRecommendedCategoriesPublisher(_ publisher: AnyPublisher) { let expect = XCTestExpectation() publisher .sink( receiveCompletion: { self.handleSubscriberCompletion($0, expectation: expect) }, receiveValue: { recommended in XCTAssertEqual(recommended.currentWeek.count, 1) XCTAssertEqual(recommended.nextWeek?.count, 1) } ) .store(in: &cancelBag) wait(for: [expect], timeout: 5.0) } }