/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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
 *
 *    https://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 'groovy'
}

version = projectVersion
group = 'org.apache.grails.testing'

dependencies {

    implementation platform(project(':grails-bom'))

    testImplementation project(':grails-testing-support-datamapping'), {
        // This is a local project dependency
        exclude group: 'org.apache.grails', module: 'grails-testing-support-core'
    }
    testImplementation project(':grails-test-suite-base')
    testImplementation project(':grails-testing-support-core')
    testImplementation project(':grails-interceptors')
    testImplementation project(':grails-rest-transforms')
    testImplementation 'org.apache.groovy:groovy'
    testImplementation 'com.fasterxml.jackson.core:jackson-databind'
    testImplementation project(':grails-testing-support-web'), {
        // This is a local project dependency
        exclude group: 'org.apache.grails', module: 'grails-testing-support-core'
        exclude group: 'org.apache.grails', module: 'grails-rest-transforms'
    }
    testImplementation 'org.spockframework:spock-core'
}

def defaultTestConfig = {
    maxParallelForks = configuredTestParallel
    forkEvery = isCiBuild ? 10 : 100
    excludes = ['**/*TestCase.class', '**/*$*.class']
}

def isolatedTests = [
        '**/ContentFormatControllerTests.class',
        '**/JSONBindingTests.class',
        '**/AutoParams*MarshallingTests.class',
        '**/JSONBindingToNullTests.class',
        '**/ControllerWithXmlConvertersTests.class',
        '**/GroovyPageAttributesTests.class',
        '**/BindingExcludeTests.class',
        '**/NestedXmlBindingTests.class',
        '**/GSPResponseWriterSpec.class',
        '**/RespondMethodSpec.class',
        '**/ContentNegotiationSpec.class',
        '**/pages/ext/jsp/*.class',
        '**/WithFormatContentTypeSpec.class',
        '**/CommandObjectNoDataSpec.class'
]

tasks.register('execIsolatedTests', Test).configure { Test it ->
    it.configure(defaultTestConfig)
    it.forkEvery = 1
    it.includes.addAll(isolatedTests)
}

tasks.register('createCombinedReport', TestReport).configure { TestReport rTask ->
    rTask.group = 'verification'
    rTask.destinationDirectory = layout.buildDirectory.dir('reports/allTests')
    rTask.testResults.from(
            tasks.named('execIsolatedTests', Test).map({ it.binaryResultsDirectory }),
            tasks.named('test', Test).map({ it.binaryResultsDirectory })
    )
}

tasks.named('test', Test).configure { Test it ->
    it.configure(defaultTestConfig)
    it.excludes.addAll(isolatedTests)
}

tasks.withType(Groovydoc).configureEach {
    // tests do not have groovydoc
    enabled = false
}

apply {
    // java-configuration must be applied first since tasks are now lazy registered
    from rootProject.layout.projectDirectory.file('gradle/java-config.gradle')
    from rootProject.layout.projectDirectory.file('gradle/test-config.gradle')
}
