Skip to content

Commit

Permalink
Made follow up to add utility method for testing order type hierarchi…
Browse files Browse the repository at this point in the history
…es - TRUNK-4290
  • Loading branch information
wluyima committed Mar 21, 2014
1 parent df704f6 commit 9445bd4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 11 deletions.
13 changes: 2 additions & 11 deletions api/src/main/java/org/openmrs/Order.java
Expand Up @@ -17,6 +17,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.order.OrderUtil;

/**
* Dates should be interpreted as follows: If startDate is null then the order has been going on
Expand Down Expand Up @@ -628,16 +629,6 @@ public Order cloneForRevision() {
* @should false if it neither the same nor a subtype
*/
public boolean isType(OrderType orderType) {
if (this.orderType.equals(orderType)) {
return true;
}
OrderType parentType = this.orderType.getParent();
while (parentType != null) {
if (parentType.equals(orderType)) {
return true;
}
parentType = parentType.getParent();
}
return false;
return OrderUtil.isType(orderType, this.orderType);
}
}
29 changes: 29 additions & 0 deletions api/src/main/java/org/openmrs/order/OrderUtil.java
Expand Up @@ -19,6 +19,7 @@
import org.apache.commons.logging.LogFactory;
import org.openmrs.Concept;
import org.openmrs.Order;
import org.openmrs.OrderType;
import org.openmrs.Patient;

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

/**
* Checks whether orderType2 matches or is a sub type of orderType1
*
* @since 1.10
* @param orderType1
* @param orderType2
* @return true if orderType2 matches or is a sub type of orderType1
* @should true if orderType2 is the same or is a subtype of orderType1
* @should false if orderType2 is neither the same nor a subtype of orderType1
* @should return false if they are both null
* @should return false if any is null and the other is not
*/
public static boolean isType(OrderType orderType1, OrderType orderType2) {
if (orderType1 != null && orderType2 != null) {
if (orderType2.equals(orderType1)) {
return true;
}
OrderType parentType = orderType2.getParent();
while (parentType != null) {
if (parentType.equals(orderType1)) {
return true;
}
parentType = parentType.getParent();
}
}
return false;
}

}
70 changes: 70 additions & 0 deletions api/src/test/java/org/openmrs/order/OrderUtilTest.java
@@ -0,0 +1,70 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.order;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

import org.junit.Test;
import org.openmrs.OrderType;

/**
* Contains test for OrderUtil
*/
public class OrderUtilTest {

/**
* @verifies true if orderType2 is the same or is a subtype of orderType1
* @see OrderUtil#isType(org.openmrs.OrderType, org.openmrs.OrderType)
*/
@Test
public void isType_shouldTrueIfOrderType2IsTheSameOrIsASubtypeOfOrderType1() throws Exception {
OrderType orderType = new OrderType();
OrderType subType1 = new OrderType();
OrderType subType2 = new OrderType();
subType2.setParent(subType1);
subType1.setParent(orderType);
assertTrue(OrderUtil.isType(subType2, subType2));
assertTrue(OrderUtil.isType(subType1, subType2));
assertTrue(OrderUtil.isType(orderType, subType2));
}

/**
* @verifies return false if they are both null
* @see OrderUtil#isType(org.openmrs.OrderType, org.openmrs.OrderType)
*/
@Test
public void isType_shouldReturnFalseIfTheyAreBothNull() throws Exception {
assertFalse(OrderUtil.isType(null, null));
}

/**
* @verifies return false if any is null and the other is not
* @see OrderUtil#isType(org.openmrs.OrderType, org.openmrs.OrderType)
*/
@Test
public void isType_shouldReturnFalseIfAnyIsNullAndTheOtherIsNot() throws Exception {
assertFalse(OrderUtil.isType(new OrderType(), null));
assertFalse(OrderUtil.isType(null, new OrderType()));
}

/**
* @verifies false if orderType2 is neither the same nor a subtype of orderType1
* @see OrderUtil#isType(org.openmrs.OrderType, org.openmrs.OrderType)
*/
@Test
public void isType_shouldFalseIfOrderType2IsNeitherTheSameNorASubtypeOfOrderType1() throws Exception {
assertFalse(OrderUtil.isType(new OrderType(), new OrderType()));
}
}

0 comments on commit 9445bd4

Please sign in to comment.