Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-409 # Added support for csv file in the parameterized csvSource #476

Merged
merged 12 commits into from
Jan 27, 2024
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
package org.jsmart.zerocode.core.domain;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class Parameterized {
private final List<Object> valueSource;
private final List<String> csvSource;
private final Boolean ignoreHeader;

public Parameterized(
@JsonProperty("valueSource") List<Object> valueSource,
@JsonProperty("csvSource") List<String> csvSource) {
@JsonProperty("csvSource") JsonNode csvSourceJsonNode,
@JsonProperty("ignoreHeader") Boolean ignoreHeader) {
this.valueSource = valueSource;
this.csvSource = csvSource;
this.ignoreHeader = Optional.ofNullable(ignoreHeader).orElse(false);
this.csvSource = getCsvSourceFrom(csvSourceJsonNode);
}

public List<Object> getValueSource() {
Expand All @@ -22,6 +38,43 @@ public List<String> getCsvSource() {
return csvSource;
}

private List<String> getCsvSourceFrom(JsonNode csvSourceJsonNode) {
try {
if (csvSourceJsonNode.isArray()) {
return readCsvSourceFromJson(csvSourceJsonNode);

} else {
return readCsvSourceFromExternalCsvFile(csvSourceJsonNode);
}
} catch (IOException e) {
throw new RuntimeException("Error deserializing csvSource", e);
}
}

private List<String> readCsvSourceFromJson(JsonNode csvSourceJsonNode) throws IOException {
ObjectMapper mapper = new ObjectMapper();
ObjectReader reader = mapper.readerFor(new TypeReference<List<String>>() {
});
return reader.readValue(csvSourceJsonNode);
}

private List<String> readCsvSourceFromExternalCsvFile(JsonNode csvSourceJsonNode) throws IOException {
String csvSourceFilePath = csvSourceJsonNode.textValue();
if (StringUtils.isNotBlank(csvSourceFilePath)) {
Path path = Paths.get("./src/test/resources/",csvSourceFilePath);
List<String> csvSourceFileLines = Files.lines(path)
.filter(StringUtils::isNotBlank)
.collect(Collectors.toList());
if (this.ignoreHeader) {
return csvSourceFileLines.stream()
.skip(1)
.collect(Collectors.toList());
}
return csvSourceFileLines;
}
return Collections.emptyList();
}

@Override
public String toString() {
return "Parameterized{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.univocity.parsers.csv.CsvParser;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.text.StrSubstitutor;
import org.jsmart.zerocode.core.domain.ScenarioSpec;
import org.slf4j.Logger;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.lang.text.StrSubstitutor;
import org.jsmart.zerocode.core.domain.ScenarioSpec;
import org.slf4j.Logger;

import static org.jsmart.zerocode.core.di.provider.CsvParserProvider.LINE_SEPARATOR;
import static org.jsmart.zerocode.core.constants.ZerocodeConstants.DSL_FORMAT;
import static org.jsmart.zerocode.core.di.provider.CsvParserProvider.LINE_SEPARATOR;
import static org.slf4j.LoggerFactory.getLogger;

/**
Expand All @@ -25,17 +27,17 @@
* <p>
* Parameters can be
* "parameterized": [
* 200,
* "Hello",
* true
* 200,
* "Hello",
* true
* ]
* <p>
* -or-
* <p>
* "parameterizedCsv": [
* "1, 2, 200",
* "11, 22, 400",
* "21, 31, 500"
* "1, 2, 200",
* "11, 22, 400",
* "21, 31, 500"
* ]
* <p>
* In each the above cases, the step will execute 3 times.
Expand Down Expand Up @@ -64,15 +66,15 @@ public ZeroCodeParameterizedProcessorImpl(ObjectMapper objectMapper, CsvParser c
@Override
public ScenarioSpec resolveParameterized(ScenarioSpec scenario, int iteration) {

if(scenario.getParameterized() == null){
if (scenario.getParameterized() == null) {

return scenario;

} else if (scenario.getParameterized().getValueSource() != null) {

return resolveParamsValues(scenario, iteration);

} else if (scenario.getParameterized().getCsvSource() != null) {
} else if (CollectionUtils.isNotEmpty(scenario.getParameterized().getCsvSource())) {

return resolveParamsCsv(scenario, iteration);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.hamcrest.CoreMatchers.hasItem;
import java.io.IOException;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

Expand Down Expand Up @@ -58,4 +60,33 @@ public void testSerDe_csvSource() throws Exception {
assertThat(parameterized.getCsvSource(), hasItem("11, 22, 400"));
}

@Test
public void shouldReadCsvSourceFromCsvFile() throws IOException {
//given
String jsonDocumentAsString =
smartUtils.getJsonDocumentAsString("unit_test_files/engine_unit_test_jsons/08.1_parameterized_csv_source_from_file.json");

//when
Parameterized parameterized = mapper.readValue(jsonDocumentAsString, Parameterized.class);

//then
assertThat(parameterized.getCsvSource(), hasItem("octocat,The Octocat,San Francisco,583231"));
assertThat(parameterized.getCsvSource(), hasItem("siddhagalaxy,Sidd,UK,33847730"));
}

@Test
public void shouldReadCsvSourceFromCsvFileIgnoringHeader() throws IOException {
//given
String jsonDocumentAsString =
smartUtils.getJsonDocumentAsString("unit_test_files/engine_unit_test_jsons/08.2_parameterized_csv_source_from_file_containing_header.json");

//when
Parameterized parameterized = mapper.readValue(jsonDocumentAsString, Parameterized.class);

//then
assertThat(parameterized.getCsvSource(), hasItem("octocat,The Octocat,San Francisco,583231"));
assertThat(parameterized.getCsvSource(), hasItem("siddhagalaxy,Sidd,UK,33847730"));
assertThat(parameterized.getCsvSource(), everyItem(not(is("user,name,city,userid"))));//assert header is ignored
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ public void willGetJsonFileIntoA_JavaString() throws Exception {
}

@Test
public void willReadAllfileNamesFrom_TestResource() throws Exception {
public void willReadAllfileNamesFrom_TestResource() {
List<String> allTestCaseFiles = SmartUtils.getAllEndPointFiles("unit_test_files/engine_unit_test_jsons");
assertThat(allTestCaseFiles.size(), is(20));
assertThat(allTestCaseFiles.size(), is(22));
assertThat(allTestCaseFiles.get(0), is("unit_test_files/engine_unit_test_jsons/00_test_json_single_step_verifications.json"));
}

@Test
public void willReadAllfileNames_AND_return_FlowSpecList() throws Exception {
public void willReadAllfileNames_AND_return_FlowSpecList() {
List<ScenarioSpec> allTestCaseFiles = smartUtils.getScenarioSpecListByPackage("unit_test_files/test_scenario_cases");

assertThat(allTestCaseFiles.size(), is(3));
Expand All @@ -91,19 +91,19 @@ public void willReadAllfileNames_AND_return_FlowSpecList() throws Exception {


@Test(expected = RuntimeException.class)
public void willReadAllfiles_find_DuplicatesScenarioNamenames_old_style() throws Exception {
public void willReadAllfiles_find_DuplicatesScenarioNamenames_old_style() {
smartUtils.checkDuplicateScenarios("unit_test_files/test_scenario_cases");
}

@Test
public void willReadAllfiles_find_DuplicateScenarioNames() throws Exception {
public void willReadAllfiles_find_DuplicateScenarioNames() {
expectedException.expect(RuntimeException.class);
expectedException.expectMessage("Oops! Can not run with multiple Scenarios with same name.");
smartUtils.checkDuplicateScenarios("unit_test_files/test_scenario_cases");
}

@Test
public void willEvaluatePlaceHolder() throws Exception {
public void willEvaluatePlaceHolder() {

String aString = "Hello_${WORLD}";
List<String> placeHolders = getTestCaseTokens(aString);
Expand All @@ -118,7 +118,7 @@ public void willEvaluatePlaceHolder() throws Exception {
}

@Test
public void testNullOrEmptyString_withPlaceHolders() throws Exception {
public void testNullOrEmptyString_withPlaceHolders() {

String aString = "";
List<String> placeHolders = getTestCaseTokens(aString);
Expand All @@ -130,7 +130,7 @@ public void testNullOrEmptyString_withPlaceHolders() throws Exception {
}

@Test
public void testReplaceTokensOrPlaceHolders() throws Exception {
public void testReplaceTokensOrPlaceHolders() {
String aString = "_${ENV_PROPERTY_NAME}";

Map<String, String> paramMap = new HashMap<>();
Expand All @@ -142,7 +142,7 @@ public void testReplaceTokensOrPlaceHolders() throws Exception {
}

@Test
public void testEnvValue() throws Exception {
public void testEnvValue() {

final String javaHomeValue = SmartUtils.getEnvPropertyValue("JAVA_HOME");
assertThat(javaHomeValue, notNullValue());
Expand Down Expand Up @@ -228,7 +228,7 @@ public void testScenarioFile_absolutePath() throws Exception {

@Ignore("Tested in local laptop. Ignored for Ci build. Follow testSuiteFolder_absolutePath() like flow ")
@Test
public void testSuiteFolder_symAbsolutePath() throws Exception {
public void testSuiteFolder_symAbsolutePath() {
String absPath = "~/dev/ZEROCODE_REPOS/zerocode/core/src/test/resources/unit_test_files/cherry_pick_tests";
List<String> allScenarios = SmartUtils.retrieveScenariosByAbsPath(absPath);
assertThat(allScenarios.size(), is(2));
Expand All @@ -242,9 +242,7 @@ private static File createCascadeIfNotExisting(String fileName) {
Path path = Paths.get(fileName);
Files.createDirectories(path.getParent());

File file = new File(fileName);

return file;
return new File(fileName);
} catch (IOException exx) {
throw new RuntimeException("Create file '" + fileName + "' Exception" + exx);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"valueSource": [
"hello",
123,
true
],
"csvSource": "unit_test_files/engine_unit_test_jsons/params.csv"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"valueSource": [
"hello",
123,
true
],
"ignoreHeader": true,
"csvSource": "unit_test_files/engine_unit_test_jsons/params_with_header.csv"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
octocat,The Octocat,San Francisco,583231
siddhagalaxy,Sidd,UK,33847730
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
user,name,city,userid
octocat,The Octocat,San Francisco,583231
siddhagalaxy,Sidd,UK,33847730
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package org.jsmart.zerocode.zerocodejavaexec.httpclient;

import org.jsmart.zerocode.core.domain.Scenario;
import org.jsmart.zerocode.core.domain.TargetEnv;
import org.jsmart.zerocode.core.domain.TestPackageRoot;
import org.jsmart.zerocode.core.runner.ZeroCodePackageRunner;
import org.jsmart.zerocode.core.runner.ZeroCodeUnitRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

@TargetEnv("hello_github_host.properties")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ public class HelloWorldParameterizedCsvTest {
public void testGetByUserNames_csv() throws Exception {
}

@Test
@Scenario("parameterized_csv/hello_world_test_parameterized_csv_source_files.json")
public void testGetByUserNames_csvSourceFiles() throws Exception {
}

@Test
@Scenario("parameterized_csv/hello_world_test_parameterized_csv_source_file_ignore_header.json")
public void testGetByUserNames_csvSourceFiles_ignoringHeader() throws Exception {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"scenarioName": "Fetch and assert GitHub userIds by their userNames",
"steps": [
{
"name": "get_user_details",
"url": "/users/${0}",
"method": "GET",
"request": {
},
"assertions": {
"status": 200,
"body": {
"login" : "${0}",
"type" : "User",
"name" : "${1}",
"location" : "${2}",
"id" : "$EQ.${3}"
}
}
}
],
"parameterized": {
"ignoreHeader": true,
"csvSource":"parameterized_csv/params_with_header.csv"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"scenarioName": "Fetch and assert GitHub userIds by their userNames",
"steps": [
{
"name": "get_user_details",
"url": "/users/${0}",
"method": "GET",
"request": {
},
"assertions": {
"status": 200,
"body": {
"login" : "${0}",
"type" : "User",
"name" : "${1}",
"location" : "${2}",
"id" : "$EQ.${3}"
}
}
}
],
"parameterized": {
"csvSource":"parameterized_csv/params.csv"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated code to read from resources folder without referring to "./test/...".
Changed method impl to include Path prefix to "./test/...".

There shohuld be a better way to do this, I have put this as a quick fix. Please feel free to amend this.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@imprashant , any news on this comment ? Or are you happy for us to merge it?

}
}
2 changes: 2 additions & 0 deletions http-testing/src/test/resources/parameterized_csv/params.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
octocat,The Octocat,San Francisco,583231
siddhagalaxy,Sidd,UK,33847730
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
user,name,city,userid
octocat,The Octocat,San Francisco,583231
siddhagalaxy,Sidd,UK,33847730