Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 5054034

Browse files
authored
Add more test cases to ProductsResourceTest (#17)
1 parent d9c5037 commit 5054034

File tree

8 files changed

+383
-6
lines changed

8 files changed

+383
-6
lines changed

code/exercise_010_Validation_and_PUT/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
import io.quarkus.test.junit.QuarkusTest;
44
import io.restassured.http.ContentType;
5+
6+
import org.junit.jupiter.api.Order;
57
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.TestMethodOrder;
9+
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
610

711
import static io.restassured.RestAssured.given;
812
import static org.hamcrest.CoreMatchers.is;
913

1014
@QuarkusTest
15+
@TestMethodOrder(OrderAnnotation.class)
1116
public class ProductsResourceTest {
1217

1318
@Test
19+
@Order(1)
1420
public void testProductsEndpoint() {
1521
given()
1622
.when().get("/products")
@@ -21,6 +27,7 @@ public void testProductsEndpoint() {
2127
}
2228

2329
@Test
30+
@Order(2)
2431
public void testProductDetailsEndpoint() {
2532
given()
2633
.when().get("/products/1")
@@ -31,10 +38,49 @@ public void testProductDetailsEndpoint() {
3138
}
3239

3340
@Test
41+
@Order(3)
3442
public void testProductDetailsEndpointNotFound() {
3543
given()
3644
.when().get("/products/11")
3745
.then()
3846
.statusCode(404);
3947
}
48+
49+
@Test
50+
@Order(4)
51+
public void testProductUpdateEndpoint() {
52+
String updateRequestBody = """
53+
{
54+
"name" : "Chair",
55+
"description" : "A steel frame chair, with oak seat",
56+
"price" : 59.95
57+
}""";
58+
59+
given()
60+
.contentType(ContentType.JSON)
61+
.body(updateRequestBody)
62+
.when().put("/products/1")
63+
.then()
64+
.statusCode(200)
65+
.contentType(ContentType.JSON)
66+
.body("description", is("A steel frame chair, with oak seat"));
67+
}
68+
69+
@Test
70+
@Order(5)
71+
public void testProductUpdateEndpointNotFound() {
72+
String updateRequestBody = """
73+
{
74+
"name" : "Chair",
75+
"description" : "A steel frame chair, with oak seat",
76+
"price" : 69.95
77+
}""";
78+
79+
given()
80+
.contentType(ContentType.JSON)
81+
.body(updateRequestBody)
82+
.when().put("/products/11")
83+
.then()
84+
.statusCode(404);
85+
}
4086
}

code/exercise_011_Going_Reactive/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ In this exercise, we will migrate all the layers of our Hiquea app to the Reacti
88
* Go to your `Product` class. Delete the old `PanacheEntity` import, and find the proper import to use now.
99
* 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.
1010
* For the `PUT` endpoint, do the following:
11-
- Start with `Product.<Product>findById(id)`, and invoke `flatMap` on the resulting `Uni`.
12-
- Within the `flatMap`, as before we want to check if the product exists
11+
- Start with `Product.<Product>findById(id)`, which now returns a `Uni`.
12+
- Decide which `Uni` operator we need to invoke (`map`, or `flatMap`, etc.)
13+
- As before we want to check if the product exists
1314
- if the product does not exist we want to return a NotFound
14-
- if the product exists we want to update the product and invoke `persistAndFlush`
15-
16-
… the `flatMap` allows us to properly ‘chain’ the operations.
17-
* Check if the frontend still works :)
15+
- if the product exists we want to update the product and invoke `persistAndFlush` (which now also returns a `Uni`)
16+
* Run the tests with `./mvnw test` to make sure they still pass
17+
* Rerun the application and check that the frontend still works :)
1818

code/exercise_011_Going_Reactive/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
import io.quarkus.test.junit.QuarkusTest;
44
import io.restassured.http.ContentType;
5+
6+
import org.junit.jupiter.api.Order;
57
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.TestMethodOrder;
9+
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
610

711
import static io.restassured.RestAssured.given;
812
import static org.hamcrest.CoreMatchers.is;
913

1014
@QuarkusTest
15+
@TestMethodOrder(OrderAnnotation.class)
1116
public class ProductsResourceTest {
1217

1318
@Test
19+
@Order(1)
1420
public void testProductsEndpoint() {
1521
given()
1622
.when().get("/products")
@@ -21,6 +27,7 @@ public void testProductsEndpoint() {
2127
}
2228

2329
@Test
30+
@Order(2)
2431
public void testProductDetailsEndpoint() {
2532
given()
2633
.when().get("/products/1")
@@ -31,10 +38,49 @@ public void testProductDetailsEndpoint() {
3138
}
3239

3340
@Test
41+
@Order(3)
3442
public void testProductDetailsEndpointNotFound() {
3543
given()
3644
.when().get("/products/11")
3745
.then()
3846
.statusCode(404);
3947
}
48+
49+
@Test
50+
@Order(4)
51+
public void testProductUpdateEndpoint() {
52+
String updateRequestBody = """
53+
{
54+
"name" : "Chair",
55+
"description" : "A steel frame chair, with oak seat",
56+
"price" : 59.95
57+
}""";
58+
59+
given()
60+
.contentType(ContentType.JSON)
61+
.body(updateRequestBody)
62+
.when().put("/products/1")
63+
.then()
64+
.statusCode(200)
65+
.contentType(ContentType.JSON)
66+
.body("description", is("A steel frame chair, with oak seat"));
67+
}
68+
69+
@Test
70+
@Order(5)
71+
public void testProductUpdateEndpointNotFound() {
72+
String updateRequestBody = """
73+
{
74+
"name" : "Chair",
75+
"description" : "A steel frame chair, with oak seat",
76+
"price" : 69.95
77+
}""";
78+
79+
given()
80+
.contentType(ContentType.JSON)
81+
.body(updateRequestBody)
82+
.when().put("/products/11")
83+
.then()
84+
.statusCode(404);
85+
}
4086
}

code/exercise_012_Reactive_search_endpoint/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
import io.quarkus.test.junit.QuarkusTest;
44
import io.restassured.http.ContentType;
5+
6+
import org.junit.jupiter.api.Order;
57
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.TestMethodOrder;
9+
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
610

711
import static io.restassured.RestAssured.given;
812
import static org.hamcrest.CoreMatchers.is;
913

1014
@QuarkusTest
15+
@TestMethodOrder(OrderAnnotation.class)
1116
public class ProductsResourceTest {
1217

1318
@Test
19+
@Order(1)
1420
public void testProductsEndpoint() {
1521
given()
1622
.when().get("/products")
@@ -21,6 +27,7 @@ public void testProductsEndpoint() {
2127
}
2228

2329
@Test
30+
@Order(2)
2431
public void testProductDetailsEndpoint() {
2532
given()
2633
.when().get("/products/1")
@@ -31,10 +38,60 @@ public void testProductDetailsEndpoint() {
3138
}
3239

3340
@Test
41+
@Order(3)
3442
public void testProductDetailsEndpointNotFound() {
3543
given()
3644
.when().get("/products/11")
3745
.then()
3846
.statusCode(404);
3947
}
48+
49+
@Test
50+
@Order(4)
51+
public void testProductUpdateEndpoint() {
52+
String updateRequestBody = """
53+
{
54+
"name" : "Chair",
55+
"description" : "A steel frame chair, with oak seat",
56+
"price" : 59.95
57+
}""";
58+
59+
given()
60+
.contentType(ContentType.JSON)
61+
.body(updateRequestBody)
62+
.when().put("/products/1")
63+
.then()
64+
.statusCode(200)
65+
.contentType(ContentType.JSON)
66+
.body("description", is("A steel frame chair, with oak seat"));
67+
}
68+
69+
@Test
70+
@Order(5)
71+
public void testProductUpdateEndpointNotFound() {
72+
String updateRequestBody = """
73+
{
74+
"name" : "Chair",
75+
"description" : "A steel frame chair, with oak seat",
76+
"price" : 69.95
77+
}""";
78+
79+
given()
80+
.contentType(ContentType.JSON)
81+
.body(updateRequestBody)
82+
.when().put("/products/11")
83+
.then()
84+
.statusCode(404);
85+
}
86+
87+
@Test
88+
@Order(6)
89+
public void testProductSearchEndpoint() {
90+
given()
91+
.when().get("/products/search/oak")
92+
.then()
93+
.statusCode(200)
94+
.contentType(ContentType.JSON)
95+
.body("size()", is(5));
96+
}
4097
}

code/exercise_013_Listen_and_Notify/src/test/java/com/lunatech/training/quarkus/ProductsResourceTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
import io.quarkus.test.junit.QuarkusTest;
44
import io.restassured.http.ContentType;
5+
6+
import org.junit.jupiter.api.Order;
57
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.TestMethodOrder;
9+
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
610

711
import static io.restassured.RestAssured.given;
812
import static org.hamcrest.CoreMatchers.is;
913

1014
@QuarkusTest
15+
@TestMethodOrder(OrderAnnotation.class)
1116
public class ProductsResourceTest {
1217

1318
@Test
19+
@Order(1)
1420
public void testProductsEndpoint() {
1521
given()
1622
.when().get("/products")
@@ -21,6 +27,7 @@ public void testProductsEndpoint() {
2127
}
2228

2329
@Test
30+
@Order(2)
2431
public void testProductDetailsEndpoint() {
2532
given()
2633
.when().get("/products/1")
@@ -31,10 +38,60 @@ public void testProductDetailsEndpoint() {
3138
}
3239

3340
@Test
41+
@Order(3)
3442
public void testProductDetailsEndpointNotFound() {
3543
given()
3644
.when().get("/products/11")
3745
.then()
3846
.statusCode(404);
3947
}
48+
49+
@Test
50+
@Order(4)
51+
public void testProductUpdateEndpoint() {
52+
String updateRequestBody = """
53+
{
54+
"name" : "Chair",
55+
"description" : "A steel frame chair, with oak seat",
56+
"price" : 59.95
57+
}""";
58+
59+
given()
60+
.contentType(ContentType.JSON)
61+
.body(updateRequestBody)
62+
.when().put("/products/1")
63+
.then()
64+
.statusCode(200)
65+
.contentType(ContentType.JSON)
66+
.body("description", is("A steel frame chair, with oak seat"));
67+
}
68+
69+
@Test
70+
@Order(5)
71+
public void testProductUpdateEndpointNotFound() {
72+
String updateRequestBody = """
73+
{
74+
"name" : "Chair",
75+
"description" : "A steel frame chair, with oak seat",
76+
"price" : 69.95
77+
}""";
78+
79+
given()
80+
.contentType(ContentType.JSON)
81+
.body(updateRequestBody)
82+
.when().put("/products/11")
83+
.then()
84+
.statusCode(404);
85+
}
86+
87+
@Test
88+
@Order(6)
89+
public void testProductSearchEndpoint() {
90+
given()
91+
.when().get("/products/search/oak")
92+
.then()
93+
.statusCode(200)
94+
.contentType(ContentType.JSON)
95+
.body("size()", is(5));
96+
}
4097
}

0 commit comments

Comments
 (0)