Search in sources :

Example 71 with Errors

use of org.springframework.validation.Errors in project openmrs-core by openmrs.

the class DrugOrderValidatorTest method validate_shouldFailValidationIfDoseUnitsIsNotADoseUnitConcept.

/**
 * @see DrugOrderValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfDoseUnitsIsNotADoseUnitConcept() {
    Concept concept = Context.getConceptService().getConcept(3);
    assertThat(concept, not(isIn(Context.getOrderService().getDrugDosingUnits())));
    DrugOrder order = new DrugOrder();
    order.setDosingType(FreeTextDosingInstructions.class);
    order.setDuration(5);
    order.setDurationUnits(concept);
    order.setDose(1.0);
    order.setDoseUnits(concept);
    order.setQuantity(30.0);
    order.setQuantityUnits(concept);
    Errors errors = new BindException(order, "order");
    new DrugOrderValidator().validate(order, errors);
    Assert.assertTrue(errors.hasFieldErrors("doseUnits"));
}
Also used : Concept(org.openmrs.Concept) DrugOrder(org.openmrs.DrugOrder) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest)

Example 72 with Errors

use of org.springframework.validation.Errors in project openmrs-core by openmrs.

the class DrugOrderValidatorTest method validate_shouldFailValidationIfDurationUnitsIsNotADurationUnitConcept.

/**
 * @see DrugOrderValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfDurationUnitsIsNotADurationUnitConcept() {
    Concept concept = Context.getConceptService().getConcept(3);
    assertThat(concept, not(isIn(Context.getOrderService().getDurationUnits())));
    DrugOrder order = new DrugOrder();
    order.setDosingType(FreeTextDosingInstructions.class);
    order.setDuration(5);
    order.setDurationUnits(concept);
    order.setDose(1.0);
    order.setDoseUnits(concept);
    order.setQuantity(30.0);
    order.setQuantityUnits(concept);
    Errors errors = new BindException(order, "order");
    new DrugOrderValidator().validate(order, errors);
    Assert.assertTrue(errors.hasFieldErrors("durationUnits"));
}
Also used : Concept(org.openmrs.Concept) DrugOrder(org.openmrs.DrugOrder) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest)

Example 73 with Errors

use of org.springframework.validation.Errors 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 74 with Errors

use of org.springframework.validation.Errors in project openmrs-core by openmrs.

the class CohortValidatorTest method validate_shouldPassIfMembershipStartDateIsAfterEndDate.

@Test
public void validate_shouldPassIfMembershipStartDateIsAfterEndDate() throws Exception {
    Cohort cohort = new Cohort(2);
    CohortMembership membership = new CohortMembership(patient.getPatientId());
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date startDate = dateFormat.parse("2016-11-01 00:00:00");
    Date endDate = dateFormat.parse("2015-01-01 00:00:00");
    membership.setStartDate(startDate);
    membership.setEndDate(endDate);
    Errors errors = new BindException(cohort, "cohort");
    new CohortValidator().validate(cohort, errors);
    Assert.assertFalse(errors.hasFieldErrors("memberships"));
}
Also used : Errors(org.springframework.validation.Errors) Cohort(org.openmrs.Cohort) BindException(org.springframework.validation.BindException) SimpleDateFormat(java.text.SimpleDateFormat) CohortMembership(org.openmrs.CohortMembership) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 75 with Errors

use of org.springframework.validation.Errors in project openmrs-core by openmrs.

the class ConceptClassValidatorTest method validate_shouldFailValidationIfFieldLengthsAreNotCorrect.

/**
 * @see ConceptClassValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() {
    ConceptClass cc = new ConceptClass();
    cc.setName("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    cc.setDescription("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    cc.setRetireReason("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    Errors errors = new BindException(cc, "cc");
    new ConceptClassValidator().validate(cc, errors);
    Assert.assertTrue(errors.hasFieldErrors("name"));
    Assert.assertTrue(errors.hasFieldErrors("description"));
    Assert.assertTrue(errors.hasFieldErrors("retireReason"));
}
Also used : Errors(org.springframework.validation.Errors) ConceptClass(org.openmrs.ConceptClass) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

Errors (org.springframework.validation.Errors)468 BindException (org.springframework.validation.BindException)387 Test (org.junit.Test)376 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)356 OrderUtilTest (org.openmrs.order.OrderUtilTest)58 DrugOrder (org.openmrs.DrugOrder)56 Date (java.util.Date)53 Test (org.junit.jupiter.api.Test)36 Patient (org.openmrs.Patient)36 TestBean (org.springframework.beans.testfixture.beans.TestBean)33 Calendar (java.util.Calendar)28 Concept (org.openmrs.Concept)25 TestOrder (org.openmrs.TestOrder)23 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)23 OrderType (org.openmrs.OrderType)22 Encounter (org.openmrs.Encounter)21 Obs (org.openmrs.Obs)20 Order (org.openmrs.Order)20 ConceptReferenceTerm (org.openmrs.ConceptReferenceTerm)19 PageContext (jakarta.servlet.jsp.PageContext)17