add integration test to create new user controller with bad request when name is empty
This commit is contained in:
parent
e852dbea7b
commit
5a9c496a04
2 changed files with 44 additions and 25 deletions
|
|
@ -14,6 +14,8 @@ import models.exceptions.ResourceNotFoundException;
|
|||
import models.exceptions.StandardError;
|
||||
|
||||
import static java.time.LocalDateTime.now;
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
|
||||
import static org.springframework.http.HttpStatus.CONFLICT;
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
|
||||
|
|
@ -35,9 +37,15 @@ public class ControllerExceptionHandler {
|
|||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
ResponseEntity<StandardError> handleMethodArgumentNotValidException(final MethodArgumentNotValidException ex,
|
||||
final HttpServletRequest request) {
|
||||
var error = ValidationException.builder().timestamp(now()).status(NOT_FOUND.value())
|
||||
.error("Validation Exception").message("Exception in validation attributes")
|
||||
.path(request.getRequestURI()).errors(new ArrayList<>()).build();
|
||||
|
||||
var error = ValidationException.builder()
|
||||
.timestamp(now())
|
||||
.status(BAD_REQUEST.value())
|
||||
.error("Validation Exception")
|
||||
.message("Exception in validation attributes")
|
||||
.path(request.getRequestURI())
|
||||
.errors(new ArrayList<>())
|
||||
.build();
|
||||
|
||||
for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
|
||||
error.addError(fieldError.getField(), fieldError.getDefaultMessage());
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
|
|||
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 static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
|
||||
import static org.springframework.http.HttpStatus.CONFLICT;
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
|
||||
|
|
@ -27,7 +29,7 @@ import models.requests.CreateUserRequest;
|
|||
@ActiveProfiles("test")
|
||||
public class UserControllerImpTest {
|
||||
|
||||
private static final String VALID_EMAIL = "k42kak2ok@mail.com";
|
||||
private static final String VALID_EMAIL = "rayanvix@gmail.com";
|
||||
|
||||
private static final String BASE_URI = "/api/users";
|
||||
|
||||
|
|
@ -52,12 +54,10 @@ public class UserControllerImpTest {
|
|||
|
||||
@Test
|
||||
void testFindByIdWithNotFoundException() throws Exception {
|
||||
mockMvc.perform(get(BASE_URI + "/{id}", "123"))
|
||||
.andExpect(status().isNotFound())
|
||||
mockMvc.perform(get(BASE_URI + "/{id}", "123")).andExpect(status().isNotFound())
|
||||
.andExpect(jsonPath("$.message").value("Object not found! Id: 123, Type: UserResponse"))
|
||||
.andExpect(jsonPath("$.error").value(NOT_FOUND.getReasonPhrase()))
|
||||
.andExpect(jsonPath("$.path").value(BASE_URI + "/123"))
|
||||
.andExpect(jsonPath("$.status").value(NOT_FOUND.value()))
|
||||
.andExpect(jsonPath("$.path").value(BASE_URI + "/123")).andExpect(jsonPath("$.status").value(NOT_FOUND.value()))
|
||||
.andExpect(jsonPath("$.timestamp").isNotEmpty());
|
||||
}
|
||||
|
||||
|
|
@ -68,10 +68,8 @@ public class UserControllerImpTest {
|
|||
|
||||
userRepository.saveAll(List.of(entity1, entity2));
|
||||
|
||||
mockMvc.perform(get(BASE_URI)).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$").isArray())
|
||||
.andExpect(jsonPath("$[0]").isNotEmpty())
|
||||
.andExpect(jsonPath("$[1]").isNotEmpty())
|
||||
mockMvc.perform(get(BASE_URI)).andExpect(status().isOk()).andExpect(jsonPath("$").isArray())
|
||||
.andExpect(jsonPath("$[0]").isNotEmpty()).andExpect(jsonPath("$[1]").isNotEmpty())
|
||||
.andExpect(jsonPath("$[0].profiles").isArray());
|
||||
|
||||
userRepository.deleteAll(List.of(entity1, entity2));
|
||||
|
|
@ -88,14 +86,6 @@ public class UserControllerImpTest {
|
|||
userRepository.deleteByEmail(VALID_EMAIL);
|
||||
}
|
||||
|
||||
private String toJson(final Object object) throws Exception {
|
||||
try {
|
||||
return new ObjectMapper().writeValueAsString(object);
|
||||
} catch (final Exception e) {
|
||||
throw new Exception("Error to convert object to json", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSaveUserWithConflict() throws Exception {
|
||||
|
||||
|
|
@ -108,12 +98,33 @@ public class UserControllerImpTest {
|
|||
mockMvc.perform(post(BASE_URI).contentType(APPLICATION_JSON).content(toJson(request)))
|
||||
.andExpect(status().isConflict())
|
||||
.andExpect(jsonPath("$.message").value("Email [" + VALID_EMAIL + "] already exists"))
|
||||
.andExpect(jsonPath("$.error").value(CONFLICT.getReasonPhrase()))
|
||||
.andExpect(jsonPath("$.path").value(BASE_URI))
|
||||
.andExpect(jsonPath("$.status").value(CONFLICT.value()))
|
||||
.andExpect(jsonPath("$.timestamp").isNotEmpty());
|
||||
.andExpect(jsonPath("$.error").value(CONFLICT.getReasonPhrase())).andExpect(jsonPath("$.path").value(BASE_URI))
|
||||
.andExpect(jsonPath("$.status").value(CONFLICT.value())).andExpect(jsonPath("$.timestamp").isNotEmpty());
|
||||
|
||||
userRepository.deleteById(entity.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSaveUserWithNameEmptyThenThrowBadRequest() throws Exception {
|
||||
final var request = generateMock(CreateUserRequest.class).withName("").withEmail(VALID_EMAIL);
|
||||
|
||||
mockMvc.perform(post(BASE_URI).contentType(APPLICATION_JSON).content(toJson(request)))
|
||||
.andExpect(status().isBadRequest())
|
||||
.andExpect(jsonPath("$.message").value("Exception in validation attributes"))
|
||||
.andExpect(jsonPath("$.error").value("Validation Exception"))
|
||||
.andExpect(jsonPath("$.path").value(BASE_URI))
|
||||
.andExpect(jsonPath("$.status").value(BAD_REQUEST.value()))
|
||||
.andExpect(jsonPath("$.timestamp").isNotEmpty())
|
||||
.andExpect(jsonPath("$.errors[?(@.fieldName=='name' && @.message=='Name must contain between 3 and 50 characters')]").exists())
|
||||
.andExpect(jsonPath("$.errors[?(@.fieldName=='name' && @.message=='Name cannot be empty')]").exists());
|
||||
}
|
||||
|
||||
private String toJson(final Object object) throws Exception {
|
||||
try {
|
||||
return new ObjectMapper().writeValueAsString(object);
|
||||
} catch (final Exception e) {
|
||||
throw new Exception("Error to convert object to json", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue