/*
 * 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
 * Public License v 1"; you may not use this file except in compliance with, at
 * your election, the "Elastic License 2.0", the "GNU Affero General Public
 * License v3.0 only", or the "Server Side Public License, v 1".
 */

apply plugin: 'elasticsearch.build'
apply plugin: 'elasticsearch.publish'

dependencies {
  api project(":client:rest")
  api project(':modules:transport-netty4')
  api project(':libs:ssl-config')
  api project(":server")
  api project(":libs:cli")
  api project(":libs:entitlement:bridge")
  api "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}"
  api "junit:junit:${versions.junit}"
  api "org.hamcrest:hamcrest:${versions.hamcrest}"
  api "org.apache.lucene:lucene-test-framework:${versions.lucene}"
  api "org.apache.lucene:lucene-codecs:${versions.lucene}"
  api "commons-logging:commons-logging:${versions.commonslogging}"
  api "commons-codec:commons-codec:${versions.commonscodec}"

  // mockito
  api 'org.mockito:mockito-core:5.15.2'
  api 'org.mockito:mockito-subclass:5.15.2'
  api 'net.bytebuddy:byte-buddy:1.15.11'
  api 'org.objenesis:objenesis:3.3'

  api "org.elasticsearch:mocksocket:${versions.mocksocket}"

  testImplementation project(":modules:mapper-extras")
  testImplementation project(':x-pack:plugin:core')
  testImplementation project(':x-pack:plugin:mapper-unsigned-long')
  testImplementation project(':x-pack:plugin:mapper-counted-keyword')
  testImplementation project(':x-pack:plugin:mapper-constant-keyword')
  testImplementation project(':x-pack:plugin:wildcard')
}

sourceSets {
  integTest {
    compileClasspath += sourceSets["main"].output + configurations["testRuntimeClasspath"]
    runtimeClasspath += output + compileClasspath
  }
}

// the main files are actually test files, so use the appropriate forbidden api sigs
tasks.named('forbiddenApisMain').configure {
  replaceSignatureFiles 'jdk-signatures', 'es-all-signatures', 'es-test-signatures'
}

// TODO: should we have licenses for our test deps?
tasks.named("dependencyLicenses").configure { enabled = false }
tasks.named("dependenciesInfo").configure { enabled = false }
tasks.named("thirdPartyAudit").configure {
  ignoreMissingClasses(
          // classes are missing
          'javax.servlet.ServletContextEvent',
          'javax.servlet.ServletContextListener',
          'org.apache.avalon.framework.logger.Logger',
          'org.apache.log.Hierarchy',
          'org.apache.log.Logger',
          'org.apache.log4j.Level',
          'org.apache.log4j.Logger',
          'org.apache.log4j.Priority',

          // mockito
          'net.bytebuddy.agent.Installer',
          'net.bytebuddy.agent.ByteBuddyAgent',
          'org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher',
          'org.opentest4j.AssertionFailedError',

          // not using JNA
         'com.sun.jna.FunctionMapper',
         'com.sun.jna.JNIEnv',
         'com.sun.jna.Library',
         'com.sun.jna.Native',
         'com.sun.jna.NativeLibrary',
         'com.sun.jna.Platform'
  )

  ignoreViolations(
    'org.objenesis.instantiator.sun.UnsafeFactoryInstantiator',
    'org.objenesis.instantiator.util.UnsafeUtils'
  )
}

tasks.named("test").configure {
  systemProperty 'tests.gradle_index_compat_versions', buildParams.bwcVersions.indexCompatible.join(',')
  systemProperty 'tests.gradle_wire_compat_versions', buildParams.bwcVersions.wireCompatible.join(',')
}

tasks.register("integTest", Test) {
  testClassesDirs = sourceSets.integTest.output.classesDirs
  classpath = sourceSets.integTest.runtimeClasspath
}

tasks.named('check').configure {
  dependsOn 'integTest'
}

tasks.register("verifyVersions") {
  dependsOn "test"
}

tasks.named('splitPackagesAudit').configure {
  // for now we always run tests with the classpath, so we are ok with split packages for tests
  enabled = false
}
