Skip to content

Commit

Permalink
Add more test cases to ProductsResourceTest
Browse files Browse the repository at this point in the history
  • Loading branch information
njlbenn committed Jun 15, 2024
1 parent d9c5037 commit 594ac09
Show file tree
Hide file tree
Showing 8 changed files with 383 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -21,6 +27,7 @@ public void testProductsEndpoint() {
}

@Test
@Order(2)
public void testProductDetailsEndpoint() {
given()
.when().get("/products/1")
Expand All @@ -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);
}
}
12 changes: 6 additions & 6 deletions code/exercise_011_Going_Reactive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<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.<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 :)

Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -21,6 +27,7 @@ public void testProductsEndpoint() {
}

@Test
@Order(2)
public void testProductDetailsEndpoint() {
given()
.when().get("/products/1")
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -21,6 +27,7 @@ public void testProductsEndpoint() {
}

@Test
@Order(2)
public void testProductDetailsEndpoint() {
given()
.when().get("/products/1")
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -21,6 +27,7 @@ public void testProductsEndpoint() {
}

@Test
@Order(2)
public void testProductDetailsEndpoint() {
given()
.when().get("/products/1")
Expand All @@ -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));
}
}
Loading

0 comments on commit 594ac09

Please sign in to comment.