Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.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;
import static org.slf4j.LoggerFactory.getLogger;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -127,16 +135,15 @@ void digReplaceContent(Map<String, Object> map) {

} else {
LOGGER.debug("Leaf node found = {}, checking for any external json file...", value);
if (value != null && value.toString().contains(JSON_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<String, Object> jsonFileContent = objectMapper.convertValue(jsonNode, Map.class);
digReplaceContent(jsonFileContent);
jsonNode = objectMapper.convertValue(jsonFileContent, JsonNode.class);
Expand Down Expand Up @@ -184,7 +191,7 @@ boolean checkDigNeeded(Step thisStep) throws JsonProcessingException {
String stepJson = objectMapper.writeValueAsString(thisStep);
List<String> allTokens = getTestCaseTokens(stepJson);

return allTokens.toString().contains(JSON_PAYLOAD_FILE);
return allTokens.toString().contains(JSON_PAYLOAD_FILE) || allTokens.toString().contains(YAML_PAYLOAD_FILE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
public class ZeroCodeValueTokens {
public static final String JSON_PAYLOAD_FILE = "JSON.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:";
Expand Down
18 changes: 18 additions & 0 deletions core/src/test/java/org/jsmart/zerocode/core/yaml/YamlUnitTest.java
Original file line number Diff line number Diff line change
@@ -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() {
}
}
Original file line number Diff line number Diff line change
@@ -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(.*)"
Original file line number Diff line number Diff line change
@@ -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: ${YAML.FILE:unit_test_files/yaml/scenario_get_api_step.yml}