Skip to content

Commit b3e6c2c

Browse files
committedFeb 17, 2014
Follow up to copy more fields in cloneForRevision methods and added tests for them - TRUNK-4150
Follow up
1 parent 3dde070 commit b3e6c2c

File tree

6 files changed

+105
-45
lines changed

6 files changed

+105
-45
lines changed
 

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public Concept getDoseUnits() {
122122

123123
/**
124124
* Sets the doseUnits of this drug order
125-
*
125+
*
126126
* @param doseUnits
127127
*/
128128
public void setDoseUnits(Concept doseUnits) {
@@ -439,8 +439,9 @@ public Order cloneForDiscontinuing() {
439439
}
440440

441441
/**
442-
* Creates a DrugOrder for revision from this order, sets the previousOrder, action field and other drug order fields.
443-
*
442+
* Creates a DrugOrder for revision from this order, sets the previousOrder, action field and
443+
* other drug order fields.
444+
*
444445
* @return the newly created order
445446
* @since 1.10
446447
* @should set all the relevant fields
@@ -454,9 +455,13 @@ public Order cloneForRevision() {
454455
newOrder.setPreviousOrder(this);
455456
newOrder.setPatient(this.getPatient());
456457
newOrder.setInstructions(this.getInstructions());
458+
newOrder.setUrgency(this.getUrgency());
457459
newOrder.setCommentToFulfiller(this.getCommentToFulfiller());
460+
newOrder.setAccessionNumber(this.getAccessionNumber());
461+
newOrder.setAutoExpireDate(this.getAutoExpireDate());
458462
newOrder.setOrderReason(this.getOrderReason());
459463
newOrder.setOrderReasonNonCoded(this.getOrderReasonNonCoded());
464+
460465
newOrder.setDose(this.getDose());
461466
newOrder.setDoseUnits(this.getDoseUnits());
462467
newOrder.setFrequency(this.getFrequency());
@@ -470,6 +475,8 @@ public Order cloneForRevision() {
470475
newOrder.setDuration(this.getDuration());
471476
newOrder.setDurationUnits(this.getDurationUnits());
472477
newOrder.setRoute(this.getRoute());
478+
newOrder.setAdministrationInstructions(this.getAdministrationInstructions());
479+
newOrder.setNumRefills(this.getNumRefills());
473480
return newOrder;
474481
}
475482

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

+4
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,10 @@ public Order cloneForRevision() {
567567
newOrder.setInstructions(this.getInstructions());
568568
newOrder.setUrgency(this.getUrgency());
569569
newOrder.setCommentToFulfiller(this.getCommentToFulfiller());
570+
newOrder.setAccessionNumber(this.getAccessionNumber());
571+
newOrder.setAutoExpireDate(this.getAutoExpireDate());
572+
newOrder.setOrderReason(this.getOrderReason());
573+
newOrder.setOrderReasonNonCoded(this.getOrderReasonNonCoded());
570574
return newOrder;
571575
}
572576
}

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ public void setNumberOfRepeats(Integer numberOfRepeats) {
151151
}
152152

153153
/**
154-
* Creates a TestOrder for revision from this order, sets the previousOrder, action field and other test order fields.
155-
*
154+
* Creates a TestOrder for revision from this order, sets the previousOrder, action field and
155+
* other test order fields.
156+
*
156157
* @return the newly created order
157158
* @since 1.10
158159
* @should set all the relevant fields
@@ -165,6 +166,13 @@ public Order cloneForRevision() {
165166
newOrder.setAction(Action.REVISE);
166167
newOrder.setPreviousOrder(this);
167168
newOrder.setPatient(this.getPatient());
169+
newOrder.setInstructions(this.getInstructions());
170+
newOrder.setUrgency(this.getUrgency());
171+
newOrder.setCommentToFulfiller(this.getCommentToFulfiller());
172+
newOrder.setAccessionNumber(this.getAccessionNumber());
173+
newOrder.setAutoExpireDate(this.getAutoExpireDate());
174+
newOrder.setOrderReason(this.getOrderReason());
175+
newOrder.setOrderReasonNonCoded(this.getOrderReasonNonCoded());
168176

169177
newOrder.setSpecimenSource(getSpecimenSource());
170178
newOrder.setLaterality(getLaterality());

‎api/src/test/java/org/openmrs/DrugOrderTest.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ public void cloneForDiscontinuing_shouldSetAllTheRelevantFields() throws Excepti
5555
*/
5656
@Test
5757
public void copy_shouldCopyAllDrugOrderFields() throws Exception {
58-
OrderTest.assertThatAllFieldsAreCopied(new DrugOrder());
58+
OrderTest.assertThatAllFieldsAreCopied(new DrugOrder(), null);
59+
}
60+
61+
/**
62+
* @verifies set all the relevant fields
63+
* @see DrugOrder#cloneForRevision()
64+
*/
65+
@Test
66+
public void cloneForRevision_shouldSetAllTheRelevantFields() throws Exception {
67+
OrderTest.assertThatAllFieldsAreCopied(new DrugOrder(), "cloneForRevision", "creator", "dateCreated", "action",
68+
"changedBy", "dateChanged", "voided", "dateVoided", "voidedBy", "voidReason", "encounter", "orderNumber",
69+
"orderer", "previousOrder", "startDate", "dateStopped");
5970
}
6071
}

‎api/src/test/java/org/openmrs/OrderTest.java

+57-38
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
import java.lang.reflect.Field;
2222
import java.text.DateFormat;
2323
import java.text.SimpleDateFormat;
24+
import java.util.ArrayList;
2425
import java.util.Arrays;
25-
import java.util.Collection;
26-
import java.util.Collections;
2726
import java.util.List;
2827

28+
import org.apache.commons.beanutils.MethodUtils;
2929
import org.junit.Test;
3030
import org.openmrs.util.Reflect;
3131

@@ -37,6 +37,50 @@
3737
*/
3838
public class OrderTest {
3939

40+
protected static void assertThatAllFieldsAreCopied(Order original, String methodName, String... otherfieldsToExclude)
41+
throws Exception {
42+
if (methodName == null) {
43+
methodName = "copy";
44+
}
45+
List<String> fieldsToExclude = new ArrayList<String>();
46+
fieldsToExclude.addAll(Arrays.asList("log", "serialVersionUID", "orderId", "uuid"));
47+
if (otherfieldsToExclude != null) {
48+
fieldsToExclude.addAll(Arrays.asList(otherfieldsToExclude));
49+
}
50+
List<Field> fields = Reflect.getAllFields(original.getClass());
51+
for (Field field : fields) {
52+
if (fieldsToExclude.contains(field.getName())) {
53+
continue;
54+
}
55+
field.setAccessible(true);
56+
Object fieldValue = null;
57+
58+
if (field.getType().isEnum()) {
59+
fieldValue = field.getType().getEnumConstants()[0];
60+
} else if (field.getType().equals(Boolean.class)) {
61+
fieldValue = true;
62+
} else if (field.getType().equals(Integer.class)) {
63+
fieldValue = 10;
64+
} else if (field.getType().equals(Double.class)) {
65+
fieldValue = 5.0;
66+
} else {
67+
fieldValue = field.getType().newInstance();
68+
}
69+
field.set(original, fieldValue);
70+
}
71+
72+
Order copy = (Order) MethodUtils.invokeExactMethod(original, methodName, null);
73+
for (Field field : fields) {
74+
Object copyValue = field.get(copy);
75+
if (fieldsToExclude.contains(field.getName())) {
76+
continue;
77+
}
78+
assertNotNull("Order." + methodName + " should set " + field.getName() + " on the new Order", copyValue);
79+
assertEquals("Order." + methodName + " should set " + field.getName() + " on the new Order",
80+
field.get(original), copyValue);
81+
}
82+
}
83+
4084
/**
4185
* Tests the {@link Order#isDiscontinuedRightNow()} method TODO this should be split into many
4286
* different tests
@@ -131,43 +175,18 @@ public void cloneForDiscontinuing_shouldSetAllTheRelevantFields() throws Excepti
131175
*/
132176
@Test
133177
public void copy_shouldCopyAllFields() throws Exception {
134-
assertThatAllFieldsAreCopied(new Order());
135-
assertThatAllFieldsAreCopied(new TestOrder());
178+
assertThatAllFieldsAreCopied(new Order(), null);
179+
assertThatAllFieldsAreCopied(new TestOrder(), null);
136180
}
137181

138-
protected static void assertThatAllFieldsAreCopied(Order original) throws Exception {
139-
Collection<?> fieldToExclude = Collections.unmodifiableCollection(Arrays.asList("log", "serialVersionUID",
140-
"orderId", "uuid"));
141-
List<Field> fields = Reflect.getAllFields(original.getClass());
142-
for (Field field : fields) {
143-
if (fieldToExclude.contains(field.getName())) {
144-
continue;
145-
}
146-
field.setAccessible(true);
147-
Object fieldValue = null;
148-
149-
if (field.getType().isEnum()) {
150-
fieldValue = field.getType().getEnumConstants()[0];
151-
} else if (field.getType().equals(Boolean.class)) {
152-
fieldValue = true;
153-
} else if (field.getType().equals(Integer.class)) {
154-
fieldValue = 10;
155-
} else if (field.getType().equals(Double.class)) {
156-
fieldValue = 5.0;
157-
} else {
158-
fieldValue = field.getType().newInstance();
159-
}
160-
field.set(original, fieldValue);
161-
}
162-
163-
Order copy = original.copy();
164-
for (Field field : fields) {
165-
Object copyValue = field.get(copy);
166-
if (fieldToExclude.contains(field.getName())) {
167-
continue;
168-
}
169-
assertNotNull("Order.copy should set " + field.getName() + " on the new Order", copyValue);
170-
assertEquals("Order.copy should set " + field.getName() + " on the new Order", field.get(original), copyValue);
171-
}
182+
/**
183+
* @verifies set all the relevant fields
184+
* @see Order#cloneForRevision()
185+
*/
186+
@Test
187+
public void cloneForRevision_shouldSetAllTheRelevantFields() throws Exception {
188+
assertThatAllFieldsAreCopied(new Order(), "cloneForRevision", "creator", "dateCreated", "action", "changedBy",
189+
"dateChanged", "voided", "dateVoided", "voidedBy", "voidReason", "encounter", "orderNumber", "orderer",
190+
"previousOrder", "startDate", "dateStopped");
172191
}
173192
}

‎api/src/test/java/org/openmrs/TestOrderTest.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ public class TestOrderTest {
2727
*/
2828
@Test
2929
public void copy_shouldCopyAllTestOrderFields() throws Exception {
30-
OrderTest.assertThatAllFieldsAreCopied(new TestOrder());
30+
OrderTest.assertThatAllFieldsAreCopied(new TestOrder(), null);
31+
}
32+
33+
/**
34+
* @verifies set all the relevant fields
35+
* @see TestOrder#cloneForRevision()
36+
*/
37+
@Test
38+
public void cloneForRevision_shouldSetAllTheRelevantFields() throws Exception {
39+
OrderTest.assertThatAllFieldsAreCopied(new TestOrder(), "cloneForRevision", "creator", "dateCreated", "action",
40+
"changedBy", "dateChanged", "voided", "dateVoided", "voidedBy", "voidReason", "encounter", "orderNumber",
41+
"orderer", "previousOrder", "startDate", "dateStopped");
3142
}
3243
}

0 commit comments

Comments
 (0)
Please sign in to comment.