/*
 * 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.apache.tools.ant.filters.ReplaceTokens

apply plugin: 'elasticsearch.legacy-yaml-rest-test'

dependencies {
  yamlRestTestImplementation project(':x-pack:plugin:core')
}

String outputDir = "${buildDir}/generated-resources/${project.name}"
def copyXPackPluginProps = tasks.register("copyXPackPluginProps", Copy) {
  from project(xpackModule('core')).file('src/main/plugin-metadata')
  from project(xpackModule('core')).tasks.pluginProperties
  into outputDir
}
project.sourceSets.yamlRestTest.output.dir(outputDir, builtBy: copyXPackPluginProps)

// location of generated keystores and certificates
File keystoreDir = new File(project.buildDir, 'keystore')
File nodeKeystore = file("$keystoreDir/testnode.jks")
File nodeKey = file("$keystoreDir/testnode.pem")
File nodeCert = file("$keystoreDir/testnode.crt")
File clientKeyStore = file("$keystoreDir/testclient.jks")
File clientKey = file("$keystoreDir/testclient.pem")
File clientCert = file("$keystoreDir/testclient.crt")

// Add keystores to test classpath: it expects it there
def copyKeyCerts = tasks.register("copyKeyCerts", Copy) {
  from('./') {
    include '*.crt', '*.pem', '*.jks'
  }
  into keystoreDir
}
// Add keystores to test classpath: it expects it there
sourceSets.yamlRestTest.resources.srcDir(keystoreDir)

['processYamlRestTestResources', 'yamlRestTest', 'filepermissions', 'forbiddenPatterns'].each {
  tasks.named(it).configure {
    dependsOn(copyKeyCerts)
  }
}

ext.pluginPaths = []
project(':plugins').getChildProjects().each { pluginName, pluginProject ->
  pluginPaths << pluginProject.path
}

testClusters.matching { it.name == "yamlRestTest" }.configureEach {
  testDistribution = 'DEFAULT'
  setting 'xpack.monitoring.collection.interval', '1s'
  setting 'xpack.monitoring.collection.enabled', 'false' //will enable in test
  setting 'xpack.monitoring.exporters._http.enabled', 'false'//will enable in test
  setting 'xpack.monitoring.exporters._http.type', "http"
  setting 'xpack.monitoring.exporters._http.host', "https://example.com" //will be replaced in test
  setting 'xpack.monitoring.exporters._http.auth.username', 'monitoring_agent'
  setting 'xpack.monitoring.exporters._http.ssl.verification_mode', 'full'
  setting 'xpack.monitoring.exporters._http.ssl.certificate_authorities', 'testnode.crt'
  keystore 'xpack.monitoring.exporters._http.auth.secure_password', 'x-pack-test-password'

  setting 'xpack.license.self_generated.type', 'trial'
  setting 'xpack.security.enabled', 'true'
  setting 'xpack.security.http.ssl.enabled', 'true'
  setting 'xpack.security.http.ssl.key', 'testnode.pem'
  setting 'xpack.security.http.ssl.certificate', 'testnode.crt'
  setting 'xpack.security.http.ssl.certificate_authorities', 'testnode.crt'
  keystore 'xpack.security.http.ssl.secure_key_passphrase', 'testnode'

  setting 'xpack.ml.enabled', 'false'
  // copy keystores, keys and certificates into config/
  extraConfigFile nodeKeystore.name, nodeKeystore
  extraConfigFile nodeKey.name, nodeKey
  extraConfigFile nodeCert.name, nodeCert
  extraConfigFile clientKeyStore.name, clientKeyStore
  extraConfigFile clientKey.name, clientKey
  extraConfigFile clientCert.name, clientCert

  user username: "test_user", password: "x-pack-test-password"
  user username: "monitoring_agent", password: "x-pack-test-password", role: "remote_monitoring_agent"

  systemProperty 'es.queryable_built_in_roles_enabled', 'false'

  pluginPaths.each { pluginPath ->
    plugin pluginPath
  }
}

ext.expansions = [
  'expected.plugins.count': pluginPaths.size()
]

tasks.named("processYamlRestTestResources").configure {
  from(sourceSets.yamlRestTest.resources.srcDirs) {
    duplicatesStrategy = DuplicatesStrategy.INCLUDE
    include '**/*.yml'
    inputs.properties(expansions)
    filter("tokens" : expansions.collectEntries {k, v -> [k, v.toString()]} /* must be a map of strings */, ReplaceTokens.class)
  }
}

tasks.named('yamlRestTestTestingConventions').configure {
  baseClass 'org.elasticsearch.test.rest.ESRestTestCase'
  baseClass 'org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase'
}
