Search in sources :

Example 56 with DrugOrder

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

the class DrugOrderValidatorTest method saveOrder_shouldPassDrugOrderWithNeitherDrugNonCodedNorDrugAreSetForDrugOrderWhenDrugRequiredISNotSet.

@Test
public void saveOrder_shouldPassDrugOrderWithNeitherDrugNonCodedNorDrugAreSetForDrugOrderWhenDrugRequiredISNotSet() {
    executeDataSet("org/openmrs/api/include/OrderServiceTest-nonCodedDrugs.xml");
    Patient patient = Context.getPatientService().getPatient(7);
    CareSetting careSetting = Context.getOrderService().getCareSetting(2);
    OrderType orderType = Context.getOrderService().getOrderTypeByName("Drug order");
    GlobalProperty gp = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_DRUG_ORDER_REQUIRE_DRUG, "false");
    Context.getAdministrationService().saveGlobalProperty(gp);
    // place drug order
    DrugOrder order = new DrugOrder();
    Encounter encounter = Context.getEncounterService().getEncounter(3);
    order.setEncounter(encounter);
    order.setPatient(patient);
    order.setCareSetting(careSetting);
    order.setConcept(Context.getOrderService().getNonCodedDrugConcept());
    order.setOrderer(Context.getProviderService().getProvider(1));
    order.setDateActivated(encounter.getEncounterDatetime());
    order.setOrderType(orderType);
    order.setDosingType(FreeTextDosingInstructions.class);
    order.setInstructions("None");
    order.setDosingInstructions("Test Instruction");
    Errors errors = new BindException(order, "order");
    new DrugOrderValidator().validate(order, errors);
    Assert.assertFalse(errors.hasFieldErrors());
}
Also used : DrugOrder(org.openmrs.DrugOrder) Errors(org.springframework.validation.Errors) OrderType(org.openmrs.OrderType) Patient(org.openmrs.Patient) CareSetting(org.openmrs.CareSetting) Encounter(org.openmrs.Encounter) BindException(org.springframework.validation.BindException) GlobalProperty(org.openmrs.GlobalProperty) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest)

Example 57 with DrugOrder

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

the class DrugOrderValidatorTest method validate_shouldApplyValidationForACustomDosingType.

/**
 * @see DrugOrderValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldApplyValidationForACustomDosingType() {
    DrugOrder order = new DrugOrder();
    Encounter encounter = new Encounter();
    Patient patient = Context.getPatientService().getPatient(2);
    order.setConcept(Context.getConceptService().getConcept(88));
    order.setOrderer(Context.getProviderService().getProvider(1));
    order.setDosingType(CustomDosingInstructions.class);
    order.setInstructions("Instructions");
    order.setPatient(patient);
    encounter.setPatient(patient);
    order.setEncounter(encounter);
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - 1);
    order.setDateActivated(cal.getTime());
    order.setAutoExpireDate(new Date());
    order.setOrderType(Context.getOrderService().getOrderTypeByName("Drug order"));
    order.setDrug(Context.getConceptService().getDrug(3));
    order.setCareSetting(Context.getOrderService().getCareSetting(1));
    order.setQuantity(2.00);
    order.setQuantityUnits(Context.getConceptService().getConcept(51));
    order.setNumRefills(10);
    Errors errors = new BindException(order, "order");
    new DrugOrderValidator().validate(order, errors);
    Assert.assertTrue(errors.hasFieldErrors("brandName"));
}
Also used : DrugOrder(org.openmrs.DrugOrder) Errors(org.springframework.validation.Errors) Calendar(java.util.Calendar) Encounter(org.openmrs.Encounter) Patient(org.openmrs.Patient) BindException(org.springframework.validation.BindException) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest)

Example 58 with DrugOrder

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

the class DrugOrderValidator 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 asNeeded is null
 * @should fail validation if dosingType is null
 * @should fail validation if drug concept is different from order concept
 * @should fail validation if dose is null for SimpleDosingInstructions dosingType
 * @should fail validation if doseUnits is null for SimpleDosingInstructions dosingType
 * @should fail validation if route is null for SimpleDosingInstructions dosingType
 * @should fail validation if frequency is null for SimpleDosingInstructions dosingType
 * @should fail validation if dosingInstructions is null for FreeTextDosingInstructions
 *         dosingType
 * @should fail validation if numberOfRefills is null for outpatient careSetting
 * @should fail validation if quantity is null for outpatient careSetting
 * @should fail validation if doseUnits is null when dose is present
 * @should fail validation if doseUnits is not a dose unit concept
 * @should fail validation if quantityUnits is null when quantity is present
 * @should fail validation if quantityUnits it not a quantity unit concept
 * @should fail validation if durationUnits is null when duration is present
 * @should fail validation if durationUnits is not a duration unit concept
 * @should pass validation if all fields are correct
 * @should not require all fields for a discontinuation order
 * @should fail if route is not a valid concept
 * @should fail if concept is null and drug is not specified
 * @should fail if concept is null and cannot infer it from drug
 * @should pass if concept is null and drug is set
 * @should not validate a custom dosing type against any other dosing type validation
 * @should apply validation for a custom dosing type
 * @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);
    DrugOrder order = (DrugOrder) obj;
    if (order == null) {
        errors.reject("error.general");
    } else {
        // for the following elements Order.hbm.xml says: not-null="true"
        ValidationUtils.rejectIfEmpty(errors, "asNeeded", "error.null");
        if (order.getAction() != Order.Action.DISCONTINUE) {
            ValidationUtils.rejectIfEmpty(errors, "dosingType", "error.null");
        }
        if (order.getDrug() == null || order.getDrug().getConcept() == null) {
            ValidationUtils.rejectIfEmpty(errors, "concept", "error.null");
        }
        if (order.getConcept() != null && order.getDrug() != null && order.getDrug().getConcept() != null && !order.getDrug().getConcept().equals(order.getConcept())) {
            errors.rejectValue("drug", "error.general");
            errors.rejectValue("concept", "error.concept");
        }
        if (order.getAction() != Order.Action.DISCONTINUE && order.getDosingType() != null) {
            DosingInstructions dosingInstructions = order.getDosingInstructionsInstance();
            dosingInstructions.validate(order, errors);
        }
        validateFieldsForOutpatientCareSettingType(order, errors);
        validatePairedFields(order, errors);
        validateUnitsAreAmongAllowedConcepts(errors, order);
        validateForRequireDrug(errors, order);
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "asNeededCondition", "brandName");
    }
}
Also used : DrugOrder(org.openmrs.DrugOrder) DosingInstructions(org.openmrs.DosingInstructions)

Example 59 with DrugOrder

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

the class OrderServiceTest method saveOrder_shouldDiscontinuePreviousNonCodedOrderIfItIsNotAlreadyDiscontinued.

@Test
public void saveOrder_shouldDiscontinuePreviousNonCodedOrderIfItIsNotAlreadyDiscontinued() {
    // We are trying to discontinue order id 584 in OrderServiceTest-nonCodedDrugs.xml
    executeDataSet("org/openmrs/api/include/OrderServiceTest-nonCodedDrugs.xml");
    DrugOrder previousOrder = (DrugOrder) orderService.getOrder(584);
    DrugOrder drugOrder = previousOrder.cloneForDiscontinuing();
    drugOrder.setPreviousOrder(previousOrder);
    drugOrder.setDateActivated(new Date());
    drugOrder.setOrderer(previousOrder.getOrderer());
    drugOrder.setEncounter(previousOrder.getEncounter());
    Order saveOrder = orderService.saveOrder(drugOrder, null);
    Assert.assertNotNull("previous order should be discontinued", previousOrder.getDateStopped());
    assertNotNull(orderService.getOrder(saveOrder.getOrderId()));
}
Also used : DrugOrder(org.openmrs.DrugOrder) DrugOrder(org.openmrs.DrugOrder) TestOrder(org.openmrs.TestOrder) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Order(org.openmrs.Order) Date(java.util.Date) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest) Test(org.junit.Test)

Example 60 with DrugOrder

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

the class OrderServiceTest method saveOrder_shouldPassIfAnActiveDrugOrderForTheSameConceptAndDifferentDrugNonCodedExists.

/**
 * @see OrderService#saveOrder(Order, OrderContext)
 */
@Test
public void saveOrder_shouldPassIfAnActiveDrugOrderForTheSameConceptAndDifferentDrugNonCodedExists() {
    executeDataSet("org/openmrs/api/include/OrderServiceTest-nonCodedDrugs.xml");
    final Concept nonCodedConcept = orderService.getNonCodedDrugConcept();
    // sanity check that we have an active order for the same concept
    DrugOrder duplicateOrder = (DrugOrder) orderService.getOrder(584);
    assertTrue(duplicateOrder.isActive());
    assertEquals(nonCodedConcept, duplicateOrder.getConcept());
    DrugOrder drugOrder = duplicateOrder.copy();
    drugOrder.setDrugNonCoded("non coded drug paracetemol");
    Order savedOrder = orderService.saveOrder(drugOrder, null);
    assertNotNull(orderService.getOrder(savedOrder.getOrderId()));
}
Also used : Concept(org.openmrs.Concept) DrugOrder(org.openmrs.DrugOrder) DrugOrder(org.openmrs.DrugOrder) TestOrder(org.openmrs.TestOrder) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Order(org.openmrs.Order) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest) Test(org.junit.Test)

Aggregations

DrugOrder (org.openmrs.DrugOrder)68 Test (org.junit.Test)64 OrderUtilTest (org.openmrs.order.OrderUtilTest)64 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)64 BindException (org.springframework.validation.BindException)38 Errors (org.springframework.validation.Errors)38 Patient (org.openmrs.Patient)22 Date (java.util.Date)18 Encounter (org.openmrs.Encounter)17 Concept (org.openmrs.Concept)12 CareSetting (org.openmrs.CareSetting)11 Order (org.openmrs.Order)11 OrderType (org.openmrs.OrderType)11 TestOrder (org.openmrs.TestOrder)11 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)7 Drug (org.openmrs.Drug)7 GlobalProperty (org.openmrs.GlobalProperty)7 Calendar (java.util.Calendar)5 SimpleDateFormat (java.text.SimpleDateFormat)1 Ignore (org.junit.Ignore)1