/*
 * Copyright (c) 2020, 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.
 *
 */

plugins {
    id 'java'
    id 'checkstyle'
}

description = 'Ballerina - gRPC Java Native'

dependencies {
    checkstyle project(':checkstyle')
    checkstyle "com.puppycrawl.tools:checkstyle:${checkstylePluginVersion}"

    implementation group: 'org.ballerinalang', name: 'ballerina-lang', version: "${ballerinaLangVersion}"
    implementation group: 'org.ballerinalang', name: 'ballerina-tools-api', version: "${ballerinaLangVersion}"
    implementation group: 'org.ballerinalang', name: 'ballerina-parser', version: "${ballerinaLangVersion}"
    implementation group: 'org.ballerinalang', name: 'toml-parser', version: "${ballerinaLangVersion}"
    implementation group: 'org.ballerinalang', name: 'formatter-core', version: "${ballerinaLangVersion}"
    implementation (group: 'org.ballerinalang', name: 'ballerina-cli', version: "${ballerinaLangVersion}") {
        exclude group: 'org.ballerinalang', module: 'maven-resolver'
    }
    implementation group: 'org.ballerinalang', name: 'ballerina-runtime', version: "${ballerinaLangVersion}"
    implementation group: 'io.ballerina.stdlib', name: 'http-native', version: "${stdlibHttpVersion}"
    implementation group: 'org.slf4j', name: 'slf4j-jdk14', version: "${slf4jVersion}"
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: "${commonsLang3Version}"
    implementation group: 'com.google.api.grpc', name: 'proto-google-common-protos', version: "${protoGoogleCommonsVersion}"
    implementation group: 'com.google.protobuf', name: 'protobuf-java', version: "${protobufJavaVersion}"
    implementation group: 'io.netty', name: 'netty-codec-http', version: "${nettyVersion}"
    implementation group: 'info.picocli', name: 'picocli', version: "${picocliVersion}"
    implementation group: 'io.ballerina.stdlib', name: 'protobuf-native', version: "${stdlibProtobufVersion}"

    testImplementation group: 'org.testng', name: 'testng', version: "${testngVersion}"
    testImplementation group: 'org.mockito', name:'mockito-core', version: "${mockitoVersion}"
    testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: "${mockitoVersion}"
}

test {
    useTestNG() {
        suites 'src/test/resources/testng.xml'
    }
    testLogging.showStandardStreams = true
    testLogging {
        events "PASSED", "FAILED", "SKIPPED"
        afterSuite { desc, result ->
            if (!desc.parent) { // will match the outermost suite
                def output = "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
                def startItem = '|  ', endItem = '  |'
                def repeatLength = startItem.length() + output.length() + endItem.length()
                println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength))
            }
        }
    }
    finalizedBy jacocoTestReport
}

jacoco {
    toolVersion = "${jacocoVersion}"
}

jacocoTestReport {
    dependsOn test
    reports {
        xml.required = true
    }
}

checkstyle {
    toolVersion "${checkstylePluginVersion}"
    configFile rootProject.file("build-config/checkstyle/build/checkstyle.xml")
    configProperties = ["suppressionFile" : file("${rootDir}/build-config/checkstyle/build/suppressions.xml")]
}

checkstyleMain.dependsOn(":checkstyle:downloadCheckstyleRuleFiles")
checkstyleTest.dependsOn(":checkstyle:downloadCheckstyleRuleFiles")

def excludePattern = '**/module-info.java'
tasks.withType(Checkstyle) {
    exclude excludePattern
}

compileJava {
    doFirst {
        options.compilerArgs = [
                '--module-path', classpath.asPath,
        ]
        classpath = files()
    }
}
