/*
 * Copyright (c) 2021, 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 plugin: 'java'

dependencies {
    testImplementation group: 'org.ballerinalang', name: 'ballerina-runtime', version: "${ballerinaLangVersion}"
    testImplementation group: 'org.ballerinalang', name: 'ballerina-test-utils', version: "${ballerinaLangVersion}"
    testImplementation group: 'org.ballerinalang', name: 'ballerina-metrics-extension', version: "${ballerinaLangVersion}"
    testImplementation group: 'org.testng', name: 'testng', version: "${testngVersion}"
}

def ballerinaDist = "build/target/extracted-distributions"

configurations {
    jbalTools {
        transitive false
    }
    testUtils {
        transitive false
    }
}

task unpackJballerinaTools(type: Copy) {
    configurations.jbalTools.resolvedConfiguration.resolvedArtifacts.each { artifact ->
        from zipTree(artifact.getFile())
        into new File(ballerinaDist, "jballerina-tools-zip")
    }
}

task copyObserveStdlib {
    dependsOn(":observe-ballerina:build")
    dependsOn(unpackJballerinaTools)
    doLast {
        /* Copy Observe module */
        copy {
            from "$project.rootDir/ballerina/build/bala_unzipped/bala"
            into "${ballerinaDist}/jballerina-tools-zip/jballerina-tools-${ballerinaLangVersion}/repo/bala"
        }
    }
}

def classFilesArray = []

task copyBallerinaClassFiles {
    doFirst {
        fileTree("$project.rootDir/ballerina/build/bal_build_target").include("**/*.jar").forEach { jar ->
            zipTree(jar).matching {
                exclude '**/tests/*'
                exclude '**/$_init.class'
                exclude '**/$value$Caller.class'
                exclude '**/$value$Detail.class'
                exclude '**/$value$DetailType.class'
                exclude '**/$value$EmptyIterator.class'
                exclude '**/$value$$anonType$_6.class'
                exclude '**/$value$$anonType$_*.class'
                exclude '**/$value$_Frame.class'
                include '**/*.class'
            }.each { file -> classFilesArray.push(file) }
        }
    }
}

jacocoTestReport {
    dependsOn test
    dependsOn copyBallerinaClassFiles
    additionalClassDirs files(classFilesArray)
    reports {
        xml.required = true
        html.required = true
    }
}

test {
    dependsOn(copyObserveStdlib)
    finalizedBy jacocoTestReport
    useTestNG() {
        suites 'src/test/resources/testng.xml'
    }
    systemProperty 'ballerina.home', "${ballerinaDist}/jballerina-tools-zip/jballerina-tools-${ballerinaLangVersion}"

    testLogging {
        events "PASSED", "FAILED", "SKIPPED"
        showStackTraces true
        showStandardStreams true
        events "failed"
        exceptionFormat "full"
        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))
            }
        }
    }
}

