org.springframework.validation.MapBindingResult Java Examples
The following examples show how to use
org.springframework.validation.MapBindingResult.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServiceBrokerWebFluxExceptionHandlerTest.java From spring-cloud-open-service-broker with Apache License 2.0 | 6 votes |
@Test void webExchangeBindException() 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); ErrorMessage errorMessage = exceptionHandler.handleException(exception); assertThat(errorMessage.getError()).isNull(); assertThat(errorMessage.getMessage()).contains("field1"); assertThat(errorMessage.getMessage()).contains("field2"); }
Example #2
Source File: ServiceBrokerWebMvcExceptionHandlerTest.java From spring-cloud-open-service-broker with Apache License 2.0 | 6 votes |
@Test void methodArgumentNotValidException() 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); ErrorMessage errorMessage = exceptionHandler.handleException(exception); assertThat(errorMessage.getError()).isNull(); assertThat(errorMessage.getMessage()).contains("field1"); assertThat(errorMessage.getMessage()).contains("field2"); }
Example #3
Source File: DataObjectModelController.java From entando-core with GNU Lesser General Public License v3.0 | 6 votes |
@RestAccessControl(permission = Permission.SUPERUSER) @RequestMapping(value = "/{dataModelId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<SimpleRestResponse<DataModelDto>> getDataObjectModel(@PathVariable String dataModelId) { logger.debug("Requested data object model -> {}", dataModelId); MapBindingResult bindingResult = new MapBindingResult(new HashMap<>(), "dataModels"); int result = this.getDataObjectModelValidator().checkModelId(dataModelId, bindingResult); if (bindingResult.hasErrors()) { if (404 == result) { throw new ResourceNotFoundException(DataObjectModelValidator.ERRCODE_DATAOBJECTMODEL_DOES_NOT_EXIST, "dataObjectModel", dataModelId); } else { throw new ValidationGenericException(bindingResult); } } DataModelDto dataModelDto = this.getDataObjectModelService().getDataObjectModel(Long.parseLong(dataModelId)); return new ResponseEntity<>(new SimpleRestResponse<>(dataModelDto), HttpStatus.OK); }
Example #4
Source File: ProblemExceptionResponseGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@Test void testCreateExceptionResponseInputInvalidObjectErrorCoded() { String errorCode = "MYERR01"; String errorMessage = "My error message"; CodedRuntimeException wrappedException = mock(CodedRuntimeException.class); when(wrappedException.getErrorCode()).thenReturn(errorCode); when(wrappedException.getLocalizedMessage()).thenReturn(errorMessage); ObjectError objectError = new ObjectError("MyChildObject0", "MyDefaultMessage0"); objectError.wrap(wrappedException); BindingResult bindingResult = new MapBindingResult(emptyMap(), "MyObject"); bindingResult.addError(objectError); BindException bindException = new BindException(bindingResult); HttpStatus httpStatus = HttpStatus.BAD_REQUEST; ResponseEntity<Problem> problemResponse = problemExceptionResponseGenerator.createExceptionResponse(bindException, httpStatus, false); assertEquals(httpStatus, problemResponse.getStatusCode()); Problem problem = problemResponse.getBody(); assertTrue(problem.getType().toString().endsWith("/input-invalid")); assertEquals( singletonList(builder().setErrorCode(errorCode).setDetail(errorMessage).build()), problem.getErrors()); }
Example #5
Source File: GuiFragmentValidatorTest.java From entando-core with GNU Lesser General Public License v3.0 | 5 votes |
@Test public void validateExistingAndInvalidFragment() throws Exception { GuiFragment existing = new GuiFragment(); existing.setCode("existing"); when(this.guiFragmentManager.getGuiFragment("existing")).thenReturn(existing); GuiFragmentRequestBody request = new GuiFragmentRequestBody("existing", ""); MapBindingResult bindingResult = new MapBindingResult(new HashMap<Object, Object>(), "fragment"); validator.validate(request, bindingResult); Assert.assertTrue(bindingResult.hasErrors()); Assert.assertEquals(2, bindingResult.getErrorCount()); }
Example #6
Source File: ProblemExceptionResponseGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 5 votes |
@Test void testCreateExceptionResponseInputInvalidObjectErrorConstraintViolation() { String errorMessage = "My error message"; ConstraintViolation constraintViolation = mock(ConstraintViolation.class); String code = "MYCODE"; String[] arguments = {"arg0"}; ObjectError objectError = new ObjectError("MyChildObject0", new String[] {code}, arguments, "MyDefaultMessage0"); objectError.wrap(constraintViolation); BindingResult bindingResult = new MapBindingResult(emptyMap(), "MyObject"); bindingResult.addError(objectError); BindException bindException = new BindException(bindingResult); HttpStatus httpStatus = HttpStatus.BAD_REQUEST; when(contextMessageSource.getMessage(code, arguments)).thenReturn(errorMessage); ResponseEntity<Problem> problemResponse = problemExceptionResponseGenerator.createExceptionResponse(bindException, httpStatus, false); assertEquals(httpStatus, problemResponse.getStatusCode()); Problem problem = problemResponse.getBody(); assertTrue(problem.getType().toString().endsWith("/input-invalid")); assertEquals( singletonList(builder().setErrorCode(code).setDetail(errorMessage).build()), problem.getErrors()); }
Example #7
Source File: DatabaseConfigServiceTest.java From cloudbreak with Apache License 2.0 | 5 votes |
@Test public void testExistingConnectionFails() { DatabaseConfig existingDatabaseConfig = new DatabaseConfig(); when(repository.findByEnvironmentIdAndName(ENVIRONMENT_CRN, DATABASE_NAME)).thenReturn(Optional.of(existingDatabaseConfig)); doAnswer((Answer) invocation -> { MapBindingResult errors = invocation.getArgument(1, MapBindingResult.class); errors.addError(new ObjectError("failed", ERROR_MESSAGE)); return null; }).when(connectionValidator).validate(any(), any()); String result = underTest.testConnection(DATABASE_NAME, ENVIRONMENT_CRN); assertEquals(ERROR_MESSAGE, result); verify(connectionValidator).validate(eq(existingDatabaseConfig), any()); }
Example #8
Source File: DatabaseConfigServiceTest.java From cloudbreak with Apache License 2.0 | 5 votes |
@Test public void testTestNewConnectionFails() { DatabaseConfig newConfig = new DatabaseConfig(); doAnswer((Answer) invocation -> { MapBindingResult errors = invocation.getArgument(1, MapBindingResult.class); errors.addError(new ObjectError("failed", ERROR_MESSAGE)); return null; }).when(connectionValidator).validate(any(), any()); String result = underTest.testConnection(newConfig); assertEquals(ERROR_MESSAGE, result); verify(connectionValidator).validate(eq(newConfig), any()); }
Example #9
Source File: DatabaseConfigServiceTest.java From cloudbreak with Apache License 2.0 | 5 votes |
@Test public void testRegisterConnectionFailure() { thrown.expect(IllegalArgumentException.class); DatabaseConfig configToRegister = new DatabaseConfig(); configToRegister.setConnectionDriver("org.postgresql.MyCustomDriver"); doAnswer((Answer) invocation -> { MapBindingResult errors = invocation.getArgument(1, MapBindingResult.class); errors.addError(new ObjectError("failed", ERROR_MESSAGE)); return null; }).when(connectionValidator).validate(any(), any()); underTest.register(configToRegister, true); }
Example #10
Source File: DatabaseConfigService.java From cloudbreak with Apache License 2.0 | 5 votes |
public String testConnection(DatabaseConfig config) { MapBindingResult errors = new MapBindingResult(new HashMap(), "database"); connectionValidator.validate(config, errors); if (!errors.hasErrors()) { return DATABASE_TEST_RESULT_SUCCESS; } return errors.getAllErrors().stream() .map(e -> (e instanceof FieldError ? ((FieldError) e).getField() + ": " : "") + e.getDefaultMessage()) .collect(Collectors.joining("; ")); }
Example #11
Source File: DatabaseServerConfigService.java From cloudbreak with Apache License 2.0 | 5 votes |
public String testConnection(DatabaseServerConfig resource) { MapBindingResult errors = new MapBindingResult(new HashMap(), "databaseServer"); connectionValidator.validate(resource, errors); if (!errors.hasErrors()) { return DATABASE_TEST_RESULT_SUCCESS; } return errors.getAllErrors().stream() .map(e -> (e instanceof FieldError ? ((FieldError) e).getField() + ": " : "") + e.getDefaultMessage()) .collect(Collectors.joining("; ")); }
Example #12
Source File: GuiFragmentValidatorTest.java From entando-core with GNU Lesser General Public License v3.0 | 5 votes |
@Test public void validateInvalidFragmentCode_2() throws Exception { String code = "wrong_characters_&_$_123"; 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()); }
Example #13
Source File: GuiFragmentValidatorTest.java From entando-core with GNU Lesser General Public License v3.0 | 5 votes |
@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()); }
Example #14
Source File: GuiFragmentValidatorTest.java From entando-core with GNU Lesser General Public License v3.0 | 5 votes |
@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()); }
Example #15
Source File: GuiFragmentValidatorTest.java From entando-core with GNU Lesser General Public License v3.0 | 5 votes |
@Test public void validateRightFragment() throws Exception { when(this.guiFragmentManager.getGuiFragment("not_existing")).thenReturn(null); GuiFragmentRequestBody request = new GuiFragmentRequestBody("not_existing", "<h1>code</h1>"); MapBindingResult bindingResult = new MapBindingResult(new HashMap<Object, Object>(), "fragment"); validator.validate(request, bindingResult); Assert.assertFalse(bindingResult.hasErrors()); Assert.assertEquals(0, bindingResult.getErrorCount()); }
Example #16
Source File: UserValidatorTest.java From entando-core with GNU Lesser General Public License v3.0 | 5 votes |
@Test public void createErrorSelfDeleteContainsErrorMessage() { MapBindingResult bindingResult = new MapBindingResult(new HashMap<Object, Object>(), "username"); BindingResult bindingSelfDeleteError = UserValidator.createSelfDeleteUserError(bindingResult); assertEquals(1, bindingSelfDeleteError.getErrorCount()); List<ObjectError> errors = bindingSelfDeleteError.getAllErrors(); ObjectError error = errors.get(0); assertEquals(UserValidator.ERRCODE_SELF_DELETE, error.getCode()); assertEquals("user.self.delete.error", error.getDefaultMessage()); }
Example #17
Source File: UserValidator.java From entando-core with GNU Lesser General Public License v3.0 | 5 votes |
public static BindingResult createDeleteAdminError() { Map<String, String> map = new HashMap<>(); map.put("username", "admin"); BindingResult bindingResult = new MapBindingResult(map, "username"); bindingResult.reject(UserValidator.ERRCODE_DELETE_ADMIN, new String[]{}, "user.admin.cant.delete"); return bindingResult; }
Example #18
Source File: DataObjectModelController.java From entando-core with GNU Lesser General Public License v3.0 | 5 votes |
@RestAccessControl(permission = Permission.SUPERUSER) @RequestMapping(value = "/{dataModelId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<SimpleRestResponse<Map>> deleteDataObjectModel(@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 SimpleRestResponse<>(payload), HttpStatus.OK); }
Example #19
Source File: HomeControllerTest.java From springboot-sample-app with Apache License 2.0 | 4 votes |
@Before public void setUp() throws Exception { bindingResult = new MapBindingResult(new HashMap<>(), "insertRecord"); }
Example #20
Source File: ValidatorTest.java From alf.io with GNU General Public License v3.0 | 4 votes |
@BeforeEach public void init() { eventModification = mock(EventModification.class); errors = new MapBindingResult(new HashMap<>(), "test"); }
Example #21
Source File: UserControllerUnitTest.java From entando-core with GNU Lesser General Public License v3.0 | 4 votes |
@Test(expected = ValidationGenericException.class) public void selfDeleteReturnsError() throws ApsSystemException { when(user.getUsername()).thenReturn("test"); MapBindingResult bindingResult = new MapBindingResult(new HashMap<Object, Object>(), "user"); new UserController().deleteUser(user,"test", bindingResult); }
Example #22
Source File: UserControllerUnitTest.java From entando-core with GNU Lesser General Public License v3.0 | 4 votes |
@Test(expected = ValidationGenericException.class) public void deleteAdminReturnsError() throws ApsSystemException { when(user.getUsername()).thenReturn("admin"); MapBindingResult bindingResult = new MapBindingResult(new HashMap<Object, Object>(), "user"); new UserController().deleteUser(user,"admin", bindingResult); }
Example #23
Source File: RegistrationValidatorTest.java From codenjoy with GNU General Public License v3.0 | 4 votes |
private static Errors makeErrors() { return new MapBindingResult(new HashMap<>(), "dummy"); }
Example #24
Source File: ProblemExceptionResponseGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 4 votes |
@Test void testCreateExceptionResponseInputInvalidFieldErrorCoded() { String errorCode = "MYERR01"; String errorMessage = "My error message"; CodedRuntimeException wrappedException = mock(CodedRuntimeException.class); when(wrappedException.getErrorCode()).thenReturn(errorCode); when(wrappedException.getLocalizedMessage()).thenReturn(errorMessage); String field = "MyField"; String value = "invalidValue"; FieldError fieldError = new FieldError( "MyObject", field, value, true, new String[] {errorCode}, new String[] {"arg0"}, "defaultMessage"); fieldError.wrap(wrappedException); BindingResult bindingResult = new MapBindingResult(emptyMap(), "MyObject"); bindingResult.addError(fieldError); BindException bindException = new BindException(bindingResult); HttpStatus httpStatus = HttpStatus.BAD_REQUEST; ResponseEntity<Problem> problemResponse = problemExceptionResponseGenerator.createExceptionResponse(bindException, httpStatus, false); assertEquals(httpStatus, problemResponse.getStatusCode()); Problem problem = problemResponse.getBody(); assertTrue(problem.getType().toString().endsWith("/input-invalid")); assertEquals( singletonList( builder() .setErrorCode(errorCode) .setField(field) .setValue(value) .setDetail(errorMessage) .build()), problem.getErrors()); }