plugins {
    id 'idea'
    id 'java'
    id 'io.qameta.allure' version '2.8.1'
    id 'io.franzbecker.gradle-lombok' version '4.0.0'
}

def props = new Properties()
file("src/test/resources/props/local.properties").withInputStream { props.load(it) }
def withoutBom = {
    exclude group: 'org.apache.jmeter', module: 'bom'
    exclude group: 'us.abstracta.jmeter', module: 'bom'
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    maven { url 'https://artifacts-internal.delphiplatform.io/repository/maven-public' }
    maven {
        url "https://plugins.gradle.org/m2/"
    }
}
ext {
    allureV = '2.14.0'//'2.13.8'
    guavaV = '30.1-jre'
    testngV = '7.4.0'
    aspectjweaverV = '1.9.4'
    allureGradleV = '2.8.1'
    allureTestngV = '2.14.0'
    allureJavaCommonsV = '2.14.0'
    gsonV = '2.8.6'
    lombokV = '1.18.26'
    awsV = '1.11.901'
    pgV = '42.2.18'
    swaggerCoverageV = '1.4.1'
    restAssuredV = '4.3.3'
    allureAssuredLogerV = '2.13.8'
    jsonSchemaValidatorV = '4.3.3'
    ownerV = '1.0.12'
    slf4jV = '2.0.0-alpha1'
    logbackV = '1.3.0-alpha4'
    hamcrestV = '2.0.0.0'
    jiraRestClientCoreV = '5.2.2'
    jiraRestClientAppV = '5.2.2'
    fugueV = '4.7.2'
    ioV = '2.6'
    commonDbUtilsV = '1.7'
    snowflakeV = '3.12.17'
    jacksonV = '2.11.0'
//    jmeterJavaDslV = '0.43'
    delphiEnv = project.hasProperty('delphi.env.run') ?
            project.getProperty('delphi.env.run') :
            props.getProperty('delphi.env.run')
    delphiHost = project.hasProperty('delphi.api.host') ?
            project.getProperty('delphi.api.host') :
            props.getProperty('delphi.api.host')
    testNgGroups = project.hasProperty('delphi.env.run.testNg.groups') ?
            project.getProperty('delphi.env.run.testNg.groups') :
            props.getProperty('delphi.env.run.testNg.groups')
}
allure {
    version = allureV
    autoconfigure = true
    aspectjweaver = true
//    resultsDir = file('../../allure/DEV/allure-results')
//    reportDir = file('../../allure/DEV/allure-report')
    downloadLink = "https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/${allureV}/allure-commandline-${allureV}.zip"
}
configurations {
    agent
}
dependencies {
    //BigTable
    implementation platform('com.google.cloud:libraries-bom:26.10.0')
    implementation 'com.google.cloud:google-cloud-bigtable'
    // Allure Steps annotations support
    agent "org.aspectj:aspectjweaver:${aspectjweaverV}"
    compile "org.aspectj:aspectjweaver:${aspectjweaverV}"
    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    compile "com.google.guava:guava:$guavaV"
    // Utility
    compile "com.google.code.gson:gson:$gsonV"
    // Use TestNG framework, also requires calling test.useTestNG() below
    testImplementation "org.testng:testng:$testngV"
    compile "org.hamcrest:java-hamcrest:$hamcrestV"
    // Aws stack
    compile "com.amazonaws:aws-java-sdk-s3:$awsV"
    compile "com.amazonaws:aws-java-sdk-lambda:$awsV"
    compile "com.amazonaws:aws-java-sdk-secretsmanager:$awsV"
    compile "com.amazonaws:aws-java-sdk-stepfunctions:$awsV"
    compile "com.amazonaws:aws-java-sdk-cloudwatch:$awsV"
    compile "com.amazonaws:aws-java-sdk-logs:$awsV"
    compile "com.amazonaws:aws-java-sdk-rds:$awsV"
    compile "org.projectlombok:lombok:$lombokV"
    compile "org.postgresql:postgresql:$pgV"
    //Allure report:
    //|_Gradle integration
    compile "io.qameta.allure:allure-gradle:${allureGradleV}"
    //|_TestNg integration
    compile "io.qameta.allure:allure-testng:${allureTestngV}"
    //|_Allure command line tool integration
    compile "io.qameta.allure:allure-java-commons:${allureJavaCommonsV}"
    //|_RestAssured logging integration
    compile "io.qameta.allure:allure-rest-assured:${allureAssuredLogerV}"
    // Json serialization
    compile "com.google.code.gson:gson:${gsonV}"
    // Swagger coverage
//    compile "com.github.viclovsky.swagger.coverage:swagger-coverage-rest-assured:${swaggerCoverageV}"
    compile "com.github.viclovsky:swagger-coverage-rest-assured:${swaggerCoverageV}"
    // Rest assured
    compile "io.rest-assured:rest-assured:${restAssuredV}"
    compile "io.rest-assured:json-schema-validator:${jsonSchemaValidatorV}"
    // Envs simplification
    compile "org.aeonbits.owner:owner:${ownerV}"
    // Logs
    compile "org.slf4j:slf4j-api:${slf4jV}"
    compile "ch.qos.logback:logback-classic:${logbackV}"
    // jira integration
    compile "com.atlassian.jira:jira-rest-java-client-core:${jiraRestClientCoreV}"
    compile "com.atlassian.jira:jira-rest-java-client-app:${jiraRestClientAppV}"
    compile "io.atlassian.fugue:fugue:${fugueV}"
    // CFile utils io
    compile "commons-io:commons-io:${ioV}"
    compile "commons-dbutils:commons-dbutils:${commonDbUtilsV}"
    // DB Snowflake driver
    compile "net.snowflake:snowflake-jdbc:${snowflakeV}"
    // CSV
    compile "com.fasterxml.jackson.dataformat:jackson-dataformat-csv:${jacksonV}"
    //    TODO: Extract version
    compile "org.slf4j:slf4j-api:2.0.0-alpha2"
    compile "com.github.dpaukov:combinatoricslib3:3.3.0"
    //    TODO: Debug
//    compile "org.apache.jmeter:ApacheJMeter_core:5.2.1", withoutBom
    //   compile "us.abstracta.jmeter:jmeter-java-dsl:${jmeterJavaDslV}", withoutBom
    //    benchmarking
    compile 'org.openjdk.jmh:jmh-core:1.33'
    compile 'org.openjdk.jmh:jmh-parent:1.33'
    compile 'org.assertj:assertj-core:3.21.0'
    compile 'junit:junit:4.13'
    compile "org.hamcrest:hamcrest:2.2"
    implementation 'com.sonymusic:delphi-etl-apps-proto_2.11:1.8.0'
}

tasks.withType(Test) {
    group 'buildTest'
    description 'Task that contains different tests'
    outputs.upToDateWhen { false }
    ignoreFailures = true
    doFirst {
        jvmArgs "-javaagent:${configurations.agent.singleFile}"
    }
//    jvmArgs("-XX:+ShowCodeDetailsInExceptionMessages")
    jvmArgs("-Ddataproviderthreadcount=25")
    systemProperty 'org.gradle.parallel', 'false'
    testLogging {
        setExceptionFormat("full") // Prints exception messages
    }
}

task _0copyAllureCategories(type: Copy) {
    from("src/test/resources/") {
        include("categories.json")
    }
    into "build/allure-results/"
}
task _1downloadSwaggerCoverage(type: Exec) {
    workingDir "$projectDir/openapi"
    commandLine 'sh', './2_download_swagger_coverage_commandline_tool_latest.sh'
}
task _2downloadSpecOpenapi3(type: Exec) {
    workingDir "$projectDir/openapi"
    commandLine 'sh', './2_download_java_api_latest_as_openapi_3_yaml.sh'
}
task _3convertSpecOpenapi3ToSwagger2(type: Exec) {
    workingDir "$projectDir/openapi"
    commandLine 'sh', './3_convert_openapi_3_yaml_2_openapi_2_json.sh'
}
task _4genSwaggerCoverage(type: Exec) {
    workingDir "$projectDir/openapi"
    commandLine 'sh', './4_generate_java_api_coverage_from_swagger_2.sh'
}
task _5localSwaggerCoverage(type: Exec) {
    workingDir "$projectDir/openapilocal"
    commandLine 'sh', './4_generate_java_api_coverage_from_swagger_2.sh'
}

task prodDelphiExpComponentTests(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run", delphiEnv
    systemProperty "delphi.env.isProd", true
    systemProperty "delphi.slz.db.pg.isAutomation", false
    useTestNG() {
        suites suite_exp_integration
    }
}

task ciDelphiExpComponentTests(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run", delphiEnv
    systemProperty "delphi.env.run.type.isLogAll", true
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", false
    systemProperty "delphi.slz.db.pg.isAutomation", false
    useTestNG() {
        suites suite_exp_integration
    }
}
task ciDelphiJavaApiComponentTests(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLogAll", false
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", false
    systemProperty "delphi.env.run.type.isDeepAssert", false
    systemProperty "delphi.slz.db.pg.isAutomation", false
    systemProperty "delphi.api.date.start.shift", 90
    systemProperty "delphi.api.date.end.shift", 30
    systemProperty "delphi.env.run.retry.maxCount", 3
    systemProperty "delphi.env.run", delphiEnv
    useTestNG() {
        suites suite_api_integration
        parallel 'methods'
        threadCount 4
        includeGroups testNgGroups
    }
}
task allSwaggerTests_JavaApi(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLogAll", false
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", false
    systemProperty "delphi.env.run.type.isDeepAssert", false
    systemProperty "delphi.slz.db.pg.isAutomation", false
    systemProperty "delphi.api.date.start.shift", 90
    systemProperty "delphi.api.date.end.shift", 30
    systemProperty "delphi.env.run.retry.maxCount", 3
    systemProperty "delphi.env.run", delphiEnv
    systemProperty "delphi.api.host", delphiHost
    useTestNG() {
        suites suite_api_integration
        parallel 'methods'
        threadCount 2
        includeGroups testNgGroups
    }
}

task api_smoke_prod_JavaApi(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLogAll", false
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", false
    systemProperty "delphi.env.run.type.isDeepAssert", false
    systemProperty "delphi.slz.db.pg.isAutomation", false
    systemProperty "delphi.api.date.start.shift", 90
    systemProperty "delphi.api.date.end.shift", 30
    systemProperty "delphi.env.run.retry.maxCount", 3
    systemProperty "delphi.env.run", delphiEnv
    systemProperty "delphi.api.host", delphiHost
    useTestNG() {
        suites suite_api_smoke_prod
        parallel 'methods'
        threadCount 3
        includeGroups testNgGroups
    }
}

task ciDelphiJavaApiEdgeTests(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLogAll", false
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", false
    systemProperty "delphi.env.run.type.isDeepAssert", false
    systemProperty "delphi.slz.db.pg.isAutomation", false
    systemProperty "delphi.env.run.retry.maxCount", 3
    systemProperty "delphi.api.host", delphiHost
    useTestNG() {
        suites suite_api_edge
        parallel 'method'
        threadCount 4
    }
}
task ciDelphiJavaApiEdgeApolloTests(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLogAll", false
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", false
    systemProperty "delphi.env.run.type.isDeepAssert", false
    systemProperty "delphi.slz.db.pg.isAutomation", false
    systemProperty "delphi.env.run.retry.maxCount", 1
    useTestNG() {
        suites suite_api_edge_apollo
        parallel 'method'
        threadCount 4
    }
}

task ciDelphiJavaApiEdgeDecibelTests_DeepAssert(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLogAll", false
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", false
    systemProperty "delphi.env.run.type.isDeepAssert", true
    systemProperty "delphi.slz.db.pg.isAutomation", false
    systemProperty "delphi.env.run.retry.maxCount", 1
    useTestNG() {
        suites suite_api_edge_decibel
        parallel 'method'
        threadCount 4
    }
}
task ciDelphiSlzComponentTests(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLocal", true
    systemProperty "delphi.env.run.type.isLogAll", true
    systemProperty "delphi.env.run.type.isLogPretty", true
    systemProperty "delphi.slz.db.pg.isAutomation", true
    systemProperty "delphi.slz.cfg.complete_criteria", 0
    systemProperty "delphi.env.run.retry.maxCount", 2
    systemProperty "delphi.env.run", delphiEnv
    useTestNG() {
        suites suite_slz_integration
        parallel 'tests'
        threadCount 2
    }
}
task ciDelphiUnitTests(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run", delphiEnv
    systemProperty "delphi.api.host", delphiHost
    useTestNG() {
        suites suite_slz_unit
        parallel 'classes'
        threadCount 32
    }
}

task ciDelphiRowLevelSecurityTests(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run", delphiEnv
    systemProperty "delphi.env.run.type.isLogAll", true
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", false
    systemProperty "delphi.slz.db.pg.isAutomation", false
    useTestNG() {
        suites suite_exp_integration
    }
}

task ciDelphiL2ArtistOnboardingApi(type: Test) {
    // Ensure properties are provided, otherwise fail the build
    doFirst {
        if (!project.hasProperty('grasId') || !project.hasProperty('artistName') || !project.hasProperty('isrc')) {
            throw new GradleException("Both 'grasId' and 'isrc' and artistName properties must be provided for the " +
                    "ciDelphiL2ArtistOnboardingApi task.")
        }
    }
    def artistName = project.getProperty('artistName')
    def grasId = project.getProperty('grasId')
    def isrc = project.getProperty('isrc')


    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run", delphiEnv
    systemProperty "delphi.api.host", delphiHost
    systemProperty "artistName", artistName
    systemProperty "grasId", grasId
    systemProperty "isrc", isrc
    useTestNG() {
        suites suite_l2_artist_onboarding_api
        parallel 'methods'
        threadCount 128
    }
}

task ciDelphiDAPDApiComparison(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run", delphiEnv
    systemProperty "delphi.api.host", delphiHost
    useTestNG() {
        suites suite_dapd_api_comparison
        parallel 'methods'
        threadCount 64
        includeGroups testNgGroups
    }
}

task ciDebug(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.slz.report.date.days.shift", 3
    systemProperty "delphi.env.run.type.isLogAll", false
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.slz.cfg.complete_criteria", -1
    systemProperty "delphi.env.run", delphiEnv
    useTestNG() {
        suites suite_debug
        parallel 'methods'
        threadCount 64
    }
}

task ciDebugSlz_CleanDb(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run", delphiEnv
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogAll", true
    systemProperty "delphi.env.run.type.isLogPretty", true
    systemProperty "delphi.slz.db.pg.isAutomation", true
    systemProperty "delphi.slz.cfg.complete_criteria", 0
    useTestNG() {
        suites suite_debug_clean_pg
        parallel 'tests'
        threadCount 8
    }
}
task ciDebugJavaApi(type: Test) {
    systemProperty "delphi.env.run", delphiEnv
    systemProperty "delphi.env.run.type.isLogAll", false
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", true
    systemProperty "delphi.env.run.type.isDeepAssert", false
    systemProperty "delphi.slz.db.pg.isAutomation", false
    systemProperty "delphi.env.run.retry.maxCount", 3
    systemProperty allure, allure
    systemProperty backEndProps, backEndPropsLocal
    useTestNG() {
        suites suite_debug
        parallel 'methods'
        threadCount 8
        includeGroups testNgGroups
    }
}

task tokenizeLogs(type: Test) {
    systemProperty allure, allure
    useTestNG() {
        suites suite_tokenize_logs
    }
}

task runEtlSnowflakeExportToPostgresAll(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run", delphiEnv
    useTestNG() {
        suites suite_etl_snowflake_export_to_postgres_run_all
    }
}

task testEtlSnowflakeExportToPostgresLinkfire(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run", delphiEnv
    useTestNG() {
        suites suite_etl_snowflake_export_to_postgres_run_all
    }
}
task underReview_JavaApi(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLogAll", false
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", false
    systemProperty "delphi.env.run.type.isDeepAssert", false
    systemProperty "delphi.slz.db.pg.isAutomation", false
    systemProperty "delphi.api.date.start.shift", 90
    systemProperty "delphi.api.date.end.shift", 30
    systemProperty "delphi.env.run.retry.maxCount", 3
    systemProperty "delphi.env.run", delphiEnv
    useTestNG() {
        suiteName = "Tests Currently Under Development"
        include("io/delphi/qa/auto/awsmfrw/tests/api/review/**")
        preserveOrder true
        parallel 'methods'
        threadCount 4
    }
}

task ads_JavaApi(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLogAll", false
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", false
    systemProperty "delphi.env.run.type.isDeepAssert", false
    systemProperty "delphi.slz.db.pg.isAutomation", false
    systemProperty "delphi.api.date.start.shift", 90
    systemProperty "delphi.api.date.end.shift", 30
    systemProperty "delphi.env.run.retry.maxCount", 3
    systemProperty "delphi.env.run", delphiEnv
    useTestNG() {
        suites suite_ads
        parallel 'methods'
        threadCount 4
        includeGroups testNgGroups
    }
}

task playlists_JavaApi(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLogAll", false
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", false
    systemProperty "delphi.env.run.type.isDeepAssert", false
    systemProperty "delphi.slz.db.pg.isAutomation", false
    systemProperty "delphi.api.date.start.shift", 90
    systemProperty "delphi.api.date.end.shift", 30
    systemProperty "delphi.env.run.retry.maxCount", 3
    systemProperty "delphi.env.run", delphiEnv
    useTestNG() {
        suites suite_playlists
        parallel 'methods'
        threadCount 4
        includeGroups testNgGroups
    }
}

task dataHealth_JavaApi(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLogAll", false
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", false
    systemProperty "delphi.env.run.type.isDeepAssert", false
    systemProperty "delphi.slz.db.pg.isAutomation", false
    systemProperty "delphi.api.date.start.shift", 90
    systemProperty "delphi.api.date.end.shift", 30
    systemProperty "delphi.env.run.retry.maxCount", 3
    systemProperty "delphi.env.run", delphiEnv
    useTestNG() {
        suites suite_data_health
        parallel 'methods'
        threadCount 4
        includeGroups testNgGroups
    }
}

task streams_JavaApi(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLogAll", false
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", false
    systemProperty "delphi.env.run.type.isDeepAssert", false
    systemProperty "delphi.slz.db.pg.isAutomation", false
    systemProperty "delphi.api.date.start.shift", 90
    systemProperty "delphi.api.date.end.shift", 30
    systemProperty "delphi.env.run.retry.maxCount", 3
    systemProperty "delphi.env.run", delphiEnv
    useTestNG() {
        suites suite_streams
        parallel 'methods'
        threadCount 4
        includeGroups testNgGroups
    }
}

task video_JavaApi(type: Test) {
    systemProperty backEndProps, backEndPropsLocal
    systemProperty allure, allure
    systemProperty "delphi.env.run.type.isLogAll", false
    systemProperty "delphi.env.run.type.isLocal", false
    systemProperty "delphi.env.run.type.isLogPretty", false
    systemProperty "delphi.env.run.type.isDeepAssert", false
    systemProperty "delphi.slz.db.pg.isAutomation", false
    systemProperty "delphi.api.date.start.shift", 90
    systemProperty "delphi.api.date.end.shift", 30
    systemProperty "delphi.env.run.retry.maxCount", 3
    systemProperty "delphi.env.run", delphiEnv
    useTestNG() {
        suites suite_video
        parallel 'methods'
        threadCount 4
        includeGroups testNgGroups
    }
}