Skip to content

Commit 9445bd4

Browse files
committedMar 21, 2014
Made follow up to add utility method for testing order type hierarchies - TRUNK-4290
1 parent df704f6 commit 9445bd4

File tree

3 files changed

+101
-11
lines changed

3 files changed

+101
-11
lines changed
 

‎api/src/main/java/org/openmrs/Order.java

+2-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.apache.commons.logging.Log;
1919
import org.apache.commons.logging.LogFactory;
20+
import org.openmrs.order.OrderUtil;
2021

2122
/**
2223
* Dates should be interpreted as follows: If startDate is null then the order has been going on
@@ -628,16 +629,6 @@ public Order cloneForRevision() {
628629
* @should false if it neither the same nor a subtype
629630
*/
630631
public boolean isType(OrderType orderType) {
631-
if (this.orderType.equals(orderType)) {
632-
return true;
633-
}
634-
OrderType parentType = this.orderType.getParent();
635-
while (parentType != null) {
636-
if (parentType.equals(orderType)) {
637-
return true;
638-
}
639-
parentType = parentType.getParent();
640-
}
641-
return false;
632+
return OrderUtil.isType(orderType, this.orderType);
642633
}
643634
}

‎api/src/main/java/org/openmrs/order/OrderUtil.java

+29
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apache.commons.logging.LogFactory;
2020
import org.openmrs.Concept;
2121
import org.openmrs.Order;
22+
import org.openmrs.OrderType;
2223
import org.openmrs.Patient;
2324

2425
/**
@@ -52,4 +53,32 @@ public static void discontinueAllOrders(Patient patient, Concept discontinueReas
5253
//See https://tickets.openmrs.org/browse/TRUNK-4185
5354
}
5455

56+
/**
57+
* Checks whether orderType2 matches or is a sub type of orderType1
58+
*
59+
* @since 1.10
60+
* @param orderType1
61+
* @param orderType2
62+
* @return true if orderType2 matches or is a sub type of orderType1
63+
* @should true if orderType2 is the same or is a subtype of orderType1
64+
* @should false if orderType2 is neither the same nor a subtype of orderType1
65+
* @should return false if they are both null
66+
* @should return false if any is null and the other is not
67+
*/
68+
public static boolean isType(OrderType orderType1, OrderType orderType2) {
69+
if (orderType1 != null && orderType2 != null) {
70+
if (orderType2.equals(orderType1)) {
71+
return true;
72+
}
73+
OrderType parentType = orderType2.getParent();
74+
while (parentType != null) {
75+
if (parentType.equals(orderType1)) {
76+
return true;
77+
}
78+
parentType = parentType.getParent();
79+
}
80+
}
81+
return false;
82+
}
83+
5584
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* The contents of this file are subject to the OpenMRS Public License
3+
* Version 1.0 (the "License"); you may not use this file except in
4+
* compliance with the License. You may obtain a copy of the License at
5+
* http://license.openmrs.org
6+
*
7+
* Software distributed under the License is distributed on an "AS IS"
8+
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9+
* License for the specific language governing rights and limitations
10+
* under the License.
11+
*
12+
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
13+
*/
14+
package org.openmrs.order;
15+
16+
import static junit.framework.Assert.assertFalse;
17+
import static junit.framework.Assert.assertTrue;
18+
19+
import org.junit.Test;
20+
import org.openmrs.OrderType;
21+
22+
/**
23+
* Contains test for OrderUtil
24+
*/
25+
public class OrderUtilTest {
26+
27+
/**
28+
* @verifies true if orderType2 is the same or is a subtype of orderType1
29+
* @see OrderUtil#isType(org.openmrs.OrderType, org.openmrs.OrderType)
30+
*/
31+
@Test
32+
public void isType_shouldTrueIfOrderType2IsTheSameOrIsASubtypeOfOrderType1() throws Exception {
33+
OrderType orderType = new OrderType();
34+
OrderType subType1 = new OrderType();
35+
OrderType subType2 = new OrderType();
36+
subType2.setParent(subType1);
37+
subType1.setParent(orderType);
38+
assertTrue(OrderUtil.isType(subType2, subType2));
39+
assertTrue(OrderUtil.isType(subType1, subType2));
40+
assertTrue(OrderUtil.isType(orderType, subType2));
41+
}
42+
43+
/**
44+
* @verifies return false if they are both null
45+
* @see OrderUtil#isType(org.openmrs.OrderType, org.openmrs.OrderType)
46+
*/
47+
@Test
48+
public void isType_shouldReturnFalseIfTheyAreBothNull() throws Exception {
49+
assertFalse(OrderUtil.isType(null, null));
50+
}
51+
52+
/**
53+
* @verifies return false if any is null and the other is not
54+
* @see OrderUtil#isType(org.openmrs.OrderType, org.openmrs.OrderType)
55+
*/
56+
@Test
57+
public void isType_shouldReturnFalseIfAnyIsNullAndTheOtherIsNot() throws Exception {
58+
assertFalse(OrderUtil.isType(new OrderType(), null));
59+
assertFalse(OrderUtil.isType(null, new OrderType()));
60+
}
61+
62+
/**
63+
* @verifies false if orderType2 is neither the same nor a subtype of orderType1
64+
* @see OrderUtil#isType(org.openmrs.OrderType, org.openmrs.OrderType)
65+
*/
66+
@Test
67+
public void isType_shouldFalseIfOrderType2IsNeitherTheSameNorASubtypeOfOrderType1() throws Exception {
68+
assertFalse(OrderUtil.isType(new OrderType(), new OrderType()));
69+
}
70+
}

0 commit comments

Comments
 (0)
Please sign in to comment.