/*
 * Licensed to Elasticsearch B.V. under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch B.V. licenses this file to you 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.
 */

description = """Logstash Integration Tests"""

repositories {
  mavenCentral()
}

buildscript {
  repositories {
    mavenCentral()
  }
}

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

test {
  exclude '/**'
}

tasks.register("copyProductionLog4jConfiguration", Copy) {
  description = "Copy the production log4j config to be tested in QA (qa/integration/fixtures/logs_rollover)"

  from "${projectDir}/../../config/log4j2.properties"
  into "${projectDir}/fixtures/logs_rollover/"
  filter {
    // modify rollover policy to make it happen in integration test without need to generate tons of log lines
    line -> line
            .replace('appender.rolling.policies.size.size = 100MB',
                    'appender.rolling.policies.size.size = 1KB')
            .replace('appender.rolling.filePattern = ${sys:ls.logs}/logstash-plain-%d{yyyy-MM-dd}-%i.log.gz',
                    'appender.rolling.filePattern = ${sys:ls.logs}/logstash-plain-%d{yyyy-MM-dd}.log')
            .replace('appender.routing.pipeline.policy.size = 100MB',
                    'appender.routing.pipeline.policy.size = 1KB')
  }
}

clean {
  delete "${projectDir}/fixtures/logs_rollover/log4j2.properties"
}

tasks.register("preparePluginTestFixtures", Exec) {

  def fixtureDir = layout.projectDirectory.dir("fixtures/plugins")
  def scriptPath = fixtureDir.file("generate-gems.sh")

  inputs.file scriptPath
  inputs.files fileTree(fixtureDir) { include("*.gemspec") }
  outputs.files fileTree(fixtureDir) { include("*.gem") }

  commandLine = scriptPath.toString()
}

tasks.register("integrationTests", 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 copyProductionLog4jConfiguration
  dependsOn preparePluginTestFixtures

  inputs.files fileTree("${projectDir}/services")
  inputs.files fileTree("${projectDir}/framework")
  inputs.files fileTree("${projectDir}/fixtures")
  inputs.files fileTree("${projectDir}/specs")

  systemProperty 'logstash.root.dir', projectDir.toPath().getParent().getParent().toString()
  include '/org/logstash/integration/RSpecTests.class'

  outputs.upToDateWhen {
    if (project.hasProperty('integrationTests.rerun')) {
      println "Rerunning Integration Tests"
      return false
    } else {
      return true
    }
  }
}
