Skip to content

Commit 11d40dd

Browse files
committed
load tests from resources
1 parent aac2fc3 commit 11d40dd

File tree

3 files changed

+120
-44
lines changed

3 files changed

+120
-44
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ kotest-bom = "io.kotest:kotest-bom:6.0.7"
3737
kotest-runner = { module = "io.kotest:kotest-runner-junit5" }
3838
kotest-table = { module = "io.kotest:kotest-assertions-table" }
3939
mockk = "io.mockk:mockk:1.14.7"
40+
classgraph = "io.github.classgraph:classgraph:4.8.165"
4041

4142
# for buildSrc convention plugins
4243
plugin-build = { module = "io.openapiprocessor:build-plugins", version.ref = "build-plugins" }

json-schema-validator/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies {
1616
testImplementation(libs.jackson.databind)
1717
testImplementation(libs.jackson.yaml)
1818
testImplementation(libs.jackson.kotlin)
19+
testImplementation(libs.classgraph)
1920
testImplementation(libs.logback)
2021

2122
constraints {

json-schema-validator/src/test/kotlin/io/openapiprocessor/jsonschema/validator/support/Draft.kt

Lines changed: 118 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.fasterxml.jackson.annotation.JsonInclude
99
import com.fasterxml.jackson.databind.ObjectMapper
1010
import com.fasterxml.jackson.databind.SerializationFeature
1111
import com.fasterxml.jackson.module.kotlin.KotlinModule
12+
import io.github.classgraph.ClassGraph
1213
import io.kotest.core.spec.style.freeSpec
1314
import io.kotest.matchers.shouldBe
1415
import io.openapiprocessor.jackson.JacksonConverter
@@ -17,10 +18,10 @@ import io.openapiprocessor.jsonschema.schema.*
1718
import io.openapiprocessor.jsonschema.support.Uris.createUri
1819
import io.openapiprocessor.jsonschema.validator.Validator
1920
import io.openapiprocessor.jsonschema.validator.ValidatorSettings
21+
import java.io.File
2022
import java.nio.file.Files
2123
import java.nio.file.Path
2224
import java.nio.file.Paths
23-
import kotlin.io.path.name
2425

2526

2627
/**
@@ -36,6 +37,7 @@ fun draftSpec(
3637
val json = ObjectMapper()
3738
json.registerModule(KotlinModule.Builder().build())
3839

40+
// val draftStream = Validator::class.java.getResourceAsStream(draftPath)
3941
val draft = Validator::class.java.getResource(draftPath)
4042
val root = Paths.get(draft!!.toURI())
4143

@@ -58,6 +60,16 @@ fun draftSpec(
5860
)
5961
}
6062

63+
fun loadSuites(content: ByteArray): Collection<Suite> {
64+
return json.readValue(
65+
content,
66+
json.typeFactory.constructCollectionType(
67+
List::class.java,
68+
Suite::class.java
69+
)
70+
)
71+
}
72+
6173
fun createStore(loader: DocumentLoader): SchemaStore {
6274
val store = SchemaStore(loader)
6375

@@ -96,57 +108,119 @@ fun draftSpec(
96108
return JsonInstance(JsonPointer.empty(), instance)
97109
}
98110

99-
fun createTestName(testPath: Path): String {
100-
val path = testPath.toAbsolutePath().toString()
101-
val draftPathIndex = path.indexOf(draftPath)
102-
return path.substring(draftPathIndex + draftPath.length + 1)
111+
// fun createTestName(testPath: Path): String {
112+
// val path = testPath.toAbsolutePath().toString()
113+
// val draftPathIndex = path.indexOf(draftPath)
114+
// return path.substring(draftPathIndex + draftPath.length + 1)
115+
// }
116+
117+
fun getTestName(testPath: String): String {
118+
val nameIdx = testPath.indexOfLast { it == File.separatorChar }
119+
val name = testPath.substring(nameIdx + 1)
120+
return name
103121
}
104122

105-
Files.walk(root)
106-
.filter { path -> !Files.isDirectory(path) }
107-
.filter { path -> !excludes.contains(path.name) }
108-
.forEach { path ->
109-
createTestName(path) - {
110-
val suites = loadSuites(path)
111-
112-
for (suite in suites) {
113-
suite.description - {
114-
val schema = createSchema(suite.schema)
115-
116-
val tests = suite.tests
117-
.filter { t -> !excludes.contains(t.description) }
118-
119-
for (test in tests) {
120-
test.description {
121-
var currentSettings = ValidatorSettings(settings)
122-
val modifier = settingsModifier[test.description]
123-
if(modifier != null) {
124-
currentSettings = modifier(currentSettings)
123+
val scan = ClassGraph()
124+
.acceptPaths(draftPath)
125+
.scan()
126+
127+
scan.use { scanResult ->
128+
scanResult.allResources
129+
.filter { res -> !excludes.contains(getTestName(res.path)) }
130+
.forEachByteArrayThrowingIOException { resource, content ->
131+
getTestName(resource.path) - {
132+
val suites = loadSuites(content)
133+
134+
for (suite in suites) {
135+
suite.description - {
136+
val schema = createSchema(suite.schema)
137+
138+
val tests = suite.tests
139+
.filter { t -> !excludes.contains(t.description) }
140+
141+
for (test in tests) {
142+
test.description {
143+
var currentSettings = ValidatorSettings(settings)
144+
val modifier = settingsModifier[test.description]
145+
if(modifier != null) {
146+
currentSettings = modifier(currentSettings)
147+
}
148+
149+
val instance = createInstance(test.data)
150+
151+
// act
152+
val validator = Validator(currentSettings)
153+
val step = validator.validate(schema, instance)
154+
155+
// output
156+
val converter = OutputConverter(Output.VERBOSE)
157+
val output = converter.convert(step)
158+
159+
val mapper = ObjectMapper()
160+
mapper.enable(SerializationFeature.INDENT_OUTPUT)
161+
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL)
162+
println(mapper.writeValueAsString(output))
163+
164+
// check valid
165+
step.isValid.shouldBe(test.valid)
166+
}
125167
}
126-
127-
val instance = createInstance(test.data)
128-
129-
// act
130-
val validator = Validator(currentSettings)
131-
val step = validator.validate(schema, instance)
132-
133-
// output
134-
val converter = OutputConverter(Output.VERBOSE)
135-
val output = converter.convert(step)
136-
137-
val mapper = ObjectMapper()
138-
mapper.enable(SerializationFeature.INDENT_OUTPUT)
139-
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL)
140-
println(mapper.writeValueAsString(output))
141-
142-
// check valid
143-
step.isValid.shouldBe(test.valid)
144168
}
145169
}
170+
146171
}
147172
}
148-
}
149173
}
174+
175+
176+
scan.close()
177+
178+
179+
// Files.walk(root)
180+
// .filter { path -> !Files.isDirectory(path) }
181+
// .filter { path -> !excludes.contains(path.name) }
182+
// .forEach { path ->
183+
// createTestName(path) - {
184+
// val suites = loadSuites(path)
185+
//
186+
// for (suite in suites) {
187+
// suite.description - {
188+
// val schema = createSchema(suite.schema)
189+
//
190+
// val tests = suite.tests
191+
// .filter { t -> !excludes.contains(t.description) }
192+
//
193+
// for (test in tests) {
194+
// test.description {
195+
// var currentSettings = ValidatorSettings(settings)
196+
// val modifier = settingsModifier[test.description]
197+
// if(modifier != null) {
198+
// currentSettings = modifier(currentSettings)
199+
// }
200+
//
201+
// val instance = createInstance(test.data)
202+
//
203+
// // act
204+
// val validator = Validator(currentSettings)
205+
// val step = validator.validate(schema, instance)
206+
//
207+
// // output
208+
// val converter = OutputConverter(Output.VERBOSE)
209+
// val output = converter.convert(step)
210+
//
211+
// val mapper = ObjectMapper()
212+
// mapper.enable(SerializationFeature.INDENT_OUTPUT)
213+
// mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL)
214+
// println(mapper.writeValueAsString(output))
215+
//
216+
// // check valid
217+
// step.isValid.shouldBe(test.valid)
218+
// }
219+
// }
220+
// }
221+
// }
222+
// }
223+
// }
150224
}
151225

152226
class Exclude(val test: String)

0 commit comments

Comments
 (0)