/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java library project to get you started.
 * For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at https://docs.gradle.org/6.8/userguide/building_java_projects.html
 */


plugins {
    // Apply the java-library plugin for API and implementation separation.
    id 'java'
    id 'java-library'
    id "nebula.lint" version "16.9.0"
    id 'checkstyle'
    id 'com.github.johnrengelman.shadow' version '7.1.2'
    id 'jacoco'
    id 'io.freefair.aspectj.post-compile-weaving' version '6.4.1'
}
// apply plugin: "eclipse" // if using Eclipse


checkstyle {
    // We use default checkstyle from Github checkstyle project.
    // https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml
    // Gradle has to use the same version of checkstyle or some rules might not be available yet
    // Make sure that you use the same tool version if you update the checkstyle.xml at
    // See https://github.com/checkstyle/checkstyle/releases
    toolVersion = '8.42'
    configFile = file('config/checkstyle/checkstyle.xml')
    // Allow some warnings before putting checkstyle in place. These warning will be gradually removed.
    maxWarnings = 26
}

jacoco {
    toolVersion = "0.8.7"
}

//gradleLint {
//    rules=['unused-dependency']
//}

repositories {
    // Use JCenter for resolving dependencies.
    mavenCentral()
}

dependencies {
    // Use JUnit Jupiter API for testing.
    testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
    testImplementation 'org.mockito:mockito-inline:3.7.7'
    testImplementation 'org.mockito:mockito-junit-jupiter:3.7.7'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
    testImplementation 'org.mockito:mockito-core:3.12.4'
    testImplementation 'com.google.code.gson:gson:2.8.9'
    testImplementation 'org.junit.jupiter:junit-jupiter-params:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'

    implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
    implementation 'com.amazonaws:aws-lambda-java-events:3.9.0'
    runtimeOnly 'mysql:mysql-connector-java:8.0.27'
    implementation 'org.json:json:20210307'

    implementation('software.amazon.awssdk:rds:2.16.38') {
        exclude module: 'netty-nio-client'
        exclude module: 'apache-client'
        exclude group: ['com.fasterxml.jackson.databind.deser', 'com.fasterxml.jackson.databind.ser', 'com.fasterxml.jackson.databind.introspect'], module: 'jackson-databind'
    }
    implementation('software.amazon.awssdk:secretsmanager:2.15.0') {
        exclude module: 'netty-nio-client'
        exclude module: 'apache-client'
        exclude group: ['com.fasterxml.jackson.databind.deser', 'com.fasterxml.jackson.databind.ser', 'com.fasterxml.jackson.databind.introspect'], module: 'jackson-databind'
    }
    implementation 'software.amazon.awssdk:url-connection-client:2.16.44'

    // Log4j2 logging framework dependencies
    implementation group: 'com.amazonaws', name: 'aws-lambda-java-log4j2', version: '1.3.0'
    implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.17.2'
    implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.17.2'
    implementation group: 'com.amazonaws', name: 'aws-lambda-java-runtime-interface-client', version: '2.1.0'
    // compileOnly means that this will not have an impact on cold start times, as it's not part of the final artifact
    compileOnly "org.projectlombok:lombok:1.18.20"
    annotationProcessor 'org.projectlombok:lombok:1.18.20'

    // Aspects for Powertools
    aspect 'software.amazon.lambda:powertools-tracing:1.12.0'
    aspect 'software.amazon.lambda:powertools-metrics:1.12.0'
    aspect 'software.amazon.lambda:powertools-logging:1.12.0'

    // Prevent using feature log4j below 2.17.0 which can contain CVE-2021-44228 and CVE-2021-45046
    // See step 3 in https://blog.gradle.org/log4j-vulnerability 
    constraints {
        implementation("org.apache.logging.log4j:log4j-core") {
            version {
                strictly("[2.17, 3[")
                prefer("2.17.2")
            }
            because("CVE-2021-44228: Log4j vulnerable to remote code execution")
            because("CVE-2021-45046: Log4j vulnerable to DoS attack")
        }
    }
}

test {
    // Use junit platform for unit tests.
    useJUnitPlatform()

    // Configures environment variable to avoid initialization of AWS X-Ray segments for each tests
    environment "LAMBDA_TASK_ROOT", "handler"

    // Use this flag to show output from running the test
    //testLogging.showStandardStreams = true

    // report is always generated after tests run
    finalizedBy jacocoTestReport
}

jacocoTestReport {
    dependsOn test // tests are required to run before generating the report
}

sourceSets {
    main.java.srcDirs = ['src']
    main.resources.srcDirs = ['scripts','resources']
    test.java.srcDirs = ['tst']
}

task buildZip(type: Zip) {
    from compileJava
    from processResources
    into('lib') {
        from configurations.runtimeClasspath
    }
}

import com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer

shadowJar {
    archiveBaseName.set('lambda-uber')
    transform(Log4j2PluginsCacheFileTransformer)
}


build.dependsOn buildZip
build.dependsOn shadowJar

sourceCompatibility = '11'
targetCompatibility = '11'
