Search in sources :

Example 1 with BindingResult

use of org.springframework.validation.BindingResult in project ORCID-Source by ORCID.

the class ClaimControllerTest method testResendClaimEmail.

@Test
public void testResendClaimEmail() {
    BindingResult bindingResult = mock(BindingResult.class);
    when(bindingResult.hasErrors()).thenReturn(false);
    when(emailManager.emailExists("billie@holiday.com")).thenReturn(true);
    when(emailManager.findOrcidIdByEmail("billie@holiday.com")).thenReturn("0000-0000-0000-0000");
    when(profileEntityCacheManager.retrieve(Mockito.anyString())).thenReturn(getProfileEntityToTestClaimResend(false));
    EmailRequest emailRequest = new EmailRequest();
    emailRequest.setEmail("billie@holiday.com");
    emailRequest = claimController.resendClaimEmail(emailRequest);
    assertNotNull(emailRequest);
    assertNotNull(emailRequest.getSuccessMessage());
}
Also used : BindingResult(org.springframework.validation.BindingResult) EmailRequest(org.orcid.pojo.EmailRequest) Test(org.junit.Test)

Example 2 with BindingResult

use of org.springframework.validation.BindingResult in project spring-framework by spring-projects.

the class ModelResultMatchers method attributeHasNoErrors.

/**
	 * Assert the given model attribute(s) do not have errors.
	 */
public ResultMatcher attributeHasNoErrors(final String... names) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult mvcResult) throws Exception {
            ModelAndView mav = getModelAndView(mvcResult);
            for (String name : names) {
                BindingResult result = getBindingResult(mav, name);
                assertTrue("No errors for attribute [" + name + "]", !result.hasErrors());
            }
        }
    };
}
Also used : BindingResult(org.springframework.validation.BindingResult) ModelAndView(org.springframework.web.servlet.ModelAndView) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 3 with BindingResult

use of org.springframework.validation.BindingResult in project spring-framework by spring-projects.

the class ModelResultMatchers method attributeHasFieldErrorCode.

/**
	 * Assert a field error code for a model attribute using exact String match.
	 * @since 4.1
	 */
public ResultMatcher attributeHasFieldErrorCode(final String name, final String fieldName, final String error) {
    return new ResultMatcher() {

        public void match(MvcResult mvcResult) throws Exception {
            ModelAndView mav = getModelAndView(mvcResult);
            BindingResult result = getBindingResult(mav, name);
            assertTrue("No errors for attribute: [" + name + "]", result.hasErrors());
            boolean hasFieldErrors = result.hasFieldErrors(fieldName);
            assertTrue("No errors for field: [" + fieldName + "] of attribute [" + name + "]", hasFieldErrors);
            String code = result.getFieldError(fieldName).getCode();
            assertTrue("Expected error code '" + error + "' but got '" + code + "'", code.equals(error));
        }
    };
}
Also used : BindingResult(org.springframework.validation.BindingResult) ModelAndView(org.springframework.web.servlet.ModelAndView) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 4 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class DefaultErrorAttributes method addErrorMessage.

private void addErrorMessage(Map<String, Object> errorAttributes, Throwable error) {
    BindingResult result = extractBindingResult(error);
    if (result == null) {
        errorAttributes.put("message", error.getMessage());
        return;
    }
    if (result.getErrorCount() > 0) {
        errorAttributes.put("errors", result.getAllErrors());
        errorAttributes.put("message", "Validation failed for object='" + result.getObjectName() + "'. Error count: " + result.getErrorCount());
    } else {
        errorAttributes.put("message", "No errors");
    }
}
Also used : BindingResult(org.springframework.validation.BindingResult)

Example 5 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class YamlConfigurationFactory method validate.

private void validate() throws BindException {
    BindingResult errors = new BeanPropertyBindingResult(this.configuration, "configuration");
    this.validator.validate(this.configuration, errors);
    if (errors.hasErrors()) {
        logger.error("YAML configuration failed validation");
        for (ObjectError error : errors.getAllErrors()) {
            logger.error(getErrorMessage(error));
        }
        throw new BindException(errors);
    }
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ObjectError(org.springframework.validation.ObjectError) BindException(org.springframework.validation.BindException)

Aggregations

BindingResult (org.springframework.validation.BindingResult)129 Test (org.junit.jupiter.api.Test)27 Test (org.junit.Test)26 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)23 List (java.util.List)22 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)20 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)20 ResponseEntity (org.springframework.http.ResponseEntity)19 HttpServletRequest (javax.servlet.http.HttpServletRequest)18 Collectors (java.util.stream.Collectors)17 FieldError (org.springframework.validation.FieldError)16 Model (org.springframework.ui.Model)15 ObjectError (org.springframework.validation.ObjectError)14 ControllerAdvice (org.springframework.web.bind.annotation.ControllerAdvice)14 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 ProblemBuilder (org.zalando.problem.ProblemBuilder)13