use of org.openmrs.EncounterRole in project openmrs-core by openmrs.
the class EncounterRoleValidator method validate.
/**
* Checks the form object for any inconsistencies/errors
*
* @see org.springframework.validation.Validator#validate(java.lang.Object,
* org.springframework.validation.Errors)
* @should fail validation if name is null or empty or whitespace
* @should fail validation if name is duplicate
* @should pass validation if all required fields have proper values
* @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) {
super.validate(obj, errors);
EncounterRole encounterRole = (EncounterRole) obj;
if (!errors.hasErrors()) {
EncounterRole duplicate = Context.getEncounterService().getEncounterRoleByName(encounterRole.getName().trim());
if (duplicate != null && duplicate.getUuid() != null && !OpenmrsUtil.nullSafeEquals(encounterRole.getUuid(), duplicate.getUuid())) {
errors.rejectValue("name", "encounterRole.duplicate.name", "Specified Encounter Role name already exists, please specify another ");
}
ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "description", "retireReason");
}
}
use of org.openmrs.EncounterRole in project openmrs-core by openmrs.
the class EncounterRoleValidatorTest method validate_shouldFailIfTheNameOfTheEncounterRoleIsNullEmptyOrWhitespace.
/**
* @see org.openmrs.validator.EncounterRoleValidator#validate(Object, org.springframework.validation.Errors)
*/
@Test
public void validate_shouldFailIfTheNameOfTheEncounterRoleIsNullEmptyOrWhitespace() {
EncounterRole encounterRoleNo1 = new EncounterRole();
encounterRoleNo1.setName(null);
Errors errorsNo1 = new BindException(encounterRoleNo1, "encounterRole");
new EncounterRoleValidator().validate(encounterRoleNo1, errorsNo1);
Assert.assertTrue(errorsNo1.hasFieldErrors("name"));
EncounterRole encounterRoleNo2 = new EncounterRole();
encounterRoleNo2.setName("");
Errors errorsNo2 = new BindException(encounterRoleNo2, "encounterRole");
new EncounterRoleValidator().validate(encounterRoleNo2, errorsNo2);
Assert.assertTrue(errorsNo2.hasFieldErrors("name"));
EncounterRole encounterRoleNo3 = new EncounterRole();
encounterRoleNo3.setName(" ");
Errors errorsNo3 = new BindException(encounterRoleNo3, "encounterRole");
new EncounterRoleValidator().validate(encounterRoleNo3, errorsNo3);
Assert.assertTrue(errorsNo3.hasFieldErrors("name"));
}
use of org.openmrs.EncounterRole in project openmrs-core by openmrs.
the class EncounterRoleValidatorTest method validate_shouldPassValidationIfFieldLengthsAreCorrect.
/**
* {@link org.openmrs.validator.EncounterRoleValidator#validate(Object, org.springframework.validation.Errors)}
*/
@Test
public void validate_shouldPassValidationIfFieldLengthsAreCorrect() {
EncounterRole encounterRole = Context.getEncounterService().getEncounterRoleByName("Unknown");
Assert.assertNotNull(encounterRole);
encounterRole.setName("name");
encounterRole.setDescription("desc");
encounterRole.setRetireReason("retireReason");
Errors errors = new BindException(encounterRole, "encounterRole");
new EncounterRoleValidator().validate(encounterRole, errors);
Assert.assertFalse(errors.hasErrors());
}
use of org.openmrs.EncounterRole in project openmrs-core by openmrs.
the class EncounterServiceTest method saveEncounter_shouldCascadeDeleteEncounterProviders.
/**
* @see EncounterService#saveEncounter(Encounter)
*/
@Test
public void saveEncounter_shouldCascadeDeleteEncounterProviders() {
// given
Encounter encounter = new Encounter();
encounter.setLocation(new Location(1));
encounter.setEncounterType(new EncounterType(1));
encounter.setEncounterDatetime(new Date());
encounter.setPatient(new Patient(3));
EncounterRole role = new EncounterRole();
role.setName("role");
role = Context.getEncounterService().saveEncounterRole(role);
Provider provider = new Provider();
provider.setIdentifier("id1");
provider.setPerson(newPerson("name"));
provider = Context.getProviderService().saveProvider(provider);
Provider provider2 = new Provider();
provider2.setIdentifier("id2");
provider2.setPerson(newPerson("name2"));
provider2 = Context.getProviderService().saveProvider(provider2);
encounter.addProvider(role, provider);
encounter.addProvider(role, provider2);
EncounterService es = Context.getEncounterService();
es.saveEncounter(encounter);
Context.flushSession();
Context.clearSession();
// when
encounter = Context.getEncounterService().getEncounter(encounter.getEncounterId());
encounter.setProvider(role, provider);
es.saveEncounter(encounter);
Context.flushSession();
Context.clearSession();
// then
encounter = Context.getEncounterService().getEncounter(encounter.getEncounterId());
Assert.assertEquals(1, encounter.getProvidersByRole(role).size());
Assert.assertTrue("Role", encounter.getProvidersByRole(role).contains(provider));
}
use of org.openmrs.EncounterRole in project openmrs-core by openmrs.
the class EncounterServiceTest method purgeEncounterRole_shouldPurgeEncounterRole.
/**
* Make sure that purging an encounter removes the row from the database
*
* @see EncounterService#purgeEncounterRole(org.openmrs.EncounterRole)
*/
@Test
public void purgeEncounterRole_shouldPurgeEncounterRole() {
EncounterService encounterService = Context.getEncounterService();
EncounterRole encounterRole = encounterService.getEncounterRole(1);
encounterService.purgeEncounterRole(encounterRole);
EncounterRole fetchedEncounterRole = encounterService.getEncounterRole(encounterRole.getEncounterRoleId());
assertNull("We shouldn't find the encounter after deletion", fetchedEncounterRole);
}
Aggregations