Repository: authorjapps/zerocode
Pull request #56 (2018-03-24 21:44:47)

ISS-55 # UUID Java 36 length


Patch:
diff --git a/src/main/java/org/jsmart/zerocode/core/engine/assertion/FieldHasSubStringIgnoreCaseValueAsserter.java b/src/main/java/org/jsmart/zerocode/core/engine/assertion/FieldHasSubStringIgnoreCaseValueAsserter.java
new file mode 100644
index 00000000..25cdfc73
--- /dev/null
+++ b/src/main/java/org/jsmart/zerocode/core/engine/assertion/FieldHasSubStringIgnoreCaseValueAsserter.java
@@ -0,0 +1,32 @@
+package org.jsmart.zerocode.core.engine.assertion;
+
+public class FieldHasSubStringIgnoreCaseValueAsserter implements JsonAsserter {
+    private final String path;
+    private final String expected;
+
+    public FieldHasSubStringIgnoreCaseValueAsserter(String path, String expected) {
+        this.path = path;
+        this.expected = expected;
+    }
+
+    @Override
+    public String getPath() {
+        return path;
+    }
+
+    @Override
+    public AssertionReport actualEqualsToExpected(Object result) {
+        boolean areEqual;
+        if (result instanceof String && expected instanceof String) {
+            String s1 = (String) result;
+            String s2 = expected;
+            areEqual = s1.toUpperCase().contains(s2.toUpperCase());
+        } else {
+            areEqual = false;
+        }
+
+        return areEqual ?
+                AssertionReport.createFieldMatchesReport() :
+                AssertionReport.createFieldDoesNotMatchReport(path, "containing sub-string with ignoring case:" + expected, result);
+    }
+}


Test patch:
diff --git a/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeJsonTestProcesorImpl.java b/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeJsonTestProcesorImpl.java
index 0d73f309..be90c405 100644
--- a/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeJsonTestProcesorImpl.java
+++ b/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeJsonTestProcesorImpl.java
@@ -14,6 +14,7 @@ import org.jsmart.zerocode.core.engine.assertion.FieldHasExactValueAsserter;
 import org.jsmart.zerocode.core.engine.assertion.FieldHasGreaterThanValueAsserter;
 import org.jsmart.zerocode.core.engine.assertion.FieldHasInEqualNumberValueAsserter;
 import org.jsmart.zerocode.core.engine.assertion.FieldHasLesserThanValueAsserter;
+import org.jsmart.zerocode.core.engine.assertion.FieldHasSubStringIgnoreCaseValueAsserter;
 import org.jsmart.zerocode.core.engine.assertion.FieldHasSubStringValueAsserter;
 import org.jsmart.zerocode.core.engine.assertion.FieldIsNotNullAsserter;
 import org.jsmart.zerocode.core.engine.assertion.FieldIsNullAsserter;
@@ -33,10 +34,12 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
+import java.util.UUID;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import static java.lang.String.format;
+import static java.util.UUID.randomUUID;
 import static org.apache.commons.lang.StringEscapeUtils.escapeJava;
 import static org.slf4j.LoggerFactory.getLogger;
 
@@ -54,6 +57,7 @@ public class ZeroCodeJsonTestProcesorImpl implements ZeroCodeJsonTestProcesor {
     private static final String LOCALDATE_TODAY = "LOCAL.DATE.TODAY:";
     private static final String LOCALDATETIME_NOW = "LOCAL.DATETIME.NOW:";
     private static final String XML_FILE = "XML.FILE:";
+    private static final String RANDOM_UU_ID = "RANDOM.UUID";
 
     private static final List<String> availableTokens = Arrays.asList(
             PREFIX_ASU,
@@ -62,7 +66,8 @@ public class ZeroCodeJsonTestProcesorImpl implements ZeroCodeJsonTestProcesor {
             STATIC_ALPHABET,
             LOCALDATE_TODAY,
             LOCALDATETIME_NOW,
-            XML_FILE
+            XML_FILE,
+            RANDOM_UU_ID
     );
 
     /*
@@ -73,6 +78,7 @@ public class ZeroCodeJsonTestProcesorImpl implements ZeroCodeJsonTestProcesor {
     public static final String ASSERT_VALUE_EMPTY_ARRAY = "$[]";
     public static final String ASSERT_PATH_SIZE = ".SIZE";
     public static final String ASSERT_VALUE_CONTAINS_STRING = "$CONTAINS.STRING:";
+    public static final String ASSERT_VALUE_CONTAINS_STRING_IGNORE_CASE = "$CONTAINS.STRING.IGNORECASE:";
     public static final String ASSERT_VALUE_EQUAL_TO_NUMBER = "$EQ.";
     public static final String ASSERT_VALUE_NOT_EQUAL_TO_NUMBER = "$NOT.EQ.";
     public static final String ASSERT_VALUE_GREATER_THAN = "$GT.";
@@ -122,6 +128,9 @@ public class ZeroCodeJsonTestProcesorImpl implements ZeroCodeJsonTestProcesor {
                         // Used escapeJava, do not use escapeXml as it replaces
                         // with GT LT etc ie what exactly you don't want
                         parammap.put(runTimeToken, escapeJava(xmlString));
+
+                    } else if (runTimeToken.startsWith(RANDOM_UU_ID)) {
+                        parammap.put(runTimeToken, randomUUID().toString());
                     }
                 }
             });
@@ -239,6 +248,10 @@ public class ZeroCodeJsonTestProcesorImpl implements ZeroCodeJsonTestProcesor {
                     String expected = ((String) value).substring(ASSERT_VALUE_CONTAINS_STRING.length());
                     asserter = new FieldHasSubStringValueAsserter(path, expected);
                 }
+                else if (value instanceof String && ((String) value).startsWith(ASSERT_VALUE_CONTAINS_STRING_IGNORE_CASE)) {
+                    String expected = ((String) value).substring(ASSERT_VALUE_CONTAINS_STRING_IGNORE_CASE.length());
+                    asserter = new FieldHasSubStringIgnoreCaseValueAsserter(path, expected);
+                }
                 else if (value instanceof String && (value.toString()).startsWith(ASSERT_VALUE_EQUAL_TO_NUMBER)) {
                     String expected = ((String) value).substring(ASSERT_VALUE_EQUAL_TO_NUMBER.length());
                     asserter = new FieldHasEqualNumberValueAsserter(path, new BigDecimal(expected));
diff --git a/src/test/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeJsonTestProcesorImplTest.java b/src/test/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeJsonTestProcesorImplTest.java
index 9bf8588c..a313c74d 100644
--- a/src/test/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeJsonTestProcesorImplTest.java
+++ b/src/test/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeJsonTestProcesorImplTest.java
@@ -13,6 +13,7 @@ import org.jsmart.zerocode.core.utils.SmartUtils;
 import org.junit.Before;
 import org.junit.Test;
 
+import java.util.HashMap;
 import java.util.List;
 
 import static org.hamcrest.CoreMatchers.containsString;
@@ -294,4 +295,71 @@ public class ZeroCodeJsonTestProcesorImplTest {
         final String resolvedRequestJson = jsonPreProcessor.resolveStringJson(requestJsonAsString, requestJsonAsString);
         assertThat(resolvedRequestJson.indexOf("LOCAL.DATETIME.NOW:"), is(-1));
     }
+
+    @Test
+    public void testRandom_UUID() throws Exception {
+
+        final String requestJsonAsString = "{\n" +
+                "\t\"onlineOrderId\": \"${RANDOM.UUID}\"\n" +
+                "}";
+
+        final List<String> placeHolders = jsonPreProcessor.getAllTokens(requestJsonAsString);
+        assertThat(placeHolders.size(), is(1));
+
+        final String resolvedRequestJson = jsonPreProcessor.resolveStringJson(requestJsonAsString, requestJsonAsString);
+        assertThat(resolvedRequestJson.indexOf("RANDOM.UUID"), is(-1));
+
+        final HashMap<String, String> hashMap = smartUtils.getMapper().readValue(resolvedRequestJson, HashMap.class);
+
+        assertThat(hashMap.get("onlineOrderId").length(), is(36)); //"onlineOrderId": "48c3b4ff-5078-40bb-8d62-11abcbdef5b3"
+    }
+
+    @Test
+    public void testIgnoreCaseWith_containsNoMatch() throws Exception {
+        ScenarioSpec scenarioSpec = smartUtils.jsonFileToJava("18_ignore_case/test_string_match_withIgnoring_case.json", ScenarioSpec.class);
+
+        final String assertionsSectionAsString = scenarioSpec.getSteps().get(0).getAssertions().toString();
+        String mockScenarioState = "{}";
+
+        final String resolvedAssertions = jsonPreProcessor.resolveStringJson(assertionsSectionAsString, mockScenarioState);
+        assertThat(resolvedAssertions, containsString("{\"name\":\"$CONTAINS.STRING.IGNORECASE:CReASY\"}}"));
+
+        List<JsonAsserter> asserters = jsonPreProcessor.createAssertersFrom(resolvedAssertions);
+        assertThat(asserters.size(), is(2));
+
+        String mockTestResponse = "{\n" +
+                "  \"status\": 201,\n" +
+                "  \"body\": {\n" +
+                "    \"name\": \"Hello CreXasy\"\n" +
+                "  }\n" +
+                "}";
+        List<AssertionReport> failedReports = jsonPreProcessor.assertAllAndReturnFailed(asserters, mockTestResponse);
+
+        assertThat(failedReports.size(), is(1));
+        assertThat(failedReports.toString(), containsString("did not match the expected value 'containing sub-string with ignoring case:"));
+    }
+
+    @Test
+    public void testIgnoreCaseWith_containsMatch() throws Exception {
+        ScenarioSpec scenarioSpec = smartUtils.jsonFileToJava("18_ignore_case/test_string_match_withIgnoring_case.json", ScenarioSpec.class);
+
+        final String assertionsSectionAsString = scenarioSpec.getSteps().get(0).getAssertions().toString();
+        String mockScenarioState = "{}";
+
+        final String resolvedAssertions = jsonPreProcessor.resolveStringJson(assertionsSectionAsString, mockScenarioState);
+        assertThat(resolvedAssertions, containsString("{\"name\":\"$CONTAINS.STRING.IGNORECASE:CReASY\"}}"));
+
+        List<JsonAsserter> asserters = jsonPreProcessor.createAssertersFrom(resolvedAssertions);
+        assertThat(asserters.size(), is(2));
+
+        String mockTestResponse = "{\n" +
+                "  \"status\": 201,\n" +
+                "  \"body\": {\n" +
+                "    \"name\": \"Hello Creasy\"\n" +
+                "  }\n" +
+                "}";
+        List<AssertionReport> failedReports = jsonPreProcessor.assertAllAndReturnFailed(asserters, mockTestResponse);
+
+        assertThat(failedReports.size(), is(0));
+    }
 }
\ No newline at end of file
diff --git a/src/test/resources/15_localdatetime/00_local_date_time_place_holders_unit_test.json b/src/test/resources/15_localdatetime/00_local_date_time_place_holders_unit_test.json
index 3cbcba83..d4d8a6ba 100644
--- a/src/test/resources/15_localdatetime/00_local_date_time_place_holders_unit_test.json
+++ b/src/test/resources/15_localdatetime/00_local_date_time_place_holders_unit_test.json
@@ -1,31 +1,30 @@
 {
-  "scenarioName": "Given_When_Then-Flow name",
-  "steps": [
-    {
-      "name": "StepNameWithoutSpaceEgCREATESD",
-      "url": "/persons",
-      "operation": "POST",
-      "request": {
-        "headers": {
-          "Content-Type": "application/json;charset=UTF-8"
-        },
-        "queryParams": {
-          "param1": "value1"
-        },
-        "body": {
-          "persons": {
-            "dateOfBirth": "${LOCAL.DATE.TODAY:yyyy-MM-dd}",
-            "birthDateOnly": "${LOCAL.DATETIME.NOW:yyyy}"
-          }
+    "scenarioName": "Given_When_Then-Flow name",
+    "steps": [
+        {
+            "name": "StepNameWithoutSpaceEgCREATESD",
+            "url": "/persons",
+            "operation": "POST",
+            "request": {
+                "headers": {
+                    "Content-Type": "application/json;charset=UTF-8"
+                },
+                "queryParams": {
+                    "param1": "value1"
+                },
+                "body": {
+                    "persons": {
+                        "dateOfBirth": "${LOCAL.DATE.TODAY:yyyy-MM-dd}",
+                        "birthDateOnly": "${LOCAL.DATETIME.NOW:yyyy}"
+                    }
+                }
+            },
+            "assertions": {
+                "status": 201,
+                "body": {
+                    "id": 1001
+                }
+            }
         }
-      },
-      "assertions": {
-        "status": 201,
-        "body": {
-          "id": 1001
-        }
-      }
-    }
-
-  ]
+    ]
 }
diff --git a/src/test/resources/18_ignore_case/test_string_match_withIgnoring_case.json b/src/test/resources/18_ignore_case/test_string_match_withIgnoring_case.json
new file mode 100644
index 00000000..27fcfcd6
--- /dev/null
+++ b/src/test/resources/18_ignore_case/test_string_match_withIgnoring_case.json
@@ -0,0 +1,23 @@
+{
+    "scenarioName": "Testing string comparable with ignoring case",
+    "steps": [
+        {
+            "name": "StepNameWithoutSpaceEgCREATESD",
+            "url": "/persons",
+            "operation": "POST",
+            "request": {
+                "body": {
+                    "persons": {
+                        "firstName": "Oliver Creasy"
+                    }
+                }
+            },
+            "assertions": {
+                "status": 201,
+                "body": {
+                    "name": "$CONTAINS.STRING.IGNORECASE:CReASY"
+                }
+            }
+        }
+    ]
+}