// Copyright 2019 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This source-less project is used to run cross-release server/SQL integration
// tests. See the README.md file in this folder for more information.

import static com.google.common.base.Preconditions.checkArgument
import static com.google.common.base.Strings.isNullOrEmpty

if (schema_version == '' || nomulus_version == '') {
  return
}

def USE_LOCAL = 'local'

if (schema_version != USE_LOCAL || nomulus_version != USE_LOCAL) {
  checkArgument(
      !isNullOrEmpty(publish_repo),
      'The publish_repo is required when remote jars are needed.')

  repositories {
    maven {
      url project.publish_repo
    }
  }
}

def testUberJarName = ''

// Might need to add this back if we re-add nebula-lint
// gradleLint.ignore('unused-dependency') {
dependencies {
  if (schema_version == USE_LOCAL) {
    testRuntimeOnly project(path: ':db', configuration: 'schema')
  } else {
    testRuntimeOnly "google.registry:schema:${schema_version}"
  }
  if (nomulus_version == USE_LOCAL) {
    testRuntimeOnly project(path: ':core', configuration: 'nomulus_test')
    testUberJarName = 'nomulus-tests-alldeps.jar'
  } else {
    testRuntimeOnly "google.registry:nomulus_test:${nomulus_version}:public"
    testRuntimeOnly "google.registry:nomulus_test:${nomulus_version}:alldeps"
    testUberJarName = "nomulus_test-${nomulus_version}-alldeps.jar"
  }
}
// }

configurations.testRuntimeOnly.transitive = false

def unpackedTestDir = "${projectDir}/build/unpackedTests/${nomulus_version}"

// Extracts SqlIntegrationTestSuite.class to a temp folder. Gradle's test
// runner only looks for runnable tests on a regular file system. However,
// it can load member classes of test suites from jars.
task extractSqlIntegrationTestSuite (type: Copy) {
  doFirst {
    file(unpackedTestDir).mkdirs()
  }
  outputs.dir unpackedTestDir
  from zipTree(
      configurations.testRuntimeClasspath
          .filter { it.name == testUberJarName}
          .singleFile).matching {
    include 'google/registry/**/SqlIntegrationTestSuite.class'
  }
  into unpackedTestDir
  includeEmptyDirs = false

  if (nomulus_version == USE_LOCAL) {
    dependsOn ':core:testUberJar'
  }
}

// TODO(weiminyu): inherit from FilteringTest (defined in :core).
task sqlIntegrationTest(type: Test) {
  // Explicitly choose JUnit 4 for test suites. See :core:sqlIntegrationTest for details.
  useJUnit()
  testClassesDirs = files(unpackedTestDir)
  classpath = configurations.testRuntimeClasspath
  include 'google/registry/schema/integration/SqlIntegrationTestSuite.*'

  dependsOn extractSqlIntegrationTestSuite

  finalizedBy tasks.create('removeUnpackedTests') {
    doLast {
      delete file(unpackedTestDir)
    }
  }

  // Disable incremental build/test since Gradle cannot detect changes
  // in dependencies on its own. Will not fix since this test is typically
  // run once (in presubmit or ci tests).
  outputs.upToDateWhen { false }
}
