Search in sources :

Example 26 with MapBindingResult

use of org.springframework.validation.MapBindingResult in project spring-cloud-open-service-broker by spring-cloud.

the class BaseControllerTest method methodArgumentNotValidExceptionGivesExpectedStatus.

@Test
public void methodArgumentNotValidExceptionGivesExpectedStatus() throws NoSuchMethodException {
    BindingResult bindingResult = new MapBindingResult(new HashMap<>(), "objectName");
    bindingResult.addError(new FieldError("objectName", "field1", "message"));
    bindingResult.addError(new FieldError("objectName", "field2", "message"));
    Method method = this.getClass().getMethod("setUp", (Class<?>[]) null);
    MethodParameter parameter = new MethodParameter(method, -1);
    MethodArgumentNotValidException exception = new MethodArgumentNotValidException(parameter, bindingResult);
    ResponseEntity<ErrorMessage> responseEntity = controller.handleException(exception);
    assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY);
    assertThat(responseEntity.getBody().getError()).isNull();
    assertThat(responseEntity.getBody().getMessage()).contains("field1");
    assertThat(responseEntity.getBody().getMessage()).contains("field2");
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) FieldError(org.springframework.validation.FieldError) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) ErrorMessage(org.springframework.cloud.servicebroker.model.error.ErrorMessage) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) Test(org.junit.Test)

Example 27 with MapBindingResult

use of org.springframework.validation.MapBindingResult in project spring-cloud-open-service-broker by spring-cloud.

the class BaseControllerTest method webExchangeBindExceptionGivesExpectedStatus.

@Test
public void webExchangeBindExceptionGivesExpectedStatus() throws NoSuchMethodException {
    BindingResult bindingResult = new MapBindingResult(new HashMap<>(), "objectName");
    bindingResult.addError(new FieldError("objectName", "field1", "message"));
    bindingResult.addError(new FieldError("objectName", "field2", "message"));
    Method method = this.getClass().getMethod("setUp", (Class<?>[]) null);
    MethodParameter parameter = new MethodParameter(method, -1);
    WebExchangeBindException exception = new WebExchangeBindException(parameter, bindingResult);
    ResponseEntity<ErrorMessage> responseEntity = controller.handleException(exception);
    assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY);
    assertThat(responseEntity.getBody().getError()).isNull();
    assertThat(responseEntity.getBody().getMessage()).contains("field1");
    assertThat(responseEntity.getBody().getMessage()).contains("field2");
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) FieldError(org.springframework.validation.FieldError) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) ErrorMessage(org.springframework.cloud.servicebroker.model.error.ErrorMessage) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) Test(org.junit.Test)

Example 28 with MapBindingResult

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

the class DataObjectModelController method deleteGroup.

@RestAccessControl(permission = Permission.SUPERUSER)
@RequestMapping(value = "/{dataModelId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<RestResponse> deleteGroup(@PathVariable String dataModelId) throws ApsSystemException {
    logger.info("deleting data object model -> {}", dataModelId);
    MapBindingResult bindingResult = new MapBindingResult(new HashMap<>(), "dataModels");
    Long dataId = this.getDataObjectModelValidator().checkValidModelId(dataModelId, bindingResult);
    if (null == dataId) {
        throw new ValidationGenericException(bindingResult);
    }
    this.getDataObjectModelService().removeDataObjectModel(Long.parseLong(dataModelId));
    Map<String, String> payload = new HashMap<>();
    payload.put("modelId", dataModelId);
    return new ResponseEntity<>(new RestResponse(payload), HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HashMap(java.util.HashMap) RestResponse(org.entando.entando.web.common.model.RestResponse) MapBindingResult(org.springframework.validation.MapBindingResult) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) RestAccessControl(org.entando.entando.web.common.annotation.RestAccessControl) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 29 with MapBindingResult

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

the class DefaultErrorAttributesTests method withBindingErrors.

@Test
void withBindingErrors() {
    BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
    bindingResult.addError(new ObjectError("c", "d"));
    Exception ex = new BindException(bindingResult);
    testBindingResult(bindingResult, ex, ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS));
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) ObjectError(org.springframework.validation.ObjectError) BindException(org.springframework.validation.BindException) MapBindingResult(org.springframework.validation.MapBindingResult) ServletException(jakarta.servlet.ServletException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) BindException(org.springframework.validation.BindException) Test(org.junit.jupiter.api.Test)

Example 30 with MapBindingResult

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

the class DefaultErrorAttributesTests method withMethodArgumentNotValidExceptionBindingErrors.

@Test
void withMethodArgumentNotValidExceptionBindingErrors() {
    Method method = ReflectionUtils.findMethod(String.class, "substring", int.class);
    MethodParameter parameter = new MethodParameter(method, 0);
    BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
    bindingResult.addError(new ObjectError("c", "d"));
    Exception ex = new MethodArgumentNotValidException(parameter, bindingResult);
    testBindingResult(bindingResult, ex, ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS));
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) ObjectError(org.springframework.validation.ObjectError) MapBindingResult(org.springframework.validation.MapBindingResult) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ServletException(jakarta.servlet.ServletException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) BindException(org.springframework.validation.BindException) Test(org.junit.jupiter.api.Test)

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