/*
 * Copyright (c) 2023 Alexander Zagniotov
 *
 * 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.
 */

configurations.all {
    resolutionStrategy {
        force "junit:junit:${junitVersion}"
        force "com.google.truth:truth:${googleTruthVersion}"

        // https://github.com/mockito/mockito/issues/2231
        force "org.mockito:mockito-inline:${mockitoVersion}"
        force "org.mockito:mockito-core:${mockitoVersion}"
        // Explicitly adding a recent Byte Buddy to Mockito for the compatibility with JDK 21 and Mockito 4.x.x
        force "net.bytebuddy:byte-buddy:${byteBuddyVersion}"

        // https://github.com/googleapis/google-http-java-client/issues/167#issuecomment-571987053
        force "com.google.http-client:google-http-client-apache-v2:${googleHttpClient}"

        force "org.eclipse.jetty.http2:http2-client:${jettyVersion}"
        force "org.eclipse.jetty.http2:http2-http-client-transport:${jettyVersion}"
    }
}

testing {
    suites {
        withType(JvmTestSuite).matching { it.name in ["test", "integrationTest", "functionalTest", "loadTest"] }.configureEach { 
            useJUnit()
            dependencies {
                implementation "junit:junit"
                implementation "org.mockito:mockito-core"
                implementation "org.mockito:mockito-inline"
                implementation "net.bytebuddy:byte-buddy"
                implementation "com.google.truth:truth"
                implementation "com.google.http-client:google-http-client-apache-v2"
                implementation "org.eclipse.jetty.http2:http2-client"
                implementation "org.eclipse.jetty.http2:http2-http-client-transport"
            }
        }

        integrationTest(JvmTestSuite) {
            sources {
                resources {
                    srcDirs += [
                            "src/main/resources"
                    ]
                }
            }
            useJUnit()
            dependencies {
                implementation project()
                if (project.hasProperty("useNativeJdkAlpnProcessor")) {
                    implementation "org.eclipse.jetty:jetty-alpn-java-client:${jettyVersion}"
                } else {
                    implementation "org.eclipse.jetty:jetty-alpn-openjdk8-client:${jettyVersion}"
                }
            }

            targets {
                all {
                    testTask.configure {
                        shouldRunAfter(test)
                    }
                }
            }
        }

        functionalTest(JvmTestSuite) {
            sources {
                resources {
                    srcDirs += [
                            "src/main/resources"
                    ]
                }
            }
            useJUnit()
            dependencies {
                implementation project()
                if (project.hasProperty("useNativeJdkAlpnProcessor")) {
                    implementation "org.eclipse.jetty:jetty-alpn-java-client:${jettyVersion}"
                } else {
                    implementation "org.eclipse.jetty:jetty-alpn-openjdk8-client:${jettyVersion}"
                }
            }

            targets {
                all {
                    testTask.configure {
                        systemProperty "overrideDisabledAlgorithms", "true"
                        shouldRunAfter(test)
                    }
                }
            }
        }

        loadTest(JvmTestSuite) {
            sources {
                resources {
                    srcDirs += [
                            "src/main/resources"
                    ]
                }
            }
            useJUnit()
            dependencies {
                implementation project()
                if (project.hasProperty("useNativeJdkAlpnProcessor")) {
                    implementation "org.eclipse.jetty:jetty-alpn-java-client:${jettyVersion}"
                } else {
                    implementation "org.eclipse.jetty:jetty-alpn-openjdk8-client:${jettyVersion}"
                }
            }

            targets {
                all {
                    testTask.configure {
                        shouldRunAfter(test)
                    }
                }
            }
        }
    }
}

tasks.withType(Test).configureEach {
    reports.html.required = false
    reports.junitXml.required = false
    ignoreFailures = false
    testLogging {
        // showStandardStreams true
        events /*"passed", */ "skipped", "failed"
        exceptionFormat "full"
        showExceptions true
        showCauses true
        showStackTraces true
    }

    filter {
        if (project.hasProperty("ciRun")) {
            excludeTestsMatching "*Flaky*"
        }
    }
}

