Search in sources :

Example 11 with MapBindingResult

use of org.springframework.validation.MapBindingResult in project openmrs-core by openmrs.

the class RelationshipValidatorTest method validate_shouldFailIfEndDateIsBeforeStartDate.

/**
 * @throws ParseException
 * @see RelationshipValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailIfEndDateIsBeforeStartDate() throws ParseException {
    Relationship relationship = new Relationship(1);
    relationship.setStartDate(Context.getDateFormat().parse("18/02/2012"));
    relationship.setEndDate(Context.getDateFormat().parse("18/02/2001"));
    Map<String, String> map = new HashMap<>();
    MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName());
    new RelationshipValidator().validate(relationship, errors);
    Assert.assertEquals(true, errors.hasErrors());
}
Also used : HashMap(java.util.HashMap) Relationship(org.openmrs.Relationship) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 12 with MapBindingResult

use of org.springframework.validation.MapBindingResult in project openmrs-core by openmrs.

the class RelationshipValidatorTest method validate_shouldPassIfStartDateIsNotInFuture.

/**
 * @see RelationshipValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassIfStartDateIsNotInFuture() {
    Relationship relationship = new Relationship(1);
    Map<String, String> map = new HashMap<>();
    MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName());
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.YEAR, -1);
    Date lastYear = cal.getTime();
    relationship.setStartDate(lastYear);
    new RelationshipValidator().validate(relationship, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : HashMap(java.util.HashMap) Relationship(org.openmrs.Relationship) Calendar(java.util.Calendar) MapBindingResult(org.springframework.validation.MapBindingResult) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 13 with MapBindingResult

use of org.springframework.validation.MapBindingResult in project openmrs-core by openmrs.

the class RelationshipValidatorTest method validate_shouldPassIfEndDateIsAfterStartDate.

/**
 * @throws ParseException
 * @see RelationshipValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassIfEndDateIsAfterStartDate() throws ParseException {
    Relationship relationship = new Relationship(1);
    relationship.setStartDate(Context.getDateFormat().parse("18/02/2012"));
    relationship.setEndDate(Context.getDateFormat().parse("18/03/2012"));
    Map<String, String> map = new HashMap<>();
    MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName());
    new RelationshipValidator().validate(relationship, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : HashMap(java.util.HashMap) Relationship(org.openmrs.Relationship) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 14 with MapBindingResult

use of org.springframework.validation.MapBindingResult in project openmrs-core by openmrs.

the class RelationshipValidatorTest method validate_shouldPassValidationIfFieldLengthsAreCorrect.

/**
 * @see RelationshipValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfFieldLengthsAreCorrect() {
    Relationship relationship = new Relationship(1);
    relationship.setVoidReason("voidReason");
    Map<String, String> map = new HashMap<>();
    MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName());
    new RelationshipValidator().validate(relationship, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : HashMap(java.util.HashMap) Relationship(org.openmrs.Relationship) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 15 with MapBindingResult

use of org.springframework.validation.MapBindingResult in project yyl_example by Relucent.

the class CustomValidatorExample method main.

public static void main(String[] args) {
    CustomValidatorBean validator = new CustomValidatorBean();
    validator.afterPropertiesSet();
    Sample sample = new Sample();
    Errors errors = new MapBindingResult(new HashMap<>(), Sample.class.getName());
    validator.validate(sample, errors);
    for (ObjectError error : errors.getAllErrors()) {
        System.out.println(error.getDefaultMessage());
    }
}
Also used : Errors(org.springframework.validation.Errors) ObjectError(org.springframework.validation.ObjectError) CustomValidatorBean(org.springframework.validation.beanvalidation.CustomValidatorBean) MapBindingResult(org.springframework.validation.MapBindingResult)

Aggregations

MapBindingResult (org.springframework.validation.MapBindingResult)40 Test (org.junit.Test)22 ObjectError (org.springframework.validation.ObjectError)12 BindingResult (org.springframework.validation.BindingResult)11 HashMap (java.util.HashMap)10 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)9 Test (org.junit.jupiter.api.Test)6 Relationship (org.openmrs.Relationship)6 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)6 Method (java.lang.reflect.Method)5 GuiFragmentRequestBody (org.entando.entando.web.guifragment.model.GuiFragmentRequestBody)5 MethodParameter (org.springframework.core.MethodParameter)5 BindException (org.springframework.validation.BindException)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 ServletException (jakarta.servlet.ServletException)3 RestAccessControl (org.entando.entando.web.common.annotation.RestAccessControl)3 ValidationGenericException (org.entando.entando.web.common.exceptions.ValidationGenericException)3 ResponseEntity (org.springframework.http.ResponseEntity)3 FieldError (org.springframework.validation.FieldError)3 WebExchangeBindException (org.springframework.web.bind.support.WebExchangeBindException)3