From 371e1e167337d92376274c2660a6dcae5fe183a8 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 24 Jan 2022 08:41:47 +0200 Subject: [PATCH] Use idiomatic Kotlin in REST Assured tests --- .../kotlin-serialization/pom.xml | 2 +- .../io/quarkus/it/kotser/ResourceTest.kt | 60 ++++++++++--------- .../it/resteasy/reactive/kotlin/EnumTest.kt | 38 ++++++------ .../reactive/kotlin/FlowResourceTest.kt | 13 ++-- .../reactive/kotlin/GreetingResourceTest.kt | 29 +++++---- .../reactive/kotlin/ReactiveClientTest.kt | 17 +++--- .../kotlin/ReactiveGreetingResourceTest.kt | 49 +++++++-------- .../reactive/kotlin/ReactiveMessagingTest.kt | 13 ++-- 8 files changed, 123 insertions(+), 98 deletions(-) diff --git a/integration-tests/kotlin-serialization/pom.xml b/integration-tests/kotlin-serialization/pom.xml index 450808021646d..f1e96d4b05990 100644 --- a/integration-tests/kotlin-serialization/pom.xml +++ b/integration-tests/kotlin-serialization/pom.xml @@ -36,7 +36,7 @@ io.rest-assured - rest-assured + kotlin-extensions test diff --git a/integration-tests/kotlin-serialization/src/test/kotlin/io/quarkus/it/kotser/ResourceTest.kt b/integration-tests/kotlin-serialization/src/test/kotlin/io/quarkus/it/kotser/ResourceTest.kt index f06c828274e12..83f5b30d4ff1a 100644 --- a/integration-tests/kotlin-serialization/src/test/kotlin/io/quarkus/it/kotser/ResourceTest.kt +++ b/integration-tests/kotlin-serialization/src/test/kotlin/io/quarkus/it/kotser/ResourceTest.kt @@ -1,8 +1,10 @@ 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 @@ -10,41 +12,43 @@ import org.junit.jupiter.api.Test 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`( """ [ { @@ -52,22 +56,24 @@ open class ResourceTest { "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())) + } } } diff --git a/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/EnumTest.kt b/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/EnumTest.kt index 0e57aa5b35321..a370591322c95 100644 --- a/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/EnumTest.kt +++ b/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/EnumTest.kt @@ -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 @@ -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]")) + } } } diff --git a/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/FlowResourceTest.kt b/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/FlowResourceTest.kt index 55ae8a3640bcc..42791e580639b 100644 --- a/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/FlowResourceTest.kt +++ b/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/FlowResourceTest.kt @@ -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.* @@ -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 diff --git a/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/GreetingResourceTest.kt b/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/GreetingResourceTest.kt index 660449b5288e1..e373be7c9f9fd 100644 --- a/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/GreetingResourceTest.kt +++ b/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/GreetingResourceTest.kt @@ -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 @@ -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) + } } } diff --git a/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/ReactiveClientTest.kt b/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/ReactiveClientTest.kt index 53441dae23ad2..e4ae4ae3930a1 100644 --- a/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/ReactiveClientTest.kt +++ b/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/ReactiveClientTest.kt @@ -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 @@ -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")) + } } } diff --git a/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/ReactiveGreetingResourceTest.kt b/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/ReactiveGreetingResourceTest.kt index 9ccdcabe2bb33..326910f09f123 100644 --- a/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/ReactiveGreetingResourceTest.kt +++ b/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/ReactiveGreetingResourceTest.kt @@ -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 @@ -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")) + } } } diff --git a/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/ReactiveMessagingTest.kt b/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/ReactiveMessagingTest.kt index 8b04f57205003..8c0ce9f48cd0f 100644 --- a/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/ReactiveMessagingTest.kt +++ b/integration-tests/resteasy-reactive-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/ReactiveMessagingTest.kt @@ -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 @@ -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) } @@ -33,4 +36,4 @@ class ReactiveMessagingTest { assertEquals(get("/country/resolved").`as`(TYPE_REF).size, num) } } -} \ No newline at end of file +}