<commit_before>/**
 * Copyright IBM Corporation 2016
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

// swiftlint:disable function_body_length force_try force_unwrapping file_length

import XCTest
import Foundation
import DiscoveryV1
import RestKit

class DiscoveryTests: XCTestCase {

    private var discovery: Discovery!
    private var environment: Environment!
    private let newsEnvironmentID = "system"
    private let newsCollectionID = "news-en"
    private var documentURL: URL!
    private let timeout: TimeInterval = 30.0

    // MARK: - Test Configuration

    override func setUp() {
        super.setUp()
        continueAfterFailure = false
        instantiateDiscovery()
        environment = lookupOrCreateTestEnvironment()
        documentURL = loadDocument(name: "KennedySpeech", ext: "html")
    }

    func instantiateDiscovery() {
        let version = "2018-10-10"
        if let apiKey = WatsonCredentials.DiscoveryAPIKey {
            discovery = Discovery(version: version, apiKey: apiKey)
        } else {
            let username = WatsonCredentials.DiscoveryUsername
            let password = <PASSWORD>
            discovery = Discovery(username: username, password: password, version: version)
        }
        if let url = WatsonCredentials.DiscoveryURL {
            discovery.serviceURL = url
        }
        discovery.defaultHeaders["X-Watson-Learning-Opt-Out"] = "true"
        discovery.defaultHeaders["X-Watson-Test"] = "true"
    }

    func loadDocument(name: String, ext: String) -> URL? {
        #if os(Linux)
            let url = URL(fileURLWithPath: "Tests/DiscoveryV1Tests/" + name + "." + ext)
        #else
            let bundle = Bundle(for: type(of: self))
            guard let url = bundle.url(forResource: name, withExtension: ext) else { return nil }
        #endif
        return url
    }

    // MARK: - Test Definition for Linux

    static var allTests: [(String, (DiscoveryTests) -> () throws -> Void)] {
        return [
            ("testListEnvironments", testListEnvironments),
            ("testListEnvironmentsByName", testListEnvironmentsByName),
            ("testGetEnvironment", testGetEnvironment),
            ("testEnvironmentCRUD", testEnvironmentCRUD),
            ("testListFields", testListFields),
            ("testListConfigurations", testListConfigurations),
            ("testListConfigurationsByName", testListConfigurationsByName),
            ("testConfigurationCRUD", testConfigurationCRUD),
            ("testConfigurationWithSource", testConfigurationWithSource),
            ("testConfigurationInEnvironment", testConfigurationInEnvironment),
            ("testListCollections", testListCollections),
            ("testListCollectionsByName", testListCollectionsByName),
            ("testCollectionsCRUD", testCollectionsCRUD),
            ("testListCollectionFields", testListCollectionFields),
            ("testExpansionsCRUD", testExpansionsCRUD),
            ("testDocumentsCRUD", testDocumentsCRUD),
            ("testQuery", testQuery),
            ("testQueryWithNaturalLanguage", testQueryWithNaturalLanguage),
            ("testQueryWithPassages", testQueryWithPassages),
            ("testQueryWithSimilar", testQueryWithSimilar),
            ("testQueryWithTermAggregation", testQueryWithTermAggregation),
            ("testQueryWithFilterAggregation", testQueryWithFilterAggregation),
            ("testQueryWithNestedAggregation", testQueryWithNestedAggregation),
            ("testQueryWithHistogramAggregation", testQueryWithHistogramAggregation),
            ("testQueryWithTimesliceAggregation", testQueryWithTimesliceAggregation),
            ("testQueryWithTopHitsAggregation", testQueryWithTopHitsAggregation),
            ("testQueryWithUniqueCountAggregation", testQueryWithUniqueCountAggregation),
            ("testQueryWithMaxAggregation", testQueryWithMaxAggregation),
            ("testQueryWithMinAggregation", testQueryWithMinAggregation),
            ("testQueryWithAverageAggregation", testQueryWithAverageAggregation),
            ("testQueryWithSumAggregation", testQueryWithSumAggregation),
            ("testQueryNotices", testQueryNotices),
            ("testFederatedQuery", testFederatedQuery),
            ("testFederatedQueryNotices", testFederatedQueryNotices),
            ("testCreateEvent", testCreateEvent),
            ("testGetMetricsQuery", testGetMetricsQuery),
            ("testQueryLog", testQueryLog),
            ("testGetMetricsQueryEvent", testGetMetricsQueryEvent),
            ("testGetMetricsQueryNoResults", testGetMetricsQueryNoResults),
            ("testGetMetricsEventRate", testGetMetricsEventRate),
            ("testGetMetricsQueryTokenEvent", testGetMetricsQueryTokenEvent),
            ("testListTrainingData", testListTrainingData),
            ("testTrainingDataCRUD", testTrainingDataCRUD),
            ("testDeleteAllTrainingData", testDeleteAllTrainingData),
            ("testListTrainingExamples", testListTrainingExamples),
            ("testTrainingExamplesCRUD", testTrainingExamplesCRUD),
            ("testListCredentials", testListCredentials),
            ("testCredentialsCRUD", testCredentialsCRUD),
            ("testGetEnvironmentWithInvalidID", testGetEnvironmentWithInvalidID),
            ("testGetConfigurationWithInvalidID", testGetConfigurationWithInvalidID),
            ("testGetCollectionWithInvalidID", testGetCollectionWithInvalidID),
            ("testQueryWithInvalidID", testQueryWithInvalidID),
        ]
    }

    // MARK: - State Management

    func lookupOrCreateTestEnvironment() -> Environment {
        var environment: Environment!
        let expectation = self.expectation(description: "listEnvironments")
        let failure = { (error: Error) in XCTFail("Failed to lookup environment: \(error.localizedDescription)") }
        discovery.listEnvironments(failure: failure) {
            response in
            environment = response.environments?.first { !($0.readOnly ?? true) }
            expectation.fulfill()
        }
        waitForExpectations(timeout: timeout)
        return environment ?? createTestEnvironment()

    }

    func createTestEnvironment() -> Environment {
        var environment: Environment!
        let expectation = self.expectation(description: "createEnvironment")
        let name = "swift-sdk-test-" + UUID().uuidString
        let description = "An environment created while testing the Swift SDK. Safe to delete."
        let failure = { (error: Error) in XCTFail("Failed to create an environment: \(error.localizedDescription)") }
        discovery.createEnvironment(name: name, description: description, size: Environment.Size.xl.rawValue, failure: failure) {
            response in
            environment = response
            expectation.fulfill()
        }
        waitForExpectations(timeout: timeout)
        return environment
    }

    func lookupOrCreateTestConfiguration(environmentID: String) -> Configuration {
        var configuration: Configuration!
        let expectation = self.expectation(description: "listConfigurations")
        let failure = { (error: Error) in XCTFail("Failed to lookup Configuration: \(error.localizedDescription)") }
        discovery.listConfigurations(environmentID: environmentID, failure: failure) {
            response in
            configuration = response.configurations?.first { $0.name.starts(with: "swift-sdk-test-") }
            expectation.fulfill()
        }
        waitForExpectations(timeout: timeout)
        return configuration ?? createTestConfiguration(environmentID: environmentID)
    }
<commit_msg>test(DiscoveryV1): Stop creating new environments for Discovery tests<commit_after>/**
 * Copyright IBM Corporation 2016
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

// swiftlint:disable function_body_length force_try force_unwrapping file_length

import XCTest
import Foundation
import DiscoveryV1
import RestKit

class DiscoveryTests: XCTestCase {

    private var discovery: Discovery!
    private var environment: Environment!
    private let newsEnvironmentID = "system"
    private let newsCollectionID = "news-en"
    private var documentURL: URL!
    private let timeout: TimeInterval = 30.0

    // MARK: - Test Configuration

    override func setUp() {
        super.setUp()
        continueAfterFailure = false
        instantiateDiscovery()
        environment = getTestEnvironment()
        documentURL = loadDocument(name: "KennedySpeech", ext: "html")
    }

    func instantiateDiscovery() {
        let version = "2018-10-10"
        if let apiKey = WatsonCredentials.DiscoveryAPIKey {
            discovery = Discovery(version: version, apiKey: apiKey)
        } else {
            let username = WatsonCredentials.DiscoveryUsername
            let password = <PASSWORD>Credentials.DiscoveryPassword
            discovery = Discovery(username: username, password: password, version: version)
        }
        if let url = WatsonCredentials.DiscoveryURL {
            discovery.serviceURL = url
        }
        discovery.defaultHeaders["X-Watson-Learning-Opt-Out"] = "true"
        discovery.defaultHeaders["X-Watson-Test"] = "true"
    }

    func loadDocument(name: String, ext: String) -> URL? {
        #if os(Linux)
            let url = URL(fileURLWithPath: "Tests/DiscoveryV1Tests/" + name + "." + ext)
        #else
            let bundle = Bundle(for: type(of: self))
            guard let url = bundle.url(forResource: name, withExtension: ext) else { return nil }
        #endif
        return url
    }

    // MARK: - Test Definition for Linux

    static var allTests: [(String, (DiscoveryTests) -> () throws -> Void)] {
        return [
            ("testListEnvironments", testListEnvironments),
            ("testListEnvironmentsByName", testListEnvironmentsByName),
            ("testGetEnvironment", testGetEnvironment),
            ("testEnvironmentCRUD", testEnvironmentCRUD),
            ("testListFields", testListFields),
            ("testListConfigurations", testListConfigurations),
            ("testListConfigurationsByName", testListConfigurationsByName),
            ("testConfigurationCRUD", testConfigurationCRUD),
            ("testConfigurationWithSource", testConfigurationWithSource),
            ("testConfigurationInEnvironment", testConfigurationInEnvironment),
            ("testListCollections", testListCollections),
            ("testListCollectionsByName", testListCollectionsByName),
            ("testCollectionsCRUD", testCollectionsCRUD),
            ("testListCollectionFields", testListCollectionFields),
            ("testExpansionsCRUD", testExpansionsCRUD),
            ("testDocumentsCRUD", testDocumentsCRUD),
            ("testQuery", testQuery),
            ("testQueryWithNaturalLanguage", testQueryWithNaturalLanguage),
            ("testQueryWithPassages", testQueryWithPassages),
            ("testQueryWithSimilar", testQueryWithSimilar),
            ("testQueryWithTermAggregation", testQueryWithTermAggregation),
            ("testQueryWithFilterAggregation", testQueryWithFilterAggregation),
            ("testQueryWithNestedAggregation", testQueryWithNestedAggregation),
            ("testQueryWithHistogramAggregation", testQueryWithHistogramAggregation),
            ("testQueryWithTimesliceAggregation", testQueryWithTimesliceAggregation),
            ("testQueryWithTopHitsAggregation", testQueryWithTopHitsAggregation),
            ("testQueryWithUniqueCountAggregation", testQueryWithUniqueCountAggregation),
            ("testQueryWithMaxAggregation", testQueryWithMaxAggregation),
            ("testQueryWithMinAggregation", testQueryWithMinAggregation),
            ("testQueryWithAverageAggregation", testQueryWithAverageAggregation),
            ("testQueryWithSumAggregation", testQueryWithSumAggregation),
            ("testQueryNotices", testQueryNotices),
            ("testFederatedQuery", testFederatedQuery),
            ("testFederatedQueryNotices", testFederatedQueryNotices),
            ("testCreateEvent", testCreateEvent),
            ("testGetMetricsQuery", testGetMetricsQuery),
            ("testQueryLog", testQueryLog),
            ("testGetMetricsQueryEvent", testGetMetricsQueryEvent),
            ("testGetMetricsQueryNoResults", testGetMetricsQueryNoResults),
            ("testGetMetricsEventRate", testGetMetricsEventRate),
            ("testGetMetricsQueryTokenEvent", testGetMetricsQueryTokenEvent),
            ("testListTrainingData", testListTrainingData),
            ("testTrainingDataCRUD", testTrainingDataCRUD),
            ("testDeleteAllTrainingData", testDeleteAllTrainingData),
            ("testListTrainingExamples", testListTrainingExamples),
            ("testTrainingExamplesCRUD", testTrainingExamplesCRUD),
            ("testListCredentials", testListCredentials),
            ("testCredentialsCRUD", testCredentialsCRUD),
            ("testGetEnvironmentWithInvalidID", testGetEnvironmentWithInvalidID),
            ("testGetConfigurationWithInvalidID", testGetConfigurationWithInvalidID),
            ("testGetCollectionWithInvalidID", testGetCollectionWithInvalidID),
            ("testQueryWithInvalidID", testQueryWithInvalidID),
        ]
    }

    // MARK: - State Management

    func getTestEnvironment() -> Environment {
        var environment: Environment!
        let expectation = self.expectation(description: "listEnvironments")
        let failure = { (error: Error) in XCTFail("Failed to lookup environment: \(error.localizedDescription)") }
        discovery.listEnvironments(failure: failure) {
            response in
            environment = response.environments?.first { !($0.readOnly ?? true) }
            expectation.fulfill()
        }
        waitForExpectations(timeout: timeout)
        return environment
    }

    func lookupOrCreateTestConfiguration(environmentID: String) -> Configuration {
        var configuration: Configuration!
        let expectation = self.expectation(description: "listConfigurations")
        let failure = { (error: Error) in XCTFail("Failed to lookup Configuration: \(error.localizedDescription)") }
        discovery.listConfigurations(environmentID: environmentID, failure: failure) {
            response in
            configuration = response.configurations?.first { $0.name.starts(with: "swift-sdk-test-") }
            expectation.fulfill()
        }
        waitForExpectations(timeout: timeout)
        return configuration ?? createTestConfiguration(environmentID: environmentID)
    }

{'fraction_non_alphanumeric': 0.08980457701208537, 'fraction_numerical': 0.003021342247364361, 'mean_word_length': 3.227445652173913, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2878808', 'n_tokens_mistral': 3786, 'n_tokens_neox': 3658, 'n_words': 869}