Skip to content
Snippets Groups Projects
Commit 8c8fca6e authored by Colin DAMON's avatar Colin DAMON
Browse files

Merge branch '6-add-management-for-bean-validation-in-parameters' into 'master'

Resolve "Add management for bean validation in parameters"

Closes #6

See merge request !6
parents 3e412278 2bb46476
No related branches found
No related tags found
1 merge request!6Resolve "Add management for bean validation in parameters"
Pipeline #30371 passed
......@@ -24,6 +24,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
......@@ -178,6 +179,20 @@ public class PouetErrorHandler extends ResponseEntityExceptionHandler {
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
@Override
protected ResponseEntity<Object> handleBindException(
BindException exception,
HttpHeaders headers,
HttpStatus status,
WebRequest request
) {
List<PouetFieldError> fieldErrors = exception.getFieldErrors().stream().map(toPouetFieldError()).collect(Collectors.toList());
PouetError error = new PouetError(BAD_REQUEST_KEY, getMessage(BAD_REQUEST_KEY, null), fieldErrors);
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler
public ResponseEntity<PouetError> handleBeanValidationError(ConstraintViolationException exception) {
logger.debug("Bean validation error {}", exception.getMessage(), exception);
......
......@@ -3,6 +3,8 @@ package com.ippon.pouet.common.infrastructure.primary;
public final class ValidationMessage {
public static final String MANDATORY = "user.mandatory";
public static final String WRONG_FORMAT = "user.wrong-format";
public static final String VALUE_TOO_LOW = "user.too-low";
public static final String VALUE_TOO_HIGH = "user.too-high";
private ValidationMessage() {}
}
......@@ -29,6 +29,8 @@ pouet.error.status-exception=Une erreur {{ status }} est survenue lors du traite
pouet.error.user.bad-request=Les données que vous avez saisies sont incorrectes.
pouet.error.user.mandatory=Le champ est obligatoire.
pouet.error.user.wrong-format=Le format n'est pas correct, il doit respecter "{{ regexp }}".
pouet.error.user.too-low=La valeur que vous avez entrée est trop petite, le minimum autorisé est {{ value }}.
pouet.error.user.too-high=La valeur que vous avez entrée est trop grande, le maximum autorisé est {{ value }}.
pouet.error.user.authentication-not-authenticated=Vous devez être authentifié pour acceder à cette ressource.
pouet.error.user.access-denied=Vous n'avez pas les droits suffisants pour acceder à cette ressource.
pouet.error.user.e-mail-already-used=Cette adresse email est déjà utilisée dans pouet.
......
......@@ -29,6 +29,8 @@ pouet.error.status-exception=An error {{ status }} occured while handling your r
pouet.error.user.bad-request=The values you entered are incorrects.
pouet.error.user.mandatory=The field is mandatory.
pouet.error.user.wrong-format=The format is incorrect, it has to match "{{ regexp }}".
pouet.error.user.too-low=The value you entered is too low, minimum autorized is {{ value }}.
pouet.error.user.too-high=The value you entered is too high, maximum authorized is {{ value }}.
pouet.error.user.authentication-not-authenticated=You must be authenticated to access this resource.
pouet.error.user.access-denied=You don't have sufficient rights to access this resource.
pouet.error.user.e-mail-already-used=This email address is already used in the pouet.
......
......@@ -57,6 +57,9 @@ class ErrorsResource {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
@GetMapping
public void queryStringWithRangedValue(@Validated QueryParameter parameter) {}
@GetMapping("/{complicated}")
public void complicatedArg(
@Validated @Pattern(message = ValidationMessage.WRONG_FORMAT, regexp = "complicated") @PathVariable("complicated") String complicated
......
......@@ -111,6 +111,34 @@ class PouetErrorHandlerIntTest {
.contains("Le format n'est pas correct, il doit respecter \\\"complicated\\\".");
}
@Test
public void shouldMapMinValueQueryStringBeanValidationErrors() throws Exception {
String response = mockMvc
.perform(get(errorEndpoint("?parameter=1")).header(HttpHeaders.ACCEPT_LANGUAGE, FRANCE_TAG))
.andExpect(status().isBadRequest())
.andReturn()
.getResponse()
.getContentAsString(UTF_8);
assertThat(response)
.contains("Les données que vous avez saisies sont incorrectes.")
.contains("La valeur que vous avez entrée est trop petite, le minimum autorisé est 42.");
}
@Test
public void shouldMapMaxValueQueryStringBeanValidationErrors() throws Exception {
String response = mockMvc
.perform(get(errorEndpoint("?parameter=100")).header(HttpHeaders.ACCEPT_LANGUAGE, FRANCE_TAG))
.andExpect(status().isBadRequest())
.andReturn()
.getResponse()
.getContentAsString(UTF_8);
assertThat(response)
.contains("Les données que vous avez saisies sont incorrectes.")
.contains("La valeur que vous avez entrée est trop grande, le maximum autorisé est 42.");
}
@Test
public void shouldMapBodyBeanValidationErrors() throws Exception {
String response = mockMvc
......
package com.ippon.pouet.common.infrastructure.primary;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
class QueryParameter {
private int parameter;
@Min(message = ValidationMessage.VALUE_TOO_LOW, value = 42)
@Max(message = ValidationMessage.VALUE_TOO_HIGH, value = 42)
public int getParameter() {
return parameter;
}
public void setParameter(int parameter) {
this.parameter = parameter;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment