/** * @see PersonService#savePersonAttributeType(PersonAttributeType) * @throws PersonAttributeTypeLockedException */ @Test(expected = PersonAttributeTypeLockedException.class) public void savePersonAttributeType_shouldThrowAnErrorWhenTryingToSavePersonAttributeTypeWhilePersonAttributeTypesAreLocked() throws Exception { PersonService ps = Context.getPersonService(); createPersonAttributeTypeLockedGPAndSetValue("true"); PersonAttributeType pat = ps.getPersonAttributeType(1); pat.setDescription("New person attribute type"); ps.savePersonAttributeType(pat); }
/** * @see PersonAttributeTypeValidator#validate(Object,Errors) */ @Test public void validate_shouldPassValidationIfDescriptionIsNullOrEmptyOrWhitespace() { PersonAttributeType type = new PersonAttributeType(); type.setName("name"); type.setDescription(null); Errors errors = new BindException(type, "type"); new PersonAttributeTypeValidator().validate(type, errors); Assert.assertFalse(errors.hasFieldErrors("description")); type.setDescription(""); errors = new BindException(type, "type"); new PersonAttributeTypeValidator().validate(type, errors); Assert.assertFalse(errors.hasFieldErrors("description")); type.setDescription(" "); errors = new BindException(type, "type"); new PersonAttributeTypeValidator().validate(type, errors); Assert.assertFalse(errors.hasFieldErrors("description")); }
/** * @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_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_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 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); }
@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()); }