Repository: authorjapps/zerocode
Pull request #550 (2022-11-04 11:55:51)

Global Unique and random number - Issue 549
# <Feature Title>

PR Branch
**_#ADD LINK TO THE PR BRANCH_**

## Motivation and Context

## Checklist:

* [x] Unit tests added

* [ ] Integration tests added

* [x] Test names are meaningful

* [x] Feature manually tested

* [x] Branch build passed

* [x] No 'package.*' in the imports

* [x] 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) ?
  * [x] 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/pom.xml b/core/pom.xml
index 169b88b3..57c5160f 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -4,7 +4,7 @@
     <parent>
         <artifactId>zerocode-tdd-parent</artifactId>
         <groupId>org.jsmart</groupId>
-        <version>1.3.30-SNAPSHOT</version>
+        <version>1.3.31-SNAPSHOT</version>
     </parent>
 
     <artifactId>zerocode-tdd</artifactId>
diff --git a/core/src/main/java/org/jsmart/zerocode/core/engine/tokens/ZeroCodeValueTokens.java b/core/src/main/java/org/jsmart/zerocode/core/engine/tokens/ZeroCodeValueTokens.java
index d6bc2fb0..a12b983b 100644
--- a/core/src/main/java/org/jsmart/zerocode/core/engine/tokens/ZeroCodeValueTokens.java
+++ b/core/src/main/java/org/jsmart/zerocode/core/engine/tokens/ZeroCodeValueTokens.java
@@ -1,6 +1,8 @@
 package org.jsmart.zerocode.core.engine.tokens;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import static java.util.Arrays.asList;
 
@@ -18,6 +20,7 @@ public class ZeroCodeValueTokens {
     public static final String RECORD_DUMP = "RECORD.DUMP:";
     public static final String RANDOM_NUMBER = "RANDOM.NUMBER";
     public static final String RANDOM_NUMBER_FIXED = "RANDOM.NUMBER.FIXED";
+    public static final String GLOBAL_RANDOM_NUMBER = "GLOBAL.RANDOM.NUMBER";
     public static final String RANDOM_STRING_ALPHA = "RANDOM.STRING:";
     public static final String RANDOM_STRING_ALPHA_NUMERIC = "RANDOM.ALPHANUMERIC:";
     public static final String STATIC_ALPHABET = "STATIC.ALPHABET:";
@@ -28,10 +31,13 @@ public class ZeroCodeValueTokens {
     public static final String $VALUE = ".$VALUE";
     public static final String ABS_PATH = "ABS.PATH:";
 
+    public static Map<String, Object> globalTokenCache = new HashMap<>();
+
     public static List<String> getKnownTokens() {
         return asList(
                 PREFIX_ASU,
                 RANDOM_NUMBER,
+                GLOBAL_RANDOM_NUMBER,
                 RANDOM_STRING_ALPHA,
                 RANDOM_STRING_ALPHA_NUMERIC,
                 STATIC_ALPHABET,
diff --git a/core/src/main/java/org/jsmart/zerocode/core/utils/TokenUtils.java b/core/src/main/java/org/jsmart/zerocode/core/utils/TokenUtils.java
index 1c263878..e54a5036 100644
--- a/core/src/main/java/org/jsmart/zerocode/core/utils/TokenUtils.java
+++ b/core/src/main/java/org/jsmart/zerocode/core/utils/TokenUtils.java
@@ -1,37 +1,42 @@
 package org.jsmart.zerocode.core.utils;
 
+import org.apache.commons.lang.text.StrSubstitutor;
+
 import java.io.File;
 import java.net.URL;
 import java.nio.file.Paths;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
-import java.util.*;
-import java.util.concurrent.ThreadLocalRandom;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import org.apache.commons.lang.text.StrSubstitutor;
 
-import static java.util.UUID.randomUUID;
 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
 import static org.apache.commons.lang.StringEscapeUtils.escapeJava;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.ABS_PATH;
+import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.GLOBAL_RANDOM_NUMBER;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.GQL_FILE;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.LOCALDATETIME_NOW;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.LOCALDATE_TODAY;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.RANDOM_NUMBER;
+import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.RANDOM_NUMBER_FIXED;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.RANDOM_STRING_ALPHA;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.RANDOM_STRING_ALPHA_NUMERIC;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.RANDOM_UU_ID;
+import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.RANDOM_UU_ID_FIXED;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.STATIC_ALPHABET;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.SYSTEM_ENV;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.SYSTEM_PROPERTY;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.XML_FILE;
-import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.RANDOM_UU_ID_FIXED;
-import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.RANDOM_NUMBER_FIXED;
-
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.getKnownTokens;
+import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.globalTokenCache;
+
 public class TokenUtils {
 
     public static String resolveKnownTokens(String requestJsonOrAnyString) {
@@ -66,15 +71,27 @@ public class TokenUtils {
                                 }
                             }
 
-                        } else if (runTimeToken.startsWith(RANDOM_STRING_ALPHA)) {
+                        }
+                        else if (runTimeToken.startsWith(GLOBAL_RANDOM_NUMBER)) {
+                            String globalRandomNumber = (String) globalTokenCache.get(GLOBAL_RANDOM_NUMBER);
+                            if(globalRandomNumber == null){
+                                globalRandomNumber = new RandomNumberGenerator().toString();
+                                globalTokenCache.put(GLOBAL_RANDOM_NUMBER, globalRandomNumber);
+                            }
+                            paramaMap.put(runTimeToken, globalRandomNumber);
+
+                        }
+                        else if (runTimeToken.startsWith(RANDOM_STRING_ALPHA)) {
                             int length = Integer.parseInt(runTimeToken.substring(RANDOM_STRING_ALPHA.length()));
                             paramaMap.put(runTimeToken, createRandomAlphaString(length));
 
-                        } else if (runTimeToken.startsWith(RANDOM_STRING_ALPHA_NUMERIC)) {
+                        }
+                        else if (runTimeToken.startsWith(RANDOM_STRING_ALPHA_NUMERIC)) {
                             int length = Integer.parseInt(runTimeToken.substring(RANDOM_STRING_ALPHA_NUMERIC.length()));
                             paramaMap.put(runTimeToken, createRandomAlphaNumericString(length));
 
-                        } else if (runTimeToken.startsWith(STATIC_ALPHABET)) {
+                        }
+                        else if (runTimeToken.startsWith(STATIC_ALPHABET)) {
                             int length = Integer.parseInt(runTimeToken.substring(STATIC_ALPHABET.length()));
                             paramaMap.put(runTimeToken, createStaticAlphaString(length));
 
diff --git a/pom.xml b/pom.xml
index 23128e7a..0396f61c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -9,7 +9,7 @@
 
     <artifactId>zerocode-tdd-parent</artifactId>
     <groupId>org.jsmart</groupId>
-    <version>1.3.30-SNAPSHOT</version>
+    <version>1.3.31-SNAPSHOT</version>
 
     <packaging>pom</packaging>
     <name>ZeroCode TDD Parent</name>
diff --git a/zerocode-maven-archetype/pom.xml b/zerocode-maven-archetype/pom.xml
index 82a35907..bf3e8d52 100644
--- a/zerocode-maven-archetype/pom.xml
+++ b/zerocode-maven-archetype/pom.xml
@@ -4,7 +4,7 @@
     <parent>
         <groupId>org.jsmart</groupId>
         <artifactId>zerocode-tdd-parent</artifactId>
-        <version>1.3.30-SNAPSHOT</version>
+        <version>1.3.31-SNAPSHOT</version>
     </parent>
     <artifactId>zerocode-maven-archetype</artifactId>
 


Test patch:
diff --git a/core/src/test/java/org/jsmart/zerocode/core/utils/TokenUtilsTest.java b/core/src/test/java/org/jsmart/zerocode/core/utils/TokenUtilsTest.java
index 1fdbb989..9566c4dc 100644
--- a/core/src/test/java/org/jsmart/zerocode/core/utils/TokenUtilsTest.java
+++ b/core/src/test/java/org/jsmart/zerocode/core/utils/TokenUtilsTest.java
@@ -18,6 +18,24 @@ public class TokenUtilsTest {
     @Rule
     public ExpectedException exceptionRule = ExpectedException.none();
 
+    static String globalRandomNumber = "";
+
+    @Test
+    public void testGlobalRandomNumberSameness_1(){
+        String result = resolveKnownTokens("${GLOBAL.RANDOM.NUMBER},${GLOBAL.RANDOM.NUMBER}");
+        String[] split = result.split(",");
+        assertTrue(split[0].equals(split[1]));
+        globalRandomNumber = split[0];
+    }
+
+    @Test
+    public void testGlobalRandomNumberSameness_2(){
+        String result = resolveKnownTokens("${GLOBAL.RANDOM.NUMBER},${GLOBAL.RANDOM.NUMBER}");
+        String[] split = result.split(",");
+        assertTrue(split[0].equals(split[1]));
+        assertTrue(split[0].equals(globalRandomNumber));
+    }
+
     @Test
     public void testResolve_knownTokens() {
         String clientId = "zerocode-clientid_${RANDOM.NUMBER}";
diff --git a/http-testing/pom.xml b/http-testing/pom.xml
index 74e4095a..388c5eb2 100644
--- a/http-testing/pom.xml
+++ b/http-testing/pom.xml
@@ -4,7 +4,7 @@
     <parent>
         <artifactId>zerocode-tdd-parent</artifactId>
         <groupId>org.jsmart</groupId>
-        <version>1.3.30-SNAPSHOT</version>
+        <version>1.3.31-SNAPSHOT</version>
     </parent>
 
     <groupId>org.jsmart</groupId>
diff --git a/junit5-testing/pom.xml b/junit5-testing/pom.xml
index 8a892582..f2fc344e 100644
--- a/junit5-testing/pom.xml
+++ b/junit5-testing/pom.xml
@@ -4,7 +4,7 @@
     <parent>
         <artifactId>zerocode-tdd-parent</artifactId>
         <groupId>org.jsmart</groupId>
-        <version>1.3.30-SNAPSHOT</version>
+        <version>1.3.31-SNAPSHOT</version>
     </parent>
 
     <artifactId>zerocode-tdd-jupiter</artifactId>
diff --git a/kafka-testing/pom.xml b/kafka-testing/pom.xml
index 9b3d0061..74b1cfe1 100644
--- a/kafka-testing/pom.xml
+++ b/kafka-testing/pom.xml
@@ -4,7 +4,7 @@
 	<parent>
 		<artifactId>zerocode-tdd-parent</artifactId>
 		<groupId>org.jsmart</groupId>
-		<version>1.3.30-SNAPSHOT</version>
+		<version>1.3.31-SNAPSHOT</version>
 	</parent>
 
 	<artifactId>kafka-testing</artifactId>