Search in sources :

Example 1 with Allergen

use of org.openmrs.Allergen in project openmrs-core by openmrs.

the class PatientServiceAllergyTest method setAllergies_shouldSetTheNonCodedConceptForNonCodedAllergenIfNotSpecified.

@Test
public void setAllergies_shouldSetTheNonCodedConceptForNonCodedAllergenIfNotSpecified() {
    Patient patient = allergyService.getPatient(2);
    Allergen allergen = new Allergen(AllergenType.DRUG, null, "Some allergy name");
    Allergy allergy = new Allergy(patient, allergen, null, null, null);
    Allergies allergies = allergyService.getAllergies(patient);
    allergies.add(allergy);
    allergyService.setAllergies(patient, allergies);
    Assert.assertFalse(allergy.getAllergen().isCoded());
}
Also used : Allergy(org.openmrs.Allergy) Patient(org.openmrs.Patient) Allergies(org.openmrs.Allergies) Allergen(org.openmrs.Allergen) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 2 with Allergen

use of org.openmrs.Allergen in project openmrs-core by openmrs.

the class PatientServiceAllergyTest method setAllergies_shouldSaveTheAllergyListAndStatus.

/**
 * @see PatientService#setAllergies(Patient,Allergies)
 */
@Test
public void setAllergies_shouldSaveTheAllergyListAndStatus() {
    // get a patient without any allergies
    Patient patient = allergyService.getPatient(7);
    Allergies allergies = allergyService.getAllergies(patient);
    Assert.assertEquals(Allergies.UNKNOWN, allergies.getAllergyStatus());
    Assert.assertEquals(0, allergies.size());
    // save some allergies for this patient
    Allergen allergen = new Allergen(AllergenType.DRUG, new Concept(3), null);
    Concept severity = new Concept(4);
    Allergy allergy = new Allergy(patient, allergen, severity, "some comment", new ArrayList<>());
    AllergyReaction reaction = new AllergyReaction(allergy, new Concept(21), null);
    allergy.addReaction(reaction);
    allergies = new Allergies();
    allergies.add(allergy);
    allergyService.setAllergies(patient, allergies);
    // now the patient should have allergies and the correct allergy status
    allergies = allergyService.getAllergies(patient);
    Assert.assertEquals(Allergies.SEE_LIST, allergies.getAllergyStatus());
    Assert.assertEquals(1, allergies.size());
    Assert.assertEquals(1, allergies.get(0).getReactions().size());
}
Also used : Concept(org.openmrs.Concept) Allergy(org.openmrs.Allergy) Patient(org.openmrs.Patient) AllergyReaction(org.openmrs.AllergyReaction) Allergies(org.openmrs.Allergies) Allergen(org.openmrs.Allergen) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 3 with Allergen

use of org.openmrs.Allergen in project openmrs-core by openmrs.

the class AllergyValidator method validate.

/**
 * @see Validator#validate(Object, org.springframework.validation.Errors)
 * @param target
 * @param errors
 * @should fail for a null value
 * @should fail if patient is null
 * @should fail id allergenType is null
 * @should fail if allergen is null
 * @should fail if codedAllergen is null
 * @should fail if nonCodedAllergen is null and allergen is set to other non coded
 * @should reject a duplicate allergen
 * @should reject a duplicate non coded allergen
 * @should pass for a valid allergy
 */
@Override
public void validate(Object target, Errors errors) {
    if (target == null) {
        throw new IllegalArgumentException("Allergy should not be null");
    }
    ValidationUtils.rejectIfEmpty(errors, "patient", "allergyapi.patient.required");
    Allergy allergy = (Allergy) target;
    if (allergy.getAllergen() == null) {
        errors.rejectValue("allergen", "allergyapi.allergen.required");
    } else {
        Allergen allergen = allergy.getAllergen();
        if (allergen.getAllergenType() == null) {
            errors.rejectValue("allergen", "allergyapi.allergenType.required");
        }
        if (allergen.getCodedAllergen() == null && StringUtils.isBlank(allergen.getNonCodedAllergen())) {
            errors.rejectValue("allergen", "allergyapi.allergen.codedOrNonCodedAllergen.required");
        } else if (!allergen.isCoded() && StringUtils.isBlank(allergen.getNonCodedAllergen())) {
            errors.rejectValue("allergen", "allergyapi.allergen.nonCodedAllergen.required");
        }
        if (allergy.getAllergyId() == null && allergy.getPatient() != null) {
            Allergies existingAllergies = patientService.getAllergies(allergy.getPatient());
            if (existingAllergies.containsAllergen(allergy)) {
                String key = "ui.i18n.Concept.name." + allergen.getCodedAllergen().getUuid();
                String name = Context.getMessageSourceService().getMessage(key);
                if (key.equals(name)) {
                    name = allergen.toString();
                }
                errors.rejectValue("allergen", "allergyapi.message.duplicateAllergen", new Object[] { name }, null);
            }
        }
    }
}
Also used : Allergy(org.openmrs.Allergy) Allergies(org.openmrs.Allergies) Allergen(org.openmrs.Allergen)

Example 4 with Allergen

use of org.openmrs.Allergen in project openmrs-core by openmrs.

the class AllergyValidatorTest method validate_shouldPassForAValidAllergy.

/**
 * @see AllergyValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldPassForAValidAllergy() {
    Allergies allergies = new Allergies();
    Concept aspirin = new Concept();
    Allergen allergen1 = new Allergen(AllergenType.DRUG, aspirin, null);
    allergies.add(new Allergy(null, allergen1, null, null, null));
    when(ps.getAllergies(any(Patient.class))).thenReturn(allergies);
    Allergen anotherAllergen = new Allergen(AllergenType.DRUG, new Concept(), null);
    Allergy allergy = new Allergy(mock(Patient.class), anotherAllergen, null, null, null);
    Errors errors = new BindException(allergy, "allergy");
    validator.validate(allergy, errors);
    assertFalse(errors.hasErrors());
}
Also used : Concept(org.openmrs.Concept) Allergy(org.openmrs.Allergy) Errors(org.springframework.validation.Errors) Patient(org.openmrs.Patient) BindException(org.springframework.validation.BindException) Allergies(org.openmrs.Allergies) Allergen(org.openmrs.Allergen) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with Allergen

use of org.openmrs.Allergen in project openmrs-core by openmrs.

the class AllergyValidatorTest method validate_shouldFailIfNonCodedAllergenIsNullAndAllergenIsSetToOtherNonCoded.

/**
 * @see AllergyValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailIfNonCodedAllergenIsNullAndAllergenIsSetToOtherNonCoded() {
    Allergen allergen = mock(Allergen.class);
    when(allergen.getAllergenType()).thenReturn(AllergenType.DRUG);
    Concept concept = createMockConcept(getOtherNonCodedConceptUuid());
    when(allergen.getCodedAllergen()).thenReturn(concept);
    when(allergen.getNonCodedAllergen()).thenReturn("");
    when(allergen.isCoded()).thenReturn(false);
    allergy.setAllergen(allergen);
    validator.validate(allergy, errors);
    assertTrue(errors.hasFieldErrors("allergen"));
    assertThat(errors.getFieldError("allergen").getCode(), is("allergyapi.allergen.nonCodedAllergen.required"));
}
Also used : Concept(org.openmrs.Concept) Allergen(org.openmrs.Allergen) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Allergen (org.openmrs.Allergen)9 Test (org.junit.Test)8 Allergies (org.openmrs.Allergies)6 Allergy (org.openmrs.Allergy)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Concept (org.openmrs.Concept)5 Patient (org.openmrs.Patient)5 BindException (org.springframework.validation.BindException)3 Errors (org.springframework.validation.Errors)3 MessageSourceService (org.openmrs.messagesource.MessageSourceService)2 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)2 AllergyReaction (org.openmrs.AllergyReaction)1