Skip to content

Commit 9675961

Browse files
authored
Merge pull request #22 from BGMSound/develop
[Refactor] Refactor document result validation structure
2 parents 0ba67f0 + 2302476 commit 9675961

File tree

23 files changed

+71
-65
lines changed

23 files changed

+71
-65
lines changed

documentify-project/documentify-core/src/main/kotlin/io/github/bgmsound/documentify/core/emitter/AbstractDocumentResult.kt

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,58 @@ package io.github.bgmsound.documentify.core.emitter
22

33
import io.github.bgmsound.documentify.core.specification.element.field.Field
44
import io.github.bgmsound.documentify.core.specification.schema.response.ResponseSpec
5+
import org.hamcrest.Matcher
6+
import org.hamcrest.Matchers
57

68
abstract class AbstractDocumentResult : DocumentResult {
7-
abstract fun validateJsonPath(jsonResultMatcher: JsonResultMatcher)
9+
abstract fun expect(jsonPath: String, matcher: Matcher<*>)
10+
11+
abstract fun expectValue(jsonPath: String, value: Any)
812

913
override fun validateWith(responseSpec: ResponseSpec) {
1014
val matchers = aggregateMatchers(responseSpec.fields)
1115
if (matchers.isEmpty()) {
1216
return
1317
}
1418
matchers.forEach { matcher ->
15-
validateJsonPath(matcher)
19+
val jsonPath = matcher.jsonPath
20+
val value = matcher.expectedValue
21+
if (jsonPath.endsWith("[*]")) {
22+
if (value !is List<*>) {
23+
throw IllegalArgumentException("sample value type must be List")
24+
}
25+
expect(jsonPath.substringBeforeLast("[*]"), Matchers.containsInAnyOrder(*value.toTypedArray()))
26+
} else if (jsonPath.contains("[*]") && !jsonPath.endsWith("[*]")) {
27+
expect(jsonPath, Matchers.hasItem(value))
28+
} else {
29+
expectValue(jsonPath, value)
30+
}
1631
}
1732
}
1833

19-
private fun aggregateMatchers(fields: List<Field>): List<JsonResultMatcher> {
34+
private fun aggregateMatchers(fields: List<Field>): List<ExpectedJsonValue> {
2035
return fields.filter {
2136
it.hasSample() || it.canHaveChild() || !it.isIgnored()
2237
}.flatMap {
2338
it.aggregateMatchers()
2439
}
2540
}
2641

27-
private fun Field.aggregateMatchers(): List<JsonResultMatcher> {
42+
private fun Field.aggregateMatchers(): List<ExpectedJsonValue> {
2843
if (isIgnored()) {
2944
return emptyList()
3045
}
3146
if (!hasSample() && !canHaveChild()) {
3247
return emptyList()
3348
}
34-
val matchers = mutableListOf<JsonResultMatcher>()
49+
val matchers = mutableListOf<ExpectedJsonValue>()
3550
if (hasSample()) {
3651
val jsonPath = StringBuilder("$.${path}").apply {
3752
if (isArray()) {
3853
append("[*]")
3954
}
4055
}.toString().replace("[]", "[*]")
41-
matchers.add(JsonResultMatcher.of(jsonPath, sample))
56+
matchers.add(ExpectedJsonValue.of(jsonPath, sample))
4257
} else if (childFields().isNotEmpty()) {
4358
val childMatchers = aggregateMatchers(childFields())
4459
matchers.addAll(childMatchers)

documentify-project/documentify-core/src/main/kotlin/io/github/bgmsound/documentify/core/emitter/JsonResultMatcher.kt renamed to documentify-project/documentify-core/src/main/kotlin/io/github/bgmsound/documentify/core/emitter/ExpectedJsonValue.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package io.github.bgmsound.documentify.core.emitter
22

3-
data class JsonResultMatcher(
3+
data class ExpectedJsonValue(
44
val jsonPath: String,
55
val expectedValue: Any
66
) {
77
companion object {
8-
fun of(path: String, expectedValue: Any) = JsonResultMatcher(path, expectedValue)
8+
fun of(path: String, expectedValue: Any) = ExpectedJsonValue(path, expectedValue)
99
}
1010
}

documentify-project/documentify-core/src/main/kotlin/io/github/bgmsound/documentify/core/specification/ResourceSpec.kt renamed to documentify-project/documentify-core/src/main/kotlin/io/github/bgmsound/documentify/core/specification/schema/ResourceSpec.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package io.github.bgmsound.documentify.core.specification
1+
package io.github.bgmsound.documentify.core.specification.schema
22

33
import com.epages.restdocs.apispec.ResourceDocumentation
44
import com.epages.restdocs.apispec.ResourceSnippetParameters
55
import com.epages.restdocs.apispec.Schema
6+
import io.github.bgmsound.documentify.core.specification.DocumentableSpec
67
import io.github.bgmsound.documentify.core.specification.element.Link
78
import io.github.bgmsound.documentify.core.specification.element.field.Field
89
import io.github.bgmsound.documentify.core.specification.schema.request.RequestSpec
@@ -28,17 +29,17 @@ class ResourceSpec(
2829
}
2930

3031
fun link(rel: String): Link {
31-
val link = Link.newLink(rel)
32+
val link = Link.Companion.newLink(rel)
3233
this.links.add(link)
3334
return link
3435
}
3536

3637
fun links(vararg links: String) {
37-
this.links.addAll(links.map { Link.newLink(it) })
38+
this.links.addAll(links.map { Link.Companion.newLink(it) })
3839
}
3940

4041
fun links(links: Collection<String>) {
41-
this.links.addAll(links.map { Link.newLink(it) })
42+
this.links.addAll(links.map { Link.Companion.newLink(it) })
4243
}
4344

4445
fun tag(tag: String) {
@@ -70,7 +71,7 @@ class ResourceSpec(
7071
}
7172

7273
override fun build(): List<Snippet> {
73-
val resourceBuilder = ResourceSnippetParameters.builder()
74+
val resourceBuilder = ResourceSnippetParameters.Companion.builder()
7475
if (tags.isNotEmpty()) {
7576
resourceBuilder.tags(*tags.toTypedArray())
7677
}
@@ -93,13 +94,13 @@ class ResourceSpec(
9394
}
9495
if (request.fields.isNotEmpty()) {
9596
if (request.schema != null) {
96-
resourceBuilder.requestSchema(Schema.schema(request.schema!!))
97+
resourceBuilder.requestSchema(Schema.Companion.schema(request.schema!!))
9798
}
9899
resourceBuilder.requestFields(buildFields(request.fields))
99100
}
100101
if (response.fields.isNotEmpty()) {
101102
if (response.schema != null) {
102-
resourceBuilder.responseSchema(Schema.schema(response.schema!!))
103+
resourceBuilder.responseSchema(Schema.Companion.schema(response.schema!!))
103104
}
104105
resourceBuilder.responseFields(buildFields(response.fields))
105106
}

documentify-project/documentify-core/src/main/kotlin/io/github/bgmsound/documentify/core/specification/schema/document/DocumentSpec.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.github.bgmsound.documentify.core.specification.schema.document
22

33
import io.github.bgmsound.documentify.core.specification.DocumentableSpec
4-
import io.github.bgmsound.documentify.core.specification.ResourceSpec
4+
import io.github.bgmsound.documentify.core.specification.schema.ResourceSpec
55
import io.github.bgmsound.documentify.core.specification.schema.Method
66
import io.github.bgmsound.documentify.core.specification.schema.request.RequestBodySpec
77
import io.github.bgmsound.documentify.core.specification.schema.request.RequestHeaderSpec

documentify-project/documentify-mvc/src/main/kotlin/io/github/bgmsound/documentify/mvc/emitter/MvcDocumentResult.kt

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
package io.github.bgmsound.documentify.mvc.emitter
22

33
import io.github.bgmsound.documentify.core.emitter.AbstractDocumentResult
4-
import io.github.bgmsound.documentify.core.emitter.JsonResultMatcher
54
import io.github.bgmsound.documentify.core.specification.schema.response.ResponseSpec
65
import io.github.bgmsound.documentify.mvc.ValidatableMockResponse
7-
import org.hamcrest.Matchers
6+
import org.hamcrest.Matcher
87
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
98

109
class MvcDocumentResult(
1110
private val actualResponse: ValidatableMockResponse
1211
) : AbstractDocumentResult() {
13-
override fun validateJsonPath(jsonResultMatcher: JsonResultMatcher) {
14-
val jsonPath = jsonResultMatcher.jsonPath
15-
val value = jsonResultMatcher.expectedValue
12+
override fun expect(jsonPath: String, matcher: Matcher<*>) {
13+
actualResponse.expect(jsonPath(jsonPath).value(matcher))
14+
}
1615

17-
if (jsonPath.endsWith("[*]")) {
18-
if (value !is List<*>) {
19-
throw IllegalArgumentException("sample value type must be List")
20-
}
21-
actualResponse.expect(jsonPath(jsonPath.substringBeforeLast("[*]")).value(Matchers.containsInAnyOrder(*value.toTypedArray())))
22-
} else if (jsonPath.contains("[*]") && !jsonPath.endsWith("[*]")) {
23-
actualResponse.expect(jsonPath(jsonPath).value(Matchers.hasItem(value)))
24-
} else {
25-
actualResponse.expect(jsonPath(jsonPath).value(value))
26-
}
16+
override fun expectValue(jsonPath: String, value: Any) {
17+
actualResponse.expect(jsonPath(jsonPath).value(value))
2718
}
2819

2920
companion object {

documentify-project/documentify-reactive/src/main/kotlin/io/github/bgmsound/documentify/reactive/emitter/ReactiveDocumentResult.kt

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
package io.github.bgmsound.documentify.reactive.emitter
22

33
import io.github.bgmsound.documentify.core.emitter.AbstractDocumentResult
4-
import io.github.bgmsound.documentify.core.emitter.JsonResultMatcher
54
import io.github.bgmsound.documentify.core.specification.schema.response.ResponseSpec
5+
import org.hamcrest.Matcher
66
import org.hamcrest.Matchers
77
import org.springframework.test.web.reactive.server.WebTestClient
88

99
class ReactiveDocumentResult(
1010
private val actualResponse: WebTestClient.BodyContentSpec
1111
) : AbstractDocumentResult() {
12-
override fun validateJsonPath(jsonResultMatcher: JsonResultMatcher) {
13-
val jsonPath = jsonResultMatcher.jsonPath
14-
val value = jsonResultMatcher.expectedValue
12+
override fun expect(jsonPath: String, matcher: Matcher<*>) {
13+
actualResponse.jsonPath(jsonPath).value(matcher)
14+
}
1515

16-
if (jsonPath.endsWith("[*]")) {
17-
if (value !is List<*>) {
18-
throw IllegalArgumentException("sample value type must be List")
19-
}
20-
actualResponse.jsonPath(jsonPath.substringBeforeLast("[*]")).value(Matchers.containsInAnyOrder(*value.toTypedArray()))
21-
} else if (jsonPath.contains("[*]") && !jsonPath.endsWith("[*]")) {
22-
actualResponse.jsonPath(jsonPath).value(Matchers.hasItem(value))
23-
} else {
24-
actualResponse.jsonPath(jsonPath).value(Matchers.equalToObject(value))
25-
}
16+
override fun expectValue(jsonPath: String, value: Any) {
17+
actualResponse.jsonPath(jsonPath).value(Matchers.equalToObject(value))
2618
}
2719

2820
companion object {

documentify-sample/mvc-sample/src/main/kotlin/io/github/bgmsound/documentify/sample/mvc/ComplexSampleController.kt renamed to documentify-sample/mvc-sample/src/main/kotlin/io/github/bgmsound/documentify/sample/mvc/controller/ComplexSampleController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.bgmsound.documentify.sample.mvc
1+
package io.github.bgmsound.documentify.sample.mvc.controller
22

33
import io.github.bgmsound.documentify.sample.mvc.dto.response.ComplexSampleResponse
44
import io.github.bgmsound.documentify.sample.mvc.dto.response.NestedSampleResponse

documentify-sample/mvc-sample/src/main/kotlin/io/github/bgmsound/documentify/sample/mvc/ErrorController.kt renamed to documentify-sample/mvc-sample/src/main/kotlin/io/github/bgmsound/documentify/sample/mvc/controller/ErrorController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.bgmsound.documentify.sample.mvc
1+
package io.github.bgmsound.documentify.sample.mvc.controller
22

33
import io.github.bgmsound.documentify.sample.mvc.dto.response.SampleResponse
44
import org.springframework.http.ResponseEntity

documentify-sample/mvc-sample/src/main/kotlin/io/github/bgmsound/documentify/sample/mvc/NestedSampleController.kt renamed to documentify-sample/mvc-sample/src/main/kotlin/io/github/bgmsound/documentify/sample/mvc/controller/NestedSampleController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.bgmsound.documentify.sample.mvc
1+
package io.github.bgmsound.documentify.sample.mvc.controller
22

33
import io.github.bgmsound.documentify.sample.mvc.dto.request.NestedSampleRequest
44
import io.github.bgmsound.documentify.sample.mvc.dto.response.NestedSampleResponse

documentify-sample/mvc-sample/src/main/kotlin/io/github/bgmsound/documentify/sample/mvc/SampleController.kt renamed to documentify-sample/mvc-sample/src/main/kotlin/io/github/bgmsound/documentify/sample/mvc/controller/SampleController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.bgmsound.documentify.sample.mvc
1+
package io.github.bgmsound.documentify.sample.mvc.controller
22

33
import io.github.bgmsound.documentify.sample.mvc.dto.request.SampleRequest
44
import io.github.bgmsound.documentify.sample.mvc.dto.response.SampleResponse

0 commit comments

Comments
 (0)