-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathResourceSnippetIntegrationTest.kt
More file actions
147 lines (132 loc) · 5.75 KB
/
ResourceSnippetIntegrationTest.kt
File metadata and controls
147 lines (132 loc) · 5.75 KB
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
package com.epages.restdocs.apispec
import com.epages.restdocs.apispec.ResourceDocumentation.parameterWithName
import com.epages.restdocs.apispec.ResourceDocumentation.resource
import jakarta.validation.constraints.NotEmpty
import org.hibernate.validator.constraints.Length
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.context.ConfigurableApplicationContext
import org.springframework.hateoas.EntityModel
import org.springframework.hateoas.IanaLinkRelations
import org.springframework.hateoas.Link
import org.springframework.hateoas.server.mvc.BasicLinkBuilder.linkToCurrentMapping
import org.springframework.http.HttpHeaders.ACCEPT
import org.springframework.http.HttpHeaders.CONTENT_TYPE
import org.springframework.http.ResponseEntity
import org.springframework.restdocs.headers.HeaderDocumentation.headerWithName
import org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel
import org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.ResultActions
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestHeader
import org.springframework.web.bind.annotation.RestController
import java.time.OffsetDateTime
import java.util.UUID
@ExtendWith(SpringExtension::class)
@WebMvcTest
@AutoConfigureRestDocs
open class ResourceSnippetIntegrationTest {
val operationName = "test-${System.currentTimeMillis()}"
lateinit var resultActions: ResultActions
@SpringBootApplication
open class TestApplication {
lateinit var applicationContext: ConfigurableApplicationContext
fun main(args: Array<String>) {
applicationContext = SpringApplication.run(TestApplication::class.java, *args)
}
@RestController
internal open class TestController {
@PostMapping(path = ["/some/{someId}/other/{otherId}"])
fun doSomething(
@PathVariable someId: String,
@PathVariable otherId: Int?,
@RequestHeader("X-Custom-Header") customHeader: String,
@RequestBody testDataHolder: TestDataHolder
): ResponseEntity<EntityModel<TestDataHolder>> {
val resource = EntityModel.of(testDataHolder.copy(id = UUID.randomUUID().toString()))
val link = linkToCurrentMapping().slash("some").slash(someId).slash("other").slash(otherId).toUri().toString()
resource.add(Link.of(link, IanaLinkRelations.SELF))
resource.add(Link.of(link, "multiple"))
resource.add(Link.of(link, "multiple"))
return ResponseEntity
.ok()
.header("X-Custom-Header", customHeader)
.body<EntityModel<TestDataHolder>>(resource)
}
@PostMapping(path = ["/join"])
fun join(
@RequestBody testDataHolder: TestJoinHolder
): ResponseEntity<User> {
val user = User(
1L,
testDataHolder.loginId,
testDataHolder.password,
OffsetDateTime.now()
)
return ResponseEntity
.ok()
.body(user)
}
}
}
internal data class TestDataHolder(
@field:Length(min = 1, max = 255)
val comment: String? = null,
val flag: Boolean = false,
val count: Int = 0,
@field:NotEmpty
val id: String? = null
)
internal data class TestJoinHolder(
val loginId: String,
val password: String
)
internal class User(
val id: Long,
val loginId: String,
val password: String,
val createdAt: OffsetDateTime
)
}
fun fieldDescriptors(): FieldDescriptors {
val fields = ConstrainedFields(ResourceSnippetIntegrationTest.TestDataHolder::class.java)
return ResourceDocumentation.fields(
fields.withPath("comment").description("the comment").optional(),
fields.withPath("flag").description("the flag"),
fields.withMappedPath("count", "count").description("the count")
)
}
fun buildFullResourceSnippet(): ResourceSnippet {
return resource(
ResourceSnippetParameters.builder()
.description("description")
.summary("summary")
.deprecated(true)
.privateResource(true)
.requestFields(fieldDescriptors())
.responseFields(fieldDescriptors().and(fieldWithPath("id").description("id")))
.requestHeaders(
headerWithName("X-Custom-Header").description("A custom header"),
headerWithName(ACCEPT).description("Accept")
)
.responseHeaders(
headerWithName("X-Custom-Header").description("A custom header"),
headerWithName(CONTENT_TYPE).description("ContentType")
)
.pathParameters(
parameterWithName("someId").description("some id"),
parameterWithName("otherId").description("otherId id").type(SimpleType.INTEGER)
)
.links(
linkWithRel("self").description("some"),
linkWithRel("multiple").description("multiple")
)
.build()
)
}