diff --git a/jpa/boot-data-envers/src/main/java/com/example/envers/services/CustomerService.java b/jpa/boot-data-envers/src/main/java/com/example/envers/services/CustomerService.java index 641f7244d..1b15f654b 100644 --- a/jpa/boot-data-envers/src/main/java/com/example/envers/services/CustomerService.java +++ b/jpa/boot-data-envers/src/main/java/com/example/envers/services/CustomerService.java @@ -10,7 +10,6 @@ import com.example.envers.model.response.PagedResult; import com.example.envers.model.response.RevisionResult; import com.example.envers.repositories.CustomerRepository; -import jakarta.persistence.EntityNotFoundException; import java.util.List; import java.util.Optional; import java.util.concurrent.CompletableFuture; @@ -70,7 +69,7 @@ public List findCustomerRevisionsById(Long id) { public PagedResult findCustomerHistoryById(Long id, Pageable pageRequest) { if (customerRepository.findById(id).isEmpty()) { - throw new EntityNotFoundException("Customer with id %d not found".formatted(id)); + throw new CustomerNotFoundException(id); } RevisionSort sortDir; diff --git a/jpa/boot-data-envers/src/test/java/com/example/envers/web/controllers/CustomerControllerIT.java b/jpa/boot-data-envers/src/test/java/com/example/envers/web/controllers/CustomerControllerIT.java index 7fba38faf..ef996c6f4 100644 --- a/jpa/boot-data-envers/src/test/java/com/example/envers/web/controllers/CustomerControllerIT.java +++ b/jpa/boot-data-envers/src/test/java/com/example/envers/web/controllers/CustomerControllerIT.java @@ -112,6 +112,20 @@ void shouldFindCustomerHistoryById() throws Exception { .andExpect(jsonPath("$.data[0].revisionType", is("UPDATE"))) .andExpect(jsonPath("$.data[0].revisionInstant", notNullValue())); } + + @Test + void cantFindCustomerHistoryById() throws Exception { + Customer customer = customerList.getFirst(); + Long customerId = customer.getId() + 10_000; + + mockMvc.perform(get("/api/customers/{id}/history?page=0&size=10&sort=revision_Number,asc", customerId)) + .andExpect(status().isNotFound()) + .andExpect(header().string(HttpHeaders.CONTENT_TYPE, is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) + .andExpect(jsonPath("$.type", is("http://api.boot-data-envers.com/errors/not-found"))) + .andExpect(jsonPath("$.title", is("Not Found"))) + .andExpect(jsonPath("$.status", is(404))) + .andExpect(jsonPath("$.detail").value("Customer with Id '%d' not found".formatted(customerId))); + } } @Test @@ -137,7 +151,7 @@ void shouldReturn400WhenCreateNewCustomerWithoutName() throws Exception { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(customerRequest))) .andExpect(status().isBadRequest()) - .andExpect(header().string("Content-Type", is("application/problem+json"))) + .andExpect(header().string(HttpHeaders.CONTENT_TYPE, is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) .andExpect(jsonPath("$.type", is("about:blank"))) .andExpect(jsonPath("$.title", is("Constraint Violation"))) .andExpect(jsonPath("$.status", is(400))) diff --git a/jpa/boot-data-envers/src/test/java/com/example/envers/web/controllers/CustomerControllerTest.java b/jpa/boot-data-envers/src/test/java/com/example/envers/web/controllers/CustomerControllerTest.java index 423093317..1e08e961b 100644 --- a/jpa/boot-data-envers/src/test/java/com/example/envers/web/controllers/CustomerControllerTest.java +++ b/jpa/boot-data-envers/src/test/java/com/example/envers/web/controllers/CustomerControllerTest.java @@ -107,7 +107,7 @@ void shouldReturn404WhenFetchingNonExistingCustomer() throws Exception { mockMvc.perform(get("/api/customers/{id}", customerId)) .andExpect(status().isNotFound()) - .andExpect(header().string("Content-Type", is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) + .andExpect(header().string(HttpHeaders.CONTENT_TYPE, is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) .andExpect(jsonPath("$.type", is("http://api.boot-data-envers.com/errors/not-found"))) .andExpect(jsonPath("$.title", is("Not Found"))) .andExpect(jsonPath("$.status", is(404))) @@ -143,7 +143,7 @@ void shouldReturn400WhenCreateNewCustomerWithoutName() throws Exception { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(customerRequest))) .andExpect(status().isBadRequest()) - .andExpect(header().string("Content-Type", is("application/problem+json"))) + .andExpect(header().string(HttpHeaders.CONTENT_TYPE, is("application/problem+json"))) .andExpect(jsonPath("$.type", is("about:blank"))) .andExpect(jsonPath("$.title", is("Constraint Violation"))) .andExpect(jsonPath("$.status", is(400))) @@ -187,7 +187,7 @@ void shouldReturn404WhenUpdatingNonExistingCustomer() throws Exception { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(customerRequest))) .andExpect(status().isNotFound()) - .andExpect(header().string("Content-Type", is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) + .andExpect(header().string(HttpHeaders.CONTENT_TYPE, is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) .andExpect(jsonPath("$.type", is("http://api.boot-data-envers.com/errors/not-found"))) .andExpect(jsonPath("$.title", is("Not Found"))) .andExpect(jsonPath("$.status", is(404))) @@ -217,7 +217,7 @@ void shouldReturn404WhenDeletingNonExistingCustomer() throws Exception { given(customerService.findCustomerById(customerId)).willReturn(Optional.empty()); mockMvc.perform(delete("/api/customers/{id}", customerId)) - .andExpect(header().string("Content-Type", is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) + .andExpect(header().string(HttpHeaders.CONTENT_TYPE, is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) .andExpect(jsonPath("$.type", is("http://api.boot-data-envers.com/errors/not-found"))) .andExpect(jsonPath("$.title", is("Not Found"))) .andExpect(jsonPath("$.status", is(404)))