Search in sources :

Example 96 with BindingResult

use of org.springframework.validation.BindingResult in project molgenis by molgenis.

the class PackageWizardPageTest method testHandleRequest.

@Test
public void testHandleRequest() throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    BindingResult bindingResult = mock(BindingResult.class);
    ImportWizard wizard = mock(ImportWizard.class);
    when(wizard.getEntityImportOption()).thenReturn(ADD.toString().toLowerCase());
    Map<String, Collection<String>> result = new HashMap<>();
    result.put("test", null);
    when(wizard.getFieldsAvailable()).thenReturn(result);
    when(wizard.getFieldsDetected()).thenReturn(result);
    when(wizard.getFieldsRequired()).thenReturn(result);
    when(wizard.getFieldsUnknown()).thenReturn(result);
    when(request.getParameter("selectedPackage")).thenReturn("pack1");
    packageWizardPage.handleRequest(request, bindingResult, wizard);
    ArgumentCaptor<HashMap> argumentCaptor1 = ArgumentCaptor.forClass(HashMap.class);
    verify(wizard).setFieldsAvailable(argumentCaptor1.capture());
    assertTrue(argumentCaptor1.getValue().keySet().contains("pack1_test"));
    ArgumentCaptor<HashMap> argumentCaptor2 = ArgumentCaptor.forClass(HashMap.class);
    verify(wizard).setFieldsAvailable(argumentCaptor2.capture());
    assertTrue(argumentCaptor2.getValue().keySet().contains("pack1_test"));
    ArgumentCaptor<HashMap> argumentCaptor3 = ArgumentCaptor.forClass(HashMap.class);
    verify(wizard).setFieldsAvailable(argumentCaptor3.capture());
    assertTrue(argumentCaptor3.getValue().keySet().contains("pack1_test"));
    ArgumentCaptor<HashMap> argumentCaptor4 = ArgumentCaptor.forClass(HashMap.class);
    verify(wizard).setFieldsAvailable(argumentCaptor4.capture());
    assertTrue(argumentCaptor4.getValue().keySet().contains("pack1_test"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BindingResult(org.springframework.validation.BindingResult) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Collection(java.util.Collection) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 97 with BindingResult

use of org.springframework.validation.BindingResult in project hello-world by haoziapple.

the class ExceptionTranslator method handleMethodArgumentNotValid.

@Override
public ResponseEntity<Problem> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, @Nonnull NativeWebRequest request) {
    BindingResult result = ex.getBindingResult();
    List<FieldErrorVM> fieldErrors = result.getFieldErrors().stream().map(f -> new FieldErrorVM(f.getObjectName(), f.getField(), f.getCode())).collect(Collectors.toList());
    Problem problem = Problem.builder().withType(ErrorConstants.CONSTRAINT_VIOLATION_TYPE).withTitle("Method argument not valid").withStatus(defaultConstraintViolationStatus()).with("message", ErrorConstants.ERR_VALIDATION).with("fieldErrors", fieldErrors).build();
    return create(ex, problem, request);
}
Also used : ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) BindingResult(org.springframework.validation.BindingResult) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ProblemHandling(org.zalando.problem.spring.web.advice.ProblemHandling) Collectors(java.util.stream.Collectors) DefaultProblem(org.zalando.problem.DefaultProblem) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) Problem(org.zalando.problem.Problem) HeaderUtil(com.haozi.app.web.rest.util.HeaderUtil) ProblemBuilder(org.zalando.problem.ProblemBuilder) Status(org.zalando.problem.Status) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseEntity(org.springframework.http.ResponseEntity) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BindingResult(org.springframework.validation.BindingResult) DefaultProblem(org.zalando.problem.DefaultProblem) Problem(org.zalando.problem.Problem) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem)

Example 98 with BindingResult

use of org.springframework.validation.BindingResult in project jhipster-registry by jhipster.

the class ExceptionTranslator method processValidationError.

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorVM processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    ErrorVM dto = new ErrorVM(ErrorConstants.ERR_VALIDATION);
    for (FieldError fieldError : fieldErrors) {
        dto.add(fieldError.getObjectName(), fieldError.getField(), fieldError.getCode());
    }
    return dto;
}
Also used : BindingResult(org.springframework.validation.BindingResult) FieldError(org.springframework.validation.FieldError)

Example 99 with BindingResult

use of org.springframework.validation.BindingResult in project mots by motech-implementations.

the class ModuleController method validateCourseForRelease.

private void validateCourseForRelease(Course course) {
    BindingResult bindingResult = new BeanPropertyBindingResult(course, COURSE);
    validator.validate(course, bindingResult, CourseReleaseCheck.class);
    checkBindingResult(bindingResult);
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult)

Example 100 with BindingResult

use of org.springframework.validation.BindingResult in project alien4cloud by alien4cloud.

the class RestTechnicalExceptionHandler method processValidationError.

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public RestResponse<FieldErrorDTO[]> processValidationError(MethodArgumentNotValidException e) {
    BindingResult result = e.getBindingResult();
    List<FieldErrorDTO> errors = Lists.newArrayList();
    for (FieldError fieldError : result.getFieldErrors()) {
        errors.add(new FieldErrorDTO(fieldError.getField(), fieldError.getCode()));
    }
    return RestResponseBuilder.<FieldErrorDTO[]>builder().data(errors.toArray(new FieldErrorDTO[errors.size()])).error(RestErrorBuilder.builder(RestErrorCode.ILLEGAL_PARAMETER).message("Method argument is invalid " + e.getMessage()).build()).build();
}
Also used : BindingResult(org.springframework.validation.BindingResult) FieldError(org.springframework.validation.FieldError) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

BindingResult (org.springframework.validation.BindingResult)143 Test (org.junit.jupiter.api.Test)29 Test (org.junit.Test)28 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)24 List (java.util.List)23 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)23 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)21 ResponseEntity (org.springframework.http.ResponseEntity)20 Collectors (java.util.stream.Collectors)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)18 FieldError (org.springframework.validation.FieldError)18 ObjectError (org.springframework.validation.ObjectError)17 Model (org.springframework.ui.Model)15 ControllerAdvice (org.springframework.web.bind.annotation.ControllerAdvice)14 HashMap (java.util.HashMap)13 Nonnull (javax.annotation.Nonnull)13 Nullable (javax.annotation.Nullable)13 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)13 DefaultProblem (org.zalando.problem.DefaultProblem)13 Problem (org.zalando.problem.Problem)13