From 59d1ff89b91d0a9dfa421d45eb19301942515ebe Mon Sep 17 00:00:00 2001 From: Pratyush Kumar Date: Fri, 5 Sep 2025 20:25:38 -0700 Subject: [PATCH 1/3] Add support for loading scenarios from relative path. --- .../main/java/org/jsmart/zerocode/core/utils/SmartUtils.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java b/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java index 966f1476..bb2ab9a8 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java +++ b/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java @@ -63,6 +63,11 @@ static String readFile(String path, Charset encoding) throws IOException { public static String readJsonAsString(String scenarioFile) { try { + String pwd = System.getProperty("user.dir"); + if (scenarioFile.startsWith("./") || scenarioFile.startsWith("../")) { + // Relative path: Resolve from PWD + scenarioFile = Paths.get(pwd, scenarioFile.substring(2)).normalize().toString(); // Adjust for ../ + } scenarioFile = replaceHome(scenarioFile); if (isValidAbsolutePath(scenarioFile)) { return readFile(scenarioFile, UTF_8); From c75acfacd5f251c1677409100938b069d9afbb74 Mon Sep 17 00:00:00 2001 From: Pratyush Kumar Date: Sat, 6 Sep 2025 18:27:05 -0700 Subject: [PATCH 2/3] resolve relative path in a separate function and add a unit test. --- .../jsmart/zerocode/core/utils/SmartUtils.java | 14 +++++++++----- .../zerocode/core/utils/SmartUtilsTest.java | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java b/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java index bb2ab9a8..cf9e2edc 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java +++ b/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java @@ -63,11 +63,7 @@ static String readFile(String path, Charset encoding) throws IOException { public static String readJsonAsString(String scenarioFile) { try { - String pwd = System.getProperty("user.dir"); - if (scenarioFile.startsWith("./") || scenarioFile.startsWith("../")) { - // Relative path: Resolve from PWD - scenarioFile = Paths.get(pwd, scenarioFile.substring(2)).normalize().toString(); // Adjust for ../ - } + scenarioFile = resolveRelativePath(scenarioFile); scenarioFile = replaceHome(scenarioFile); if (isValidAbsolutePath(scenarioFile)) { return readFile(scenarioFile, UTF_8); @@ -149,6 +145,14 @@ public static String replaceHome(String path) { return path; } + public static String resolveRelativePath(String path){ + String pwd = System.getProperty("user.dir"); + if (path.startsWith("./")) { + path = Paths.get(pwd, path.substring(2)).normalize().toString(); + } + return path; + } + public List getScenarioSpecListByPackage(String packageName) { List allEndPointFiles = getAllEndPointFiles(packageName); List scenarioSpecList = allEndPointFiles.stream() diff --git a/core/src/test/java/org/jsmart/zerocode/core/utils/SmartUtilsTest.java b/core/src/test/java/org/jsmart/zerocode/core/utils/SmartUtilsTest.java index e879a33d..8ab6237b 100644 --- a/core/src/test/java/org/jsmart/zerocode/core/utils/SmartUtilsTest.java +++ b/core/src/test/java/org/jsmart/zerocode/core/utils/SmartUtilsTest.java @@ -226,6 +226,24 @@ public void testScenarioFile_absolutePath() throws Exception { // mvn clean } + @Test + public void testScenarioFile_relativePath() throws Exception { + + // Test Relative Path. + String testPath = "./target/temp/unit_test_files/cherry_pick_tests/folder_a/relative_path_test_case.json"; + + String resolvedPath = SmartUtils.resolveRelativePath(testPath); + String pwd = System.getProperty("user.dir"); + String expectedResolvedPath = pwd + "/" + testPath.substring(2); + boolean resolvedBool = resolvedPath.equals(expectedResolvedPath); + + System.out.println("Test File Input Before Relative Path Resolution: " + testPath); + System.out.println("Test File Input After Relative Path Resolution: " + resolvedPath); + System.out.println("Expected Test File After Relative Path Resolution: " + expectedResolvedPath); + System.out.println("Is resolved path equal to the expected path (pwd + relative path): " + resolvedBool); + + } + @Ignore("Tested in local laptop. Ignored for Ci build. Follow testSuiteFolder_absolutePath() like flow ") @Test public void testSuiteFolder_symAbsolutePath() { From 4a5e462695c3cf10f90ed949c62159a5075b35f7 Mon Sep 17 00:00:00 2001 From: Pratyush Kumar Date: Sat, 13 Sep 2025 10:10:30 -0700 Subject: [PATCH 3/3] Remove the resolveRelativePath function since relative paths are also already handled, update unit and integration tests. --- .../zerocode/core/utils/SmartUtils.java | 9 -------- .../zerocode/core/utils/SmartUtilsTest.java | 21 +++++++++--------- .../RelativePathScenarioLoadingTest.java | 22 +++++++++++++++++++ 3 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 core/src/test/java/org/jsmart/zerocode/integrationtests/RelativePathScenarioLoadingTest.java diff --git a/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java b/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java index cf9e2edc..966f1476 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java +++ b/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java @@ -63,7 +63,6 @@ static String readFile(String path, Charset encoding) throws IOException { public static String readJsonAsString(String scenarioFile) { try { - scenarioFile = resolveRelativePath(scenarioFile); scenarioFile = replaceHome(scenarioFile); if (isValidAbsolutePath(scenarioFile)) { return readFile(scenarioFile, UTF_8); @@ -145,14 +144,6 @@ public static String replaceHome(String path) { return path; } - public static String resolveRelativePath(String path){ - String pwd = System.getProperty("user.dir"); - if (path.startsWith("./")) { - path = Paths.get(pwd, path.substring(2)).normalize().toString(); - } - return path; - } - public List getScenarioSpecListByPackage(String packageName) { List allEndPointFiles = getAllEndPointFiles(packageName); List scenarioSpecList = allEndPointFiles.stream() diff --git a/core/src/test/java/org/jsmart/zerocode/core/utils/SmartUtilsTest.java b/core/src/test/java/org/jsmart/zerocode/core/utils/SmartUtilsTest.java index 8ab6237b..cc9b2b4b 100644 --- a/core/src/test/java/org/jsmart/zerocode/core/utils/SmartUtilsTest.java +++ b/core/src/test/java/org/jsmart/zerocode/core/utils/SmartUtilsTest.java @@ -228,19 +228,18 @@ public void testScenarioFile_absolutePath() throws Exception { @Test public void testScenarioFile_relativePath() throws Exception { + // Function to test that the readJsonAsString function reads scenarios with relative paths as well. // Test Relative Path. - String testPath = "./target/temp/unit_test_files/cherry_pick_tests/folder_a/relative_path_test_case.json"; - - String resolvedPath = SmartUtils.resolveRelativePath(testPath); - String pwd = System.getProperty("user.dir"); - String expectedResolvedPath = pwd + "/" + testPath.substring(2); - boolean resolvedBool = resolvedPath.equals(expectedResolvedPath); - - System.out.println("Test File Input Before Relative Path Resolution: " + testPath); - System.out.println("Test File Input After Relative Path Resolution: " + resolvedPath); - System.out.println("Expected Test File After Relative Path Resolution: " + expectedResolvedPath); - System.out.println("Is resolved path equal to the expected path (pwd + relative path): " + resolvedBool); + String classPath = "unit_test_files/cherry_pick_tests/folder_a/test_case_1.json"; + String relativeTestPath = "./src/test/resources/unit_test_files/cherry_pick_tests/folder_a/test_case_1.json"; + + String jsonStringFromClassPath = SmartUtils.readJsonAsString(classPath); + String jsonStringFromRelativePath = SmartUtils.readJsonAsString(relativeTestPath); + boolean classPathRelativePathCheckBool = jsonStringFromRelativePath.equals(jsonStringFromClassPath); + assert(classPathRelativePathCheckBool); + + System.out.println("Does readJsonAsString load the same file from relative and classpth: " + classPathRelativePathCheckBool); } diff --git a/core/src/test/java/org/jsmart/zerocode/integrationtests/RelativePathScenarioLoadingTest.java b/core/src/test/java/org/jsmart/zerocode/integrationtests/RelativePathScenarioLoadingTest.java new file mode 100644 index 00000000..1bfd813f --- /dev/null +++ b/core/src/test/java/org/jsmart/zerocode/integrationtests/RelativePathScenarioLoadingTest.java @@ -0,0 +1,22 @@ +package org.jsmart.zerocode.integrationtests; + +import org.jsmart.zerocode.core.domain.HostProperties; +import org.jsmart.zerocode.core.domain.Scenario; +import org.jsmart.zerocode.core.tests.customrunner.TestOnlyZeroCodeUnitRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +@HostProperties(host = "http://localhost", port = 9998, context = "") +@RunWith(TestOnlyZeroCodeUnitRunner.class) +public class RelativePathScenarioLoadingTest { + + @Test + @Scenario("./src/test/resources/integration_test_files/helloworld/get_api_integration_test.json") + public void testValidateSortedResponse() throws Exception { + + } + +} + + +