-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactiveResourceTest.kt
94 lines (81 loc) · 3.33 KB
/
ReactiveResourceTest.kt
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
package io.github.simonscholz
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.matching.ContainsPattern
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder
import com.github.tomakehurst.wiremock.matching.StringValuePattern
import io.github.simonscholz.adapter.auth.Token
import io.github.simonscholz.adapter.beverage.Beverage
import io.github.simonscholz.wiremock.InjectWireMock
import io.github.simonscholz.wiremock.WireMockQuarkusTestResource
import io.quarkus.test.common.QuarkusTestResource
import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured.given
import org.hamcrest.CoreMatchers.`is`
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import javax.inject.Inject
import javax.ws.rs.core.HttpHeaders
@QuarkusTest
@QuarkusTestResource(value = WireMockQuarkusTestResource::class, restrictToAnnotatedClass = true)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ReactiveResourceTest {
@Inject
lateinit var objectMapper: ObjectMapper
@InjectWireMock
lateinit var wireMockServer: WireMockServer
@BeforeAll
fun init() {
wireMockServer.stubFor(
WireMock.post("/token").willReturn(
WireMock.aResponse().withHeader("Content-Type", "application/json")
.withBody(
toJson(Token(
accessToken = "secret-token",
expiresIn = 3000,
tokenType = "Bearer",
))?.trimIndent()
)
)
)
val flens = Beverage(brand = "Flensburger", name = "Pils")
val beverages = listOf(flens, flens.copy(name = "Edles Helles"), flens.copy(name = "Catch the cat"))
wireMockServer.stubFor(
WireMock.get("/v1/beverage/beverages").willReturn(
WireMock.aResponse().withHeader("Content-Type", "application/json")
.withBody(
toJson(beverages)?.trimIndent()
)
)
)
println("Started Wiremock")
}
@Test
fun testAuthEndpoint() {
given()
.`when`().get("/auth")
.then()
.statusCode(200)
.body(`is`("Token(accessToken=secret-token, expiresIn=3000, tokenType=Bearer)"))
}
@Test
fun testBeveragesEndpoint() {
given()
.`when`().get("/beverages")
.then()
.statusCode(200)
.body(`is`("[Beverage(brand=Flensburger, name=Pils), Beverage(brand=Flensburger, name=Edles Helles), Beverage(brand=Flensburger, name=Catch the cat)]"))
val findAllRequests = wireMockServer.findAll(RequestPatternBuilder.allRequests())
println(findAllRequests)
wireMockServer.verify(RequestPatternBuilder().withHeader(HttpHeaders.AUTHORIZATION, ContainsPattern("Bearer")))
}
private fun toJson(requestObject: Any?): String? {
return try {
objectMapper.writeValueAsString(requestObject)
} catch (e: Exception) {
throw IllegalStateException("ObjectMapper was unable to convert requestObject to JSON in ITs.")
}
}
}