Skip to content

Commit

Permalink
Follow up to copy more fields in cloneForRevision methods and added t…
Browse files Browse the repository at this point in the history
…ests for them - TRUNK-4150

Follow up
  • Loading branch information
wluyima committed Feb 17, 2014
1 parent 3dde070 commit b3e6c2c
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 45 deletions.
13 changes: 10 additions & 3 deletions api/src/main/java/org/openmrs/DrugOrder.java
Expand Up @@ -122,7 +122,7 @@ public Concept getDoseUnits() {

/**
* Sets the doseUnits of this drug order
*
*
* @param doseUnits
*/
public void setDoseUnits(Concept doseUnits) {
Expand Down Expand Up @@ -439,8 +439,9 @@ public Order cloneForDiscontinuing() {
}

/**
* Creates a DrugOrder for revision from this order, sets the previousOrder, action field and other drug order fields.
*
* Creates a DrugOrder for revision from this order, sets the previousOrder, action field and
* other drug order fields.
*
* @return the newly created order
* @since 1.10
* @should set all the relevant fields
Expand All @@ -454,9 +455,13 @@ public Order cloneForRevision() {
newOrder.setPreviousOrder(this);
newOrder.setPatient(this.getPatient());
newOrder.setInstructions(this.getInstructions());
newOrder.setUrgency(this.getUrgency());
newOrder.setCommentToFulfiller(this.getCommentToFulfiller());
newOrder.setAccessionNumber(this.getAccessionNumber());
newOrder.setAutoExpireDate(this.getAutoExpireDate());
newOrder.setOrderReason(this.getOrderReason());
newOrder.setOrderReasonNonCoded(this.getOrderReasonNonCoded());

newOrder.setDose(this.getDose());
newOrder.setDoseUnits(this.getDoseUnits());
newOrder.setFrequency(this.getFrequency());
Expand All @@ -470,6 +475,8 @@ public Order cloneForRevision() {
newOrder.setDuration(this.getDuration());
newOrder.setDurationUnits(this.getDurationUnits());
newOrder.setRoute(this.getRoute());
newOrder.setAdministrationInstructions(this.getAdministrationInstructions());
newOrder.setNumRefills(this.getNumRefills());
return newOrder;
}

Expand Down
4 changes: 4 additions & 0 deletions api/src/main/java/org/openmrs/Order.java
Expand Up @@ -567,6 +567,10 @@ public Order cloneForRevision() {
newOrder.setInstructions(this.getInstructions());
newOrder.setUrgency(this.getUrgency());
newOrder.setCommentToFulfiller(this.getCommentToFulfiller());
newOrder.setAccessionNumber(this.getAccessionNumber());
newOrder.setAutoExpireDate(this.getAutoExpireDate());
newOrder.setOrderReason(this.getOrderReason());
newOrder.setOrderReasonNonCoded(this.getOrderReasonNonCoded());
return newOrder;
}
}
12 changes: 10 additions & 2 deletions api/src/main/java/org/openmrs/TestOrder.java
Expand Up @@ -151,8 +151,9 @@ public void setNumberOfRepeats(Integer numberOfRepeats) {
}

/**
* Creates a TestOrder for revision from this order, sets the previousOrder, action field and other test order fields.
*
* Creates a TestOrder for revision from this order, sets the previousOrder, action field and
* other test order fields.
*
* @return the newly created order
* @since 1.10
* @should set all the relevant fields
Expand All @@ -165,6 +166,13 @@ public Order cloneForRevision() {
newOrder.setAction(Action.REVISE);
newOrder.setPreviousOrder(this);
newOrder.setPatient(this.getPatient());
newOrder.setInstructions(this.getInstructions());
newOrder.setUrgency(this.getUrgency());
newOrder.setCommentToFulfiller(this.getCommentToFulfiller());
newOrder.setAccessionNumber(this.getAccessionNumber());
newOrder.setAutoExpireDate(this.getAutoExpireDate());
newOrder.setOrderReason(this.getOrderReason());
newOrder.setOrderReasonNonCoded(this.getOrderReasonNonCoded());

newOrder.setSpecimenSource(getSpecimenSource());
newOrder.setLaterality(getLaterality());
Expand Down
13 changes: 12 additions & 1 deletion api/src/test/java/org/openmrs/DrugOrderTest.java
Expand Up @@ -55,6 +55,17 @@ public void cloneForDiscontinuing_shouldSetAllTheRelevantFields() throws Excepti
*/
@Test
public void copy_shouldCopyAllDrugOrderFields() throws Exception {
OrderTest.assertThatAllFieldsAreCopied(new DrugOrder());
OrderTest.assertThatAllFieldsAreCopied(new DrugOrder(), null);
}

/**
* @verifies set all the relevant fields
* @see DrugOrder#cloneForRevision()
*/
@Test
public void cloneForRevision_shouldSetAllTheRelevantFields() throws Exception {
OrderTest.assertThatAllFieldsAreCopied(new DrugOrder(), "cloneForRevision", "creator", "dateCreated", "action",
"changedBy", "dateChanged", "voided", "dateVoided", "voidedBy", "voidReason", "encounter", "orderNumber",
"orderer", "previousOrder", "startDate", "dateStopped");
}
}
95 changes: 57 additions & 38 deletions api/src/test/java/org/openmrs/OrderTest.java
Expand Up @@ -21,11 +21,11 @@
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.commons.beanutils.MethodUtils;
import org.junit.Test;
import org.openmrs.util.Reflect;

Expand All @@ -37,6 +37,50 @@
*/
public class OrderTest {

protected static void assertThatAllFieldsAreCopied(Order original, String methodName, String... otherfieldsToExclude)
throws Exception {
if (methodName == null) {
methodName = "copy";
}
List<String> fieldsToExclude = new ArrayList<String>();
fieldsToExclude.addAll(Arrays.asList("log", "serialVersionUID", "orderId", "uuid"));
if (otherfieldsToExclude != null) {
fieldsToExclude.addAll(Arrays.asList(otherfieldsToExclude));
}
List<Field> fields = Reflect.getAllFields(original.getClass());
for (Field field : fields) {
if (fieldsToExclude.contains(field.getName())) {
continue;
}
field.setAccessible(true);
Object fieldValue = null;

if (field.getType().isEnum()) {
fieldValue = field.getType().getEnumConstants()[0];
} else if (field.getType().equals(Boolean.class)) {
fieldValue = true;
} else if (field.getType().equals(Integer.class)) {
fieldValue = 10;
} else if (field.getType().equals(Double.class)) {
fieldValue = 5.0;
} else {
fieldValue = field.getType().newInstance();
}
field.set(original, fieldValue);
}

Order copy = (Order) MethodUtils.invokeExactMethod(original, methodName, null);
for (Field field : fields) {
Object copyValue = field.get(copy);
if (fieldsToExclude.contains(field.getName())) {
continue;
}
assertNotNull("Order." + methodName + " should set " + field.getName() + " on the new Order", copyValue);
assertEquals("Order." + methodName + " should set " + field.getName() + " on the new Order",
field.get(original), copyValue);
}
}

/**
* Tests the {@link Order#isDiscontinuedRightNow()} method TODO this should be split into many
* different tests
Expand Down Expand Up @@ -131,43 +175,18 @@ public void cloneForDiscontinuing_shouldSetAllTheRelevantFields() throws Excepti
*/
@Test
public void copy_shouldCopyAllFields() throws Exception {
assertThatAllFieldsAreCopied(new Order());
assertThatAllFieldsAreCopied(new TestOrder());
assertThatAllFieldsAreCopied(new Order(), null);
assertThatAllFieldsAreCopied(new TestOrder(), null);
}

protected static void assertThatAllFieldsAreCopied(Order original) throws Exception {
Collection<?> fieldToExclude = Collections.unmodifiableCollection(Arrays.asList("log", "serialVersionUID",
"orderId", "uuid"));
List<Field> fields = Reflect.getAllFields(original.getClass());
for (Field field : fields) {
if (fieldToExclude.contains(field.getName())) {
continue;
}
field.setAccessible(true);
Object fieldValue = null;

if (field.getType().isEnum()) {
fieldValue = field.getType().getEnumConstants()[0];
} else if (field.getType().equals(Boolean.class)) {
fieldValue = true;
} else if (field.getType().equals(Integer.class)) {
fieldValue = 10;
} else if (field.getType().equals(Double.class)) {
fieldValue = 5.0;
} else {
fieldValue = field.getType().newInstance();
}
field.set(original, fieldValue);
}

Order copy = original.copy();
for (Field field : fields) {
Object copyValue = field.get(copy);
if (fieldToExclude.contains(field.getName())) {
continue;
}
assertNotNull("Order.copy should set " + field.getName() + " on the new Order", copyValue);
assertEquals("Order.copy should set " + field.getName() + " on the new Order", field.get(original), copyValue);
}
/**
* @verifies set all the relevant fields
* @see Order#cloneForRevision()
*/
@Test
public void cloneForRevision_shouldSetAllTheRelevantFields() throws Exception {
assertThatAllFieldsAreCopied(new Order(), "cloneForRevision", "creator", "dateCreated", "action", "changedBy",
"dateChanged", "voided", "dateVoided", "voidedBy", "voidReason", "encounter", "orderNumber", "orderer",
"previousOrder", "startDate", "dateStopped");
}
}
13 changes: 12 additions & 1 deletion api/src/test/java/org/openmrs/TestOrderTest.java
Expand Up @@ -27,6 +27,17 @@ public class TestOrderTest {
*/
@Test
public void copy_shouldCopyAllTestOrderFields() throws Exception {
OrderTest.assertThatAllFieldsAreCopied(new TestOrder());
OrderTest.assertThatAllFieldsAreCopied(new TestOrder(), null);
}

/**
* @verifies set all the relevant fields
* @see TestOrder#cloneForRevision()
*/
@Test
public void cloneForRevision_shouldSetAllTheRelevantFields() throws Exception {
OrderTest.assertThatAllFieldsAreCopied(new TestOrder(), "cloneForRevision", "creator", "dateCreated", "action",
"changedBy", "dateChanged", "voided", "dateVoided", "voidedBy", "voidReason", "encounter", "orderNumber",
"orderer", "previousOrder", "startDate", "dateStopped");
}
}

0 comments on commit b3e6c2c

Please sign in to comment.