Michael Hoennig
2022-10-25 d7eed084202ed16b1c8a2514dae0ca94018a1c74
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
plugins {
    id 'java'
    id 'org.springframework.boot' version '2.7.5'
    id 'io.openapiprocessor.openapi-processor' version '2022.2'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'com.github.jk1.dependency-license-report' version '2.1'
    id "org.owasp.dependencycheck" version "7.3.0"
    id "com.diffplug.spotless" version "6.11.0"
    id 'jacoco'
    id 'info.solidsoft.pitest' version '1.9.0'
    id 'se.patrikerdes.use-latest-versions' version '0.2.18'
    id 'com.github.ben-manes.versions' version '0.43.0'
}
 
group = 'net.hostsharing'
version = '0.0.1-SNAPSHOT'
 
wrapper {
    distributionType = Wrapper.DistributionType.BIN
    gradleVersion = '7.5'
}
 
configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    testCompile {
        extendsFrom testAnnotationProcessor
 
        // Only JUNit 5 (Jupiter) should be used at compile time.
        // For runtime it's still needed by testcontainers, though.
        exclude group: 'junit', module: 'junit'
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
 
repositories {
    mavenCentral()
}
 
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}
 
ext {
    set('testcontainersVersion', "1.17.3")
}
 
// wrapper
 
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'com.github.gavlyukovskiy:datasource-proxy-spring-boot-starter:1.8.1'
    implementation 'org.springdoc:springdoc-openapi-ui:1.6.12'
    implementation 'org.liquibase:liquibase-core'
    implementation 'com.vladmihalcea:hibernate-types-55:2.20.0'
    implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.4'
    implementation 'org.openapitools:jackson-databind-nullable:0.2.3'
    implementation 'org.modelmapper:modelmapper:3.1.0'
    implementation 'org.iban4j:iban4j:3.2.3-RELEASE'
 
    compileOnly 'org.projectlombok:lombok'
    testCompileOnly 'org.projectlombok:lombok'
 
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
 
    runtimeOnly 'org.postgresql:postgresql'
 
    annotationProcessor 'org.projectlombok:lombok'
    testAnnotationProcessor 'org.projectlombok:lombok'
 
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.testcontainers:testcontainers'
    testImplementation 'org.testcontainers:junit-jupiter'
    testImplementation 'org.testcontainers:postgresql'
    testImplementation 'com.tngtech.archunit:archunit-junit5:1.0.0'
    testImplementation 'io.rest-assured:spring-mock-mvc'
    testImplementation 'org.hamcrest:hamcrest-core:2.2'
    testImplementation 'org.pitest:pitest-junit5-plugin:1.1.0'
}
 
dependencyManagement {
    imports {
        mavenBom "org.testcontainers:testcontainers-bom:${testcontainersVersion}"
    }
}
 
// Java Compiler Options
tasks.withType(JavaCompile) {
    options.compilerArgs += [
            "-parameters" // keep parameter names => no need for @Param for SpringData
    ]
}
 
// Configure tests
tasks.named('test') {
    useJUnitPlatform()
    jvmArgs '-Duser.language=en'
    jvmArgs '-Duser.country=US'
}
 
// OpenAPI Source Code Generation
openapiProcessor {
    springRoot {
        processorName 'spring'
        processor 'io.openapiprocessor:openapi-processor-spring:2022.5'
        apiPath "$projectDir/src/main/resources/api-definition.yaml"
        mapping "$projectDir/src/main/resources/api-mappings.yaml"
        targetDir "$projectDir/build/generated/sources/openapi"
        showWarnings true
        openApiNullable true
    }
    springRbac {
        processorName 'spring'
        processor 'io.openapiprocessor:openapi-processor-spring:2022.5'
        apiPath "$projectDir/src/main/resources/api-definition/rbac/rbac.yaml"
        mapping "$projectDir/src/main/resources/api-definition/rbac/api-mappings.yaml"
        targetDir "$projectDir/build/generated/sources/openapi"
        showWarnings true
        openApiNullable true
    }
    springTest {
        processorName 'spring'
        processor 'io.openapiprocessor:openapi-processor-spring:2022.5'
        apiPath "$projectDir/src/main/resources/api-definition/test/test.yaml"
        mapping "$projectDir/src/main/resources/api-definition/test/api-mappings.yaml"
        targetDir "$projectDir/build/generated/sources/openapi"
        showWarnings true
        openApiNullable true
    }
    springHs {
        processorName 'spring'
        processor 'io.openapiprocessor:openapi-processor-spring:2022.5'
        apiPath "$projectDir/src/main/resources/api-definition/hs-office/hs-office.yaml"
        mapping "$projectDir/src/main/resources/api-definition/hs-office/api-mappings.yaml"
        targetDir "$projectDir/build/generated/sources/openapi"
        showWarnings true
        openApiNullable true
    }
}
sourceSets.main.java.srcDir 'build/generated/sources/openapi'
abstract class ProcessSpring extends DefaultTask {}
tasks.register('processSpring', ProcessSpring)
['processSpringRoot', 'processSpringRbac', 'processSpringTest', 'processSpringHs'].each {
    project.tasks.processSpring.dependsOn it
}
project.tasks.processResources.dependsOn processSpring
project.tasks.compileJava.dependsOn processSpring
 
// Spotless Code Formatting
spotless {
    java {
        // removeUnusedImports() TODO: reactivate once it can deal with multi-line-strings
        indentWithSpaces(4)
        endWithNewline()
        toggleOffOn()
 
        target fileTree(rootDir) {
            include '**/*.java'
            exclude '**/generated/**/*.java'
        }
    }
}
project.tasks.check.dependsOn(spotlessCheck)
 
// OWASP Dependency Security Test
dependencyCheck {
    cveValidForHours=4
    format = 'ALL'
    suppressionFile = 'etc/owasp-dependency-check-suppression.xml'
    failOnError = true
    failBuildOnCVSS = 7
}
project.tasks.check.dependsOn(dependencyCheckAnalyze)
project.tasks.dependencyCheckAnalyze.doFirst { // Why not doLast? See README.md!
    println "OWASP Dependency Security Report: file:///${project.rootDir}/build/reports/dependency-check-report.html"
}
 
 
// License Check
licenseReport {
    excludeBoms = true
    allowedLicensesFile = new File("$projectDir/etc/allowed-licenses.json")
}
project.tasks.check.dependsOn(checkLicense)
 
// JaCoCo Test Code Coverage
jacoco {
    toolVersion = "0.8.8"
}
test {
    finalizedBy jacocoTestReport // generate report after tests
    excludes = [
            'net.hostsharing.hsadminng.**.generated.**',
    ]
}
jacocoTestReport {
    dependsOn test
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: [
                    "net/hostsharing/hsadminng/**/generated/**/*.class",
                    "net/hostsharing/hsadminng/hs/HsadminNgApplication.class"
            ])
        }))
    }
    doFirst { // Why not doLast? See README.md!
        println "HTML Jacoco Test Code Coverage Report: file://${reports.html.outputLocation.get()}/index.html"
    }
}
project.tasks.check.dependsOn(jacocoTestCoverageVerification)
jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                minimum = 0.92
            }
        }
 
        // element: PACKAGE, BUNDLE, CLASS, SOURCEFILE or METHOD
        // counter:  INSTRUCTION, BRANCH, LINE, COMPLEXITY, METHOD, or CLASS
        // value: TOTALCOUNT, COVEREDCOUNT, MISSEDCOUNT, COVEREDRATIO or MISSEDRATIO
 
        rule {
            element = 'CLASS'
            excludes = [
                    'net.hostsharing.hsadminng.**.generated.**',
                    'net.hostsharing.hsadminng.HsadminNgApplication',
                    'net.hostsharing.hsadminng.ping.PingController',
                    'net.hostsharing.hsadminng.mapper.Mapper'
            ]
 
            limit {
                counter = 'LINE'
                value = 'COVEREDRATIO'
                minimum = 0.98
            }
        }
        rule {
            element = 'METHOD'
            excludes = [
                    'net.hostsharing.hsadminng.**.generated.**',
                    'net.hostsharing.hsadminng.HsadminNgApplication.main',
                    'net.hostsharing.hsadminng.ping.PingController.*'
            ]
 
            limit {
                counter = 'BRANCH'
                value = 'COVEREDRATIO'
                minimum = 1.00
            }
        }
    }
}
 
// pitest mutation testing
pitest {
    targetClasses = ['net.hostsharing.hsadminng.**']
    excludedClasses = [
            'net.hostsharing.hsadminng.config.**',
            'net.hostsharing.hsadminng.**.*Controller',
            'net.hostsharing.hsadminng.**.generated.**'
    ]
 
    targetTests = ['net.hostsharing.hsadminng.**.*UnitTest', 'net.hostsharing.hsadminng.**.*RestTest']
    excludedTestClasses = ['**AcceptanceTest*', '**IntegrationTest*']
 
    pitestVersion = '1.9.0'
    junit5PluginVersion = '1.0.0'
 
    threads = 4
 
    // As Java unit tests are pretty pointless in our case, this maybe makes not much sense.
    mutationThreshold = 71
    coverageThreshold = 57
    testStrengthThreshold = 87
 
    outputFormats = ['XML', 'HTML']
    timestampedReports = false
}
project.tasks.check.dependsOn(project.tasks.pitest)
project.tasks.pitest.doFirst { // Why not doLast? See README.md!
    println "PiTest Mutation Report: file:///${project.rootDir}/build/reports/pitest/index.html"
}
 
 
// Dependency Versions Upgrade
useLatestVersions {
    finalizedBy check
}
 
def isNonStable = { String version ->
    def stableKeyword = ['RELEASE', 'FINAL', 'GA'].any { it -> version.toUpperCase().contains(it) }
    def regex = /^[0-9,.v-]+(-r)?$/
    return !stableKeyword && !(version ==~ regex)
}
 
tasks.named("dependencyUpdates").configure {
    rejectVersionIf {
        isNonStable(it.candidate.version)
    }
}