Search in sources :

Example 6 with Allergy

use of org.openmrs.Allergy 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 7 with Allergy

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

the class PatientServiceAllergyTest method getAllergyByUuid_shouldGetAllergyByUuid.

/**
 * @see PatientService#getAllergies(Patient)
 */
@Test
public void getAllergyByUuid_shouldGetAllergyByUuid() {
    Allergy allergy = allergyService.getAllergyByUuid("21543629-7d8c-11e1-909d-c80aa9edcf4e");
    Assert.assertNotNull(allergy);
}
Also used : Allergy(org.openmrs.Allergy) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 8 with Allergy

use of org.openmrs.Allergy 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 9 with Allergy

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

the class AllergyValidatorTest method setUp.

@Before
public void setUp() {
    allergy = new Allergy();
    errors = new BindException(allergy, "allergy");
}
Also used : Allergy(org.openmrs.Allergy) BindException(org.springframework.validation.BindException) Before(org.junit.Before)

Example 10 with Allergy

use of org.openmrs.Allergy 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)

Aggregations

Allergy (org.openmrs.Allergy)18 Allergies (org.openmrs.Allergies)15 Test (org.junit.Test)14 Patient (org.openmrs.Patient)13 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)11 Concept (org.openmrs.Concept)9 Allergen (org.openmrs.Allergen)6 AllergyReaction (org.openmrs.AllergyReaction)4 BindException (org.springframework.validation.BindException)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Errors (org.springframework.validation.Errors)3 MessageSourceService (org.openmrs.messagesource.MessageSourceService)2 Before (org.junit.Before)1 APIException (org.openmrs.api.APIException)1 BlankIdentifierException (org.openmrs.api.BlankIdentifierException)1 DuplicateIdentifierException (org.openmrs.api.DuplicateIdentifierException)1 InsufficientIdentifiersException (org.openmrs.api.InsufficientIdentifiersException)1 MissingRequiredIdentifierException (org.openmrs.api.MissingRequiredIdentifierException)1 PatientIdentifierException (org.openmrs.api.PatientIdentifierException)1 PatientIdentifierTypeLockedException (org.openmrs.api.PatientIdentifierTypeLockedException)1