@Test public void testBindExceptionSerializable() throws Exception { SerializablePerson tb = new SerializablePerson(); tb.setName("myName"); tb.setAge(99); BindException ex = new BindException(tb, "tb"); ex.reject("invalid", "someMessage"); ex.rejectValue("age", "invalidField", "someMessage"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(ex); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); BindException ex2 = (BindException) ois.readObject(); assertTrue(ex2.hasGlobalErrors()); assertEquals("invalid", ex2.getGlobalError().getCode()); assertTrue(ex2.hasFieldErrors("age")); assertEquals("invalidField", ex2.getFieldError("age").getCode()); assertEquals(new Integer(99), ex2.getFieldValue("age")); ex2.rejectValue("name", "invalidField", "someMessage"); assertTrue(ex2.hasFieldErrors("name")); assertEquals("invalidField", ex2.getFieldError("name").getCode()); assertEquals("myName", ex2.getFieldValue("name")); }
@Test public void validate_shouldRequireDatatypeClassname() { validator.validate(attributeType, errors); Assert.assertTrue(errors.hasFieldErrors("datatypeClassname")); assertThat(errors.getFieldErrors("datatypeClassname").get(0).getCode(), is("error.null")); }
@Test public void shouldFailIfNameIsNull() { validator.validate(attributeType, errors); Assert.assertTrue(errors.hasFieldErrors("name")); assertThat(errors.getFieldErrors("name").get(0).getCode(), is("error.name")); }
@Test public void shouldFailIfNameIsEmpty() { attributeType.setName(""); validator.validate(attributeType, errors); Assert.assertTrue(errors.hasFieldErrors("name")); assertThat(errors.getFieldErrors("name").get(0).getCode(), is("error.name")); }
@Test public void shouldFailIfDatatypeConfigurationIsInvalidIfDatatypeEqualsRegexValidatedText() { attributeType.setDatatypeClassname(RegexValidatedTextDatatype.class.getName()); attributeType.setDatatypeConfig(null); validator.validate(attributeType, errors); Assert.assertTrue(errors.hasFieldErrors("datatypeConfig")); assertThat(errors.getFieldErrors("datatypeConfig").get(0).getCode(), is("AttributeType.datatypeConfig.invalid")); }
@Test public void shouldFailIfDatatypeConfigurationIsBlankIfDatatypeEqualsRegexValidatedText() { attributeType.setDatatypeClassname(RegexValidatedTextDatatype.class.getName()); attributeType.setDatatypeConfig(""); validator.validate(attributeType, errors); Assert.assertTrue(errors.hasFieldErrors("datatypeConfig")); assertThat(errors.getFieldErrors("datatypeConfig").get(0).getCode(), is("error.null")); }
@Test public void validate_shouldRequireMinOccurs() { attributeType.setMinOccurs(null); validator.validate(attributeType, errors); Assert.assertTrue(errors.hasFieldErrors("minOccurs")); assertThat(errors.getFieldErrors("minOccurs").get(0).getCode(), is("error.null")); }
@Test public void shouldFailIfNameIsOnlyWhitespaces() { attributeType.setName(" "); validator.validate(attributeType, errors); Assert.assertTrue(errors.hasFieldErrors("name")); assertThat(errors.getFieldErrors("name").get(0).getCode(), is("error.name")); }
@Test public void shouldFailIfPreferredHandlerClassIsOfWrongDatatype() { attributeType.setDatatypeClassname(RegexValidatedTextDatatype.class.getName()); attributeType.setDatatypeConfig("some valid config"); attributeType.setPreferredHandlerClassname("org.openmrs.attribute.handler.DateDatatypeHandler"); validator.validate(attributeType, errors); Assert.assertTrue(errors.hasFieldErrors("preferredHandlerClassname")); assertThat(errors.getFieldErrors("preferredHandlerClassname").get(0).getCode(), is("AttributeType.preferredHandlerClassname.wrongDatatype")); }
@Test public void shouldFailIfPreferredHandlerClassIsInvalid() { attributeType.setDatatypeClassname(RegexValidatedTextDatatype.class.getName()); attributeType.setDatatypeConfig("some valid config"); attributeType.setPreferredHandlerClassname("uncompatible class"); validator.validate(attributeType, errors); Assert.assertTrue(errors.hasFieldErrors("handlerConfig")); assertThat(errors.getFieldErrors("handlerConfig").get(0).getCode(), is("AttributeType.handlerConfig.invalid")); }
@Test public void validate_shouldNotAllowMaxOccursLessThanMinOccurs() { attributeType.setMinOccurs(3); attributeType.setMaxOccurs(2); validator.validate(attributeType, errors); Assert.assertTrue(errors.hasFieldErrors("maxOccurs")); assertThat(errors.getFieldErrors("maxOccurs").get(0).getCode(), is("AttributeType.maxOccursShouldNotBeLessThanMinOccurs")); }
/** * @see PatientProgramValidator#validate(Object,Errors) */ @Test public void validate_shouldFailIfPatientProgramEndDateIsInFuture() { PatientProgram program = Context.getProgramWorkflowService().getPatientProgram(1); Date date10DaysAfterSystemCurrentDate = new Date(System.currentTimeMillis() + 10 * 24 * 60 * 60 * 1000); program.setDateCompleted(date10DaysAfterSystemCurrentDate); BindException errors = new BindException(program, ""); new PatientProgramValidator().validate(program, errors); Assert.assertTrue(errors.hasFieldErrors("dateCompleted")); }
/** * @see PatientProgramValidator#validate(Object,Errors) */ @Test public void validate_shouldFailIfPatientProgramEnrolledDateIsInFuture() { PatientProgram program = Context.getProgramWorkflowService().getPatientProgram(1); Date date10DaysAfterSystemCurrentDate = new Date(System.currentTimeMillis() + 10 * 24 * 60 * 60 * 1000); program.setDateEnrolled(date10DaysAfterSystemCurrentDate); BindException errors = new BindException(program, ""); new PatientProgramValidator().validate(program, errors); Assert.assertTrue(errors.hasFieldErrors("dateEnrolled")); }
/** * @see PatientProgramValidator#validate(Object,Errors) */ @Test public void validate_shouldFailIfAnyPatientStateHasANullWorkFlowState() { PatientProgram program = Context.getProgramWorkflowService().getPatientProgram(1); PatientState patientState = program.getStates().iterator().next(); patientState.setState(null); BindException errors = new BindException(program, ""); new PatientProgramValidator().validate(program, errors); Assert.assertTrue(errors.hasFieldErrors("states")); }
/** * @see ValidateUtil#validateFieldLengths(org.springframework.validation.Errors, Class, String...) */ @Test public void validateFieldLength_shouldRejectValueWhenNameIsToLong() { PatientIdentifierType patientIdentifierType = new PatientIdentifierType(); patientIdentifierType.setName("asdfghjkl asdfghjkl asdfghjkl asdfghjkl asdfghjkl xx"); BindException errors = new BindException(patientIdentifierType, "patientIdentifierType"); ValidateUtil.validateFieldLengths(errors, PatientIdentifierType.class, "name"); assertTrue(errors.hasFieldErrors("name")); }
/** * @see ValidateUtil#validateFieldLengths(org.springframework.validation.Errors, Class, String...) */ @Test public void validateFieldLength_shouldNotRejectValueWhenNameIsEqualMax() { PatientIdentifierType patientIdentifierType = new PatientIdentifierType(); patientIdentifierType.setName("asdfghjkl asdfghjkl asdfghjkl asdfghjkl asdfghjkl "); BindException errors = new BindException(patientIdentifierType, "patientIdentifierType"); ValidateUtil.validateFieldLengths(errors, PatientIdentifierType.class, "name"); assertFalse(errors.hasFieldErrors("name")); }
/** * @see PatientProgramValidator#validate(Object,Errors) */ @Test public void validate_shouldFailIfThePatientFieldIsBlank() { PatientProgram program = new PatientProgram(); BindException errors = new BindException(program, "program"); new PatientProgramValidator().validate(program, errors); Assert.assertEquals(true, errors.hasFieldErrors("patient")); }
/** * @see PatientProgramValidator#validate(Object,Errors) */ @Test public void validate_shouldFailIfAPatientStateHasAnInvalidWorkFlowState() { executeDataSet("org/openmrs/api/include/ProgramWorkflowServiceTest-otherProgramWorkflows.xml"); PatientProgram program = Context.getProgramWorkflowService().getPatientProgram(1); PatientState patientState = program.getStates().iterator().next(); patientState.setState(Context.getProgramWorkflowService().getStateByUuid("31c82d66-245c-11e1-9cf0-00248140a5eb")); BindException errors = new BindException(program, ""); new PatientProgramValidator().validate(program, errors); Assert.assertTrue(errors.hasFieldErrors("states")); }
/** * @see PatientProgramValidator#validate(Object,Errors) */ @Test public void validate_shouldPassForPatientStatesThatHaveTheSameStartDatesInTheSameWorkFlow() { PatientProgram program = Context.getProgramWorkflowService().getPatientProgram(1); PatientState patientState = program.getStates().iterator().next(); //add a new state by moving the patient to a another one ProgramWorkflowState nextState = patientState.getState().getProgramWorkflow().getState(4); patientState.getPatientProgram().transitionToState(nextState, patientState.getStartDate()); BindException errors = new BindException(program, ""); new PatientProgramValidator().validate(program, errors); Assert.assertEquals(false, errors.hasFieldErrors("states")); }
/** * @see PatientProgramValidator#validate(Object,Errors) */ @Test public void validate_shouldFailIfPatientProgramEnrollDateIsEmpty() { PatientProgram program = Context.getProgramWorkflowService().getPatientProgram(1); program.setDateEnrolled(null); BindException errors = new BindException(program, ""); new PatientProgramValidator().validate(program, errors); Assert.assertTrue(errors.hasFieldErrors("dateEnrolled")); }