Skip to content

Commit 138d434

Browse files
committed
add new test api task
1 parent 54748fd commit 138d434

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ plugins {
1212
id("openapiprocessor.testInt")
1313
id("openapiprocessor.publish")
1414
id("jacoco-report-aggregation")
15+
id("openapiprocessor.newapi")
1516
}
1617

1718
group = "io.openapiprocessor"

buildSrc/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ dependencies {
1010
// implementation(libs.plugin.checker)
1111
// implementation(libs.plugin.updates)
1212
implementation(libs.plugin.build)
13+
14+
implementation(platform(libs.jackson.bom))
15+
implementation(libs.jackson.kotlin)
16+
implementation(libs.jackson.yaml)
1317
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import com.fasterxml.jackson.core.type.TypeReference
2+
import com.fasterxml.jackson.databind.ObjectMapper
3+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
4+
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
5+
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
6+
7+
abstract class NewTestApiTask : DefaultTask() {
8+
9+
@get:InputDirectory
10+
abstract val srcDir: DirectoryProperty
11+
12+
@get:Input
13+
abstract val srcVersion: Property<String>
14+
15+
@get:Input
16+
abstract val dstVersion: Property<String>
17+
18+
@get:Input
19+
abstract val apiVersion: Property<String>
20+
21+
@TaskAction
22+
fun run() {
23+
val yamlFactory = YAMLFactory.builder()
24+
.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
25+
.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
26+
.enable(YAMLGenerator.Feature.INDENT_ARRAYS_WITH_INDICATOR)
27+
.build()
28+
29+
val mapper = ObjectMapper(yamlFactory).registerKotlinModule()
30+
31+
val tests = srcDir.get().asFile
32+
val testDirs = tests.listFiles()
33+
?.sorted()
34+
?.toList()
35+
?.filter { it.isDirectory }
36+
?: emptyList()
37+
38+
for (testDir in testDirs) {
39+
println("checking 'tests/${testDir.name}' ...")
40+
val srcFile = File(testDir, "inputs/openapi${srcVersion.get()}.yaml")
41+
val dstFile = File(testDir, "inputs/openapi${dstVersion.get()}.yaml")
42+
43+
if(!srcFile.exists()) {
44+
println("no '${srcFile.name}' ...")
45+
continue
46+
}
47+
48+
if (!dstFile.exists()) {
49+
println("creating '${dstFile.name}' ...")
50+
srcFile.copyTo(dstFile)
51+
}
52+
53+
val apiYaml = mapper.readValue(dstFile, object : TypeReference<MutableMap<String, Any?>>() {})
54+
if (apiYaml["openapi"] != apiVersion.get()) {
55+
println("updating '${dstFile.name}' ...")
56+
apiYaml["openapi"] = apiVersion.get()
57+
mapper.writeValue(dstFile, apiYaml)
58+
}
59+
60+
val inputsFile = File(testDir, "inputs.yaml")
61+
val inputsYaml = mapper.readValue(inputsFile, object : TypeReference<MutableMap<String, Any?>>() {})
62+
@Suppress("UNCHECKED_CAST") val items = inputsYaml["items"] as MutableList<String>
63+
if (!items.contains("inputs/openapi${dstVersion.get()}.yaml")) {
64+
println("updating inputs.yaml ...")
65+
items.indexOf("inputs/openapi${srcVersion.get()}.yaml").let { index ->
66+
items.add(index + 1, "inputs/openapi${dstVersion.get()}.yaml")
67+
}
68+
mapper.writeValue(inputsFile, inputsYaml)
69+
}
70+
}
71+
}
72+
}
73+
74+
tasks.register<NewTestApiTask>("newTestApi") {
75+
group = "utility"
76+
description = "setup new openapiXX.yaml test files."
77+
78+
srcDir = layout.projectDirectory.dir("src/testInt/resources/tests")
79+
srcVersion = "31"
80+
dstVersion = "32"
81+
apiVersion = "3.2.0"
82+
}

gradle/libs.versions.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ openapi-processor-parser-api = { module = "io.openapiprocessor:openapi-processor
2424
openapi-processor-parser-swagger = { module = "io.openapiprocessor:openapi-processor-core-parser-swagger", version.ref = "base" }
2525
openapi-processor-parser-openapi4j = { module ="io.openapiprocessor:openapi-processor-core-parser-openapi4j", version.ref = "base" }
2626

27+
jackson-bom = "com.fasterxml.jackson:jackson-bom:2.20.0"
28+
jackson-yaml = { module = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml" }
29+
jackson-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin" }
30+
2731
spring-web = { module ="org.springframework:spring-web", version.ref = "spring-web" }
2832
spring-data = { module ="org.springframework.data:spring-data-commons", version.ref = "spring-data" }
2933

0 commit comments

Comments
 (0)