Search in sources :

Example 1 with OrderBuilder

use of org.openmrs.api.builder.OrderBuilder in project openmrs-core by openmrs.

the class OrderServiceTest method shouldSetTheCorrectSortWeightWhenAddingAnOrderWithANegativePosition.

@Test
public void shouldSetTheCorrectSortWeightWhenAddingAnOrderWithANegativePosition() {
    executeDataSet(ORDER_SET);
    Encounter encounter = encounterService.getEncounter(3);
    OrderSet orderSet = Context.getOrderSetService().getOrderSet(2000);
    OrderGroup orderGroup = new OrderGroup();
    orderGroup.setOrderSet(orderSet);
    orderGroup.setPatient(encounter.getPatient());
    orderGroup.setEncounter(encounter);
    Order firstOrderWithOrderGroup = new OrderBuilder().withAction(Order.Action.NEW).withPatient(7).withConcept(1000).withCareSetting(1).withOrderer(1).withEncounter(3).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(orderGroup).build();
    Order secondOrderWithOrderGroup = new OrderBuilder().withAction(Order.Action.NEW).withPatient(7).withConcept(1001).withCareSetting(1).withOrderer(1).withEncounter(3).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(orderGroup).build();
    Set<Order> orders = new LinkedHashSet<>();
    orders.add(firstOrderWithOrderGroup);
    orders.add(secondOrderWithOrderGroup);
    encounter.setOrders(orders);
    for (OrderGroup og : encounter.getOrderGroups()) {
        if (og.getId() == null) {
            Context.getOrderService().saveOrderGroup(og);
        }
    }
    Context.flushSession();
    OrderGroup savedOrderGroup = Context.getOrderService().getOrderGroupByUuid(orderGroup.getUuid());
    Order newOrderWithNegativePosition = new OrderBuilder().withAction(Order.Action.NEW).withPatient(7).withConcept(1000).withCareSetting(1).withOrderer(1).withEncounter(3).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(savedOrderGroup).build();
    savedOrderGroup.addOrder(newOrderWithNegativePosition, -1);
    Context.getOrderService().saveOrderGroup(savedOrderGroup);
    Context.flushSession();
    OrderGroup secondSavedOrderGroup = Context.getOrderService().getOrderGroupByUuid(orderGroup.getUuid());
    assertEquals(3, secondSavedOrderGroup.getOrders().size());
    assertEquals("The new order gets added at the last position", newOrderWithNegativePosition.getUuid(), secondSavedOrderGroup.getOrders().get(2).getUuid());
    assertThat("The new order has a higher sortWeight than the second", secondSavedOrderGroup.getOrders().get(2).getSortWeight().compareTo(secondSavedOrderGroup.getOrders().get(1).getSortWeight()), is(1));
    Order newOrderWithInvalidPosition = new OrderBuilder().withAction(Order.Action.NEW).withPatient(7).withConcept(1000).withCareSetting(1).withOrderer(1).withEncounter(3).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(savedOrderGroup).build();
    expectedException.expect(APIException.class);
    expectedException.expectMessage("Cannot add a member which is out of range of the list");
    secondSavedOrderGroup.addOrder(newOrderWithInvalidPosition, secondSavedOrderGroup.getOrders().size() + 1);
}
Also used : DrugOrder(org.openmrs.DrugOrder) TestOrder(org.openmrs.TestOrder) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Order(org.openmrs.Order) LinkedHashSet(java.util.LinkedHashSet) OrderBuilder(org.openmrs.api.builder.OrderBuilder) OrderGroup(org.openmrs.OrderGroup) OrderSet(org.openmrs.OrderSet) Encounter(org.openmrs.Encounter) Date(java.util.Date) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest) Test(org.junit.Test)

Example 2 with OrderBuilder

use of org.openmrs.api.builder.OrderBuilder in project openmrs-core by openmrs.

the class OrderServiceTest method shouldSetTheCorrectSortWeightWhenAddingAnOrderInOrderGroup.

@Test
public void shouldSetTheCorrectSortWeightWhenAddingAnOrderInOrderGroup() {
    executeDataSet(ORDER_SET);
    Encounter encounter = encounterService.getEncounter(3);
    OrderSet orderSet = Context.getOrderSetService().getOrderSet(2000);
    OrderGroup orderGroup = new OrderGroup();
    orderGroup.setOrderSet(orderSet);
    orderGroup.setPatient(encounter.getPatient());
    orderGroup.setEncounter(encounter);
    Order firstOrderWithOrderGroup = new OrderBuilder().withAction(Order.Action.NEW).withPatient(7).withConcept(1000).withCareSetting(1).withOrderer(1).withEncounter(3).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(orderGroup).build();
    Order secondOrderWithOrderGroup = new OrderBuilder().withAction(Order.Action.NEW).withPatient(7).withConcept(1001).withCareSetting(1).withOrderer(1).withEncounter(3).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(orderGroup).build();
    Set<Order> orders = new LinkedHashSet<>();
    orders.add(firstOrderWithOrderGroup);
    orders.add(secondOrderWithOrderGroup);
    encounter.setOrders(orders);
    for (OrderGroup og : encounter.getOrderGroups()) {
        if (og.getId() == null) {
            Context.getOrderService().saveOrderGroup(og);
        }
    }
    Context.flushSession();
    OrderGroup savedOrderGroup = Context.getOrderService().getOrderGroupByUuid(orderGroup.getUuid());
    assertEquals("The first order in  savedOrderGroup is the same which is sent first in the List", firstOrderWithOrderGroup.getUuid(), savedOrderGroup.getOrders().get(0).getUuid());
    assertEquals("The second order in  savedOrderGroup is the same which is sent second in the List", secondOrderWithOrderGroup.getUuid(), savedOrderGroup.getOrders().get(1).getUuid());
    assertThat("The first order has a lower sortWeight than the second", savedOrderGroup.getOrders().get(0).getSortWeight().compareTo(savedOrderGroup.getOrders().get(1).getSortWeight()), is(-1));
    Order newOrderWithoutAnyPosition = new OrderBuilder().withAction(Order.Action.NEW).withPatient(7).withConcept(1000).withCareSetting(1).withOrderer(1).withEncounter(3).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(savedOrderGroup).build();
    savedOrderGroup.addOrder(newOrderWithoutAnyPosition);
    Context.getOrderService().saveOrderGroup(savedOrderGroup);
    Context.flushSession();
    OrderGroup secondSavedOrderGroup = Context.getOrderService().getOrderGroupByUuid(orderGroup.getUuid());
    assertEquals("The first order in  savedOrderGroup is the same which is sent first in the List", firstOrderWithOrderGroup.getUuid(), savedOrderGroup.getOrders().get(0).getUuid());
    assertEquals("The second order in  savedOrderGroup is the same which is sent second in the List", secondOrderWithOrderGroup.getUuid(), savedOrderGroup.getOrders().get(1).getUuid());
    assertEquals("The third order in  savedOrderGroup is the same which is sent third in the List", secondSavedOrderGroup.getOrders().get(2).getUuid(), newOrderWithoutAnyPosition.getUuid());
    assertThat("The third order has a higher sortWeight than the second", savedOrderGroup.getOrders().get(2).getSortWeight().compareTo(savedOrderGroup.getOrders().get(1).getSortWeight()), is(1));
}
Also used : DrugOrder(org.openmrs.DrugOrder) TestOrder(org.openmrs.TestOrder) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Order(org.openmrs.Order) LinkedHashSet(java.util.LinkedHashSet) OrderBuilder(org.openmrs.api.builder.OrderBuilder) OrderGroup(org.openmrs.OrderGroup) OrderSet(org.openmrs.OrderSet) Encounter(org.openmrs.Encounter) Date(java.util.Date) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest) Test(org.junit.Test)

Example 3 with OrderBuilder

use of org.openmrs.api.builder.OrderBuilder in project openmrs-core by openmrs.

the class OrderServiceTest method shouldSaveOrdersWithSortWeightWhenWithinAOrderGroup.

@Test
public void shouldSaveOrdersWithSortWeightWhenWithinAOrderGroup() {
    executeDataSet(ORDER_SET);
    Encounter encounter = encounterService.getEncounter(3);
    OrderSet orderSet = Context.getOrderSetService().getOrderSet(2000);
    OrderGroup orderGroup = new OrderGroup();
    orderGroup.setOrderSet(orderSet);
    orderGroup.setPatient(encounter.getPatient());
    orderGroup.setEncounter(encounter);
    Order firstOrderWithOrderGroup = new OrderBuilder().withAction(Order.Action.NEW).withPatient(7).withConcept(1000).withCareSetting(1).withOrderer(1).withEncounter(3).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(orderGroup).build();
    Order secondOrderWithOrderGroup = new OrderBuilder().withAction(Order.Action.NEW).withPatient(7).withConcept(1001).withCareSetting(1).withOrderer(1).withEncounter(3).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(orderGroup).build();
    Order orderWithoutOrderGroup = new OrderBuilder().withAction(Order.Action.NEW).withPatient(7).withConcept(1000).withCareSetting(1).withOrderer(1).withEncounter(3).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).build();
    Set<Order> orders = new LinkedHashSet<>();
    orders.add(firstOrderWithOrderGroup);
    orders.add(secondOrderWithOrderGroup);
    orders.add(orderWithoutOrderGroup);
    encounter.setOrders(orders);
    for (OrderGroup og : encounter.getOrderGroups()) {
        if (og.getId() == null) {
            Context.getOrderService().saveOrderGroup(og);
        }
    }
    for (Order o : encounter.getOrdersWithoutOrderGroups()) {
        if (o.getId() == null) {
            Context.getOrderService().saveOrder(o, null);
        }
    }
    Context.flushSession();
    OrderGroup savedOrderGroup = Context.getOrderService().getOrderGroupByUuid(orderGroup.getUuid());
    Order savedOrder = Context.getOrderService().getOrderByUuid(orderWithoutOrderGroup.getUuid());
    assertEquals("The first order in  savedOrderGroup is the same which is sent first in the List", firstOrderWithOrderGroup.getUuid(), savedOrderGroup.getOrders().get(0).getUuid());
    assertEquals("The second order in  savedOrderGroup is the same which is sent second in the List", secondOrderWithOrderGroup.getUuid(), savedOrderGroup.getOrders().get(1).getUuid());
    assertNull("The order which doesn't belong to an orderGroup has no sortWeight", savedOrder.getSortWeight());
    assertThat("The first order has a lower sortWeight than the second", savedOrderGroup.getOrders().get(0).getSortWeight().compareTo(savedOrderGroup.getOrders().get(1).getSortWeight()), is(-1));
}
Also used : DrugOrder(org.openmrs.DrugOrder) TestOrder(org.openmrs.TestOrder) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Order(org.openmrs.Order) LinkedHashSet(java.util.LinkedHashSet) OrderBuilder(org.openmrs.api.builder.OrderBuilder) OrderGroup(org.openmrs.OrderGroup) OrderSet(org.openmrs.OrderSet) Encounter(org.openmrs.Encounter) Date(java.util.Date) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest) Test(org.junit.Test)

Example 4 with OrderBuilder

use of org.openmrs.api.builder.OrderBuilder in project openmrs-core by openmrs.

the class EncounterServiceTest method shouldSaveMultipleOrderGroupsIfDifferentOrdersHaveDifferentOrderGroups.

@Test
public void shouldSaveMultipleOrderGroupsIfDifferentOrdersHaveDifferentOrderGroups() {
    executeDataSet(ORDER_SET);
    Encounter encounter = new Encounter();
    encounter.setPatient(Context.getPatientService().getPatient(3));
    encounter.setEncounterType(Context.getEncounterService().getEncounterType(1));
    encounter.setEncounterDatetime(new Date());
    Context.getEncounterService().saveEncounter(encounter);
    Integer encounterId = Context.getEncounterService().getEncounterByUuid(encounter.getUuid()).getId();
    // Created a new OrderGroup
    OrderSet orderSet = Context.getOrderSetService().getOrderSet(2000);
    OrderGroup orderGroup = new OrderGroup();
    orderGroup.setOrderSet(orderSet);
    orderGroup.setPatient(encounter.getPatient());
    orderGroup.setEncounter(Context.getEncounterService().getEncounter(encounterId));
    // Created a new OrderGroup
    OrderGroup orderGroup2 = new OrderGroup();
    orderGroup2.setEncounter(Context.getEncounterService().getEncounter(encounterId));
    orderGroup2.setPatient(encounter.getPatient());
    // Added this OrderGroup to two new orders
    Order newOrder1 = new OrderBuilder().withAction(Order.Action.NEW).withPatient(3).withConcept(1000).withCareSetting(1).withOrderer(1).withEncounter(encounterId).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(orderGroup).build();
    Order newOrder2 = new OrderBuilder().withAction(Order.Action.NEW).withPatient(3).withConcept(1001).withCareSetting(1).withOrderer(1).withEncounter(encounterId).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(orderGroup).build();
    Order newOrder3 = new OrderBuilder().withAction(Order.Action.NEW).withPatient(3).withConcept(1002).withCareSetting(1).withOrderer(1).withEncounter(encounterId).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(orderGroup2).build();
    Order newOrder4 = new OrderBuilder().withAction(Order.Action.NEW).withPatient(3).withConcept(1000).withCareSetting(1).withOrderer(1).withEncounter(encounterId).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).build();
    Order newOrder5 = new OrderBuilder().withAction(Order.Action.NEW).withPatient(3).withConcept(1001).withCareSetting(1).withOrderer(1).withEncounter(encounterId).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(orderGroup2).build();
    // Add these orders to the Encounter
    encounter.addOrder(newOrder1);
    encounter.addOrder(newOrder2);
    encounter.addOrder(newOrder3);
    encounter.addOrder(newOrder4);
    encounter.addOrder(newOrder5);
    Context.getEncounterService().saveEncounter(encounter);
    Context.flushSession();
    List<Order> orders = new ArrayList<>(Context.getEncounterService().getEncounterByUuid(encounter.getUuid()).getOrders());
    HashMap<Integer, OrderGroup> orderGroups = new HashMap<>();
    for (Order order : orders) {
        if (order.getOrderGroup() != null) {
            orderGroups.put(order.getOrderGroup().getId(), order.getOrderGroup());
        }
    }
    assertEquals("Two New Order Groups Get Saved", 2, orderGroups.size());
}
Also used : TestOrder(org.openmrs.TestOrder) Order(org.openmrs.Order) OrderBuilder(org.openmrs.api.builder.OrderBuilder) OrderGroup(org.openmrs.OrderGroup) HashMap(java.util.HashMap) OrderSet(org.openmrs.OrderSet) ArrayList(java.util.ArrayList) Encounter(org.openmrs.Encounter) Date(java.util.Date) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 5 with OrderBuilder

use of org.openmrs.api.builder.OrderBuilder in project openmrs-core by openmrs.

the class EncounterServiceTest method shouldSaveOrderGroupAlongWithOrders.

@Test
public void shouldSaveOrderGroupAlongWithOrders() {
    executeDataSet(ORDER_SET);
    // Created a new Encounter
    Encounter encounter = new Encounter();
    encounter.setPatient(Context.getPatientService().getPatient(3));
    encounter.setEncounterType(Context.getEncounterService().getEncounterType(1));
    encounter.setEncounterDatetime(new Date());
    Context.getEncounterService().saveEncounter(encounter);
    Integer encounterId = Context.getEncounterService().getEncounterByUuid(encounter.getUuid()).getId();
    // Created a new OrderGroup
    OrderSet orderSet = Context.getOrderSetService().getOrderSet(2000);
    OrderGroup orderGroup = new OrderGroup();
    orderGroup.setOrderSet(orderSet);
    orderGroup.setPatient(encounter.getPatient());
    orderGroup.setEncounter(Context.getEncounterService().getEncounter(encounterId));
    // Added this OrderGroup to two new orders
    Order firstOrderWithOrderGroup = new OrderBuilder().withAction(Order.Action.NEW).withPatient(3).withConcept(1000).withCareSetting(1).withOrderer(1).withEncounter(encounterId).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(orderGroup).build();
    Order secondOrderWithOrderGroup = new OrderBuilder().withAction(Order.Action.NEW).withPatient(3).withConcept(1001).withCareSetting(1).withOrderer(1).withEncounter(encounterId).withDateActivated(new Date()).withOrderType(17).withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).withOrderGroup(orderGroup).build();
    // Add these orders to the Encounter
    encounter.addOrder(firstOrderWithOrderGroup);
    encounter.addOrder(secondOrderWithOrderGroup);
    Context.getEncounterService().saveEncounter(encounter);
    Context.flushSession();
    List<Order> orders = new ArrayList<>(Context.getEncounterService().getEncounterByUuid(encounter.getUuid()).getOrders());
    assertNotNull("OrderGroup is saved", orders.get(0).getOrderGroup());
    assertEquals("OrderGroup isa same for both the orders ", true, orders.get(0).getOrderGroup().equals(orders.get(1).getOrderGroup()));
}
Also used : TestOrder(org.openmrs.TestOrder) Order(org.openmrs.Order) OrderBuilder(org.openmrs.api.builder.OrderBuilder) OrderGroup(org.openmrs.OrderGroup) OrderSet(org.openmrs.OrderSet) ArrayList(java.util.ArrayList) Encounter(org.openmrs.Encounter) Date(java.util.Date) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Aggregations

Date (java.util.Date)9 Test (org.junit.Test)9 Order (org.openmrs.Order)9 OrderGroup (org.openmrs.OrderGroup)9 OrderBuilder (org.openmrs.api.builder.OrderBuilder)9 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)9 TestOrder (org.openmrs.TestOrder)8 DrugOrder (org.openmrs.DrugOrder)6 Encounter (org.openmrs.Encounter)6 OrderSet (org.openmrs.OrderSet)6 OrderUtilTest (org.openmrs.order.OrderUtilTest)6 LinkedHashSet (java.util.LinkedHashSet)4 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)4 ArrayList (java.util.ArrayList)2 BindException (org.springframework.validation.BindException)2 Errors (org.springframework.validation.Errors)2 HashMap (java.util.HashMap)1