MapBindingResult errors = new MapBindingResult(new HashMap<String, Object>(), "dummy"); PrintWriter stackTrace = new PrintWriter(new StringWriter()); e.printStackTrace(stackTrace);
/** * @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")); }
/** * @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")); }
/** * @see RelationshipValidator#validate(Object,Errors) */ @Test public void validate_shouldFailIfStartDateIsInFuture() { 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 nextYear = cal.getTime(); relationship.setStartDate(nextYear); new RelationshipValidator().validate(relationship, errors); Assert.assertTrue(errors.hasErrors()); }
/** * @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()); }
/** * @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()); }
/** * @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()); }
/** * @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()); }
/** * @see RelationshipValidator#validate(Object,Errors) */ @Test public void validate_shouldPassIfStartDateIsEmpty() { Map<String, String> map = new HashMap<>(); MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName()); Relationship relationship = new Relationship(1); relationship.setStartDate(null); new RelationshipValidator().validate(relationship, errors); Assert.assertFalse(errors.hasErrors()); } }
/** * 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())); } }
public static BindingResult rejectedResult() { BindingResult result = new MapBindingResult(new HashMap<>(), "test"); result.reject("error"); return result; }
@Before public void init() { errors = new MapBindingResult(new HashMap<>(), "OrgEnrollmentConfig"); }
@Test public void test_validate_all_properties_with_no_errors() throws Exception { errors = new MapBindingResult(new HashMap<>(), "OrgEnrollmentConfig"); when(request.getIdleDuration()).thenReturn("PT2M"); when(request.getExcludeSpacesFromAutoEnrollment()).thenReturn(".*correct_space"); when(request.getAutoEnrollment()).thenReturn("standard"); when(request.getState()).thenReturn("backoffice_enrolled"); enrollmentValidator.validate(request, errors); assertTrue(errors.getErrorCount() == 0); }
@Test public void test_validate_enrollment_state_to_throw_an_error_with_wrong_input() throws Exception { errors = new MapBindingResult(new HashMap<>(), "OrgEnrollmentConfig"); when(request.getState()).thenReturn("not_backoffice_enrolled"); when(stateReader.readParameter("not_backoffice_enrolled", false)) .thenThrow(new InvalidParameterException("state", "Error")); enrollmentValidator.validate(request, errors); assertTrue(errors.getErrorCount() == 1); }
@Test public void test_validate_idle_duration_to_throw_an_error_with_wrong_input() throws Exception { errors = new MapBindingResult(new HashMap<>(), "OrgEnrollmentConfig"); when(request.getIdleDuration()).thenReturn("PT_invalid_idle_duration"); when(idleDurationReader.readParameter("PT_invalid_idle_duration", false)).thenThrow( new InvalidParameterException(Config.ServiceInstanceParameters.IDLE_DURATION, "Error")); enrollmentValidator.validate(request, errors); assertTrue(errors.getErrorCount() == 1); }
@Test public void test_validate_auto_enrollment_to_throw_an_error_with_wrong_input() throws Exception { errors = new MapBindingResult(new HashMap<>(), "OrgEnrollmentConfig"); when(request.getAutoEnrollment()).thenReturn("not_standard"); when(autoEnrollmentReader.readParameter("not_standard", false)) .thenThrow(new InvalidParameterException("auto-enrollment", "Error")); enrollmentValidator.validate(request, errors); assertTrue(errors.getErrorCount() == 1); }
@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()); }
@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()); }
@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()); }
@Test public 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"); }