/*
 * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) 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.
 *
 */

apply from: "$rootDir/gradle/javaProject.gradle"

configurations {
    extensionPlugin
    tooling
    jacocoRuntime
}

dependencies {
    implementation project(":cloud-compiler-plugin")
    implementation project(":cloud-tooling")

    testImplementation project(":cloud-util")
    testImplementation "org.ballerinalang:ballerina-lang:${ballerinaLangVersion}"
    testImplementation "com.github.docker-java:docker-java:${dockerJavaVersion}"
    testImplementation "com.github.docker-java:docker-java-transport-httpclient5:${dockerJavaVersion}"
    testImplementation "io.fabric8:kubernetes-client:${fabric8KubernetesClientVersion}"
    testImplementation "org.slf4j:slf4j-api:${slf4jVersion}"
    testImplementation "org.testng:testng:${testngVersion}"
    testImplementation "org.ballerinalang:language-server-core:${ballerinaLangVersion}"
    testImplementation "org.ballerinalang:language-server-commons:${ballerinaLangVersion}"
    testImplementation "org.ballerinalang:language-server-cli:${ballerinaLangVersion}"
    testImplementation "org.ballerinalang:ballerina-cli:${ballerinaLangVersion}"
    testImplementation "org.ballerinalang:toml-parser:${ballerinaLangVersion}"
    testImplementation "org.ballerinalang:ballerina-tools-api:${ballerinaLangVersion}"

    testImplementation 'org.eclipse.lsp4j:org.eclipse.lsp4j:0.12.0'
    testImplementation 'org.eclipse.lsp4j:org.eclipse.lsp4j.jsonrpc:0.12.0'

    extensionPlugin project(path: ":cloud-compiler-plugin", configuration: "shadow")
    tooling project(path: ":cloud-tooling")
}

description = 'Ballerina - C2C Extension Tests'

task copyTooling {
    dependsOn configurations.tooling
    doLast {
        copy {
            from file("${projectDir}/../cloud-tooling/build/libs/")
            into file("${project.rootDir}/target/ballerina-runtime/lib/tools/lang-server/lib/")
        }
    }
}

test {
    jvmArgs = ['--add-opens=java.base/java.util=ALL-UNNAMED']
    dependsOn copyTooling
    useTestNG() {
        if (project.hasProperty('Travis')) {
            suites 'src/test/resources/testng-integration.xml'
        } else {
            suites 'src/test/resources/testng.xml'
        }
        systemProperties = [
                "ballerina.home" : "${project.rootDir}/target/ballerina-runtime/",
                "ballerina.version" : "${version}",
                "experimental" : "true",
                sampleDir      : System.getProperty('sampleDir', project.projectDir.absolutePath + "/../examples"),
                jacocoAgentLine: "-javaagent:${configurations.jacocoRuntime.asPath}=destfile=${test.jacoco.destinationFile.absolutePath},append=true,inclnolocationclasses=true,exclclassloader=jdk.internal.reflect.DelegatingClassLoader"
        ]
    }
    finalizedBy jacocoTestReport
}

test.dependsOn ":cloud-ballerina:build"

jacocoTestReport {
    dependsOn test
    def classFiles = new File("${rootDir}/compiler-plugin/build/classes/java/main/io/ballerina/c2c")
    additionalClassDirs(classFiles)
    reports {
        xml.required = true
        html.required = true
        csv.required = true
        xml.destination = new File("${buildDir}/reports/jacoco/report.xml")
        html.destination = new File("${buildDir}/reports/jacoco/report.html")
        csv.destination = new File("${buildDir}/reports/jacoco/report.csv")
    }
}

// Disable publishing artifacts
tasks.withType(PublishToMavenRepository) {
    enabled = false
}
tasks.withType(PublishToMavenLocal) {
    enabled = false
}

tasks.checkstyleTest.dependsOn(":checkstyle:downloadMultipleFiles")
