Repository: authorjapps/zerocode
Pull request #377 (2020-04-06 16:12:58)

ISS-376 # [ABS.PATH] in Kafka properties
- On user request(raise an issue)

# <Feature Title>

PR Branch
**_#ADD LINK TO THE PR BRANCH_**

## Motivation and Context

## Checklist:

* [x] Unit tests added?

* [x] Integration tests added?

* [x] Test names are meaningful?

* [x] Feature manually tested?

* [x] Branch build passed?

* [x] No 'package.*' in the imports

* [ ] Relevant Wiki page updated with clear instruction for the end user

* [ ] Http test added to `http-testing` module(if applicable) ?

* [x] Kafka test added to `kafka-testing` module(if applicable) ?



Patch:
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 bc5633a9..8d35802d 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
@@ -6,7 +6,7 @@ import static java.util.Arrays.asList;
 
 /**
  * This class contains Dynamic Placeholder Value Tokens.
- * These are replaced by their actual value in runtime
+ * These are replaced by their actual value during runtime
  */
 public class ZeroCodeValueTokens {
     public static final String JSON_PAYLOAD_FILE = "JSON.FILE:";
@@ -23,6 +23,7 @@ public class ZeroCodeValueTokens {
     public static final String SYSTEM_PROPERTY = "SYSTEM.PROPERTY:";
     public static final String SYSTEM_ENV = "SYSTEM.ENV:";
     public static final String $VALUE = ".$VALUE";
+    public static final String ABS_PATH = "ABS.PATH:";
 
     public static List<String> getKnownTokens() {
         return asList(
@@ -37,6 +38,7 @@ public class ZeroCodeValueTokens {
                 XML_FILE,
                 RANDOM_UU_ID,
                 RECORD_DUMP,
+                ABS_PATH,
                 SYSTEM_ENV
         );
     }
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 f90bafdd..570ffec5 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,5 +1,8 @@
 package org.jsmart.zerocode.core.utils;
 
+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;
@@ -15,6 +18,7 @@ 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.LOCALDATETIME_NOW;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.LOCALDATE_TODAY;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.RANDOM_NUMBER;
@@ -76,12 +80,10 @@ public class TokenUtils {
                             paramaMap.put(runTimeToken, LocalDateTime.now().format(formatter));
 
                         } else if (runTimeToken.startsWith(SYSTEM_PROPERTY)) {
-
                             String propertyName = runTimeToken.substring(SYSTEM_PROPERTY.length());
                             paramaMap.put(runTimeToken, System.getProperty(propertyName));
 
-                        }else if (runTimeToken.startsWith(SYSTEM_ENV)) {
-
+                        } else if (runTimeToken.startsWith(SYSTEM_ENV)) {
                             String propertyName = runTimeToken.substring(SYSTEM_ENV.length());
                             paramaMap.put(runTimeToken, System.getenv(propertyName));
 
@@ -94,6 +96,10 @@ public class TokenUtils {
 
                         } else if (runTimeToken.startsWith(RANDOM_UU_ID)) {
                             paramaMap.put(runTimeToken, randomUUID().toString());
+
+                        } else if (runTimeToken.startsWith(ABS_PATH)) {
+                            String propertyName = runTimeToken.substring(ABS_PATH.length());
+                            paramaMap.put(runTimeToken, absolutePathOf(propertyName));
                         }
                     }
                 }
@@ -101,6 +107,7 @@ public class TokenUtils {
 
     }
 
+
     /**
      * This method was introduced later,
      * But Framework uses- ZeroCodeJsonTestProcesorImpl#getTestCaseTokens(java.lang.String)
@@ -151,5 +158,20 @@ public class TokenUtils {
         }
     }
 
+    public static String absolutePathOf(String resourceFilePath) {
+        URL res = TokenUtils.class.getClassLoader().getResource(resourceFilePath);
+        if(res == null){
+            throw new RuntimeException("Wrong file name or path found '" + resourceFilePath + "', Please fix it and rerun.");
+        }
 
+        File file = null;
+        try {
+            file = Paths.get(res.toURI()).toFile();
+        } catch (Exception e) {
+            throw new RuntimeException("Something went wrong while fetching abs path of '" + resourceFilePath + "', " +
+                    "Please recheck the file/path. Full exception is : " + e);
+        }
+
+        return file.getAbsolutePath();
+    }
 }
diff --git a/docker/compose/kafka-single-node.yml b/docker/compose/kafka-single-node.yml
index 928b2492..81adbbcb 100644
--- a/docker/compose/kafka-single-node.yml
+++ b/docker/compose/kafka-single-node.yml
@@ -2,7 +2,7 @@
 version: '2'
 services:
   zookeeper:
-    image: confluentinc/cp-zookeeper:5.0.1
+    image: confluentinc/cp-zookeeper:5.1.0
     environment:
       ZOOKEEPER_CLIENT_PORT: 2181
       ZOOKEEPER_TICK_TIME: 2000
@@ -14,7 +14,7 @@ services:
     #
     # See https://rmoff.net/2018/08/02/kafka-listeners-explained/ for details
     # -----------------------------------------------------------------------------
-    image: confluentinc/cp-kafka:5.0.1
+    image: confluentinc/cp-kafka:5.1.0
     depends_on:
       - zookeeper
     ports:


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 f4142862..7eb0b72c 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
@@ -1,19 +1,19 @@
 package org.jsmart.zerocode.core.utils;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.jsmart.zerocode.core.utils.TokenUtils.resolveKnownTokens;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-
-import java.nio.file.Paths;
 import java.util.Map;
 import java.util.stream.IntStream;
-
 import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.is;
+import static org.jsmart.zerocode.core.utils.TokenUtils.absolutePathOf;
+import static org.jsmart.zerocode.core.utils.TokenUtils.resolveKnownTokens;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
 public class TokenUtilsTest {
 
     @Rule
@@ -111,5 +111,16 @@ public class TokenUtilsTest {
         });
     }
 
+    @Test
+    public void testAbsolutePathOf_wrongPath() {
+        exceptionRule.expectMessage("Wrong file name or path found");
+        exceptionRule.expectMessage("Please fix it and rerun.");
+        absolutePathOf("WRONG_PATH/jks_files/dummy_key_store.jks");
+    }
 
+    @Test
+    public void testAbsolutePathOf() {
+        assertThat(absolutePathOf("unit_test_files/jks_files/dummy_key_store.jks"),
+                containsString("zerocode/core/target/test-classes/unit_test_files/jks_files/dummy_key_store.jks"));
+    }
 }
\ No newline at end of file
diff --git a/core/src/test/resources/unit_test_files/jks_files/dummy_key_store.jks b/core/src/test/resources/unit_test_files/jks_files/dummy_key_store.jks
new file mode 100644
index 00000000..f4fba2b9
--- /dev/null
+++ b/core/src/test/resources/unit_test_files/jks_files/dummy_key_store.jks
@@ -0,0 +1,3 @@
+-----key start----
+dummy
+-----key ends-----
\ No newline at end of file
diff --git a/kafka-testing/src/test/java/org/jsmart/zerocode/integration/tests/kafka/consume/KafkaConsumeTest.java b/kafka-testing/src/test/java/org/jsmart/zerocode/integration/tests/kafka/consume/KafkaConsumeTest.java
index 568afe83..be4db89e 100644
--- a/kafka-testing/src/test/java/org/jsmart/zerocode/integration/tests/kafka/consume/KafkaConsumeTest.java
+++ b/kafka-testing/src/test/java/org/jsmart/zerocode/integration/tests/kafka/consume/KafkaConsumeTest.java
@@ -1,6 +1,6 @@
 package org.jsmart.zerocode.integration.tests.kafka.consume;
 
-import org.jsmart.zerocode.core.domain.JsonTestCase;
+import org.jsmart.zerocode.core.domain.Scenario;
 import org.jsmart.zerocode.core.domain.TargetEnv;
 import org.jsmart.zerocode.core.runner.ZeroCodeUnitRunner;
 import org.junit.Ignore;
@@ -12,19 +12,19 @@ import org.junit.runner.RunWith;
 public class KafkaConsumeTest {
 
     @Test
-    @JsonTestCase("kafka/consume/test_kafka_consume.json")
+    @Scenario("kafka/consume/test_kafka_consume.json")
     public void testKafkaConsume() throws Exception {
     }
 
     @Ignore("TODO wip_")
     @Test
-    @JsonTestCase("kafka/consume/WIP_test_kafka_consume_with_properties.json")
+    @Scenario("kafka/consume/WIP_test_kafka_consume_with_properties.json")
     public void testKafkaLocalProperties() throws Exception {
     }
 
     @Ignore("TODO wip_")
     @Test
-    @JsonTestCase("kafka/consume/file_dump/WIP_test_kafka_consume_json_dump.json")
+    @Scenario("kafka/consume/file_dump/WIP_test_kafka_consume_json_dump.json")
     public void testKafkaJsonDump() throws Exception {
     }
 
diff --git a/kafka-testing/src/test/resources/security_files/sample_key_store.jks b/kafka-testing/src/test/resources/security_files/sample_key_store.jks
new file mode 100644
index 00000000..0b161683
--- /dev/null
+++ b/kafka-testing/src/test/resources/security_files/sample_key_store.jks
@@ -0,0 +1,3 @@
+-----key start----
+dummy cert.
+-----key ends-----
\ No newline at end of file