From 6d09a66f0df608ac83164f4e1abb44a75b3a1d19 Mon Sep 17 00:00:00 2001 From: Hywenklis Date: Tue, 23 Apr 2024 01:39:44 -0300 Subject: [PATCH] Adiciona testes integrados para o registro de estados --- .../desafio1/builders/StateBuilder.java | 2 +- .../state}/FindStateControllerTest.java | 2 +- .../state/SaveStateControllerTest.java | 111 ++++++++++++++++++ .../{ => author}/AuthorControllerTest.java | 2 +- .../CategoryControllerTest.java | 2 +- 5 files changed, 115 insertions(+), 4 deletions(-) rename src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/{ => address/state}/FindStateControllerTest.java (98%) create mode 100644 src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/address/state/SaveStateControllerTest.java rename src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/{ => author}/AuthorControllerTest.java (99%) rename src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/{ => category}/CategoryControllerTest.java (99%) diff --git a/src/test/java/com/jornada/dev/eficiente/desafio1/builders/StateBuilder.java b/src/test/java/com/jornada/dev/eficiente/desafio1/builders/StateBuilder.java index 3bce54a49..690be0e0e 100644 --- a/src/test/java/com/jornada/dev/eficiente/desafio1/builders/StateBuilder.java +++ b/src/test/java/com/jornada/dev/eficiente/desafio1/builders/StateBuilder.java @@ -15,7 +15,7 @@ public class StateBuilder { public static StateRequest createStateRequest(String name, CountryRequest countryRequest) { return StateRequest.builder() - .name(name.toLowerCase(Locale.ROOT)) + .name(name) .country(countryRequest) .build(); diff --git a/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/FindStateControllerTest.java b/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/address/state/FindStateControllerTest.java similarity index 98% rename from src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/FindStateControllerTest.java rename to src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/address/state/FindStateControllerTest.java index 72a5a6171..f3bdcc996 100644 --- a/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/FindStateControllerTest.java +++ b/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/address/state/FindStateControllerTest.java @@ -1,4 +1,4 @@ -package com.jornada.dev.eficiente.desafio1.integrations.web.controllers; +package com.jornada.dev.eficiente.desafio1.integrations.web.controllers.address.state; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; diff --git a/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/address/state/SaveStateControllerTest.java b/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/address/state/SaveStateControllerTest.java new file mode 100644 index 000000000..0bdf89e84 --- /dev/null +++ b/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/address/state/SaveStateControllerTest.java @@ -0,0 +1,111 @@ +package com.jornada.dev.eficiente.desafio1.integrations.web.controllers.address.state; + +import static com.jornada.dev.eficiente.desafio1.builders.CountryBuilder.createCountryRequest; +import static com.jornada.dev.eficiente.desafio1.builders.StateBuilder.createStateRequest; +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.jornada.dev.eficiente.desafio1.integrations.IntegrationTestAbstract; +import java.util.Locale; +import java.util.stream.Stream; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.http.HttpStatus; + +@DisplayName("POST /v1/books/register") +class SaveStateControllerTest extends IntegrationTestAbstract { + + @Test + @DisplayName("Should register a new state successfully") + void registration_NewState_Success() throws Exception { + + var country = countryComponent.createCountry(randomAlphabetic(10)); + var countryRequest = createCountryRequest(country.getName()); + var request = + createStateRequest(randomAlphabetic(10).toLowerCase(Locale.ROOT), countryRequest); + + mockMvc + .perform(post("/v1/states/register") + .contentType(APPLICATION_JSON) + .content(objectMapper.writeValueAsString(request))) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$").isNotEmpty()) + .andExpect(jsonPath("$.id").isNotEmpty()) + .andExpect(jsonPath("$.name").value(request.name())) + .andExpect(jsonPath("$.createDate").isNotEmpty()) + .andExpect(jsonPath("$.updateDate").isNotEmpty()) + .andExpect(jsonPath("$.country.id").isNotEmpty()) + .andExpect(jsonPath("$.country.name").value(request.country().name())) + .andExpect(jsonPath("$.country.createDate").isNotEmpty()) + .andExpect(jsonPath("$.country.updateDate").isNotEmpty()); + } + + @Test + @DisplayName("Should return 400 Bad Request when state name is not unique") + void registration_NonUniqueTitle_BadRequest() throws Exception { + var state = dataInitializerComponent.initializeStateData(); + var countryRequest = createCountryRequest(randomAlphabetic(10)); + var request = createStateRequest(state.getName(), countryRequest); + + mockMvc.perform(post("/v1/states/register") + .contentType(APPLICATION_JSON) + .content(objectMapper.writeValueAsString(request))) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.errors[0].field").value("name")) + .andExpect(jsonPath("$.errors[0].message").value("State name must be unique")) + .andExpect(jsonPath("$.errors[0].httpStatus").value("BAD_REQUEST")) + .andExpect(jsonPath("$.errors[0].errorCode").value(HttpStatus.BAD_REQUEST.value())) + .andExpect(jsonPath("$.errors[0].timestamp").isNotEmpty()); + } + + @ParameterizedTest + @MethodSource("provideNullOrEmptyValues") + @DisplayName("Should return 400 Bad Request when state name is missing") + void registration_MissingOrEmptyName_BadRequest(String value) throws Exception { + var country = countryComponent.createCountry(randomAlphabetic(10)); + var countryRequest = createCountryRequest(country.getName()); + var request = createStateRequest(value, countryRequest); + + mockMvc + .perform(post("/v1/states/register") + .contentType(APPLICATION_JSON) + .content(objectMapper.writeValueAsString(request))) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.errors[0].field").value("name")) + .andExpect(jsonPath("$.errors[0].message").value("Name is required")) + .andExpect(jsonPath("$.errors[0].httpStatus").value("BAD_REQUEST")) + .andExpect(jsonPath("$.errors[0].errorCode").value(HttpStatus.BAD_REQUEST.value())) + .andExpect(jsonPath("$.errors[0].timestamp").isNotEmpty()); + } + + @Test + @DisplayName("Should return 404 Not Found when country is not found") + void registration_NonExistentCountry_NotFound() throws Exception { + var countryRequest = createCountryRequest(randomAlphabetic(10)); + var request = createStateRequest(randomAlphabetic(10), countryRequest); + + mockMvc.perform(post("/v1/states/register") + .contentType(APPLICATION_JSON) + .content(objectMapper.writeValueAsString(request))) + .andExpect(status().isNotFound()) + .andExpect(jsonPath("$.errors[0].field").value("countryName")) + .andExpect(jsonPath("$.errors[0].message").value( + "Country not found with name: " + request.country().name())) + .andExpect(jsonPath("$.errors[0].httpStatus").value("NOT_FOUND")) + .andExpect(jsonPath("$.errors[0].errorCode").value(HttpStatus.NOT_FOUND.value())) + .andExpect(jsonPath("$.errors[0].timestamp").isNotEmpty()); + } + + private static Stream provideNullOrEmptyValues() { + return Stream.of( + Arguments.of((String) null), + Arguments.of("") + ); + } +} diff --git a/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/AuthorControllerTest.java b/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/author/AuthorControllerTest.java similarity index 99% rename from src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/AuthorControllerTest.java rename to src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/author/AuthorControllerTest.java index b1e4fa0f5..b62a21e49 100644 --- a/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/AuthorControllerTest.java +++ b/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/author/AuthorControllerTest.java @@ -1,4 +1,4 @@ -package com.jornada.dev.eficiente.desafio1.integrations.web.controllers; +package com.jornada.dev.eficiente.desafio1.integrations.web.controllers.author; import static com.jornada.dev.eficiente.desafio1.builders.AuthorBuilder.createAuthorRequest; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; diff --git a/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/CategoryControllerTest.java b/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/category/CategoryControllerTest.java similarity index 99% rename from src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/CategoryControllerTest.java rename to src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/category/CategoryControllerTest.java index 3af26305c..ea2e95584 100644 --- a/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/CategoryControllerTest.java +++ b/src/test/java/com/jornada/dev/eficiente/desafio1/integrations/web/controllers/category/CategoryControllerTest.java @@ -1,4 +1,4 @@ -package com.jornada.dev.eficiente.desafio1.integrations.web.controllers; +package com.jornada.dev.eficiente.desafio1.integrations.web.controllers.category; import static com.jornada.dev.eficiente.desafio1.builders.CategoryBuilder.createCategoryRequest; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;