From 1476e63eccbb86e4e389640cb1389949bb5623ea Mon Sep 17 00:00:00 2001 From: Ralf Ueberfuhr Date: Tue, 18 Jun 2024 10:56:35 +0200 Subject: [PATCH] Return 404 if customer is not found. --- .../sample/quarkus/CustomersResource.java | 21 ++++-- .../sample/quarkus/CustomerApiTests.java | 69 +++++++++++++++++-- 2 files changed, 79 insertions(+), 11 deletions(-) diff --git a/customer-api-provider/src/main/java/de/schulung/sample/quarkus/CustomersResource.java b/customer-api-provider/src/main/java/de/schulung/sample/quarkus/CustomersResource.java index 0f61fe7..7582f0a 100644 --- a/customer-api-provider/src/main/java/de/schulung/sample/quarkus/CustomersResource.java +++ b/customer-api-provider/src/main/java/de/schulung/sample/quarkus/CustomersResource.java @@ -7,10 +7,7 @@ import java.time.LocalDate; import java.time.Month; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; +import java.util.*; @Path("/customers") public class CustomersResource { @@ -55,10 +52,24 @@ public Response createCustomer(Customer customer) { .build(); } + // Exceptions: https://cristian.sulea.net/blog/rest-java-jax-rs-exception-handling/ + @GET @Path("/{uuid}") public Customer findCustomerById(@PathParam("uuid") UUID uuid) { - return customers.get(uuid); + return Optional.ofNullable(customers.get(uuid)) + .orElseThrow(NotFoundException::new); + } + + @DELETE + @Path("/{uuid}") + public Response deleteCustomerById(@PathParam("uuid") UUID uuid) { + if (customers.remove(uuid) == null) { + throw new NotFoundException(); + } + return Response + .noContent() + .build(); } } diff --git a/customer-api-provider/src/test/java/de/schulung/sample/quarkus/CustomerApiTests.java b/customer-api-provider/src/test/java/de/schulung/sample/quarkus/CustomerApiTests.java index 6a9e068..9a0a58c 100644 --- a/customer-api-provider/src/test/java/de/schulung/sample/quarkus/CustomerApiTests.java +++ b/customer-api-provider/src/test/java/de/schulung/sample/quarkus/CustomerApiTests.java @@ -2,6 +2,7 @@ import io.quarkus.test.junit.QuarkusTest; import io.restassured.http.ContentType; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -62,12 +63,12 @@ void shouldCreateCustomer() { .when() .contentType(ContentType.JSON) .body(""" - { - "name": "Tom", - "birth_date": "2000-10-04", - "state": "active" - } - """) + { + "name": "Tom", + "birth_date": "2000-10-04", + "state": "active" + } + """) .accept(ContentType.JSON) .post("/customers") .then() @@ -92,4 +93,60 @@ void shouldCreateCustomer() { } } + + @Nested + @DisplayName("GET /customers/uuid") + class GetSingleCustomerTests { + + String customerLocation; + + @BeforeEach + // we need to create a customer to be sure that one exist + void setup() { + customerLocation = given() + .when() + .contentType(ContentType.JSON) + .body(""" + { + "name": "Tom", + "birth_date": "2000-10-04", + "state": "active" + } + """) + .accept(ContentType.JSON) + .post("/customers") + .then() + .statusCode(201) + .header("Location", is(notNullValue())) + .extract() + .header("Location"); + } + + @Nested + @DisplayName("with customer that does NOT exist") + class DeletedCustomerTests { + + @BeforeEach + void setup() { + given() + .when() + .delete(customerLocation) + .then() + .statusCode(204); + } + + @Test + void shouldReturn404WhenNotFound() { + given() + .when() + .accept(ContentType.JSON) + .get(customerLocation) + .then() + .statusCode(404); + } + + } + + } + } \ No newline at end of file