import org.gradle.internal.os.OperatingSystem

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "http://repo.spring.io/plugins-release" }
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${spring_boot_version}"
        classpath "io.spring.gradle:propdeps-plugin:0.0.10.RELEASE"
        classpath "org.openapitools:openapi-generator-gradle-plugin:3.3.0"
        classpath "gradle.plugin.com.gorylenko.gradle-git-properties:gradle-git-properties:1.5.2"
        //jhipster-needle-gradle-buildscript-dependency - JHipster will add additional gradle build script plugins here
    }
    dependencies {
        classpath 'org.owasp:dependency-check-gradle:4.0.2'
    }
}

plugins {
    id "org.sonarqube" version "2.6.2"
    id "net.ltgt.apt-eclipse" version "0.19"
    id "net.ltgt.apt-idea" version "0.19"
    id "net.ltgt.apt" version "0.19"
    id "io.spring.dependency-management" version "1.0.6.RELEASE"
    id "com.moowork.node" version "1.2.0"
    id 'org.liquibase.gradle' version '2.0.1'
    id 'info.solidsoft.pitest' version '1.4.0'
    //jhipster-needle-gradle-plugins - JHipster will add additional gradle plugins here
}

apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'org.owasp.dependencycheck'
sourceCompatibility=1.8
targetCompatibility=1.8
// Until JHipster supports JDK 9
assert System.properties['java.specification.version'] == '1.8'

apply plugin: 'maven'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
apply plugin: 'propdeps'
apply plugin: 'com.moowork.node'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'idea'

idea {
  module {
    excludeDirs += files('node_modules')
  }
}

dependencyManagement {
  imports {
    mavenBom 'io.github.jhipster:jhipster-dependencies:' + jhipster_dependencies_version
    //jhipster-needle-gradle-dependency-management - JHipster will add additional dependencies management here
  }
}

defaultTasks 'bootRun'

group = 'org.hostsharing.hsadminng'
version = '0.0.1-SNAPSHOT'

description = ''

bootWar {
    mainClassName = 'org.hostsharing.hsadminng.HsadminNgApp'
}

war {
    webAppDirName = 'build/www/'
    enabled = true
    extension = 'war.original'
}

springBoot {
    mainClassName = 'org.hostsharing.hsadminng.HsadminNgApp'
}

if (OperatingSystem.current().isWindows()) {
    // https://stackoverflow.com/questions/40037487/the-filename-or-extension-is-too-long-error-using-gradle
    task classpathJar(type: Jar) {
        dependsOn configurations.runtime
        appendix = 'classpath'

        doFirst {
            manifest {
                attributes 'Class-Path': configurations.runtime.files.collect {
                    it.toURI().toURL().toString().replaceFirst(/file:\/+/, '/').replaceAll(' ', '%20')
                }.join(' ')
            }
        }
    }

    bootRun {
        dependsOn classpathJar
        doFirst {
            classpath = files("$buildDir/classes/java/main", "$buildDir/resources/main", classpathJar.archivePath)
        }
    }
}

test {
    exclude '**/CucumberTest*'

    // uncomment if the tests reports are not generated
    // see https://github.com/jhipster/generator-jhipster/pull/2771 and https://github.com/jhipster/generator-jhipster/pull/4484
    // ignoreFailures true
    reports.html.enabled = false
}

// --- JaCoCo Code Coverage ---

jacoco {
    toolVersion = "0.8.3"
}

test.finalizedBy jacocoTestReport
check.dependsOn jacocoTestCoverageVerification

// Only for purely JHipster/MapStruct generated classes.
// Please do NOT add any self coded classes!
// Keep in mind, git will blame you ;-)
def jhipsterGeneratedClassesWithDecentCoverage = [
    'org.hostsharing.hsadminng.repository.CustomAuditEventRepository',
    'org.hostsharing.hsadminng.service.ContactQueryService',
    'org.hostsharing.hsadminng.service.UserService',
    'org.hostsharing.hsadminng.service.CustomerContactQueryService'
]

// Only for purely JHipster/MapStruct generated classes.
// Please do NOT add any self coded classes!
// Keep in mind, git will blame you ;-)
def jhipsterGeneratedClassesWithLowCoverage = [
    'org.hostsharing.hsadminng.service.MailService',
    'org.hostsharing.hsadminng.security.SecurityUtils',
    'org.hostsharing.hsadminng.config.DefaultProfileUtil',
    'org.hostsharing.hsadminng.config.WebConfigurer',
    'org.hostsharing.hsadminng.web.rest.AccountResource',
    'org.hostsharing.hsadminng.web.rest.errors.ExceptionTranslator',
    'org.hostsharing.hsadminng.web.rest.errors.CustomParameterizedException',
    'org.hostsharing.hsadminng.config.audit.AuditEventConverter',
    'org.hostsharing.hsadminng.security.jwt.TokenProvider',
    'org.hostsharing.hsadminng.aop.logging.LoggingAspect',
    'org.hostsharing.hsadminng.HsadminNgApp',
    '*.*QueryService',
    '*.*Configuration',
    '*MapperImpl',
    '*Criteria',
    '*_'
]

jacocoTestCoverageVerification {
    violationRules {
        rule {
            element = 'CLASS'
            limit {
                counter = 'BRANCH'
                value = 'COVEREDRATIO'
                // Increasing the threshold is fine, decreasing is not.
                // Keep in mind, git will blame you ;-)
                minimum = 0.95
            }
            excludes = jhipsterGeneratedClassesWithDecentCoverage + jhipsterGeneratedClassesWithLowCoverage
        }

        rule {
            element = 'CLASS'
            limit {
                counter = 'BRANCH'
                value = 'COVEREDRATIO'
                minimum = 0.80
            }
            includes = jhipsterGeneratedClassesWithDecentCoverage
        }
    }
}

// --- Cucumber BDD Tests ---

task cucumberTest(type: Test) {
    description = "Execute cucumber BDD tests."
    group = "verification"
    include '**/CucumberTest*'

    // uncomment if the tests reports are not generated
    // see https://github.com/jhipster/generator-jhipster/pull/2771 and https://github.com/jhipster/generator-jhipster/pull/4484
    // ignoreFailures true
    reports.html.enabled = false
}

check.dependsOn cucumberTest
task testReport(type: TestReport) {
    destinationDir = file("$buildDir/reports/tests")
    reportOn test
}

task cucumberTestReport(type: TestReport) {
    destinationDir = file("$buildDir/reports/tests")
    reportOn cucumberTest
}

// --- PiTest mutation testing ---

pitest {
    targetClasses = ['org.hostsharing.hsadminng.*']
    threads = 2

    // Do not set these limit even lower, they are already pretty bad values!
    // 83%*78% means that only ~66% of the code is properly covered by automated tests.
    mutationThreshold = 78
    coverageThreshold = 83

    outputFormats = ['XML', 'HTML']
    timestampedReports = false
    verbose = false
}

// ------------------------------

apply from: 'gradle/docker.gradle'
apply from: 'gradle/sonar.gradle'
apply from: 'gradle/swagger.gradle'
//jhipster-needle-gradle-apply-from - JHipster will add additional gradle scripts to be applied here

if (project.hasProperty('prod')) {
    apply from: 'gradle/profile_prod.gradle'
} else {
    apply from: 'gradle/profile_dev.gradle'
}


if (!project.hasProperty('runList')) {
    project.ext.runList = 'main'
}

project.ext.diffChangelogFile = 'src/main/resources/config/liquibase/changelog/' + new Date().format('yyyyMMddHHmmss') + '_changelog.xml'

liquibase {
    activities {
        main {
            driver ''
            url ''
            username 'hsadminNg'
            password ''
            changeLogFile 'src/main/resources/config/liquibase/master.xml'
            defaultSchemaName ''
            logLevel 'debug'
            classpath 'src/main/resources/'
        }
        diffLog {
            driver ''
            url ''
            username 'hsadminNg'
            password ''
            changeLogFile project.ext.diffChangelogFile
            referenceUrl 'hibernate:spring:org.hostsharing.hsadminng.domain?dialect=&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy'
            defaultSchemaName ''
            logLevel 'debug'
            classpath "$buildDir/classes/java/main"
        }
    }

    runList = project.ext.runList
}

configurations {
    providedRuntime
    compile.exclude module: "spring-boot-starter-tomcat"
}

repositories {
    mavenLocal()
    mavenCentral()
    jcenter()
    //jhipster-needle-gradle-repositories - JHipster will add additional repositories
}

dependencies {
    // Use ", version: jhipster_dependencies_version, changing: true" if you want
    // to use a SNAPSHOT release instead of a stable release
    compile group: "io.github.jhipster", name: "jhipster-framework"
    compile "org.springframework.boot:spring-boot-starter-cache"
    compile "io.dropwizard.metrics:metrics-core"
    compile 'io.micrometer:micrometer-registry-prometheus'
    compile "net.logstash.logback:logstash-logback-encoder"
    compile "com.fasterxml.jackson.datatype:jackson-datatype-hppc"
    compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310"
    compile "com.fasterxml.jackson.datatype:jackson-datatype-hibernate5"
    compile "com.fasterxml.jackson.core:jackson-annotations"
    compile "com.fasterxml.jackson.core:jackson-databind"
    compile "com.fasterxml.jackson.module:jackson-module-afterburner"
    compile "javax.cache:cache-api"
    compile "org.hibernate:hibernate-core"
    compile "com.zaxxer:HikariCP"
    compile "org.apache.commons:commons-lang3"
    compile "commons-io:commons-io"
    compile "javax.transaction:javax.transaction-api"
    compile "org.ehcache:ehcache"
    compile "org.hibernate:hibernate-entitymanager"
    compile "org.hibernate:hibernate-envers"
    compile "org.hibernate.validator:hibernate-validator"
    compile "org.liquibase:liquibase-core"
    compile "com.mattbertolini:liquibase-slf4j"
    liquibaseRuntime "org.liquibase:liquibase-core"
    liquibaseRuntime "org.liquibase.ext:liquibase-hibernate5:${liquibase_hibernate5_version}"
    liquibaseRuntime sourceSets.main.compileClasspath
    compile "org.springframework.boot:spring-boot-loader-tools"
    compile "org.springframework.boot:spring-boot-starter-mail"
    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-starter-actuator"
    compile "org.springframework.boot:spring-boot-starter-aop"
    compile "org.springframework.boot:spring-boot-starter-data-jpa"
    compile "org.springframework.boot:spring-boot-starter-security"
    compile ("org.springframework.boot:spring-boot-starter-web") {
        exclude module: 'spring-boot-starter-tomcat'
    }
    compile "org.springframework.boot:spring-boot-starter-undertow"
    compile "org.springframework.boot:spring-boot-starter-thymeleaf"
    compile "org.zalando:problem-spring-web:0.24.0-RC.0"
    compile "org.springframework.boot:spring-boot-starter-cloud-connectors"
    compile "org.springframework.security:spring-security-config"
    compile "org.springframework.security:spring-security-data"
    compile "org.springframework.security:spring-security-web"
    compile "io.jsonwebtoken:jjwt-api"
    runtime "io.jsonwebtoken:jjwt-impl"
    runtime "io.jsonwebtoken:jjwt-jackson"
    compile ("io.springfox:springfox-swagger2") {
        exclude module: 'mapstruct'
    }
    compile "io.springfox:springfox-bean-validators"
    compile "org.postgresql:postgresql"
    liquibaseRuntime "org.postgresql:postgresql"
    compile "org.mapstruct:mapstruct-jdk8:${mapstruct_version}"
    annotationProcessor "org.mapstruct:mapstruct-processor:${mapstruct_version}"
    annotationProcessor "org.hibernate:hibernate-jpamodelgen"
    annotationProcessor ("org.springframework.boot:spring-boot-configuration-processor") {
        exclude group: 'com.vaadin.external.google', module: 'android-json'
    }
    testCompile "com.jayway.jsonpath:json-path"
    testCompile "io.cucumber:cucumber-junit"
    testCompile "io.cucumber:cucumber-spring"
    testCompile ("org.springframework.boot:spring-boot-starter-test") {
        exclude group: 'com.vaadin.external.google', module: 'android-json'
    }
    testCompile "org.springframework.security:spring-security-test"
    testCompile "org.springframework.boot:spring-boot-test"
    testCompile "org.assertj:assertj-core"
    testCompile "junit:junit"
    testCompile "org.mockito:mockito-core"
    testCompile "com.mattbertolini:liquibase-slf4j"
    testCompile "org.hamcrest:hamcrest-library"
    testCompile "com.h2database:h2"
    liquibaseRuntime "com.h2database:h2"
    //jhipster-needle-gradle-dependency - JHipster will add additional dependencies here
}

task cleanResources(type: Delete) {
    delete 'build/resources'
}

wrapper {
    gradleVersion = '4.10.2'
}

task stage(dependsOn: 'bootWar') {
}

if (project.hasProperty('nodeInstall')) {
    node {
        version = "${node_version}"
        npmVersion = "${npm_version}"
        yarnVersion = "${yarn_version}"
        download = false
    }
}