Skip to content

Commit

Permalink
Use existing exception rather than creating newer one
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Apr 11, 2024
1 parent d664b38 commit 3bf3aad
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,7 +69,7 @@ public List<RevisionResult> findCustomerRevisionsById(Long id) {

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

0 comments on commit 3bf3aad

Please sign in to comment.