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 0b6885099..72c58e230 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 @@ -18,6 +18,8 @@ import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; @@ -56,33 +58,36 @@ void shouldFetchAllCustomers() throws Exception { .andExpect(jsonPath("$.hasPrevious", is(false))); } - @Test - void shouldFindCustomerById() throws Exception { - Customer customer = customerList.get(0); - Long customerId = customer.getId(); - - this.mockMvc - .perform(get("/api/customers/{id}", customerId)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.id", is(customer.getId()), Long.class)) - .andExpect(jsonPath("$.name", is(customer.getName()))) - .andExpect(jsonPath("$.address", is(customer.getAddress()))); - } - - @Test - void shouldFindCustomerRevisionsById() throws Exception { - Customer customer = customerList.get(0); - Long customerId = customer.getId(); - - this.mockMvc - .perform(get("/api/customers/{id}/revisions", customerId)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.size()", is(1))) - .andExpect(jsonPath("$[0].entity.id", is(customer.getId()), Long.class)) - .andExpect(jsonPath("$[0].entity.name", is(customer.getName()))) - .andExpect(jsonPath("$[0].entity.address", is(customer.getAddress()))) - .andExpect(jsonPath("$[0].revisionNumber", notNullValue())) - .andExpect(jsonPath("$[0].revisionType", is("INSERT"))); + @Nested + @DisplayName("find methods") + class Find { + + @Test + void shouldFindCustomerById() throws Exception { + Customer customer = customerList.get(0); + Long customerId = customer.getId(); + + mockMvc.perform(get("/api/customers/{id}", customerId)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id", is(customer.getId()), Long.class)) + .andExpect(jsonPath("$.name", is(customer.getName()))) + .andExpect(jsonPath("$.address", is(customer.getAddress()))); + } + + @Test + void shouldFindCustomerRevisionsById() throws Exception { + Customer customer = customerList.get(0); + Long customerId = customer.getId(); + + mockMvc.perform(get("/api/customers/{id}/revisions", customerId)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.size()", is(1))) + .andExpect(jsonPath("$[0].entity.id", is(customer.getId()), Long.class)) + .andExpect(jsonPath("$[0].entity.name", is(customer.getName()))) + .andExpect(jsonPath("$[0].entity.address", is(customer.getAddress()))) + .andExpect(jsonPath("$[0].revisionNumber", notNullValue())) + .andExpect(jsonPath("$[0].revisionType", is("INSERT"))); + } } @Test 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 ecafb1cf4..85fc67284 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 @@ -28,6 +28,8 @@ import java.util.List; import java.util.Optional; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; @@ -83,136 +85,144 @@ void shouldFetchAllCustomers() throws Exception { .andExpect(jsonPath("$.hasPrevious", is(false))); } - @Test - void shouldFindCustomerById() throws Exception { - Long customerId = 1L; - CustomerResponse customer = new CustomerResponse(customerId, "text 1", "Junit Address"); - given(customerService.findCustomerById(customerId)).willReturn(Optional.of(customer)); - - this.mockMvc - .perform(get("/api/customers/{id}", customerId)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.name", is(customer.name()))) - .andExpect(jsonPath("$.address", is(customer.address()))); - } - - @Test - void shouldReturn404WhenFetchingNonExistingCustomer() throws Exception { - Long customerId = 1L; - given(customerService.findCustomerById(customerId)).willReturn(Optional.empty()); - - this.mockMvc - .perform(get("/api/customers/{id}", customerId)) - .andExpect(status().isNotFound()) - .andExpect(header().string("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 - void shouldCreateNewCustomer() throws Exception { - - CustomerResponse customer = new CustomerResponse(1L, "some text", "Junit Address"); - CustomerRequest customerRequest = new CustomerRequest("some text", "Junit Address"); - given(customerService.saveCustomer(any(CustomerRequest.class))).willReturn(customer); - - this.mockMvc - .perform(post("/api/customers") - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(customerRequest))) - .andExpect(status().isCreated()) - .andExpect(header().exists(HttpHeaders.LOCATION)) - .andExpect(jsonPath("$.id", notNullValue())) - .andExpect(jsonPath("$.name", is(customer.name()))) - .andExpect(jsonPath("$.address", is(customer.address()))); - } - - @Test - void shouldReturn400WhenCreateNewCustomerWithoutName() throws Exception { - CustomerRequest customerRequest = new CustomerRequest(null, null); - - this.mockMvc - .perform(post("/api/customers") - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(customerRequest))) - .andExpect(status().isBadRequest()) - .andExpect(header().string("Content-Type", is("application/problem+json"))) - .andExpect(jsonPath("$.type", is("about:blank"))) - .andExpect(jsonPath("$.title", is("Constraint Violation"))) - .andExpect(jsonPath("$.status", is(400))) - .andExpect(jsonPath("$.detail", is("Invalid request content."))) - .andExpect(jsonPath("$.instance", is("/api/customers"))) - .andExpect(jsonPath("$.violations", hasSize(1))) - .andExpect(jsonPath("$.violations[0].field", is("name"))) - .andExpect(jsonPath("$.violations[0].message", is("Name cannot be empty"))) - .andReturn(); + @Nested + @DisplayName("find methods") + class Find { + @Test + void shouldFindCustomerById() throws Exception { + Long customerId = 1L; + CustomerResponse customer = new CustomerResponse(customerId, "text 1", "Junit Address"); + given(customerService.findCustomerById(customerId)).willReturn(Optional.of(customer)); + + mockMvc.perform(get("/api/customers/{id}", customerId)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.name", is(customer.name()))) + .andExpect(jsonPath("$.address", is(customer.address()))); + } + + @Test + void shouldReturn404WhenFetchingNonExistingCustomer() throws Exception { + Long customerId = 1L; + given(customerService.findCustomerById(customerId)).willReturn(Optional.empty()); + + mockMvc.perform(get("/api/customers/{id}", customerId)) + .andExpect(status().isNotFound()) + .andExpect(header().string("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 - void shouldUpdateCustomer() throws Exception { - Long customerId = 1L; - CustomerResponse customer = new CustomerResponse(customerId, "Updated text", "Junit Address"); - CustomerRequest customerRequest = new CustomerRequest("Updated text", "Junit Address"); - given(customerService.updateCustomer(eq(customerId), any(CustomerRequest.class))) - .willReturn(customer); - - this.mockMvc - .perform(put("/api/customers/{id}", customerId) - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(customerRequest))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.id", is(customerId), Long.class)) - .andExpect(jsonPath("$.name", is(customer.name()))) - .andExpect(jsonPath("$.address", is(customer.address()))); - } - - @Test - void shouldReturn404WhenUpdatingNonExistingCustomer() throws Exception { - Long customerId = 1L; - CustomerRequest customerRequest = new CustomerRequest("Updated text", "Junit Address"); - given(customerService.updateCustomer(eq(customerId), any(CustomerRequest.class))) - .willThrow(new CustomerNotFoundException(customerId)); - - this.mockMvc - .perform(put("/api/customers/{id}", customerId) - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(customerRequest))) - .andExpect(status().isNotFound()) - .andExpect(header().string("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))); + @Nested + @DisplayName("save methods") + class Save { + @Test + void shouldCreateNewCustomer() throws Exception { + + CustomerResponse customer = new CustomerResponse(1L, "some text", "Junit Address"); + CustomerRequest customerRequest = new CustomerRequest("some text", "Junit Address"); + given(customerService.saveCustomer(any(CustomerRequest.class))).willReturn(customer); + + mockMvc.perform(post("/api/customers") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(customerRequest))) + .andExpect(status().isCreated()) + .andExpect(header().exists(HttpHeaders.LOCATION)) + .andExpect(jsonPath("$.id", notNullValue())) + .andExpect(jsonPath("$.name", is(customer.name()))) + .andExpect(jsonPath("$.address", is(customer.address()))); + } + + @Test + void shouldReturn400WhenCreateNewCustomerWithoutName() throws Exception { + CustomerRequest customerRequest = new CustomerRequest(null, null); + + mockMvc.perform(post("/api/customers") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(customerRequest))) + .andExpect(status().isBadRequest()) + .andExpect(header().string("Content-Type", is("application/problem+json"))) + .andExpect(jsonPath("$.type", is("about:blank"))) + .andExpect(jsonPath("$.title", is("Constraint Violation"))) + .andExpect(jsonPath("$.status", is(400))) + .andExpect(jsonPath("$.detail", is("Invalid request content."))) + .andExpect(jsonPath("$.instance", is("/api/customers"))) + .andExpect(jsonPath("$.violations", hasSize(1))) + .andExpect(jsonPath("$.violations[0].field", is("name"))) + .andExpect(jsonPath("$.violations[0].message", is("Name cannot be empty"))) + .andReturn(); + } } - @Test - void shouldDeleteCustomer() throws Exception { - Long customerId = 1L; - CustomerResponse customer = new CustomerResponse(customerId, "Some text", "Junit Address"); - given(customerService.findCustomerById(customerId)).willReturn(Optional.of(customer)); - doNothing().when(customerService).deleteCustomerById(customerId); - - this.mockMvc - .perform(delete("/api/customers/{id}", customerId)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.name", is(customer.name()))) - .andExpect(jsonPath("$.address", is(customer.address()))); + @Nested + @DisplayName("update methods") + class Update { + @Test + void shouldUpdateCustomer() throws Exception { + Long customerId = 1L; + CustomerResponse customer = new CustomerResponse(customerId, "Updated text", "Junit Address"); + CustomerRequest customerRequest = new CustomerRequest("Updated text", "Junit Address"); + given(customerService.updateCustomer(eq(customerId), any(CustomerRequest.class))) + .willReturn(customer); + + mockMvc.perform(put("/api/customers/{id}", customerId) + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(customerRequest))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id", is(customerId), Long.class)) + .andExpect(jsonPath("$.name", is(customer.name()))) + .andExpect(jsonPath("$.address", is(customer.address()))); + } + + @Test + void shouldReturn404WhenUpdatingNonExistingCustomer() throws Exception { + Long customerId = 1L; + CustomerRequest customerRequest = new CustomerRequest("Updated text", "Junit Address"); + given(customerService.updateCustomer(eq(customerId), any(CustomerRequest.class))) + .willThrow(new CustomerNotFoundException(customerId)); + + mockMvc.perform(put("/api/customers/{id}", customerId) + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(customerRequest))) + .andExpect(status().isNotFound()) + .andExpect(header().string("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 - void shouldReturn404WhenDeletingNonExistingCustomer() throws Exception { - Long customerId = 1L; - given(customerService.findCustomerById(customerId)).willReturn(Optional.empty()); - - this.mockMvc - .perform(delete("/api/customers/{id}", customerId)) - .andExpect(header().string("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))); + @Nested + @DisplayName("delete methods") + class Delete { + @Test + void shouldDeleteCustomer() throws Exception { + Long customerId = 1L; + CustomerResponse customer = new CustomerResponse(customerId, "Some text", "Junit Address"); + given(customerService.findCustomerById(customerId)).willReturn(Optional.of(customer)); + doNothing().when(customerService).deleteCustomerById(customerId); + + mockMvc.perform(delete("/api/customers/{id}", customerId)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.name", is(customer.name()))) + .andExpect(jsonPath("$.address", is(customer.address()))); + } + + @Test + void shouldReturn404WhenDeletingNonExistingCustomer() throws Exception { + Long customerId = 1L; + 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(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))); + } } List getCustomerResponseList() {