Search in sources :

Example 6 with Allergen

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

the class AllergyValidatorTest method validate_shouldRejectADuplicateNonCodedAllergen.

/**
 * @see AllergyValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldRejectADuplicateNonCodedAllergen() {
    PowerMockito.mockStatic(Context.class);
    MessageSourceService ms = mock(MessageSourceService.class);
    when(Context.getMessageSourceService()).thenReturn(ms);
    Allergies allergies = new Allergies();
    Concept nonCodedConcept = createMockConcept(getOtherNonCodedConceptUuid());
    final String freeText = "some text";
    Allergen allergen1 = new Allergen(AllergenType.DRUG, nonCodedConcept, freeText);
    allergies.add(new Allergy(null, allergen1, null, null, null));
    when(ps.getAllergies(any(Patient.class))).thenReturn(allergies);
    Allergen duplicateAllergen = new Allergen(AllergenType.FOOD, nonCodedConcept, freeText);
    Allergy allergy = new Allergy(mock(Patient.class), duplicateAllergen, null, null, null);
    Errors errors = new BindException(allergy, "allergy");
    validator.validate(allergy, errors);
    assertTrue(errors.hasFieldErrors("allergen"));
    assertThat(errors.getFieldError("allergen").getCode(), is("allergyapi.message.duplicateAllergen"));
}
Also used : Concept(org.openmrs.Concept) Allergy(org.openmrs.Allergy) Errors(org.springframework.validation.Errors) MessageSourceService(org.openmrs.messagesource.MessageSourceService) 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 7 with Allergen

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

the class AllergyValidatorTest method validate_shouldFailIfCodedAndNonCodedAllergenAreNull.

/**
 * @see AllergyValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailIfCodedAndNonCodedAllergenAreNull() {
    Allergen allergen = new Allergen(AllergenType.DRUG, null, null);
    allergy.setAllergen(allergen);
    validator.validate(allergy, errors);
    assertTrue(errors.hasFieldErrors("allergen"));
    assertThat(errors.getFieldError("allergen").getCode(), is("allergyapi.allergen.codedOrNonCodedAllergen.required"));
}
Also used : Allergen(org.openmrs.Allergen) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with Allergen

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

the class AllergyValidatorTest method validate_shouldFailIdAllergenTypeIsNull.

/**
 * @see AllergyValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailIdAllergenTypeIsNull() {
    allergy.setAllergen(new Allergen());
    validator.validate(allergy, errors);
    assertTrue(errors.hasFieldErrors("allergen"));
    assertThat(errors.getFieldError("allergen").getCode(), is("allergyapi.allergenType.required"));
}
Also used : Allergen(org.openmrs.Allergen) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with Allergen

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

the class AllergyValidatorTest method validate_shouldRejectADuplicateAllergen.

/**
 * @see AllergyValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldRejectADuplicateAllergen() {
    PowerMockito.mockStatic(Context.class);
    MessageSourceService ms = mock(MessageSourceService.class);
    when(Context.getMessageSourceService()).thenReturn(ms);
    Allergies allergies = new Allergies();
    Concept aspirin = createMockConcept();
    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 duplicateAllergen = new Allergen(AllergenType.FOOD, aspirin, null);
    Allergy allergy = new Allergy(mock(Patient.class), duplicateAllergen, null, null, null);
    Errors errors = new BindException(allergy, "allergy");
    validator.validate(allergy, errors);
    assertTrue(errors.hasFieldErrors("allergen"));
    assertThat(errors.getFieldError("allergen").getCode(), is("allergyapi.message.duplicateAllergen"));
}
Also used : Concept(org.openmrs.Concept) Allergy(org.openmrs.Allergy) Errors(org.springframework.validation.Errors) MessageSourceService(org.openmrs.messagesource.MessageSourceService) 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

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