/*
 * 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
 *
 *      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.
 */

sourceSets {
    jobs
    unit1
    unit2
}

def registerJarTask(SourceSet sourceSet, String baseName) {
    tasks.register(sourceSet.jarTaskName, Jar) {
        group = 'build'
        archiveBaseName = baseName
        archiveVersion = '1.0-SNAPSHOT'
        from sourceSet.output
    }
}

registerJarTask(sourceSets.jobs, 'ignite-integration-test-jobs')
registerJarTask(sourceSets.unit1, 'ignite-unit-test-job1')
registerJarTask(sourceSets.unit2, 'ignite-unit-test-job2')

processTestResources {
    into('units/unit1') {
        into('1.0.0') {
            from unit1Jar
        }
        into('2.0.0') {
            from unit2Jar
        }
        into('3.0.1') {
            from unit1Jar
            from unit2Jar
        }
        into('3.0.2') {
            into('subdir') {
                from unit2Jar
            }
            from unit1Jar
        }
    }
    into('units/unit2') {
        into('1.0.0') {
            from unit1Jar
        }
        into('2.0.0') {
            from unit2Jar
        }
    }

    File corruptedJarFile = file("$destinationDir/units/unit1/4.0.0/unit1-1.0-corrupted.jar")
    doLast {
        // Generate a 100 bytes long file with random contents to simulate corrupted jar
        byte[] randomBytes = new byte[100]
        new Random().nextBytes(randomBytes)
        corruptedJarFile.parentFile.mkdirs()
        corruptedJarFile.bytes = randomBytes
    }
}

processIntegrationTestResources {
    into('units') {
        from jobsJar
        from unit1Jar
        from unit2Jar
    }
}

dependencies {
    jobsImplementation project(':ignite-api')
    jobsImplementation project(':ignite-core')
    unit1Implementation project(':ignite-api')
    unit2Implementation project(':ignite-api')

    integrationTestImplementation sourceSets.jobs.output
}
