Search in sources :

Example 6 with MapBindingResult

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

the class GuiFragmentValidatorTest method validateInvalidFragmentCode_1.

@Test
public void validateInvalidFragmentCode_1() throws Exception {
    String code = "very_long";
    for (int i = 0; i < 10; i++) {
        code += code;
    }
    when(this.guiFragmentManager.getGuiFragment(code)).thenReturn(null);
    GuiFragmentRequestBody request = new GuiFragmentRequestBody(code, "<h1>prova</h1>");
    MapBindingResult bindingResult = new MapBindingResult(new HashMap<Object, Object>(), "fragment");
    validator.validate(request, bindingResult);
    Assert.assertTrue(bindingResult.hasErrors());
    Assert.assertEquals(1, bindingResult.getErrorCount());
}
Also used : GuiFragmentRequestBody(org.entando.entando.web.guifragment.model.GuiFragmentRequestBody) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test)

Example 7 with MapBindingResult

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

the class GuiFragmentValidatorTest method validateExistingFragment.

@Test
public void validateExistingFragment() throws Exception {
    GuiFragment existing = new GuiFragment();
    existing.setCode("existing");
    when(this.guiFragmentManager.getGuiFragment("existing")).thenReturn(existing);
    GuiFragmentRequestBody request = new GuiFragmentRequestBody("existing", "<h1>code</h1>");
    MapBindingResult bindingResult = new MapBindingResult(new HashMap<Object, Object>(), "fragment");
    validator.validate(request, bindingResult);
    Assert.assertTrue(bindingResult.hasErrors());
    Assert.assertEquals(1, bindingResult.getErrorCount());
}
Also used : GuiFragment(org.entando.entando.aps.system.services.guifragment.GuiFragment) GuiFragmentRequestBody(org.entando.entando.web.guifragment.model.GuiFragmentRequestBody) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test)

Example 8 with MapBindingResult

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

the class MessageSourceServiceTest method getMessage_shouldReturnTheLastErrorCodeIfnoLocalizationIsFound.

/**
 * MessageSourceServiceImpl.getMessage()should return last error code if no localization found
 *
 * @see MessageSourceServiceImpl#getMessage(MessageSourceResolvable resolvable, Locale locale)
 */
@Test
public void getMessage_shouldReturnTheLastErrorCodeIfnoLocalizationIsFound() {
    MapBindingResult errors = new MapBindingResult(new HashMap<String, Object>(), "request");
    errors.rejectValue("myField", "myErrorCode");
    MessageSourceResolvable fieldError = errors.getFieldError("myField");
    Assert.assertEquals(3, fieldError.getCodes().length);
    Assert.assertEquals("myErrorCode.request.myField", fieldError.getCodes()[0]);
    Assert.assertEquals("myErrorCode.myField", fieldError.getCodes()[1]);
    Assert.assertEquals("myErrorCode", fieldError.getCodes()[2]);
    Assert.assertEquals("myErrorCode", Context.getMessageSourceService().getMessage(fieldError, Context.getLocale()));
}
Also used : MessageSourceResolvable(org.springframework.context.MessageSourceResolvable) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 9 with MapBindingResult

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

the class PersonNameValidatorTest method validate_shouldReportErrorsOnCorrectFieldNames.

/**
 * @see PersonNameValidator#validate(Object, Errors)
 */
@Test
public void validate_shouldReportErrorsOnCorrectFieldNames() {
    PersonName personName = new PersonName("", "reb", "feb");
    MapBindingResult errors = new MapBindingResult(new HashMap<String, Object>(), "personName");
    validator.validate(personName, errors);
    assertThat(errors, hasFieldErrors("givenName"));
}
Also used : PersonName(org.openmrs.PersonName) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 10 with MapBindingResult

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

the class PersonNameValidatorTest method validatePersonName_shouldReportErrorsWithNonStandardPrefixWhenCalledInHistoricWay.

/**
 * @see PersonNameValidator#validatePersonName(PersonName, Errors, Boolean, Boolean)
 */
@Test
public void validatePersonName_shouldReportErrorsWithNonStandardPrefixWhenCalledInHistoricWay() {
    PersonName personName = new PersonName("", "reb", "feb");
    MapBindingResult errors = new MapBindingResult(new HashMap<String, Object>(), "personName");
    validator.validatePersonName(personName, errors, true, false);
    assertThat(errors, hasFieldErrors("names[0]." + "givenName"));
}
Also used : PersonName(org.openmrs.PersonName) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

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