import org.elasticsearch.gradle.internal.test.RestIntegTestTask
import org.elasticsearch.gradle.testclusters.TestClusterValueSource
import org.elasticsearch.gradle.testclusters.TestClustersPlugin
import org.elasticsearch.gradle.testclusters.TestClustersRegistry
import org.elasticsearch.gradle.util.GradleUtils

apply plugin: 'elasticsearch.internal-test-artifact'

dependencies {
  testImplementation project(':x-pack:plugin:core')
  testImplementation(testArtifact(project(xpackModule('core'))))
  testImplementation project(":test:framework")
  testImplementation(project(':x-pack:plugin:sql:qa:server')) {
    transitive = false
  }
  api project(xpackModule('ql:test-fixtures'))
}

Project mainProject = project

subprojects {
  // Use tests from the root security qa project in subprojects
  configurations.create('testArtifacts').transitive = false

  def clusterPath = getPath()

  dependencies {
    javaRestTestImplementation project(":x-pack:plugin:core")
    javaRestTestImplementation(testArtifact(project(xpackModule('core'))))
    testArtifacts testArtifact(project(mainProject.path))
  }

  testClusters.matching { it.name == "javaRestTest" }.configureEach {
    testDistribution = 'DEFAULT'
    // Setup auditing so we can use it in some tests
    setting 'xpack.security.audit.enabled', 'true'
    setting 'xpack.security.enabled', 'true'
    setting 'xpack.license.self_generated.type', 'trial'
    // skip automatically creating the "elastic" user (and the associated .security index)
    setting 'xpack.security.autoconfiguration.enabled', 'false'
    // Setup roles used by tests
    rolesFile mainProject.file('roles.yml')
    /* Setup the one admin user that we run the tests as.
     * Tests use "run as" to get different users. */
    user username: "test_admin", password: "x-pack-test-password"
    user username: "user1", password: 'x-pack-test-password', role: "user1"
    user username: "user2", password: 'x-pack-test-password', role: "user2"
    user username: "manage_user", password: 'x-pack-test-password', role: "manage_user"
  }

  File testArtifactsDir = project.file("$buildDir/testArtifacts")
  TaskProvider copyTestClasses = tasks.register("copyTestClasses", Copy) {
    dependsOn configurations.testArtifacts
    from { zipTree(configurations.testArtifacts.singleFile) }
    into testArtifactsDir
  }

  tasks.named("javaRestTest").configure {
    dependsOn copyTestClasses

    Provider<TestClustersRegistry> serviceProvider = GradleUtils.getBuildService(
      project.gradle.sharedServices,
      TestClustersPlugin.REGISTRY_SERVICE_NAME
    )

    def clusterInfo = getClusterInfo('javaRestTest')
    testClassesDirs += project.files(testArtifactsDir)
    classpath += configurations.testArtifacts
    nonInputProperties.systemProperty 'tests.audit.logfile', clusterInfo.map { it.auditLogs.get(0) }
    nonInputProperties.systemProperty 'tests.audit.yesterday.logfile',
      clusterInfo.map { it.auditLogs.get(0).getParentFile().toString() + "/javaRestTest_audit-${new Date().format('yyyy-MM-dd')}-1.json.gz"}
  }

}
