/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */

description = """Logstash X-Pack"""

project.ext.LOGSTASH_CORE_PATH = "${projectDir}/../logstash-core"
apply from: "../rubyUtils.gradle"

repositories {
  mavenCentral()
}

buildscript {
  repositories {
    mavenCentral()
  }
}

configurations {
  geolite2
}

dependencies {
  testImplementation project(':logstash-core')
  testImplementation 'org.assertj:assertj-core:3.8.0'
  testImplementation 'junit:junit:4.13.2'

  geolite2('org.elasticsearch:geolite2-databases:20191119') {
    transitive = false
  }
}

test {
  exclude '/**'
}

tasks.register("unzipGeolite", Copy) {
  from(zipTree(configurations.geolite2.singleFile)) {
    include "GeoLite2-ASN.mmdb"
    include "GeoLite2-City.mmdb"
  }
  into file("${projectDir}/spec/filters/geoip/vendor")
}

tasks.register("rubyTests", Test) {
  dependsOn unzipGeolite
  testClassesDirs = sourceSets.test.output.classesDirs
  classpath = sourceSets.test.runtimeClasspath
  inputs.files fileTree("${projectDir}/spec")
  inputs.files fileTree("${projectDir}/lib")
  inputs.files fileTree("${projectDir}/modules")
  systemProperty 'logstash.root.dir', projectDir.parent
  include '/org/logstash/xpack/test/RSpecTests.class'
}

tasks.register("rubyIntegrationTests", Test) {
  if ((JavaVersion.current().getMajorVersion() as int) >= 17) {
    jvmArgs = ['--add-opens', 'java.base/sun.nio.ch=ALL-UNNAMED', '--add-opens', 'java.base/java.io=ALL-UNNAMED']
  }
  testClassesDirs = sourceSets.test.output.classesDirs
  classpath = sourceSets.test.runtimeClasspath
  dependsOn (":copyEs")
  dependsOn ":assemble"
  dependsOn "buildFipsValidationGem"
  inputs.files fileTree("${projectDir}/qa")
  inputs.files fileTree("${projectDir}/lib")
  inputs.files fileTree("${projectDir}/modules")
  inputs.files fileTree("${rootProject.projectDir}/Gemfile.lock")
  inputs.files fileTree("${rootProject.projectDir}/logstash-core/lib")
  systemProperty 'logstash.root.dir', projectDir.parent
  include '/org/logstash/xpack/test/RSpecIntegrationTests.class'
}

tasks.register("buildFipsValidationGem") {
  doLast {
    rake(rootProject.projectDir, rootProject.buildDir, 'plugin:build-fips-validation-plugin')
  }
}

tasks.register("observabilitySREsmokeTests", Test) {
    description = "Run ObservabilitySRE smoke tests using RSpec with docker-compose orchestration"
    testClassesDirs = sourceSets.test.output.classesDirs
    classpath = sourceSets.test.runtimeClasspath
    // Need to have set up the ruby environment for rspec even through we are running in container
    dependsOn(":bootstrap", ":logstash-core:assemble", ":installDevelopmentGems")
    inputs.files fileTree("${projectDir}/distributions/internal/observabilitySRE/qa/smoke")
    doFirst {
        // Generate the certificates first
        exec {
            workingDir file("distributions/internal/observabilitySRE/qa/smoke/docker/certs")
            commandLine 'bash', './generate.sh'
            ignoreExitValue = false
        }
    }
    systemProperty 'logstash.root.dir', projectDir.parent
    include '**/org/logstash/xpack/test/RSpecObservabilitySRETests.class'
    doLast {
        // Clean up the generated certificates
        delete fileTree("distributions/internal/observabilitySRE/qa/smoke/docker/certs").include("*.key", "*.crt", "*.csr", "*.srl")
    }
}

tasks.register("observabilitySREacceptanceTests", Test) {
    description = "Run ObservabilitySRE acceptance tests using RSpec with docker-compose orchestration"
    testClassesDirs = sourceSets.test.output.classesDirs
    classpath = sourceSets.test.runtimeClasspath
    // Need to have set up the ruby environment for rspec even through we are running in container
    dependsOn(":bootstrap", ":logstash-core:assemble", ":installDevelopmentGems")
    inputs.files fileTree("${projectDir}/distributions/internal/observabilitySRE/qa/acceptance")
    doFirst {
        // Generate the certificates first
        exec {
            workingDir file("distributions/internal/observabilitySRE/qa/acceptance/docker/certs")
            commandLine 'bash', './generate.sh'
            ignoreExitValue = false
        }
    }
    systemProperty 'logstash.root.dir', projectDir.parent
    include '**/org/logstash/xpack/test/RSpecObservabilitySREAcceptanceTests.class'
    doLast {
        // Clean up the generated certificates
        delete fileTree("distributions/internal/observabilitySRE/qa/acceptance/docker/certs").include("*.key", "*.crt", "*.csr", "*.srl")
    }
}
