From 8791e20c3703c8682542143ccc302a85d5b6ce43 Mon Sep 17 00:00:00 2001 From: Bharat Kumar Malviya Date: Tue, 9 Nov 2021 18:22:55 +0530 Subject: [PATCH 1/4] ISS-492 added support for yaml as step file --- .../ZeroCodeExternalFileProcessorImpl.java | 27 +++++++++++++++++-- .../engine/tokens/ZeroCodeValueTokens.java | 1 + .../zerocode/core/yaml/YamlUnitTest.java | 18 +++++++++++++ .../yaml/scenario_get_api_step.yml | 12 +++++++++ .../yaml/scenario_get_api_step_test.yml | 4 +++ 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 core/src/test/java/org/jsmart/zerocode/core/yaml/YamlUnitTest.java create mode 100644 core/src/test/resources/unit_test_files/yaml/scenario_get_api_step.yml create mode 100644 core/src/test/resources/unit_test_files/yaml/scenario_get_api_step_test.yml diff --git a/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeExternalFileProcessorImpl.java b/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeExternalFileProcessorImpl.java index 0384f71dd..5a5099f7e 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeExternalFileProcessorImpl.java +++ b/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeExternalFileProcessorImpl.java @@ -10,11 +10,15 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; + +import com.google.inject.name.Named; import org.jsmart.zerocode.core.domain.Step; import org.slf4j.Logger; import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.JSON_PAYLOAD_FILE; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.YML_PAYLOAD_FILE; import static org.jsmart.zerocode.core.utils.SmartUtils.readJsonAsString; +import static org.jsmart.zerocode.core.utils.SmartUtils.readYamlAsString; import static org.jsmart.zerocode.core.utils.TokenUtils.getTestCaseTokens; import static org.slf4j.LoggerFactory.getLogger; @@ -42,6 +46,10 @@ public class ZeroCodeExternalFileProcessorImpl implements ZeroCodeExternalFilePr private final ObjectMapper objectMapper; + @Inject + @Named("YamlMapper") + private ObjectMapper yamlMapper; + @Inject public ZeroCodeExternalFileProcessorImpl(ObjectMapper objectMapper) { this.objectMapper = objectMapper; @@ -127,7 +135,7 @@ void digReplaceContent(Map map) { } else { LOGGER.debug("Leaf node found = {}, checking for any external json file...", value); - if (value != null && value.toString().contains(JSON_PAYLOAD_FILE)) { + if (value != null && (value.toString().contains(JSON_PAYLOAD_FILE) || value.toString().contains(YML_PAYLOAD_FILE))) { LOGGER.info("Found external JSON file place holder = {}. Replacing with content", value); String valueString = value.toString(); String token = getJsonFilePhToken(valueString); @@ -147,6 +155,21 @@ void digReplaceContent(Map map) { throw new RuntimeException(exx); } } + if (token != null && token.startsWith(YML_PAYLOAD_FILE)) { + String resourceJsonFile = token.substring(YML_PAYLOAD_FILE.length()); + try { + JsonNode jsonNode = yamlMapper.readTree(readYamlAsString(resourceJsonFile)); + if (jsonNode.isObject()) { + final Map yamlFileContent = objectMapper.convertValue(jsonNode, Map.class); + digReplaceContent(yamlFileContent); + jsonNode = objectMapper.convertValue(yamlFileContent, JsonNode.class); + } + entry.setValue(jsonNode); + } catch (Exception exx) { + LOGGER.error("External file reference exception - {}", exx.getMessage()); + throw new RuntimeException(exx); + } + } // ---------------------------------------------------- // Extension- for XML file type in case a ticket raised // ---------------------------------------------------- @@ -184,7 +207,7 @@ boolean checkDigNeeded(Step thisStep) throws JsonProcessingException { String stepJson = objectMapper.writeValueAsString(thisStep); List allTokens = getTestCaseTokens(stepJson); - return allTokens.toString().contains(JSON_PAYLOAD_FILE); + return allTokens.toString().contains(JSON_PAYLOAD_FILE) || allTokens.toString().contains(YML_PAYLOAD_FILE); } } 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 d6bc2fb06..8a0d76e36 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 @@ -10,6 +10,7 @@ */ public class ZeroCodeValueTokens { public static final String JSON_PAYLOAD_FILE = "JSON.FILE:"; + public static final String YML_PAYLOAD_FILE = "YML.FILE:"; public static final String PREFIX_ASU = "ASU"; public static final String XML_FILE = "XML.FILE:"; public static final String GQL_FILE = "GQL.FILE:"; diff --git a/core/src/test/java/org/jsmart/zerocode/core/yaml/YamlUnitTest.java b/core/src/test/java/org/jsmart/zerocode/core/yaml/YamlUnitTest.java new file mode 100644 index 000000000..4dfbb761b --- /dev/null +++ b/core/src/test/java/org/jsmart/zerocode/core/yaml/YamlUnitTest.java @@ -0,0 +1,18 @@ +package org.jsmart.zerocode.core.yaml; + +import org.jsmart.zerocode.core.domain.JsonTestCase; +import org.jsmart.zerocode.core.domain.TargetEnv; +import org.jsmart.zerocode.core.runner.ZeroCodeUnitRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + + +@TargetEnv("github_host_test.properties") +@RunWith(ZeroCodeUnitRunner.class) +public class YamlUnitTest { + + @Test + @JsonTestCase("unit_test_files/yaml/scenario_get_api_step_test.yml") + public void testGitHubApi_get() { + } +} diff --git a/core/src/test/resources/unit_test_files/yaml/scenario_get_api_step.yml b/core/src/test/resources/unit_test_files/yaml/scenario_get_api_step.yml new file mode 100644 index 000000000..671ce3141 --- /dev/null +++ b/core/src/test/resources/unit_test_files/yaml/scenario_get_api_step.yml @@ -0,0 +1,12 @@ +name: "get_user_details" +url: "/users/octocat" +operation: "GET" +request: + - +verify: + status: 200 + body: + login: "octocat" + id: 583231 + type: "User" + location: "$MATCHES.STRING:San Fra(.*)" \ No newline at end of file diff --git a/core/src/test/resources/unit_test_files/yaml/scenario_get_api_step_test.yml b/core/src/test/resources/unit_test_files/yaml/scenario_get_api_step_test.yml new file mode 100644 index 000000000..2fe85e24c --- /dev/null +++ b/core/src/test/resources/unit_test_files/yaml/scenario_get_api_step_test.yml @@ -0,0 +1,4 @@ +--- +scenarioName: "GIVEN-the GitHub REST end point, WHEN-I invoke GET, THEN-I will receive the 200 status with body" +steps: + - stepFile: ${YML.FILE:unit_test_files/yaml/scenario_get_api_step.yml} \ No newline at end of file From 6c74db12c446fafbaca64a0902ee41de49f37438 Mon Sep 17 00:00:00 2001 From: Bharat Kumar Malviya Date: Thu, 17 Nov 2022 20:24:06 +0530 Subject: [PATCH 2/4] Update core/src/main/java/org/jsmart/zerocode/core/engine/tokens/ZeroCodeValueTokens.java Co-authored-by: Ludovic Dussart --- .../jsmart/zerocode/core/engine/tokens/ZeroCodeValueTokens.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 8a0d76e36..b2466f32d 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 @@ -10,7 +10,7 @@ */ public class ZeroCodeValueTokens { public static final String JSON_PAYLOAD_FILE = "JSON.FILE:"; - public static final String YML_PAYLOAD_FILE = "YML.FILE:"; + public static final String YAML_PAYLOAD_FILE = "YAML.FILE:"; public static final String PREFIX_ASU = "ASU"; public static final String XML_FILE = "XML.FILE:"; public static final String GQL_FILE = "GQL.FILE:"; From 11b36c3db65a22caa491ffc517067954df1b0fbc Mon Sep 17 00:00:00 2001 From: Bharat Kumar Malviya Date: Thu, 17 Nov 2022 20:25:00 +0530 Subject: [PATCH 3/4] Update core/src/test/resources/unit_test_files/yaml/scenario_get_api_step_test.yml Co-authored-by: Ludovic Dussart --- .../unit_test_files/yaml/scenario_get_api_step_test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/test/resources/unit_test_files/yaml/scenario_get_api_step_test.yml b/core/src/test/resources/unit_test_files/yaml/scenario_get_api_step_test.yml index 2fe85e24c..4eb2b42e6 100644 --- a/core/src/test/resources/unit_test_files/yaml/scenario_get_api_step_test.yml +++ b/core/src/test/resources/unit_test_files/yaml/scenario_get_api_step_test.yml @@ -1,4 +1,4 @@ --- scenarioName: "GIVEN-the GitHub REST end point, WHEN-I invoke GET, THEN-I will receive the 200 status with body" steps: - - stepFile: ${YML.FILE:unit_test_files/yaml/scenario_get_api_step.yml} \ No newline at end of file + - stepFile: ${YAML.FILE:unit_test_files/yaml/scenario_get_api_step.yml} \ No newline at end of file From d87f6995216fb46b4e2125a8f39fb77725229144 Mon Sep 17 00:00:00 2001 From: Bharat Kumar Malviya Date: Fri, 18 Nov 2022 09:58:38 +0530 Subject: [PATCH 4/4] Code refactoring based on input --- .../ZeroCodeExternalFileProcessorImpl.java | 30 +++++-------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeExternalFileProcessorImpl.java b/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeExternalFileProcessorImpl.java index 5a5099f7e..9da65ceea 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeExternalFileProcessorImpl.java +++ b/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeExternalFileProcessorImpl.java @@ -16,7 +16,7 @@ import org.slf4j.Logger; import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.JSON_PAYLOAD_FILE; -import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.YML_PAYLOAD_FILE; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.YAML_PAYLOAD_FILE; import static org.jsmart.zerocode.core.utils.SmartUtils.readJsonAsString; import static org.jsmart.zerocode.core.utils.SmartUtils.readYamlAsString; import static org.jsmart.zerocode.core.utils.TokenUtils.getTestCaseTokens; @@ -135,16 +135,15 @@ void digReplaceContent(Map map) { } else { LOGGER.debug("Leaf node found = {}, checking for any external json file...", value); - if (value != null && (value.toString().contains(JSON_PAYLOAD_FILE) || value.toString().contains(YML_PAYLOAD_FILE))) { - LOGGER.info("Found external JSON file place holder = {}. Replacing with content", value); + if (value != null && (value.toString().contains(JSON_PAYLOAD_FILE) || value.toString().contains(YAML_PAYLOAD_FILE))) { + LOGGER.info("Found external JSON/YAML file place holder = {}. Replacing with content", value); String valueString = value.toString(); String token = getJsonFilePhToken(valueString); - if (token != null && token.startsWith(JSON_PAYLOAD_FILE)) { - String resourceJsonFile = token.substring(JSON_PAYLOAD_FILE.length()); + if (token != null && (token.startsWith(JSON_PAYLOAD_FILE) || token.startsWith(YAML_PAYLOAD_FILE))) { try { - JsonNode jsonNode = objectMapper.readTree(readJsonAsString(resourceJsonFile)); + JsonNode jsonNode = token.startsWith(YAML_PAYLOAD_FILE) ? yamlMapper.readTree(readYamlAsString(token.substring(YAML_PAYLOAD_FILE.length()))) : objectMapper.readTree(readJsonAsString(token.substring(JSON_PAYLOAD_FILE.length()))); if (jsonNode.isObject()) { - //also replace content of just read json file (recursively) + //also replace content of just read json/yaml file (recursively) final Map jsonFileContent = objectMapper.convertValue(jsonNode, Map.class); digReplaceContent(jsonFileContent); jsonNode = objectMapper.convertValue(jsonFileContent, JsonNode.class); @@ -155,21 +154,6 @@ void digReplaceContent(Map map) { throw new RuntimeException(exx); } } - if (token != null && token.startsWith(YML_PAYLOAD_FILE)) { - String resourceJsonFile = token.substring(YML_PAYLOAD_FILE.length()); - try { - JsonNode jsonNode = yamlMapper.readTree(readYamlAsString(resourceJsonFile)); - if (jsonNode.isObject()) { - final Map yamlFileContent = objectMapper.convertValue(jsonNode, Map.class); - digReplaceContent(yamlFileContent); - jsonNode = objectMapper.convertValue(yamlFileContent, JsonNode.class); - } - entry.setValue(jsonNode); - } catch (Exception exx) { - LOGGER.error("External file reference exception - {}", exx.getMessage()); - throw new RuntimeException(exx); - } - } // ---------------------------------------------------- // Extension- for XML file type in case a ticket raised // ---------------------------------------------------- @@ -207,7 +191,7 @@ boolean checkDigNeeded(Step thisStep) throws JsonProcessingException { String stepJson = objectMapper.writeValueAsString(thisStep); List allTokens = getTestCaseTokens(stepJson); - return allTokens.toString().contains(JSON_PAYLOAD_FILE) || allTokens.toString().contains(YML_PAYLOAD_FILE); + return allTokens.toString().contains(JSON_PAYLOAD_FILE) || allTokens.toString().contains(YAML_PAYLOAD_FILE); } }