/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0; you may not use this file except in compliance with the Elastic License
 * 2.0.
 */

import org.elasticsearch.gradle.internal.test.RestIntegTestTask
import org.elasticsearch.gradle.Version
import org.elasticsearch.gradle.VersionProperties
import static org.elasticsearch.gradle.PropertyNormalization.IGNORE_VALUE

apply plugin: 'elasticsearch.internal-testclusters'
apply plugin: 'elasticsearch.standalone-rest-test'
apply plugin: 'elasticsearch.rest-resources'

dependencies {
  testImplementation project(':x-pack:qa')
}

Version ccsCompatVersion = buildParams.bwcVersions.minimumWireCompatibleVersion

restResources {
  restApi {
    include '_common', 'bulk', 'indices', 'cluster', 'search', 'security', 'transform'
  }
}

def remoteCluster = testClusters.register('remote-cluster') {
    testDistribution = 'DEFAULT'
    versions = [ccsCompatVersion.toString(), project.version]
    numberOfNodes = 2
    setting 'node.roles', '[data,ingest,master]'
    setting 'xpack.security.enabled', 'true'
    setting 'xpack.watcher.enabled', 'false'
    setting 'xpack.license.self_generated.type', 'trial'

    user username: "test_user", password: "x-pack-test-password"
}

testClusters.register('mixed-cluster') {
    testDistribution = 'DEFAULT'
    numberOfNodes = 2
    // Node roles are configured this way in order to verify redirecting the transform request from the node lacking
    // remote_cluster_client role to the node that is remote_cluster_client.
    nodes."mixed-cluster-0".setting 'node.roles', '[data,ingest,master,transform]'
    nodes."mixed-cluster-1".setting 'node.roles', '[data,ingest,master,transform,remote_cluster_client]'
    setting 'xpack.security.enabled', 'true'
    setting 'xpack.watcher.enabled', 'false'
    setting 'xpack.license.self_generated.type', 'trial'
    setting 'cluster.remote.my_remote_cluster.seeds', {
      remoteCluster.get().getAllTransportPortURI().collect { "\"$it\"" }.toString()
    }, IGNORE_VALUE
    setting 'cluster.remote.connections_per_cluster', "1"

    user username: "test_user", password: "x-pack-test-password"
}

tasks.register('remote-cluster', RestIntegTestTask) {
  mustRunAfter("precommit")
  systemProperty 'tests.rest.suite', 'remote_cluster'
}

tasks.register('mixed-cluster', RestIntegTestTask) {
  dependsOn 'remote-cluster'
  useCluster remoteCluster
  systemProperty 'tests.rest.suite', 'multi_cluster'
}

tasks.register("integTest") {
  dependsOn 'mixed-cluster'
}

tasks.named("check").configure { dependsOn("integTest") }

