From 594ac097abd679000b8b99410495197da085ac68 Mon Sep 17 00:00:00 2001 From: Neil Benn Date: Fri, 14 Jun 2024 01:14:04 +0200 Subject: [PATCH] Add more test cases to ProductsResourceTest --- .../quarkus/ProductsResourceTest.java | 46 +++++++++++++++ code/exercise_011_Going_Reactive/README.md | 12 ++-- .../quarkus/ProductsResourceTest.java | 46 +++++++++++++++ .../quarkus/ProductsResourceTest.java | 57 +++++++++++++++++++ .../quarkus/ProductsResourceTest.java | 57 +++++++++++++++++++ .../quarkus/ProductsResourceTest.java | 57 +++++++++++++++++++ .../quarkus/ProductsResourceTest.java | 57 +++++++++++++++++++ .../quarkus/ProductsResourceTest.java | 57 +++++++++++++++++++ 8 files changed, 383 insertions(+), 6 deletions(-) diff --git a/code/exercise_010_Validation_and_PUT/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java b/code/exercise_010_Validation_and_PUT/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java index f8e23d7..6f63588 100644 --- a/code/exercise_010_Validation_and_PUT/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java +++ b/code/exercise_010_Validation_and_PUT/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java @@ -2,15 +2,21 @@ import io.quarkus.test.junit.QuarkusTest; import io.restassured.http.ContentType; + +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import static io.restassured.RestAssured.given; import static org.hamcrest.CoreMatchers.is; @QuarkusTest +@TestMethodOrder(OrderAnnotation.class) public class ProductsResourceTest { @Test + @Order(1) public void testProductsEndpoint() { given() .when().get("/products") @@ -21,6 +27,7 @@ public void testProductsEndpoint() { } @Test + @Order(2) public void testProductDetailsEndpoint() { given() .when().get("/products/1") @@ -31,10 +38,49 @@ public void testProductDetailsEndpoint() { } @Test + @Order(3) public void testProductDetailsEndpointNotFound() { given() .when().get("/products/11") .then() .statusCode(404); } + + @Test + @Order(4) + public void testProductUpdateEndpoint() { + String updateRequestBody = """ + { + "name" : "Chair", + "description" : "A steel frame chair, with oak seat", + "price" : 59.95 + }"""; + + given() + .contentType(ContentType.JSON) + .body(updateRequestBody) + .when().put("/products/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("description", is("A steel frame chair, with oak seat")); + } + + @Test + @Order(5) + public void testProductUpdateEndpointNotFound() { + String updateRequestBody = """ + { + "name" : "Chair", + "description" : "A steel frame chair, with oak seat", + "price" : 69.95 + }"""; + + given() + .contentType(ContentType.JSON) + .body(updateRequestBody) + .when().put("/products/11") + .then() + .statusCode(404); + } } diff --git a/code/exercise_011_Going_Reactive/README.md b/code/exercise_011_Going_Reactive/README.md index aac49b3..4540834 100644 --- a/code/exercise_011_Going_Reactive/README.md +++ b/code/exercise_011_Going_Reactive/README.md @@ -8,11 +8,11 @@ In this exercise, we will migrate all the layers of our Hiquea app to the Reacti * Go to your `Product` class. Delete the old `PanacheEntity` import, and find the proper import to use now. * Now, go to `ProductsResource`, and make it work again. Note that you can return `Uni` responses from your resource methods now that you have RESTeasy reactive. * For the `PUT` endpoint, do the following: - - Start with `Product.findById(id)`, and invoke `flatMap` on the resulting `Uni`. - - Within the `flatMap`, as before we want to check if the product exists + - Start with `Product.findById(id)`, which now returns a `Uni`. + - Decide which `Uni` operator we need to invoke (`map`, or `flatMap`, etc.) + - As before we want to check if the product exists - if the product does not exist we want to return a NotFound - - if the product exists we want to update the product and invoke `persistAndFlush` - - … the `flatMap` allows us to properly ‘chain’ the operations. -* Check if the frontend still works :) + - if the product exists we want to update the product and invoke `persistAndFlush` (which now also returns a `Uni`) +* Run the tests with `./mvnw test` to make sure they still pass +* Rerun the application and check that the frontend still works :) diff --git a/code/exercise_011_Going_Reactive/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java b/code/exercise_011_Going_Reactive/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java index f8e23d7..6f63588 100644 --- a/code/exercise_011_Going_Reactive/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java +++ b/code/exercise_011_Going_Reactive/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java @@ -2,15 +2,21 @@ import io.quarkus.test.junit.QuarkusTest; import io.restassured.http.ContentType; + +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import static io.restassured.RestAssured.given; import static org.hamcrest.CoreMatchers.is; @QuarkusTest +@TestMethodOrder(OrderAnnotation.class) public class ProductsResourceTest { @Test + @Order(1) public void testProductsEndpoint() { given() .when().get("/products") @@ -21,6 +27,7 @@ public void testProductsEndpoint() { } @Test + @Order(2) public void testProductDetailsEndpoint() { given() .when().get("/products/1") @@ -31,10 +38,49 @@ public void testProductDetailsEndpoint() { } @Test + @Order(3) public void testProductDetailsEndpointNotFound() { given() .when().get("/products/11") .then() .statusCode(404); } + + @Test + @Order(4) + public void testProductUpdateEndpoint() { + String updateRequestBody = """ + { + "name" : "Chair", + "description" : "A steel frame chair, with oak seat", + "price" : 59.95 + }"""; + + given() + .contentType(ContentType.JSON) + .body(updateRequestBody) + .when().put("/products/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("description", is("A steel frame chair, with oak seat")); + } + + @Test + @Order(5) + public void testProductUpdateEndpointNotFound() { + String updateRequestBody = """ + { + "name" : "Chair", + "description" : "A steel frame chair, with oak seat", + "price" : 69.95 + }"""; + + given() + .contentType(ContentType.JSON) + .body(updateRequestBody) + .when().put("/products/11") + .then() + .statusCode(404); + } } diff --git a/code/exercise_012_Reactive_search_endpoint/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java b/code/exercise_012_Reactive_search_endpoint/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java index f8e23d7..d692246 100644 --- a/code/exercise_012_Reactive_search_endpoint/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java +++ b/code/exercise_012_Reactive_search_endpoint/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java @@ -2,15 +2,21 @@ import io.quarkus.test.junit.QuarkusTest; import io.restassured.http.ContentType; + +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import static io.restassured.RestAssured.given; import static org.hamcrest.CoreMatchers.is; @QuarkusTest +@TestMethodOrder(OrderAnnotation.class) public class ProductsResourceTest { @Test + @Order(1) public void testProductsEndpoint() { given() .when().get("/products") @@ -21,6 +27,7 @@ public void testProductsEndpoint() { } @Test + @Order(2) public void testProductDetailsEndpoint() { given() .when().get("/products/1") @@ -31,10 +38,60 @@ public void testProductDetailsEndpoint() { } @Test + @Order(3) public void testProductDetailsEndpointNotFound() { given() .when().get("/products/11") .then() .statusCode(404); } + + @Test + @Order(4) + public void testProductUpdateEndpoint() { + String updateRequestBody = """ + { + "name" : "Chair", + "description" : "A steel frame chair, with oak seat", + "price" : 59.95 + }"""; + + given() + .contentType(ContentType.JSON) + .body(updateRequestBody) + .when().put("/products/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("description", is("A steel frame chair, with oak seat")); + } + + @Test + @Order(5) + public void testProductUpdateEndpointNotFound() { + String updateRequestBody = """ + { + "name" : "Chair", + "description" : "A steel frame chair, with oak seat", + "price" : 69.95 + }"""; + + given() + .contentType(ContentType.JSON) + .body(updateRequestBody) + .when().put("/products/11") + .then() + .statusCode(404); + } + + @Test + @Order(6) + public void testProductSearchEndpoint() { + given() + .when().get("/products/search/oak") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("size()", is(5)); + } } diff --git a/code/exercise_013_Listen_and_Notify/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java b/code/exercise_013_Listen_and_Notify/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java index f8e23d7..d692246 100644 --- a/code/exercise_013_Listen_and_Notify/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java +++ b/code/exercise_013_Listen_and_Notify/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java @@ -2,15 +2,21 @@ import io.quarkus.test.junit.QuarkusTest; import io.restassured.http.ContentType; + +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import static io.restassured.RestAssured.given; import static org.hamcrest.CoreMatchers.is; @QuarkusTest +@TestMethodOrder(OrderAnnotation.class) public class ProductsResourceTest { @Test + @Order(1) public void testProductsEndpoint() { given() .when().get("/products") @@ -21,6 +27,7 @@ public void testProductsEndpoint() { } @Test + @Order(2) public void testProductDetailsEndpoint() { given() .when().get("/products/1") @@ -31,10 +38,60 @@ public void testProductDetailsEndpoint() { } @Test + @Order(3) public void testProductDetailsEndpointNotFound() { given() .when().get("/products/11") .then() .statusCode(404); } + + @Test + @Order(4) + public void testProductUpdateEndpoint() { + String updateRequestBody = """ + { + "name" : "Chair", + "description" : "A steel frame chair, with oak seat", + "price" : 59.95 + }"""; + + given() + .contentType(ContentType.JSON) + .body(updateRequestBody) + .when().put("/products/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("description", is("A steel frame chair, with oak seat")); + } + + @Test + @Order(5) + public void testProductUpdateEndpointNotFound() { + String updateRequestBody = """ + { + "name" : "Chair", + "description" : "A steel frame chair, with oak seat", + "price" : 69.95 + }"""; + + given() + .contentType(ContentType.JSON) + .body(updateRequestBody) + .when().put("/products/11") + .then() + .statusCode(404); + } + + @Test + @Order(6) + public void testProductSearchEndpoint() { + given() + .when().get("/products/search/oak") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("size()", is(5)); + } } diff --git a/code/exercise_014_Internal_Channels/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java b/code/exercise_014_Internal_Channels/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java index f8e23d7..d692246 100644 --- a/code/exercise_014_Internal_Channels/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java +++ b/code/exercise_014_Internal_Channels/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java @@ -2,15 +2,21 @@ import io.quarkus.test.junit.QuarkusTest; import io.restassured.http.ContentType; + +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import static io.restassured.RestAssured.given; import static org.hamcrest.CoreMatchers.is; @QuarkusTest +@TestMethodOrder(OrderAnnotation.class) public class ProductsResourceTest { @Test + @Order(1) public void testProductsEndpoint() { given() .when().get("/products") @@ -21,6 +27,7 @@ public void testProductsEndpoint() { } @Test + @Order(2) public void testProductDetailsEndpoint() { given() .when().get("/products/1") @@ -31,10 +38,60 @@ public void testProductDetailsEndpoint() { } @Test + @Order(3) public void testProductDetailsEndpointNotFound() { given() .when().get("/products/11") .then() .statusCode(404); } + + @Test + @Order(4) + public void testProductUpdateEndpoint() { + String updateRequestBody = """ + { + "name" : "Chair", + "description" : "A steel frame chair, with oak seat", + "price" : 59.95 + }"""; + + given() + .contentType(ContentType.JSON) + .body(updateRequestBody) + .when().put("/products/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("description", is("A steel frame chair, with oak seat")); + } + + @Test + @Order(5) + public void testProductUpdateEndpointNotFound() { + String updateRequestBody = """ + { + "name" : "Chair", + "description" : "A steel frame chair, with oak seat", + "price" : 69.95 + }"""; + + given() + .contentType(ContentType.JSON) + .body(updateRequestBody) + .when().put("/products/11") + .then() + .statusCode(404); + } + + @Test + @Order(6) + public void testProductSearchEndpoint() { + given() + .when().get("/products/search/oak") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("size()", is(5)); + } } diff --git a/code/exercise_015_Connecting_to_Kafka/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java b/code/exercise_015_Connecting_to_Kafka/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java index f8e23d7..d692246 100644 --- a/code/exercise_015_Connecting_to_Kafka/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java +++ b/code/exercise_015_Connecting_to_Kafka/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java @@ -2,15 +2,21 @@ import io.quarkus.test.junit.QuarkusTest; import io.restassured.http.ContentType; + +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import static io.restassured.RestAssured.given; import static org.hamcrest.CoreMatchers.is; @QuarkusTest +@TestMethodOrder(OrderAnnotation.class) public class ProductsResourceTest { @Test + @Order(1) public void testProductsEndpoint() { given() .when().get("/products") @@ -21,6 +27,7 @@ public void testProductsEndpoint() { } @Test + @Order(2) public void testProductDetailsEndpoint() { given() .when().get("/products/1") @@ -31,10 +38,60 @@ public void testProductDetailsEndpoint() { } @Test + @Order(3) public void testProductDetailsEndpointNotFound() { given() .when().get("/products/11") .then() .statusCode(404); } + + @Test + @Order(4) + public void testProductUpdateEndpoint() { + String updateRequestBody = """ + { + "name" : "Chair", + "description" : "A steel frame chair, with oak seat", + "price" : 59.95 + }"""; + + given() + .contentType(ContentType.JSON) + .body(updateRequestBody) + .when().put("/products/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("description", is("A steel frame chair, with oak seat")); + } + + @Test + @Order(5) + public void testProductUpdateEndpointNotFound() { + String updateRequestBody = """ + { + "name" : "Chair", + "description" : "A steel frame chair, with oak seat", + "price" : 69.95 + }"""; + + given() + .contentType(ContentType.JSON) + .body(updateRequestBody) + .when().put("/products/11") + .then() + .statusCode(404); + } + + @Test + @Order(6) + public void testProductSearchEndpoint() { + given() + .when().get("/products/search/oak") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("size()", is(5)); + } } diff --git a/code/exercise_016_Dead_Letter_Queue_and_Stream_filtering/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java b/code/exercise_016_Dead_Letter_Queue_and_Stream_filtering/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java index f8e23d7..d692246 100644 --- a/code/exercise_016_Dead_Letter_Queue_and_Stream_filtering/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java +++ b/code/exercise_016_Dead_Letter_Queue_and_Stream_filtering/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java @@ -2,15 +2,21 @@ import io.quarkus.test.junit.QuarkusTest; import io.restassured.http.ContentType; + +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import static io.restassured.RestAssured.given; import static org.hamcrest.CoreMatchers.is; @QuarkusTest +@TestMethodOrder(OrderAnnotation.class) public class ProductsResourceTest { @Test + @Order(1) public void testProductsEndpoint() { given() .when().get("/products") @@ -21,6 +27,7 @@ public void testProductsEndpoint() { } @Test + @Order(2) public void testProductDetailsEndpoint() { given() .when().get("/products/1") @@ -31,10 +38,60 @@ public void testProductDetailsEndpoint() { } @Test + @Order(3) public void testProductDetailsEndpointNotFound() { given() .when().get("/products/11") .then() .statusCode(404); } + + @Test + @Order(4) + public void testProductUpdateEndpoint() { + String updateRequestBody = """ + { + "name" : "Chair", + "description" : "A steel frame chair, with oak seat", + "price" : 59.95 + }"""; + + given() + .contentType(ContentType.JSON) + .body(updateRequestBody) + .when().put("/products/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("description", is("A steel frame chair, with oak seat")); + } + + @Test + @Order(5) + public void testProductUpdateEndpointNotFound() { + String updateRequestBody = """ + { + "name" : "Chair", + "description" : "A steel frame chair, with oak seat", + "price" : 69.95 + }"""; + + given() + .contentType(ContentType.JSON) + .body(updateRequestBody) + .when().put("/products/11") + .then() + .statusCode(404); + } + + @Test + @Order(6) + public void testProductSearchEndpoint() { + given() + .when().get("/products/search/oak") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("size()", is(5)); + } }