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

dependencies {
  javaRestTestImplementation(testArtifact(project(xpackModule('core'))))
  javaRestTestImplementation(testArtifact(project(xpackModule('ml'))))
  javaRestTestImplementation(testArtifact(project(xpackModule('security'))))
  javaRestTestImplementation project(path: ':modules:ingest-common')
  javaRestTestImplementation(project(':modules:lang-mustache'))
  javaRestTestImplementation project(path: ':modules:reindex')
  javaRestTestImplementation project(path: ':modules:transport-netty4')
  javaRestTestImplementation project(path: xpackModule('autoscaling'))
  javaRestTestImplementation project(':modules:data-streams')
  javaRestTestImplementation project(path: xpackModule('ilm'))
  javaRestTestImplementation project(path: xpackModule('slm'))
  javaRestTestImplementation project(path: xpackModule('monitoring'))
  javaRestTestImplementation project(path: xpackModule('transform'))
  javaRestTestImplementation project(path: xpackModule('rank-rrf'))
  javaRestTestImplementation project(path: xpackModule('esql-core'))
  javaRestTestImplementation project(path: xpackModule('esql'))
  javaRestTestImplementation project(path: xpackModule('snapshot-repo-test-kit'))
  javaRestTestImplementation project(path: xpackModule('ent-search'))
  javaRestTestImplementation project(path: xpackModule('inference'))
  javaRestTestImplementation project(':test:external-modules:test-multi-project')
}

// location for keys and certificates
File keystoreDir = new File(project.buildDir, 'keystore')
File nodeKey = file("$keystoreDir/testnode.pem")
File nodeCert = file("$keystoreDir/testnode.crt")
// Add key and certs to test classpath: it expects it there
tasks.register("copyKeyCerts", Copy) {
  from(project(':x-pack:plugin:core').file('src/test/resources/org/elasticsearch/xpack/security/transport/ssl/certs/simple/')) {
    include 'testnode.crt', 'testnode.pem'
  }
  into keystoreDir
}
// Add keys and cets to test classpath: it expects it there
sourceSets.javaRestTest.resources.srcDir(keystoreDir)
tasks.named("processJavaRestTestResources").configure { dependsOn("copyKeyCerts") }

tasks.named("javaRestTest").configure {
  dependsOn "copyKeyCerts"
}

testClusters.configureEach {
  numberOfNodes = 3
  testDistribution = 'DEFAULT'

  setting 'xpack.security.enabled', 'true'
  setting 'xpack.ml.enabled', 'true'
  setting 'xpack.watcher.enabled', 'false'
  setting 'xpack.security.authc.token.enabled', 'true'
  setting 'xpack.security.transport.ssl.enabled', 'true'
  setting 'xpack.security.transport.ssl.key', nodeKey.name
  setting 'xpack.security.transport.ssl.certificate', nodeCert.name
  setting 'xpack.security.transport.ssl.verification_mode', 'certificate'
  setting 'xpack.security.audit.enabled', 'false'
  setting 'xpack.license.self_generated.type', 'trial'
  setting 'xpack.ml.min_disk_space_off_heap', '200mb'
  setting 'indices.lifecycle.history_index_enabled', 'false'
  setting 'slm.history_index_enabled', 'false'
  setting 'stack.templates.enabled', 'false'
  setting 'xpack.ent_search.enabled', 'false'
  setting 'xpack.apm_data.enabled', 'false'
  setting 'xpack.otel_data.registry.enabled', 'false'
  // To spice things up a bit, one of the nodes is not an ML node
  nodes.'javaRestTest-0'.setting 'node.roles', '["master","data","ingest"]'
  nodes.'javaRestTest-1'.setting 'node.roles', '["master","data","ingest","ml"]'
  nodes.'javaRestTest-2'.setting 'node.roles', '["master","data","ingest","ml"]'

  keystore 'bootstrap.password', 'x-pack-test-password'
  keystore 'xpack.security.transport.ssl.secure_key_passphrase', 'testnode'

  user username: "x_pack_rest_user", password: "x-pack-test-password"

  extraConfigFile nodeKey.name, nodeKey
  extraConfigFile nodeCert.name, nodeCert
}
