/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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.
 */
import groovy.json.JsonOutput

plugins { id 'org.apache.beam.module' }
applyJavaNature(
        automaticModuleName: 'org.apache.beam.sdk.io.debezium',
        mavenRepositories: [
                [id: 'io.confluent', url: 'https://packages.confluent.io/maven/']
        ],
        requireJavaVersion: JavaVersion.VERSION_17,
)
provideIntegrationTestingDependencies()

description = "Apache Beam :: SDKs :: Java :: IO :: Debezium"
ext.summary = "Library to work with Debezium data."

dependencies {
    implementation library.java.vendored_guava_32_1_2_jre
    implementation library.java.vendored_grpc_1_69_0
    implementation project(path: ":sdks:java:core", configuration: "shadow")
    implementation library.java.slf4j_api
    implementation library.java.joda_time
    // Explicitly declare Jackson dependencies
    // and align with the 2.17.1 version.
    implementation 'com.fasterxml.jackson.core:jackson-core:2.17.1'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.1'
    permitUnusedDeclared 'com.fasterxml.jackson.core:jackson-core:2.17.1'
    permitUnusedDeclared 'com.fasterxml.jackson.core:jackson-databind:2.17.1'
    provided library.java.jackson_dataformat_csv
    permitUnusedDeclared library.java.jackson_dataformat_csv

    // Kafka connect dependencies
    implementation "org.apache.kafka:connect-api:3.9.0"

    // Debezium dependencies
    implementation group: 'io.debezium', name: 'debezium-core', version: '3.1.1.Final'

    // Test dependencies
    testImplementation project(path: ":sdks:java:core", configuration: "shadowTest")
    testImplementation project(path: ":sdks:java:io:common")
    testImplementation library.java.junit
    testImplementation project(path: ":sdks:java:io:jdbc")
    testRuntimeOnly library.java.slf4j_jdk14
    testRuntimeOnly project(path: ":runners:direct-java", configuration: "shadow")
    testImplementation project(":runners:google-cloud-dataflow-java")
    testImplementation library.java.hamcrest
    testImplementation library.java.testcontainers_base
    testImplementation "org.testcontainers:kafka"
    testImplementation "org.testcontainers:mysql"
    testImplementation "org.testcontainers:postgresql"
    testImplementation "io.debezium:debezium-testing-testcontainers:3.1.1.Final"
    testImplementation 'com.zaxxer:HikariCP:5.1.0'

    // Debezium connector implementations for testing
    testImplementation group: 'io.debezium', name: 'debezium-connector-mysql', version: '3.1.1.Final'
    testImplementation group: 'io.debezium', name: 'debezium-connector-postgres', version: '3.1.1.Final'
}

// Pin the Antlr version to 4.10
// and force Jackson versions to 2.17.1
// TODO: Remove pin after upgrading Beam's Jackson version
// This overrides the global 2.15.4 forced by BeamModulePlugin.
configurations.all {
    resolutionStrategy {
        force 'org.antlr:antlr4:4.10',
                'org.antlr:antlr4-runtime:4.10',
                'com.fasterxml.jackson.core:jackson-core:2.17.1',
                'com.fasterxml.jackson.core:jackson-annotations:2.17.1',
                'com.fasterxml.jackson.core:jackson-databind:2.17.1',
                'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.17.1'
    }
}

def configureTestJvmArgs(Task task) {
    List<String> currentJvmArgs = task.jvmArgs ? new ArrayList<>(task.jvmArgs) : new ArrayList<>()

    // Add standard opens required for Jackson, Afterburner, and other reflective frameworks
    // when dealing with Java Modules or complex classloader scenarios.
    currentJvmArgs.addAll([
        '--add-opens=java.base/java.lang=ALL-UNNAMED',
        '--add-opens=java.base/java.util=ALL-UNNAMED',
        '--add-opens=java.base/java.util.concurrent=ALL-UNNAMED',
        '--add-opens=java.base/java.lang.reflect=ALL-UNNAMED',
        '--add-opens=java.base/java.io=ALL-UNNAMED',
        '--add-opens=java.base/java.nio=ALL-UNNAMED',
        '--add-opens=java.base/java.math=ALL-UNNAMED',
        '--add-opens=java.base/java.time=ALL-UNNAMED', // For JSR310 types
    ])

    task.jvmArgs = currentJvmArgs.unique() // Assign the new, filtered list back, removing duplicates
    project.logger.lifecycle("Task ${task.name} final jvmArgs: ${task.jvmArgs.join(' ')}")
}

test {
    testLogging {
        outputs.upToDateWhen {false}
        showStandardStreams = true
        exceptionFormat = 'full'
    }
    configureTestJvmArgs(delegate)

}

task integrationTest(type: Test, dependsOn: processTestResources) {
  group = "Verification"
  systemProperty "beamTestPipelineOptions", JsonOutput.toJson([
          "--runner=DirectRunner",
  ])

  // Disable Gradle cache: these ITs interact with live service that should always be considered "out of date"
  outputs.upToDateWhen { false }

  include '**/*IT.class'
  classpath = sourceSets.test.runtimeClasspath
  testClassesDirs = sourceSets.test.output.classesDirs

  useJUnit {
  }
}
