Search in sources :

Example 11 with OrderType

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

the class OrderServiceTest method saveOrder_shouldSetConceptForDrugOrdersIfNull.

/**
 * @see OrderService#saveOrder(org.openmrs.Order, OrderContext)
 */
@Test
public void saveOrder_shouldSetConceptForDrugOrdersIfNull() {
    Patient patient = patientService.getPatient(7);
    CareSetting careSetting = orderService.getCareSetting(2);
    OrderType orderType = orderService.getOrderTypeByName("Drug order");
    // place drug order
    DrugOrder order = new DrugOrder();
    Encounter encounter = encounterService.getEncounter(3);
    order.setEncounter(encounter);
    order.setPatient(patient);
    order.setDrug(conceptService.getDrug(2));
    order.setCareSetting(careSetting);
    order.setOrderer(Context.getProviderService().getProvider(1));
    order.setDateActivated(encounter.getEncounterDatetime());
    order.setOrderType(orderType);
    order.setDosingType(FreeTextDosingInstructions.class);
    order.setInstructions("None");
    order.setDosingInstructions("Test Instruction");
    orderService.saveOrder(order, null);
    assertNotNull(order.getOrderId());
}
Also used : DrugOrder(org.openmrs.DrugOrder) OrderType(org.openmrs.OrderType) Patient(org.openmrs.Patient) CareSetting(org.openmrs.CareSetting) Encounter(org.openmrs.Encounter) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest) Test(org.junit.Test)

Example 12 with OrderType

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

the class OrderServiceTest method saveOrderType_shouldEditAnExistingOrderType.

/**
 * @see OrderService#saveOrderType(org.openmrs.OrderType)
 */
@Test
public void saveOrderType_shouldEditAnExistingOrderType() {
    OrderType orderType = orderService.getOrderType(1);
    assertNull(orderType.getDateChanged());
    assertNull(orderType.getChangedBy());
    final String newDescription = "new";
    orderType.setDescription(newDescription);
    orderService.saveOrderType(orderType);
    Context.flushSession();
    assertNotNull(orderType.getDateChanged());
    assertNotNull(orderType.getChangedBy());
}
Also used : OrderType(org.openmrs.OrderType) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest) Test(org.junit.Test)

Example 13 with OrderType

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

the class OrderServiceTest method unretireOrderType_shouldUnretireOrderType.

/**
 * @see OrderService#unretireOrderType(org.openmrs.OrderType)
 */
@Test
public void unretireOrderType_shouldUnretireOrderType() {
    OrderType orderType = orderService.getOrderType(16);
    assertTrue(orderType.getRetired());
    assertNotNull(orderType.getRetiredBy());
    assertNotNull(orderType.getRetireReason());
    assertNotNull(orderType.getDateRetired());
    orderService.unretireOrderType(orderType);
    orderType = orderService.getOrderType(16);
    assertFalse(orderType.getRetired());
    assertNull(orderType.getRetiredBy());
    assertNull(orderType.getRetireReason());
    assertNull(orderType.getDateRetired());
}
Also used : OrderType(org.openmrs.OrderType) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest) Test(org.junit.Test)

Example 14 with OrderType

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

the class OrderTypeValidator method validate.

/**
 * Validates an Order object
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail if the orderType object is null
 * @should fail if name is null
 * @should fail if name is empty
 * @should fail if name is whitespace
 * @should fail if name is a duplicate
 * @should fail if conceptClass is a duplicate
 * @should fail if parent is among its descendants
 * @should fail if parent is also a direct child
 * @should pass if all fields are correct for a new order type
 * @should pass if all fields are correct for an existing order type
 * @should be invoked when an order type is saved
 * @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) {
    if (obj == null || !(obj instanceof OrderType)) {
        throw new IllegalArgumentException("The parameter obj should not be null and must be of type" + OrderType.class);
    } else {
        OrderType orderType = (OrderType) obj;
        String name = orderType.getName();
        if (!StringUtils.hasText(name)) {
            errors.rejectValue("name", "error.name");
            return;
        }
        if (orderType.getParent() != null && OrderUtil.isType(orderType, orderType.getParent())) {
            errors.rejectValue("parent", "OrderType.parent.amongDescendants", new Object[] { orderType.getName() }, "Parent of " + orderType.getName() + " is among its descendants");
        }
        OrderType duplicate = Context.getOrderService().getOrderTypeByName(name);
        if (duplicate != null && !orderType.equals(duplicate)) {
            errors.rejectValue("name", "OrderType.duplicate.name", "Duplicate order type name: " + name);
        }
        for (OrderType ot : Context.getOrderService().getOrderTypes(true)) {
            if (ot != null) {
                // If this was an edit, skip past the order we are actually validating
                if (orderType.equals(ot)) {
                    continue;
                }
                int index = 0;
                for (ConceptClass cc : ot.getConceptClasses()) {
                    if (cc != null && orderType.getConceptClasses().contains(cc)) {
                        errors.rejectValue("conceptClasses[" + index + "]", "OrderType.duplicate", new Object[] { cc.getName(), orderType.getName() }, cc.getName() + " is already associated to another order type:" + orderType.getName());
                    }
                    index++;
                }
            }
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "description", "retireReason", "javaClassName");
    }
}
Also used : ConceptClass(org.openmrs.ConceptClass) OrderType(org.openmrs.OrderType)

Example 15 with OrderType

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

the class DrugOrderValidatorTest method saveOrder_shouldFailDrugOrderWithNeitherDrugNonCodedNorDrugAreSetForDrugOrderWhenDrugRequiredSet.

@Test
public void saveOrder_shouldFailDrugOrderWithNeitherDrugNonCodedNorDrugAreSetForDrugOrderWhenDrugRequiredSet() {
    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, "true");
    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.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.assertTrue(errors.hasFieldErrors());
    assertEquals("DrugOrder.error.drugIsRequired", errors.getFieldError("drug").getCode());
}
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)

Aggregations

OrderType (org.openmrs.OrderType)54 Test (org.junit.Test)46 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)45 OrderUtilTest (org.openmrs.order.OrderUtilTest)29 BindException (org.springframework.validation.BindException)22 Errors (org.springframework.validation.Errors)22 Patient (org.openmrs.Patient)17 DrugOrder (org.openmrs.DrugOrder)16 CareSetting (org.openmrs.CareSetting)15 Encounter (org.openmrs.Encounter)11 GlobalProperty (org.openmrs.GlobalProperty)8 Order (org.openmrs.Order)8 TestOrder (org.openmrs.TestOrder)6 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)5 ConceptClass (org.openmrs.ConceptClass)5 HashSet (java.util.HashSet)4 Date (java.util.Date)3 ArrayList (java.util.ArrayList)2 Criteria (org.hibernate.Criteria)2 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)2