/** * @see java.lang.Object#toString() */ @Override public String toString() { return getName(); }
/** * @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")); }
checkIfPersonAttributeTypesAreLocked(); if (type.getSortWeight() == null) { List<PersonAttributeType> allTypes = Context.getPersonService().getAllPersonAttributeTypes(); if (!allTypes.isEmpty()) { type.setSortWeight(allTypes.get(allTypes.size() - 1).getSortWeight() + 1); } else { type.setSortWeight(1.0); if (type.getId() != null) { updateExisting = true; String newTypeName = type.getName();
/** * @see org.springframework.validation.Validator#validate(java.lang.Object, * org.springframework.validation.Errors) * @should fail validation if name is null * @should fail validation if format is empty * @should fail validation if name already in use * @should pass validation if description is null or empty or whitespace * @should pass validation if all fields are correct * @should pass validation if field lengths are correct * @should fail validation if field lengths are not correct */ @Override public void validate(Object obj, Errors errors) { PersonAttributeType patObj = (PersonAttributeType) obj; ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "PersonAttributeType.error.nameEmpty"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "format", "PersonAttributeType.error.formatEmpty"); PersonService ps = Context.getPersonService(); PersonAttributeType pat = ps.getPersonAttributeTypeByName(patObj.getName()); if (pat != null && !pat.getUuid().equals(patObj.getUuid())) { errors.rejectValue("name", "PersonAttributeType.error.nameAlreadyInUse"); } ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "format", "retireReason"); } }
/** * @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()); }
@Override public void validateDefaultRepresentation() throws Exception { super.validateDefaultRepresentation(); assertPropEquals("name", getObject().getName()); assertPropEquals("description", getObject().getDescription()); assertPropEquals("format", getObject().getFormat()); assertPropEquals("foreignKey", getObject().getForeignKey()); assertPropEquals("sortWeight", getObject().getSortWeight()); assertPropEquals("searchable", getObject().getSearchable()); assertPropEquals("editPrivilege", getObject().getEditPrivilege()); assertPropEquals("retired", getObject().getRetired()); }
@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()); }
/** * Gets the display string for a person attribute. * * @param pa the person attribute. * @return attribute type + value (for concise display purposes) */ @PropertyGetter("display") public String getDisplayString(PersonAttribute pa) { if (pa.getAttributeType() == null) return ""; if (Concept.class.getName().equals(pa.getAttributeType().getFormat()) && pa.getValue() != null) { Concept concept = Context.getConceptService().getConcept(pa.getValue()); return concept == null ? null : concept.getDisplayString(); } return pa.getAttributeType().getName() + " = " + pa.getValue(); }
/** * @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 PersonDAO#getSavedPersonAttributeTypeName(org.openmrs.PersonAttributeType) */ @Test public void getSavedPersonAttributeTypeName_shouldGetSavedPersonAttributeTypeNameFromDatabase() { PersonAttributeType pat = Context.getPersonService().getPersonAttributeType(1); // save the name from the db for later checks String origName = pat.getName(); String newName = "Race Updated"; assertFalse(newName.equals(origName)); // change the name on the java pojo (NOT in the database) pat.setName(newName); // the value from the database should match the original name from the // pat right after /it/ was fetched from the database String nameFromDatabase = dao.getSavedPersonAttributeTypeName(pat); assertEquals(origName, nameFromDatabase); }
/** * @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 Person#getAttribute(PersonAttributeType) */ @Test public void getAttribute_shouldReturnNullWhenExistingPersonAttributeTypeIsVoided() { Person person = personHelper(true, 1, 2, 3, "name1", "name2", "name3", "value1", "value2", "value3"); PersonAttributeType type = new PersonAttributeType(3); type.setName("name3"); Assert.assertNull(person.getAttribute(type)); }
@Test public void shouldRetirePersonAttributeType() { PersonAttributeType pat = personService.getPersonAttributeType(UNRETIRED_PERSON_ATTRIBUTE_TYPE); assertFalse("need an unretired PersonAttributeType", pat.getRetired()); String retireReason = "reason"; personService.retirePersonAttributeType(pat, retireReason); PersonAttributeType retiredPat = personService.getPersonAttributeType(UNRETIRED_PERSON_ATTRIBUTE_TYPE); assertTrue(retiredPat.getRetired()); assertThat(retiredPat.getRetiredBy(), is(Context.getAuthenticatedUser())); assertThat(retiredPat.getRetireReason(), is(retireReason)); assertNotNull(retiredPat.getDateRetired()); }
/** * @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")); }
Class c = OpenmrsClassLoader.getInstance().loadClass(getAttributeType().getFormat()); try { Object o = c.newInstance();
/** * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#newDelegate() */ @Override public PersonAttributeType newDelegate() { return new PersonAttributeType(); }
@PropertyGetter("concept") public Object getConcept(PersonAttributeType delegate) { if (OpenmrsUtil.nullSafeEquals(delegate.getFormat(), Concept.class.getCanonicalName())) { Concept concept = Context.getConceptService().getConcept(delegate.getForeignKey()); return ConversionUtil.convertToRepresentation(concept, Representation.FULL); } return null; } }
/** * @since 1.5 * @see org.openmrs.OpenmrsObject#getId() */ @Override public Integer getId() { return getPersonAttributeTypeId(); }
/** * @see PersonService#getAllPersonAttributeTypes() */ @Test public void getAllPersonAttributeTypes_shouldReturnAllPersonAttributeTypesIncludingRetired() throws Exception { executeDataSet("org/openmrs/api/include/PersonServiceTest-createRetiredPersonAttributeType.xml"); List<PersonAttributeType> attributeTypes = Context.getPersonService().getAllPersonAttributeTypes(); assertTrue("At least one element, otherwise no checking for retired will take place", attributeTypes.size() > 0); boolean foundRetired = false; for (PersonAttributeType personAttributeType : attributeTypes) { if (personAttributeType.getRetired()) { foundRetired = true; break; } } assertTrue("There should be at least one retired person attribute type found in the list", foundRetired); }
/** * @see PersonAttributeTypeController#retirePersonAttributeType(PersonAttributeType,String,WebRequest) * @throws Exception * @verifies void a person attribute type */ @Test public void retirePersonAttributeType_shouldRetireAPersonAttributeType() throws Exception { final String nonRetiredAttribute = "a0f5521c-dbbd-4c10-81b2-1b7ab18330df"; PersonAttributeType obj = service.getPersonAttributeTypeByUuid(nonRetiredAttribute); Assert.assertNotNull(obj); Assert.assertFalse(obj.isRetired()); MockHttpServletRequest delRequest = request(RequestMethod.DELETE, getURI() + "/" + nonRetiredAttribute); delRequest.addParameter("!purge", ""); delRequest.addParameter("reason", "unit test"); handle(delRequest); obj = service.getPersonAttributeTypeByUuid(nonRetiredAttribute); Assert.assertNotNull(obj); Assert.assertTrue(obj.isRetired()); Assert.assertTrue("unit test".equals(obj.getRetireReason())); }