Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Return 404 if customer is not found.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf Ueberfuhr committed Jun 18, 2024
1 parent 3020a03 commit 1476e63
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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);
}

}

}

}

0 comments on commit 1476e63

Please sign in to comment.