/** * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors) * @should fail validation if aIsToB(or A is To B) is null or empty or whitespace * @should fail validation if bIsToA(or B is To A) is null or empty or whitespace * @should fail validation if description is null or empty or whitespace * @should pass validation if all required fields are set * @should fail validation if relationshipTypeName already exist * @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) { RelationshipType relationshipType = (RelationshipType) obj; if (relationshipType == null) { errors.rejectValue("relationshipType", "error.general"); } else { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "aIsToB", "RelationshipType.aIsToB.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "bIsToA", "RelationshipType.bIsToA.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "RelationshipType.description.required"); RelationshipType exist = Context.getPersonService().getRelationshipTypeByName( relationshipType.getaIsToB() + "/" + relationshipType.getbIsToA()); if (exist != null && !exist.getRetired() && !OpenmrsUtil.nullSafeEquals(relationshipType.getUuid(), exist.getUuid())) { errors.reject("duplicate.relationshipType"); } ValidateUtil.validateFieldLengths(errors, obj.getClass(), "aIsToB", "bIsToA", "description", "retireReason"); } } }
/** * @see PersonService#saveRelationshipType(RelationshipType) */ @Test public void saveRelationshipType_shouldCreateNewObjectWhenRelationshipTypeIdIsNull() throws Exception { RelationshipType relationshipType = new RelationshipType(); relationshipType.setDescription("Test relationship"); relationshipType.setaIsToB("Sister"); relationshipType.setbIsToA("Brother"); Assert.assertNull(relationshipType.getRelationshipTypeId()); RelationshipType savedRelationshipType = personService.saveRelationshipType(relationshipType); Assert.assertNotNull(savedRelationshipType.getRelationshipTypeId()); }
/** * @see PersonService#saveRelationshipType(RelationshipType) */ @Test(expected = APIException.class) public void saveRelationshipType_shouldFailIfTheDescriptionIsNotSpecified() throws Exception { RelationshipType relationshipType = new RelationshipType(); relationshipType.setaIsToB("Sister"); relationshipType.setbIsToA("Brother"); personService.saveRelationshipType(relationshipType); }
@Override public void validateDefaultRepresentation() throws Exception { super.validateDefaultRepresentation(); assertPropEquals("display", getObject().toString()); assertPropEquals("description", getObject().getDescription()); assertPropEquals("aIsToB", getObject().getaIsToB()); assertPropEquals("bIsToA", getObject().getbIsToA()); assertPropEquals("displayAIsToB", getObject().getaIsToB()); assertPropEquals("displayBIsToA", getObject().getbIsToA()); assertPropEquals("retired", getObject().isRetired()); }
String output = "<update tableName=\"relationship_type\"><column name=\"uuid\" value=\"" + type.getUuid() + "\"/><where> relationship_type_id = '" + type.getRelationshipTypeId() + "' and a_is_to_b = '" + type.getaIsToB().replace("'", "\\'") + "' and b_is_to_a = '" + type.getbIsToA().replace("'", "\\'") + "'</where></update>"; System.out.println(output);
/** * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doSearch(org.openmrs.module.webservices.rest.web.RequestContext) */ @Override protected NeedsPaging<RelationshipType> doSearch(RequestContext context) { String queryString = context.getParameter("q"); List<RelationshipType> allRelationshipTypes = Context.getPersonService().getAllRelationshipTypes(); List<RelationshipType> unRetiredRelationshipTypes = new ArrayList<RelationshipType>(); for (RelationshipType relationshipType : allRelationshipTypes) { if (!relationshipType.isRetired() && (relationshipType.getaIsToB().contains(queryString) || relationshipType.getbIsToA().contains( queryString))) unRetiredRelationshipTypes.add(relationshipType); } return new NeedsPaging<RelationshipType>(unRetiredRelationshipTypes, context); }
@Test(expected = APIException.class) public void retireRelationshipType_shouldFailIfGivenReasonIsEmptyString() { personService.retireRelationshipType(new RelationshipType(), ""); }
@PropertyGetter("displayAIsToB") public String getDisplayAIsToBe(RelationshipType delegate) { String localization = getLocalization(delegate.getUuid(), "aIsToB"); if (localization != null) { return localization; } else { return StringUtils.isEmpty(delegate.getaIsToB()) ? "" : delegate.getaIsToB(); } }
@PropertyGetter("displayBIsToA") public String getDisplayBIsToAe(RelationshipType delegate) { String localization = getLocalization(delegate.getUuid(), "bIsToA"); if (localization != null) { return localization; } else { return StringUtils.isEmpty(delegate.getbIsToA()) ? "" : delegate.getbIsToA(); } }
@Test public void retireRelationshipType_shouldRetireGivenRelationshipType() { RelationshipType rt = personService.getRelationshipType(1); assertFalse(rt.getRetired()); String reason = "reason"; personService.retireRelationshipType(rt, reason); assertTrue(rt.getRetired()); assertThat(rt.getRetiredBy(), is(Context.getAuthenticatedUser())); assertNotNull(rt.getDateRetired()); assertThat(rt.getRetireReason(), is(reason)); }
@Override public String toString() { String relType = getRelationshipType() == null ? "NULL" : getRelationshipType().getaIsToB(); return personA + " is the " + relType + " of " + personB; }
/** * @see PersonService#saveRelationshipType(RelationshipType) */ @Test public void saveRelationshipType_shouldUpdateExistingObjectWhenRelationshipTypeIdIsNotNull() throws Exception { RelationshipType savedRelationshipType = Context.getPersonService().getRelationshipType(1); Assert.assertNotNull(savedRelationshipType.getRelationshipTypeId()); savedRelationshipType.setPreferred(true); RelationshipType updatedRelationshipType = personService.saveRelationshipType(savedRelationshipType); Assert.assertEquals(true, updatedRelationshipType.getPreferred()); }
/** * @see org.openmrs.api.PersonService#saveRelationshipType(org.openmrs.RelationshipType) */ @Override public RelationshipType saveRelationshipType(RelationshipType relationshipType) throws APIException { if (StringUtils.isBlank(relationshipType.getDescription())) { throw new APIException("error.required", new Object[] { Context.getMessageSourceService().getMessage("general.description") }); } return dao.saveRelationshipType(relationshipType); }
/** * @see RelationshipTypeValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfaIsToBIsNullOrEmptyOrWhitespace() { RelationshipType type = new RelationshipType(); Errors errors = new BindException(type, "type"); new RelationshipTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("aIsToB")); type.setaIsToB(""); errors = new BindException(type, "type"); new RelationshipTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("aIsToB")); type.setaIsToB(" "); errors = new BindException(type, "type"); new RelationshipTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("aIsToB")); }
/** * @see RelationshipTypeValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfbIsToAIsNullOrEmptyOrWhitespace() { RelationshipType type = new RelationshipType(); Errors errors = new BindException(type, "type"); new RelationshipTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("bIsToA")); type.setbIsToA(""); errors = new BindException(type, "type"); new RelationshipTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("bIsToA")); type.setbIsToA(" "); errors = new BindException(type, "type"); new RelationshipTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("bIsToA")); }
/** * @see RelationshipTypeValidator#validate(Object,Errors) */ @Test public void validate_shouldFailValidationIfDescriptionIsNullOrEmptyOrWhitespace() { RelationshipType type = new RelationshipType(); Errors errors = new BindException(type, "type"); new RelationshipTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("description")); type.setDescription(""); errors = new BindException(type, "type"); new RelationshipTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("description")); type.setDescription(" "); errors = new BindException(type, "type"); new RelationshipTypeValidator().validate(type, errors); Assert.assertTrue(errors.hasFieldErrors("description")); }
@Test public void shouldRetireARelationshipType() throws Exception { RelationshipType relationshipType = service.getRelationshipTypeByUuid(RestTestConstants1_8.RELATIONSHIP_TYPE_UUID); Assert.assertFalse(relationshipType.isRetired()); handle(newDeleteRequest(getURI() + "/" + getUuid(), new Parameter("reason", "test reason"))); relationshipType = service.getRelationshipTypeByUuid(RestTestConstants1_8.RELATIONSHIP_TYPE_UUID); Assert.assertTrue(relationshipType.isRetired()); Assert.assertEquals("test reason", relationshipType.getRetireReason()); }
@Test public void shouldEditARelationship() throws Exception { final String newRelationshipTypeUuid = "d47f056e-f147-49a3-88e1-0c91d199510d"; Relationship relationship = service.getRelationshipByUuid(RestTestConstants1_8.RELATIONSHIP_UUID); Assert.assertNotNull(relationship); //sanity checks Assert.assertFalse(newRelationshipTypeUuid.equalsIgnoreCase(relationship.getRelationshipType().getUuid())); String json = "{\"relationshipType\":\"" + newRelationshipTypeUuid + "\"" + "}"; handle(newPostRequest(getURI() + "/" + getUuid(), json)); Relationship updated = service.getRelationshipByUuid(RestTestConstants1_8.RELATIONSHIP_UUID); Assert.assertNotNull(updated); Assert.assertEquals(newRelationshipTypeUuid, updated.getRelationshipType().getUuid()); }