/*
 * 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.BwcVersions.UnreleasedVersionInfo
import org.elasticsearch.gradle.Version
import org.elasticsearch.gradle.VersionProperties
import org.elasticsearch.gradle.testclusters.StandaloneRestIntegTestTask

description = 'Integration tests for SQL JDBC driver'
apply plugin: 'elasticsearch.java'

// Avoid circular dependency
group = 'org.elasticsearch.x-pack.qa.sql.jdbc'

dependencies {
  api project(':test:framework')
  implementation project(':x-pack:plugin:sql:sql-proto')

  // Actual tests will use the shadow jar
  compileOnly(project(path: xpackModule('sql:jdbc'))) {
    // Since dependencies will be relocated in the shadow jar, don't attempt to compile against them
    transitive = false
  }
}

// disable unit tests because these are all integration tests used other qa projects
tasks.named("test").configure {
  enabled = false
}

subprojects {
  if (subprojects.isEmpty()) {
    // leaf project
    apply plugin: 'elasticsearch.bwc-test'
  } else {
    apply plugin: 'elasticsearch.java'
  }

  repositories {
    maven {
      // Repository for downloading BWC compatible JDBC driver releases
      url = 'https://artifacts-no-kpi.elastic.co/maven'
    }
  }

  configurations {
    jdbcDriver
  }

  dependencies {
    // We use the shadowjar for testing since that's the actual artifact we deliver to users
    jdbcDriver project(path: xpackModule('sql:jdbc'), configuration: 'shadow')
  }

  if (project.name != 'security') {
    // The security project just configures its subprojects
    apply plugin: 'elasticsearch.legacy-java-rest-test'

    dependencies {
      javaRestTestImplementation(project(':x-pack:plugin:sql:qa:jdbc'))
      // We use the shadowjar for testing since that's the actual artifact we deliver to users
      javaRestTestCompileOnly project(path: xpackModule('sql:jdbc'), configuration: 'shadow')
    }
    testClusters.configureEach {
      testDistribution = 'DEFAULT'
      setting 'xpack.ml.enabled', 'false'
      setting 'xpack.watcher.enabled', 'false'
    }

    tasks.named("javaRestTest").configure {
        classpath += configurations.jdbcDriver
        systemProperty 'jdbc.driver.version', VersionProperties.elasticsearch
    }

    // Configure compatibility testing tasks
    // Compatibility testing for JDBC driver started with version 7.9.0
    buildParams.bwcVersions.indexCompatible.findAll({ it.onOrAfter(Version.fromString("7.9.0")) && it != VersionProperties.elasticsearchVersion }).each { bwcVersion ->
      def baseName = "v${bwcVersion}"
      def cluster = testClusters.register(baseName)

      UnreleasedVersionInfo unreleasedVersion = buildParams.bwcVersions.unreleasedInfo(bwcVersion)
      Configuration driverConfiguration = configurations.create("jdbcDriver${baseName}") {
        // TODO: Temporary workaround for https://github.com/elastic/elasticsearch/issues/73433
        transitive = false
      }
      Object driverDependency = null

      if (unreleasedVersion) {
        // For unreleased snapshot versions, build them from source
        driverDependency = files(project(unreleasedVersion.gradleProjectPath).tasks.named('buildBwcJdbc'))
      } else {
        // For released versions, download it
        driverDependency = "org.elasticsearch.plugin:x-pack-sql-jdbc:${bwcVersion}"
      }

      dependencies {
        "jdbcDriver${baseName}"(driverDependency)
      }

      final String bwcVersionString = bwcVersion.toString()
      tasks.register(bwcTaskName(bwcVersion), StandaloneRestIntegTestTask) {
          useCluster cluster
          classpath = sourceSets.javaRestTest.runtimeClasspath + driverConfiguration
          testClassesDirs = sourceSets.javaRestTest.output.classesDirs
          systemProperty 'jdbc.driver.version', bwcVersionString
          nonInputProperties.systemProperty('tests.rest.cluster', "${-> cluster.get().allHttpSocketURI.join(",")}")
          nonInputProperties.systemProperty('tests.clustername', baseName)
      }
    }
  }
}
