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

Parametrisation issue fixed #417

Closed
wants to merge 2 commits into from
Closed
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
@@ -1,8 +1,11 @@
package org.jsmart.zerocode.core.engine.preprocessor;

import org.jsmart.zerocode.core.domain.ScenarioSpec;
import org.jsmart.zerocode.core.domain.Step;

public interface ZeroCodeParameterizedProcessor {

ScenarioSpec resolveParameterized(ScenarioSpec scenario, int iteration);

Step resolveStepParameters(ScenarioSpec scenario, int iteration, Step step);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jsmart.zerocode.core.engine.preprocessor;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Inject;
import com.google.inject.Singleton;
Expand All @@ -11,6 +12,7 @@
import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.lang.text.StrSubstitutor;
import org.jsmart.zerocode.core.domain.ScenarioSpec;
import org.jsmart.zerocode.core.domain.Step;
import org.slf4j.Logger;

import static org.jsmart.zerocode.core.di.provider.CsvParserProvider.LINE_SEPARATOR;
Expand Down Expand Up @@ -81,6 +83,72 @@ public ScenarioSpec resolveParameterized(ScenarioSpec scenario, int iteration) {
throw new RuntimeException("Scenario spec was invalid. Please check the DSL format \ne.g. \n" + DSL_FORMAT);
}

@Override
public Step resolveStepParameters(ScenarioSpec scenario, int iteration, Step step) {
if(scenario.getParameterized() == null){

return step;

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

return resolveStepParamsValues(scenario, iteration , step);

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

return resolveStepParamsCsv(scenario, iteration, step);

}
return null;
}

private Step resolveStepParamsCsv(ScenarioSpec scenario, int index,
Step step) {
LOGGER.info("Resolving parameter CSV-source for row number - {}", index);
try {
String stepJson = objectMapper.writeValueAsString(step);
List<String> parameterizedCsvList = scenario.getParameterized().getCsvSource();

if (parameterizedCsvList == null || parameterizedCsvList.isEmpty()) {
return step;
}

Map<String, Object> valuesMap = new HashMap<>();
String csvLine = parameterizedCsvList.get(index);

resolveCsvLine(valuesMap, csvLine);

String resultantStepJson = replaceWithValues(stepJson, valuesMap);

return objectMapper.readValue(resultantStepJson, Step.class);

} catch (JsonProcessingException ex) {
throw new RuntimeException("Error while resolving parameterizedCsv values for step - " + ex);
}
}

private Step resolveStepParamsValues(ScenarioSpec scenario, int index,
Step step) {
LOGGER.info("Resolving parameter CSV-source for row number - {}", index);
try {
String stepJson = objectMapper.writeValueAsString(step);
List<Object> parametersList = scenario.getParameterized().getValueSource();

if (parametersList == null || parametersList.isEmpty()) {
return step;
}

Map<String, Object> valuesMap = new HashMap<>();
valuesMap.put(VALUE_SOURCE_KEY, parametersList.get(index));

String resultantStepJson = replaceWithValues(stepJson, valuesMap);

return objectMapper.readValue(resultantStepJson, Step.class);

} catch (JsonProcessingException ex) {
throw new RuntimeException("Error while resolving parameterized values for step- " + ex);
}
}

private ScenarioSpec resolveParamsValues(ScenarioSpec scenario, int paramIndex) {
LOGGER.info("Resolving parameter value-source for index - {}", paramIndex);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public synchronized boolean runScenario(ScenarioSpec scenario, RunNotifier notif
.loop(scnCount)
.scenarioName(parameterizedScenario.getScenarioName());

wasExecSuccessful = executeSteps(notifier, description, scenarioExecutionState, parameterizedScenario);
wasExecSuccessful = executeSteps(notifier, description, scenarioExecutionState, parameterizedScenario, scnCount);

ioWriterBuilder.result(resultReportBuilder.build());
}
Expand All @@ -145,9 +145,9 @@ public synchronized boolean runScenario(ScenarioSpec scenario, RunNotifier notif
}

private boolean executeSteps(RunNotifier notifier,
Description description,
ScenarioExecutionState scenarioExecutionState,
ScenarioSpec parameterizedScenario) {
Description description,
ScenarioExecutionState scenarioExecutionState,
ScenarioSpec parameterizedScenario, int scnCount) {

ScenarioSpec scenario = parameterizedScenario;

Expand All @@ -159,27 +159,29 @@ private boolean executeSteps(RunNotifier notifier,

correlLogger = ZerocodeCorrelationshipLogger.newInstance(LOGGER);

Boolean wasExecSuccess = executeRetryWithSteps(notifier, description, scenarioExecutionState, scenario, thisStep);
Boolean wasExecSuccess = executeRetryWithSteps(notifier, description, scenarioExecutionState, scenario, thisStep, scnCount);
if (wasExecSuccess != null) return wasExecSuccess;
}

return false;
}

private Boolean executeRetryWithSteps(RunNotifier notifier,
Description description,
ScenarioExecutionState scenarioExecutionState,
ScenarioSpec scenario, Step thisStep) {
Description description,
ScenarioExecutionState scenarioExecutionState,
ScenarioSpec scenario, Step thisStep, int scnCount) {

thisStep = extFileProcessor.resolveExtJsonFile(thisStep);
List<Step> thisSteps = extFileProcessor.createFromStepFile(thisStep, thisStep.getId());
if(null == thisSteps || thisSteps.isEmpty()) thisSteps.add(thisStep);
Boolean wasExecSuccess = null;
for(Step step : thisSteps) {
Step parametrizedStep = this.parameterizedProcessor.resolveStepParameters(scenario, scnCount, step);
wasExecSuccess = executeRetry(notifier,
description,
scenarioExecutionState,
scenario,
step);
parametrizedStep);
if (wasExecSuccess != null) {
return wasExecSuccess;
}
Expand Down