Skip to content

Commit

Permalink
Drug orders for different formulations of the same concept should be …
Browse files Browse the repository at this point in the history
…allowed - TRUNK-4436

TRUNK-4436: Improve the order service test and use nullSafeEquals

TRUNK-4436: Use nullSafeEquals in Order
  • Loading branch information
Miss Beens authored and wluyima committed Jul 25, 2014
1 parent dc8af1a commit dc48a37
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 13 deletions.
24 changes: 24 additions & 0 deletions api/src/main/java/org/openmrs/DrugOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package org.openmrs;

import org.openmrs.util.OpenmrsUtil;

/**
* DrugOrder
*
Expand Down Expand Up @@ -516,4 +518,26 @@ public String toString() {
public void setDosing(DosingInstructions di) {
di.setDosingInstructions(this);
}

/**
* Checks whether orderable of this drug order is same as other order
*
* @since 1.10
* @param otherOrder the other order to match on
* @return true if the drug of the orders match
* @should false if the concept of the orders do not match
* @should false if the drug of the orders do not match
* @should false if other order is null
* @should false if other order is not a drug order
* @should false if drugs are different but have same concept
*/
@Override
public boolean hasSameOrderableAs(Order otherOrder) {
if (!super.hasSameOrderableAs(otherOrder))
return false;
if (!(otherOrder instanceof DrugOrder))
return false;
DrugOrder otherDrugOrder = (DrugOrder) otherOrder;
return OpenmrsUtil.nullSafeEquals(this.getDrug(), otherDrugOrder.getDrug());
}
}
22 changes: 20 additions & 2 deletions api/src/main/java/org/openmrs/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
*/
package org.openmrs;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.api.db.hibernate.HibernateUtil;
import org.openmrs.order.OrderUtil;
import org.openmrs.util.OpenmrsUtil;

import java.util.Date;

/**
* Dates should be interpreted as follows: If startDate is null then the order has been going on
Expand Down Expand Up @@ -638,4 +639,21 @@ protected Order cloneForRevisionHelper(Order target) {
public boolean isType(OrderType orderType) {
return OrderUtil.isType(orderType, this.orderType);
}

/**
* Checks whether orderable of this order is same as other order
*
* @see org.openmrs.DrugOrder for overridden behaviour
*
* @since 1.10
* @param otherOrder the other order to match on
* @return true if the concept of the orders match
* @should false if the concept of the orders do not match
* @should false if other order is null
*/
public boolean hasSameOrderableAs(Order otherOrder) {
if (otherOrder == null)
return false;
return OpenmrsUtil.nullSafeEquals(this.getConcept(), otherOrder.getConcept());
}
}
6 changes: 3 additions & 3 deletions api/src/main/java/org/openmrs/api/impl/OrderServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ public synchronized Order saveOrder(Order order, OrderContext orderContext) thro

if (!isDiscontinueOrReviseOrder(order)) {
List<Order> activeOrders = getActiveOrders(order.getPatient(), null, order.getCareSetting(), null);
for (Order o : activeOrders) {
if (o.getConcept().equals(concept)) {
throw new APIException("Cannot have more than one active order for the same concept and care setting");
for (Order activeOrder : activeOrders) {
if (order.hasSameOrderableAs(activeOrder)) {
throw new APIException("Cannot have more than one active order for the same orderable and care setting");
}
}
}
Expand Down
93 changes: 90 additions & 3 deletions api/src/test/java/org/openmrs/DrugOrderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
*/
package org.openmrs;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.util.Date;

import org.junit.Test;
import org.openmrs.order.OrderUtilTest;

import static org.junit.Assert.*;

/**
* Contains tests for DrugOrder
*/
Expand Down Expand Up @@ -110,4 +109,92 @@ public void cloneForRevision_shouldSetTheRelevantFieldsForADCOrder() throws Exce
assertNull(clone.getDateStopped());
assertNull(clone.getAccessionNumber());
}


/**
* @verifies false if the concept of the orders do not match
* @see DrugOrder#hasSameOrderableAs(Order)
*/
@Test
public void hasSameOrderableAs_shouldBeFalseIfConceptsDoNotMatch() {
DrugOrder order = new DrugOrder();
Drug drug1 = new Drug();
drug1.setConcept(new Concept(123));
order.setDrug(drug1);

DrugOrder otherOrder = new DrugOrder();
Drug drug2 = new Drug();
drug2.setConcept(new Concept(456));
otherOrder.setDrug(drug2);

assertFalse(order.hasSameOrderableAs(otherOrder));
}

/**
* @verifies false if other order is not a drug order
* @see DrugOrder#hasSameOrderableAs(Order)
*/
@Test
public void hasSameOrderableAs_shouldBeFalseIfOtherOrderIsNotADrugOrder() {
DrugOrder order = new DrugOrder();
Drug drug1 = new Drug();
Concept concept = new Concept(123);
drug1.setConcept(concept);
order.setDrug(drug1);

Order otherOrder = new Order();
otherOrder.setConcept(concept);

assertFalse(order.hasSameOrderableAs(otherOrder));
}

/**
* @verifies false if other order is null
* @see DrugOrder#hasSameOrderableAs(Order)
*/
@Test
public void hasSameOrderableAs_shouldBeFalseIfOtherOrderIsNull() {
DrugOrder order = new DrugOrder();
order.setConcept(new Concept(123));

assertFalse(order.hasSameOrderableAs(null));
}

/**
* @verifies false if drugs are different but have same concept
* @see DrugOrder#hasSameOrderableAs(Order)
*/
@Test
public void hasSameOrderableAs_shouldBeFalseIfDrugsAreDifferentButConceptsMatch() {
DrugOrder order = new DrugOrder();
Concept concept = new Concept();
Drug drug1 = new Drug();
drug1.setConcept(concept);
order.setDrug(drug1);

DrugOrder otherOrder = new DrugOrder();
Drug drug2 = new Drug();
drug2.setConcept(concept);
otherOrder.setDrug(drug2);

assertFalse(order.hasSameOrderableAs(otherOrder));
}

/**
* @verifies true if the drug of the orders match
* @see DrugOrder#hasSameOrderableAs(Order)
*/
@Test
public void hasSameOrderableAs_shouldBeTrueIfDrugsMatch() {
DrugOrder order = new DrugOrder();
Concept concept = new Concept();
Drug drug1 = new Drug();
drug1.setConcept(concept);
order.setDrug(drug1);

DrugOrder otherOrder = new DrugOrder();
otherOrder.setDrug(drug1);

assertTrue(order.hasSameOrderableAs(otherOrder));
}
}
48 changes: 44 additions & 4 deletions api/src/test/java/org/openmrs/OrderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.*;

import org.apache.commons.beanutils.MethodUtils;
import org.junit.Test;
Expand Down Expand Up @@ -252,4 +249,47 @@ public void cloneForRevision_shouldSetTheRelevantFieldsForADCOrder() throws Exce
assertNull(clone.getDateStopped());
assertNull(clone.getAccessionNumber());
}

/**
* @verifies false if the concept of the orders do not match
* @see Order#hasSameOrderableAs(Order)
*/
@Test
public void hasSameOrderableAs_shouldBeFalseIfConceptsDoNotMatch() {
Order order = new Order();
order.setConcept(new Concept(123));

Order otherOrder = new Order();
otherOrder.setConcept(new Concept(456));

assertFalse(order.hasSameOrderableAs(otherOrder));
}

/**
* @verifies false if other order is null
* @see Order#hasSameOrderableAs(Order)
*/
@Test
public void hasSameOrderableAs_shouldBeFalseIfOtherOrderIsNull() {
Order order = new Order();
order.setConcept(new Concept(123));

assertFalse(order.hasSameOrderableAs(null));
}

/**
* @verifies true if the concept of the orders match
* @see Order#hasSameOrderableAs(Order)
*/
@Test
public void hasSameOrderableAs_shouldBeTrueIfConceptsMatch() {
Order order = new Order();
Concept concept = new Concept();
order.setConcept(concept);

Order otherOrder = new Order();
otherOrder.setConcept(concept);

assertTrue(order.hasSameOrderableAs(otherOrder));
}
}
63 changes: 62 additions & 1 deletion api/src/test/java/org/openmrs/api/OrderServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1405,10 +1405,71 @@ public void saveOrder_shouldFailIfAnActiveOrderForTheSameConceptAndCareSettingEx
order.setCareSetting(duplicateOrder.getCareSetting());

expectedException.expect(APIException.class);
expectedException.expectMessage("Cannot have more than one active order for the same concept and care setting");
expectedException.expectMessage("Cannot have more than one active order for the same orderable and care setting");
orderService.saveOrder(order, null);
}

/**
* @verifies pass if an active drug order for the same concept and care setting but different formulation exists
* @see OrderService#saveOrder(org.openmrs.Order, OrderContext)
*/
@Test
public void saveOrder_shouldPassIfAnActiveDrugOrderForTheSameConceptAndCareSettingButDifferentFormulationExists() throws Exception {
executeDataSet("org/openmrs/api/include/OrderServiceTest-drugOrdersWitSameConceptAndDifferentFormAndStrength.xml");
final Patient patient = patientService.getPatient(2);
//sanity check that we have an active order
DrugOrder existingOrder = (DrugOrder) orderService.getOrder(1000);
assertTrue(existingOrder.isCurrent());
//New Drug order
DrugOrder order = new DrugOrder();
order.setPatient(patient);
order.setConcept(existingOrder.getConcept());
order.setEncounter(encounterService.getEncounter(6));
order.setOrderer(providerService.getProvider(1));
order.setCareSetting(existingOrder.getCareSetting());
order.setDrug(conceptService.getDrug(3001));
order.setDosingType(DrugOrder.DosingType.FREE_TEXT);
order.setDosingInstructions("2 for 5 days");
order.setQuantity(10.0);
order.setQuantityUnits(conceptService.getConcept(51));
order.setNumRefills(2);

Order savedDrugOrder = orderService.saveOrder(order, null);

assertNotNull(orderService.getOrder(savedDrugOrder.getOrderId()));
}

/**
* @verifies fail if an active drug order for the same drug formulation exists
* @see OrderService#saveOrder(org.openmrs.Order, OrderContext)
*/
@Test
public void saveOrder_shouldFailIfAnActiveOrderForTheSanmeDrugFormulationExists()
throws Exception {
executeDataSet("org/openmrs/api/include/OrderServiceTest-drugOrdersWitSameConceptAndDifferentFormAndStrength.xml");
final Patient patient = patientService.getPatient(2);
//sanity check that we have an active order for the same concept
DrugOrder existingOrder = (DrugOrder) orderService.getOrder(1000);
assertTrue(existingOrder.isCurrent());

//New Drug order
DrugOrder order = new DrugOrder();
order.setPatient(patient);
order.setDrug(existingOrder.getDrug());
order.setEncounter(encounterService.getEncounter(6));
order.setOrderer(providerService.getProvider(1));
order.setCareSetting(existingOrder.getCareSetting());
order.setDosingType(DrugOrder.DosingType.FREE_TEXT);
order.setDosingInstructions("2 for 5 days");
order.setQuantity(10.0);
order.setQuantityUnits(conceptService.getConcept(51));
order.setNumRefills(2);

expectedException.expect(APIException.class);
expectedException.expectMessage("Cannot have more than one active order for the same orderable and care setting");
Order savedDrugOrder = orderService.saveOrder(order, null);
}

/**
* @verifies pass if an active order for the same concept exists in a different care setting
* @see OrderService#saveOrder(org.openmrs.Order, OrderContext)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<concept concept_id="1000" retired="0" datatype_id="4" class_id="3" is_set="0" creator="1" date_created="2004-08-12 00:00:00.0" version="" uuid="8e02d1a0-7869-11e3-981f-0800200c9a76"/>
<concept_name concept_name_id="2000" concept_id="1000" name="Paracetamol" locale="en" creator="1" date_created="2008-08-15 13:52:53.0" concept_name_type="FULLY_SPECIFIED" locale_preferred="1" voided="0" uuid="93f24a00-7869-11e3-981f-0800200c9a67"/>
<concept concept_id="1001" retired="0" datatype_id="4" class_id="11" is_set="0" creator="1" date_created="2004-08-12 00:00:00.0" version="" uuid="290cb421-4e42-425e-b8d3-0b91224b7994"/>
<concept_name concept_name_id="2001" concept_id="1001" name="Tablet" locale="en" creator="1" date_created="2008-08-15 13:52:53.0" concept_name_type="FULLY_SPECIFIED" locale_preferred="1" voided="0" uuid="34d4d8c1-988b-4667-b127-199ab44f6e8e"/>
<concept concept_id="1002" retired="0" datatype_id="4" class_id="11" is_set="0" creator="1" date_created="2004-08-12 00:00:00.0" version="" uuid="6be8113a-f1a3-43bb-b661-a276a1330360"/>
<concept_name concept_name_id="2002" concept_id="1002" name="Injection" locale="en" creator="1" date_created="2008-08-15 13:52:53.0" concept_name_type="FULLY_SPECIFIED" locale_preferred="1" voided="0" uuid="4a6def87-5766-40f6-80ce-003f73b126df"/>
<drug drug_id="3000" concept_id="1000" name="Paracetamol 500mg" dosage_form="1001" combination="false" strength="500mg" creator="1" date_created="2005-02-24 00:00:00.0" retired="false" uuid="e338a9b5-d6a6-419b-a381-1b2461f0d886"/>
<drug drug_id="3001" concept_id="1000" name="Paracetamol 2ml" dosage_form="1002" combination="false" strength="2ml" creator="1" date_created="2005-02-24 00:00:00.0" retired="false" uuid="865fceb1-ad0c-4102-aead-26ca25c77b09"/>
<orders order_id="1000" order_type_id="1" order_number="ORD-1000" urgency="ROUTINE" order_action="NEW" concept_id="1000" orderer="1" start_date="2008-10-19 09:24:10.0" creator="1" date_created="2008-10-19 09:24:10.0" voided="false" patient_id="2" uuid="56b9196c-bcac-4c2f-b3a2-625264a96439" care_setting="1" encounter_id="6" />
<drug_order order_id="1000" drug_inventory_id="3000" dose="3" dose_units="50" as_needed="false" frequency="1" dosing_type="SIMPLE" route="22" quantity="1.0" quantity_units="2" num_refills="10" dispense_as_written="0" />
</dataset>

0 comments on commit dc48a37

Please sign in to comment.