/** * @see PersonAttribute#getHydratedObject() */ @Test public void getHydratedObject_shouldLoadClassInFormatProperty() { PersonAttributeType type = new PersonAttributeType(); type.setFormat("org.openmrs.Concept"); PersonAttribute pa = new PersonAttribute(2); pa.setAttributeType(type); pa.setValue("5089"); Concept concept = (Concept) pa.getHydratedObject(); Assert.assertEquals(5089, concept.getConceptId().intValue()); }
/** * @see PersonAttribute#getHydratedObject() */ @Test public void getHydratedObject_shouldStillLoadClassInFormatPropertyIfNotAttributable() { PersonAttributeType type = new PersonAttributeType(); type.setFormat("java.lang.String"); PersonAttribute pa = new PersonAttribute(2); pa.setAttributeType(type); pa.setValue("lalapalooza"); String value = (String) pa.getHydratedObject(); Assert.assertEquals("lalapalooza", value); }
/** * @see PersonAttribute#getHydratedObject() */ @Test public void getHydratedObject_shouldLoadUserClassInFormatProperty() { PersonAttributeType type = new PersonAttributeType(); type.setFormat("org.openmrs.User"); PersonAttribute pa = new PersonAttribute(2); pa.setAttributeType(type); pa.setValue("1"); Object value = pa.getHydratedObject(); Assert.assertTrue("should load user class in format property", (value instanceof User)); }
/** * @see PersonService#savePersonAttributeType(PersonAttributeType) */ @Test public void savePersonAttributeType_shouldSetTheDateCreatedAndCreatorOnNew() throws Exception { PersonService service = Context.getPersonService(); PersonAttributeType pat = new PersonAttributeType(); pat.setName("attr type name"); pat.setDescription("attr type desc"); pat.setFormat("java.lang.String"); service.savePersonAttributeType(pat); assertEquals(1, pat.getCreator().getId().intValue()); assertNotNull(pat.getDateCreated()); }
/** * @see PersonAttributeTypeValidator#validate(Object,Errors) */ @Test public void validate_shouldPassValidationIfAllFieldsAreCorreect() { PersonAttributeType type = new PersonAttributeType(); type.setName("Zodiac"); type.setFormat("java.lang.String"); type.setDescription("Zodiac Description"); Errors errors = new BindException(type, "patObj"); new PersonAttributeTypeValidator().validate(type, errors); Assert.assertFalse(errors.hasErrors()); }
/** * @see PersonAttributeTypeValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfFormatIsEmpty() { PersonAttributeType type = new PersonAttributeType(); type.setName("Zodiac"); type.setDescription("Zodiac Description"); type.setFormat(""); Errors errors = new BindException(type, "patObj"); new PersonAttributeTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("format")); }
/** * @see PersonAttributeTypeValidator#validate(Object,Errors) */ @Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { PersonAttributeType type = new PersonAttributeType(); type.setName("name"); type.setFormat("java.lang.String"); type.setRetireReason("retireReason"); Errors errors = new BindException(type, "patObj"); new PersonAttributeTypeValidator().validate(type, errors); Assert.assertFalse(errors.hasErrors()); }
/** * @see PersonService#purgePersonAttributeType(PersonAttributeType) */ @Test public void purgePersonAttributeType_shouldDeletePersonAttributeTypeFromDatabase() throws Exception { PersonService service = Context.getPersonService(); PersonAttributeType pat = new PersonAttributeType(); pat.setName("attr type name"); pat.setDescription("attr type desc"); pat.setFormat("java.lang.String"); service.savePersonAttributeType(pat); assertNotNull(pat.getId()); service.purgePersonAttributeType(pat); PersonAttributeType deletedPersonAttributeType = service.getPersonAttributeType(pat.getId()); Assert.assertNull(deletedPersonAttributeType); }
/** * @see PersonAttributeTypeValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() { PersonAttributeType type = new PersonAttributeType(); type .setName("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text"); type .setFormat("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text"); type .setRetireReason("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text"); Errors errors = new BindException(type, "patObj"); new PersonAttributeTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("name")); Assert.assertTrue(errors.hasFieldErrors("format")); Assert.assertTrue(errors.hasFieldErrors("retireReason")); } }
@Test public void setValue_shouldSetPassedValueIfCouldNotBeConvertedToAttributable() throws ClassNotFoundException { PersonAttributeType type = new PersonAttributeType(); type.setFormat("org.openmrs.Location"); type.setName("Second Home"); type.setDescription("Testing Attributable domain objects"); type.setSortWeight(5.5); type.setSearchable(false); type = personService.savePersonAttributeType(type); String nonExistenceLocationUuid = "this-uuid-does-not-exist-of-course"; PersonAttribute attribute = new PersonAttribute(type, null); Assert.assertNull(attribute.getValue()); Assert.assertTrue(Attributable.class.isAssignableFrom(Context.loadClass(type.getFormat()))); resource.setValue(attribute, nonExistenceLocationUuid); Assert.assertEquals(nonExistenceLocationUuid, attribute.getValue()); }
@Test public void setValue_shouldSetProperAttributableIdIfFound() { PersonAttributeType type = new PersonAttributeType(); type.setFormat("org.openmrs.Location"); type.setName("Second Home"); type.setDescription("Testing Attributable domain objects"); type.setSortWeight(5.5); type.setSearchable(false); type = personService.savePersonAttributeType(type); //Get the first location in from the list Location location = locationService.getAllLocations().get(0); PersonAttribute attribute = new PersonAttribute(type, null); attribute.setAttributeType(type); Assert.assertNull(attribute.getValue()); resource.setValue(attribute, location.getUuid()); Assert.assertEquals(location.getUuid(), attribute.getValue()); }