Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use idiomatic Kotlin in REST Assured tests #23124

Merged
merged 1 commit into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integration-tests/kotlin-serialization/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<artifactId>kotlin-extensions</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,73 +1,79 @@
package io.quarkus.it.kotser

import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured.given
import io.restassured.http.ContentType.JSON
import io.restassured.module.kotlin.extensions.Given
import io.restassured.module.kotlin.extensions.Then
import io.restassured.module.kotlin.extensions.When
import org.hamcrest.CoreMatchers.`is`
import org.junit.jupiter.api.Test

@QuarkusTest
open class ResourceTest {
@Test
fun testGet() {
given()
.`when`().get("/")
.then()
.statusCode(200)
.body(`is`(
"""
When {
get("/")
} Then {
statusCode(200)
body(`is`(
"""
{
"name": "Jim Halpert",
"defaulted": "hi there!"
}""".trimIndent()
))
))
}
}

@Test
fun testSuspendGet() {
given()
.`when`().get("/suspend")
.then()
.statusCode(200)
.body(`is`(
When {
get("/suspend")
} Then {
statusCode(200)
body(`is`(
"""
{
"name": "Jim Halpert",
"defaulted": "hi there!"
}""".trimIndent()
))
}
}

@Test
fun testSuspendGetList() {
given()
.`when`().get("/suspendList")
.then()
.statusCode(200)
.body(`is`(
When {
get("/suspendList")
} Then {
statusCode(200)
body(`is`(
"""
[
{
"name": "Jim Halpert",
"defaulted": "hi there!"
}
]
""".trimIndent()
))
""".trimIndent()))
}
}

@Test
fun testPost() {
given()
.body("{\"name\":\"Pam Beasley\"}")
.contentType(JSON)
.`when`().post("/")
.then()
.statusCode(200)
.body(`is`("""
Given {
body("""{ "name": "Pam Beasley" }""")
contentType(JSON)
} When {
post("/")
} Then {
statusCode(200)
body(`is`("""
{
"name": "Pam Halpert",
"defaulted": "hi there!"
}""".trimIndent()))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.quarkus.it.resteasy.reactive.kotlin

import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured.given
import io.restassured.module.kotlin.extensions.Then
import io.restassured.module.kotlin.extensions.When
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.Test

Expand All @@ -10,31 +12,31 @@ class EnumTest {

@Test
fun testNoStates() {
given()
.`when`()
.get("/enum")
.then()
.statusCode(200)
.body(equalTo("States: []"))
When {
get("/enum")
} Then {
statusCode(200)
body(equalTo("States: []"))
}
}

@Test
fun testSingleState() {
given()
.`when`()
.get("/enum?state=State1")
.then()
.statusCode(200)
.body(equalTo("States: [State1]"))
When {
get("/enum?state=State1")
} Then {
statusCode(200)
body(equalTo("States: [State1]"))
}
}

@Test
fun testMultipleStates() {
given()
.`when`()
.get("/enum?state=State1&state=State2&state=State3")
.then()
.statusCode(200)
.body(equalTo("States: [State1, State2, State3]"))
When {
get("/enum?state=State1&state=State2&state=State3")
} Then {
statusCode(200)
body(equalTo("States: [State1, State2, State3]"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package io.quarkus.it.resteasy.reactive.kotlin
import io.quarkus.test.common.http.TestHTTPResource
import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured
import io.restassured.module.kotlin.extensions.Then
import io.restassured.module.kotlin.extensions.When
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.util.*
Expand Down Expand Up @@ -35,11 +37,12 @@ class FlowResourceTest {

@Test
fun testResponseStatusAndHeaders() {
RestAssured
.get("/flow/str")
.then()
.statusCode(201)
.headers(Map.of("foo", "bar"))
When {
get("/flow/str")
} Then {
statusCode(201)
headers(mapOf("foo" to "bar"))
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package io.quarkus.it.resteasy.reactive.kotlin
import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured
import io.restassured.http.ContentType
import io.restassured.module.kotlin.extensions.Given
import io.restassured.module.kotlin.extensions.Then
import io.restassured.module.kotlin.extensions.When
import org.hamcrest.CoreMatchers
import org.junit.jupiter.api.Test

Expand All @@ -11,20 +14,24 @@ class GreetingResourceTest {

@Test
fun testDataClassAndCustomFilters() {
RestAssured.given()
.`when`()["/greeting"]
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.body("message", CoreMatchers.`is`("hello foo bar"))
.header("method", "testSuspend")
When {
get("/greeting")
} Then {
statusCode(200)
contentType(ContentType.JSON)
body("message", CoreMatchers.`is`("hello foo bar"))
header("method", "testSuspend")
}
}

@Test
fun testAbortingCustomFilters() {
RestAssured.given().header("abort", "true")
.`when`()["/greeting"]
.then()
.statusCode(204)
Given {
header("abort", "true")
} When {
get("/greeting")
} Then {
statusCode(204)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package io.quarkus.it.resteasy.reactive.kotlin
import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured
import io.restassured.http.ContentType
import io.restassured.module.kotlin.extensions.Then
import io.restassured.module.kotlin.extensions.When
import org.hamcrest.CoreMatchers
import org.junit.jupiter.api.Test

Expand All @@ -11,12 +13,13 @@ class ReactiveClientTest {

@Test
fun testGetCountryByName() {
RestAssured.given()
.`when`()["/country/name/foo"]
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.body("$.size()", CoreMatchers.`is`(1),
"[0].capital", CoreMatchers.`is`("foo-capital"))
When {
get("/country/name/foo")
} Then {
statusCode(200)
contentType(ContentType.JSON)
body("$.size()", CoreMatchers.`is`(1),
"[0].capital", CoreMatchers.`is`("foo-capital"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.quarkus.it.resteasy.reactive.kotlin

import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured.given
import io.restassured.module.kotlin.extensions.Then
import io.restassured.module.kotlin.extensions.When
import org.hamcrest.CoreMatchers.`is`
import org.junit.jupiter.api.Test

Expand All @@ -10,41 +12,40 @@ class ReactiveGreetingResourceTest {

@Test
fun testResource() {
given()
.`when`().get("/test.txt")
.then()
.statusCode(200)

given()
.`when`().get("/test2.txt")
.then()
.statusCode(404)
When {
get("/test.txt")
} Then {
statusCode(200)
}
}

@Test
fun testHello() {
given()
.`when`().get("/hello-resteasy-reactive/")
.then()
.statusCode(200)
.body(`is`("Hello RestEASY Reactive"))
When {
get("/hello-resteasy-reactive/")
} Then {
statusCode(200)
body(`is`("Hello RestEASY Reactive"))
}
}

@Test
fun testStandard() {
given()
.`when`().get("/hello-resteasy-reactive/standard")
.then()
.statusCode(200)
.body(`is`("Hello RestEASY Reactive"))
When {
get("/hello-resteasy-reactive/standard")
} Then {
statusCode(200)
body(`is`("Hello RestEASY Reactive"))
}
}

@Test
fun testNamedHello() {
given()
.`when`().get("/hello-resteasy-reactive/Bob")
.then()
.statusCode(200)
.body(`is`("Hello Bob"))
When {
get("/hello-resteasy-reactive/Bob")
} Then {
statusCode(200)
body(`is`("Hello Bob"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured.get
import io.restassured.RestAssured.given
import io.restassured.common.mapper.TypeRef
import io.restassured.module.kotlin.extensions.Then
import io.restassured.module.kotlin.extensions.When
import org.awaitility.Awaitility.await
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
Expand All @@ -20,10 +22,11 @@ class ReactiveMessagingTest {
fun test() {
assertCountries(6)

given()
.`when`().post("/country/kafka/dummy")
.then()
.statusCode(200)
When {
post("/country/kafka/dummy")
} Then {
statusCode(200)
}

assertCountries(8)
}
Expand All @@ -33,4 +36,4 @@ class ReactiveMessagingTest {
assertEquals(get("/country/resolved").`as`(TYPE_REF).size, num)
}
}
}
}