Skip to content

Commit 36032cb

Browse files
committed
#149|Added double method to fix casting issue.
1 parent 05b0e38 commit 36032cb

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

goonj/src/main/java/org/avni_integration_service/goonj/repository/DispatchReceiptRepository.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import static org.avni_integration_service.goonj.config.GoonjMappingDbConstants.MappingGroup_DispatchReceipt;
3131
import static org.avni_integration_service.goonj.config.GoonjMappingDbConstants.MappingType_Obs;
32+
import static org.avni_integration_service.goonj.util.NumberUtil.getDouble;
3233

3334
@Component("DispatchReceiptRepository")
3435
public class DispatchReceiptRepository extends GoonjBaseRepository
@@ -109,13 +110,14 @@ public DispatchReceivedStatusLineItem createDispatchReceivedStatusLineItem(HashM
109110
String itemName = typeOfMaterial.equals(CONTRIBUTED_ITEM) ?
110111
(String) entry.get(CONTRIBUTED_ITEM_NAME) :
111112
(typeOfMaterial.equals(KIT) ? (String) entry.get(KIT_NAME) : (String) entry.get(MATERIAL_NAME));
112-
double dispatchedQuantity = entry.get(QUANTITY_DISPATCHED) != null ?
113-
(double) entry.get(QUANTITY_DISPATCHED) : 0d;
113+
double dispatchedQuantity = getDouble(entry.get(QUANTITY_DISPATCHED)) != null ?
114+
getDouble(entry.get(QUANTITY_DISPATCHED)) : 0d;
114115
boolean quantityMatching = entry.get(QUANTITY_MATCHING).equals("Yes");
115116
if (quantityMatching) {
116117
receivedQuantity = dispatchedQuantity;
117118
} else {
118-
receivedQuantity = entry.get(QUANTITY) != null ? (double) entry.get(QUANTITY) : 0d;
119+
receivedQuantity = getDouble(entry.get(QUANTITY)) != null ?
120+
getDouble(entry.get(QUANTITY)) : 0d;
119121
}
120122
return new DispatchReceivedStatusLineItem(dispatchStatusLineItemId, mapTypeOfMaterial(entry), itemName,
121123
dispatchStatusLineItemId, EMPTY_STRING, EMPTY_STRING, dispatchedQuantity, receivedQuantity);

goonj/src/main/java/org/avni_integration_service/goonj/util/NumberUtil.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@ public static Integer getInteger(Object value) {
77
}
88
return Double.valueOf(value.toString()).intValue();
99
}
10+
11+
public static Double getDouble(Object value) {
12+
if (value == null) {
13+
return null;
14+
}
15+
return Double.valueOf(value.toString());
16+
}
1017
}

goonj/src/main/java/org/avni_integration_service/goonj/worker/goonj/InventoryEventWorker.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.HashMap;
2323
import java.util.Map;
2424

25+
import static org.avni_integration_service.goonj.util.NumberUtil.getDouble;
26+
2527
@Component
2628
public class InventoryEventWorker extends GoonjEventWorker implements ErrorRecordWorker {
2729
private static final Logger logger = Logger.getLogger(InventoryEventWorker.class);
@@ -57,8 +59,8 @@ private void processImplementationInventory(Map<String, Object> inventoryRespons
5759
Subject subject = inventoryItems.subjectWithoutObservations();
5860
inventoryService.populateObservations(subject, inventoryItems);
5961
if (subject.getVoided() == Boolean.FALSE){
60-
Object currentQuantity = subject.getObservation("Current Quantity");
61-
if (currentQuantity != null && (Double) currentQuantity == 0d){
62+
Double currentQuantity = getDouble(subject.getObservation("Current Quantity"));
63+
if (currentQuantity != null && currentQuantity == 0d){
6264
subject.setVoided(Boolean.TRUE);
6365
logger.info(String.format("0 currentQuantity, Voiding implementation inventory: name %s || uuid %s", inventoryResponse.get("ImplementationInventoryName"), inventoryResponse.get("ImplementationInventoryId")));
6466
}

goonj/src/test/java/org/avni_integration_service/goonj/util/NumberUtilTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import org.avni_integration_service.util.ObjectJsonMapper;
44
import org.junit.jupiter.api.Test;
55

6+
import java.util.HashMap;
67
import java.util.Map;
78

9+
import static org.avni_integration_service.goonj.util.NumberUtil.getDouble;
810
import static org.avni_integration_service.goonj.util.NumberUtil.getInteger;
911
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertThrows;
1013

1114
public class NumberUtilTest {
1215

@@ -24,4 +27,32 @@ public void shouldConvertToInteger() {
2427
assertEquals(getInteger(map.get("d")), null);
2528
}
2629

30+
@Test
31+
public void shouldConvertToDouble() {
32+
Map<String, Object> map = ObjectJsonMapper.readValue(
33+
"{ \n" +
34+
" \"a\": \"1\", \n" +
35+
" \"b\": 1, \n" +
36+
" \"c\": 1.5, \n" +
37+
" \"e\": \"1.2345\" \n" +
38+
"}", java.util.Map.class);
39+
40+
assertEquals(getDouble(map.get("a")), Double.valueOf(1.0));
41+
assertEquals(getDouble(map.get("b")), Double.valueOf(1.0));
42+
assertEquals(getDouble(map.get("c")), Double.valueOf(1.5));
43+
assertEquals(getDouble(map.get("e")), Double.valueOf(1.2345));
44+
}
45+
46+
@Test
47+
public void shouldThrowClassCastExceptionWhenCastingIntegerToDouble() {
48+
Map<String, Object> map = new HashMap<>();
49+
map.put("value", Integer.valueOf(1));
50+
map.put("value1", String.valueOf(1));
51+
assertThrows(ClassCastException.class, () -> {
52+
Double d = (Double) map.get("value");
53+
});
54+
assertThrows(ClassCastException.class, () -> {
55+
Double d = (Double) map.get("value1");
56+
});
57+
}
2758
}

0 commit comments

Comments
 (0)