Repository: authorjapps/zerocode
Pull request #408 (2020-06-16 08:06:29)

iss-407 FieldTypeConversionUtils add long #407
# <Feature Title>

PR Branch
**_#ADD LINK TO THE PR BRANCH_**

## Motivation and Context

I need it for the project I’m working on.

## Checklist:

* [x] Unit tests added

* [ ] Integration tests added

* [ ] Test names are meaningful

* [x] Feature manually tested

* [x] Branch build passed

* [ ] No 'package.*' in the imports

* [ ] Relevant Wiki page updated with clear instruction for the end user
  * [ ] Not applicable. This was only a refactor change, no functional or behaviour changes were introduced

* [ ] Http test added to `http-testing` module(if applicable) ?
  * [ ] Not applicable. The changes did not affect HTTP automation flow

* [ ] Kafka test added to `kafka-testing` module(if applicable) ?
  * [x] Not applicable. The changes did not affect Kafka automation flow



Patch:
diff --git a/core/src/main/java/org/jsmart/zerocode/core/utils/FieldTypeConversionUtils.java b/core/src/main/java/org/jsmart/zerocode/core/utils/FieldTypeConversionUtils.java
index 3b821316..3634c08a 100755
--- a/core/src/main/java/org/jsmart/zerocode/core/utils/FieldTypeConversionUtils.java
+++ b/core/src/main/java/org/jsmart/zerocode/core/utils/FieldTypeConversionUtils.java
@@ -1,12 +1,14 @@
 package org.jsmart.zerocode.core.utils;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
+
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.function.Function;
+
 import org.jsmart.zerocode.core.di.provider.ObjectMapperProvider;
 import org.slf4j.LoggerFactory;
 
@@ -15,15 +17,19 @@ public class FieldTypeConversionUtils {
     private static ObjectMapper mapper = new ObjectMapperProvider().get();
 
     public static final String INT = "(int)";
+    public static final String LONG = "(long)";
     public static final String FLOAT = "(float)";
     public static final String BOOLEAN = "(boolean)";
     public static final String DECIMAL = "(decimal)";
 
-    public static List<String> fieldTypes = Arrays.asList(INT, FLOAT, BOOLEAN, DECIMAL);
+    public static List<String> fieldTypes = Arrays.asList(INT, FLOAT, BOOLEAN, DECIMAL, LONG);
 
     static Function<String, Integer> integerFunction = (input) ->
             Integer.valueOf(input.substring(INT.length()));
 
+    static Function<String, Long> longFunction = input ->
+            Long.valueOf(input.substring(LONG.length()));
+
     static Function<String, Float> floatFunction = (input) ->
             Float.valueOf(input.substring(FLOAT.length()));
 
@@ -33,9 +39,10 @@ public class FieldTypeConversionUtils {
     static Function<String, Boolean> booleanFUnction = (input) ->
             Boolean.valueOf(input.substring(BOOLEAN.length()));
 
-    static Map<String, Function> typeMap = new HashMap<String, Function>(){
+    static Map<String, Function> typeMap = new HashMap<String, Function>() {
         {
             put(INT, integerFunction);
+            put(LONG, longFunction);
             put(FLOAT, floatFunction);
             put(DECIMAL, decimalFunction);
             put(BOOLEAN, booleanFUnction);
@@ -51,16 +58,15 @@ public class FieldTypeConversionUtils {
             if (value instanceof Map) {
                 digTypeCast((Map<String, Object>) value);
 
-            } else if(value instanceof ArrayList){
-                ((ArrayList)value).forEach(thisItem -> {
+            } else if (value instanceof ArrayList) {
+                ((ArrayList) value).forEach(thisItem -> {
                     if (thisItem instanceof Map) {
                         digTypeCast((Map<String, Object>) thisItem);
                     }
                     LOGGER.debug("ARRAY - Leaf node found = {}, checking for type value...", thisItem);
                     replaceNodeValue(entry, thisItem);
                 });
-            }
-            else {
+            } else {
                 LOGGER.debug("Leaf node found = {}, checking for type value...", value);
                 replaceNodeValue(entry, value);
             }
@@ -69,20 +75,15 @@ public class FieldTypeConversionUtils {
 
 
     private static void replaceNodeValue(Map.Entry<String, Object> entry, Object thisItem) {
-        try{
-            if (thisItem != null && thisItem.toString().startsWith(INT)) {
-                entry.setValue((typeMap.get(INT)).apply(thisItem.toString()));
-            }
-            else if (thisItem != null && thisItem.toString().startsWith(DECIMAL)) {
-                entry.setValue((typeMap.get(DECIMAL)).apply(thisItem.toString()));
-            }
-            else if (thisItem != null && thisItem.toString().startsWith(FLOAT)) {
-                entry.setValue((typeMap.get(FLOAT)).apply(thisItem.toString()));
-            }
-            else if (thisItem != null && thisItem.toString().startsWith(BOOLEAN)) {
-                entry.setValue((typeMap.get(BOOLEAN)).apply(thisItem.toString()));
+        try {
+            if (thisItem != null) {
+                fieldTypes.stream().forEach(currentType -> {
+                    if (thisItem.toString().startsWith(currentType)) {
+                        entry.setValue((typeMap.get(currentType)).apply(thisItem.toString()));
+                    }
+                });
             }
-        } catch(Exception exx){
+        } catch (Exception exx) {
             String errorMsg = "Can not convert '" + entry.getValue() + "'.";
             LOGGER.error(errorMsg + "\nException Details:" + exx);
             throw new RuntimeException(errorMsg + exx);


Test patch:
diff --git a/core/src/test/java/org/jsmart/zerocode/core/utils/FieldTypeConversionUtilsTest.java b/core/src/test/java/org/jsmart/zerocode/core/utils/FieldTypeConversionUtilsTest.java
index b6c1c741..2a03d1f9 100644
--- a/core/src/test/java/org/jsmart/zerocode/core/utils/FieldTypeConversionUtilsTest.java
+++ b/core/src/test/java/org/jsmart/zerocode/core/utils/FieldTypeConversionUtilsTest.java
@@ -26,7 +26,7 @@ public class FieldTypeConversionUtilsTest {
     ObjectMapper mapper = new ObjectMapperProvider().get();
 
     @Test
-    public void testSubstituted_v3() throws IOException {
+    public void testSubstituted_v4() throws IOException {
         String originalJson = "{\n" +
                 "    \"found\": true,\n" +
                 "    \"currentAddress\":{\n" +
@@ -41,7 +41,8 @@ public class FieldTypeConversionUtilsTest {
                 "        {\n" +
                 "            \"id\": 2.35,\n" +
                 "            \"name\": \"Bar\",\n" +
-                "            \"isActive\": false\n" +
+                "            \"isActive\": false,\n" +
+                "            \"longField\": 1569683094000\n" +
                 "        }\n" +
                 "    ]\n" +
                 "}";
@@ -60,7 +61,8 @@ public class FieldTypeConversionUtilsTest {
                 "        {\n" +
                 "            \"id\": \"(float)${$.results[1].id}\",\n" +
                 "            \"name\": \"Bar - ${$.results[1].id}\",\n" +
-                "            \"isActive\": \"(boolean)${$.results[1].isActive}\"\n" +
+                "            \"isActive\": \"(boolean)${$.results[1].isActive}\",\n" +
+                "            \"longField\": \"(long)${$.results[1].longField}\"\n" +
                 "        }\n" +
                 "    ]\n" +
                 "}";
@@ -70,6 +72,7 @@ public class FieldTypeConversionUtilsTest {
         tokens.add("$.results[0].id");
         tokens.add("$.results[1].id");
         tokens.add("$.results[1].isActive");
+        tokens.add("$.results[1].longField");
 
         Map<String, Object> paramMap = new HashMap<>();
 
@@ -89,7 +92,7 @@ public class FieldTypeConversionUtilsTest {
         JsonNode jsonNode = mapper.valueToTree(stepMap);
 
         assertEquals(true, jsonNode.get("found").asBoolean());
-        assertEquals("{\"id\":2.35,\"name\":\"Bar - 2.35\",\"isActive\":false}",
+        assertEquals("{\"id\":2.35,\"name\":\"Bar - 2.35\",\"isActive\":false,\"longField\":1569683094000}",
                 jsonNode.get("results").get(1).toString());
         assertEquals("address line1", jsonNode.get("currentAddress").get("line1").asText());
     }