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"));
}
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);
}
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;
}
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);
}
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();
}
Aggregations