use of com.trivago.rta.exceptions.CucablePluginException in project cucable-plugin by trivago.
the class GherkinDocumentParser method getGherkinDocumentFromFeatureFileContent.
/**
* Get a {@link GherkinDocument} from a feature file for further processing.
*
* @param featureContent a feature string.
* @return a {@link GherkinDocument}.
* @throws CucablePluginException see {@link CucablePluginException}.
*/
private GherkinDocument getGherkinDocumentFromFeatureFileContent(final String featureContent) throws CucablePluginException {
Parser<GherkinDocument> gherkinDocumentParser = new Parser<>(new AstBuilder());
GherkinDocument gherkinDocument;
try {
gherkinDocument = gherkinDocumentParser.parse(featureContent);
} catch (ParserException parserException) {
throw new CucablePluginException("Could not parse feature!");
}
if (gherkinDocument == null || gherkinDocument.getFeature() == null) {
throw new CucablePluginException("Could not parse feature!");
}
return gherkinDocument;
}
use of com.trivago.rta.exceptions.CucablePluginException in project cucable-plugin by trivago.
the class FeatureFileConverter method convertToSingleScenariosAndRunners.
/**
* Converts all scenarios in the given feature file to single
* scenario feature files and their respective runners.
*
* @param featureFilePath feature file to process.
* @return Number of created scenarios.
* @throws CucablePluginException see {@link CucablePluginException}
*/
private int convertToSingleScenariosAndRunners(final Path featureFilePath) throws CucablePluginException {
String featureFilePathString = featureFilePath.toString();
if (featureFilePathString == null || featureFilePathString.equals("")) {
throw new MissingFileException(featureFilePathString);
}
List<Integer> lineNumbers = propertyManager.getScenarioLineNumbers();
List<String> includeScenarioTags = propertyManager.getIncludeScenarioTags();
List<String> excludeScenarioTags = propertyManager.getExcludeScenarioTags();
String featureFileContent = fileIO.readContentFromFile(featureFilePathString);
List<SingleScenario> singleScenarios;
try {
singleScenarios = gherkinDocumentParser.getSingleScenariosFromFeature(featureFileContent, featureFilePathString, lineNumbers, includeScenarioTags, excludeScenarioTags);
} catch (CucablePluginException e) {
throw new FeatureFileParseException(featureFilePathString);
}
// that means that the provided line number is wrong.
if (propertyManager.hasValidScenarioLineNumbers() && singleScenarios.size() == 0) {
throw new CucablePluginException("There is no parseable scenario or scenario outline at line " + lineNumbers);
}
for (SingleScenario singleScenario : singleScenarios) {
String renderedFeatureFileContent = featureFileContentRenderer.getRenderedFeatureFileContent(singleScenario);
String featureFileName = getFeatureFileNameFromPath(featureFilePath);
Integer featureCounter = singleFeatureCounters.getOrDefault(featureFileName, 0);
featureCounter++;
String scenarioCounterFilenamePart = String.format(SCENARIO_COUNTER_FORMAT, featureCounter);
for (int testRuns = 1; testRuns <= propertyManager.getNumberOfTestRuns(); testRuns++) {
String testRunsCounterFilenamePart = String.format(TEST_RUNS_COUNTER_FORMAT, testRuns);
// Append the scenario and test run counters to the filename.
// Also add the "_IT" postfix so Maven Failsafe considers it an integration test automatically.
String generatedFileName = featureFileName.concat(scenarioCounterFilenamePart).concat(testRunsCounterFilenamePart).concat(INTEGRATION_TEST_POSTFIX);
String generatedFeatureFilePath = propertyManager.getGeneratedFeatureDirectory().concat(PATH_SEPARATOR).concat(generatedFileName).concat(FEATURE_FILE_EXTENSION);
singleFeatureCounters.put(featureFileName, featureCounter);
// Save scenario information to new feature file
fileIO.writeContentToFile(renderedFeatureFileContent, generatedFeatureFilePath);
// Generate runner for the newly generated single scenario feature file
SingleScenarioRunner singleScenarioRunner = new SingleScenarioRunner(propertyManager.getSourceRunnerTemplateFile(), generatedFileName);
String renderedRunnerFileContent = runnerFileContentRenderer.getRenderedRunnerFileContent(singleScenarioRunner, singleScenario);
String generatedRunnerFilePath = propertyManager.getGeneratedRunnerDirectory().concat(PATH_SEPARATOR).concat(generatedFileName).concat(RUNNER_FILE_EXTENSION);
fileIO.writeContentToFile(renderedRunnerFileContent, generatedRunnerFilePath);
}
}
int createdScenarios = singleScenarios.size();
logProcessCompleteMessage(featureFilePathString, createdScenarios);
return createdScenarios;
}
use of com.trivago.rta.exceptions.CucablePluginException in project cucable-plugin by trivago.
the class FileSystemManager method getFeatureFilePaths.
/**
* Returns a list of feature file paths located in the specified source feature directory.
*
* @return a list of feature file paths.
* @throws CucablePluginException see {@link CucablePluginException}
*/
public List<Path> getFeatureFilePaths() throws CucablePluginException {
List<Path> featureFilePaths = new ArrayList<>();
String sourceFeatures = propertyManager.getSourceFeatures();
File sourceFeaturesFile = new File(sourceFeatures);
// Check if the property value is a single file or a directory
if (sourceFeaturesFile.isFile() && sourceFeatures.endsWith(FEATURE_SUFFIX)) {
featureFilePaths.add(Paths.get(sourceFeatures));
} else if (sourceFeaturesFile.isDirectory()) {
try {
featureFilePaths = Files.walk(Paths.get(sourceFeatures)).filter(Files::isRegularFile).filter(p -> p.toString().endsWith(FEATURE_SUFFIX)).collect(Collectors.toList());
} catch (IOException e) {
throw new CucablePluginException("Unable to traverse feature files in " + sourceFeatures);
}
} else {
throw new CucablePluginException(sourceFeatures + " is not a feature file or a directory.");
}
return featureFilePaths;
}
use of com.trivago.rta.exceptions.CucablePluginException in project cucable-plugin by trivago.
the class GherkinDocumentParser method getSingleScenariosFromOutline.
/**
* Returns a list of Cucable single scenarios from a Gherkin scenario outline.
*
* @param scenarioOutline A Gherkin {@link ScenarioOutline}.
* @param featureName The name of the feature this scenario outline belongs to.
* @param featureFilePath The source path of the feature file.
* @param featureLanguage The feature language this scenario outline belongs to.
* @param featureTags The feature tags of the parent feature.
* @param backgroundSteps Return a Cucable {@link SingleScenario} list.
* @param includeScenarioTags Optional scenario tags to include in scenario generation.
* @throws CucablePluginException Thrown when the scenario outline does not contain an example table.
*/
private List<SingleScenario> getSingleScenariosFromOutline(final ScenarioOutline scenarioOutline, final String featureName, final String featureFilePath, final String featureLanguage, final String featureDescription, final List<String> featureTags, final List<Step> backgroundSteps, final List<String> includeScenarioTags, final List<String> excludeScenarioTags) throws CucablePluginException {
// Retrieve the translation of "Scenario" in the target language and add it to the scenario
String translatedScenarioKeyword = gherkinTranslations.getScenarioKeyword(featureLanguage);
String scenarioName = translatedScenarioKeyword + ": " + scenarioOutline.getName();
String scenarioDescription = scenarioOutline.getDescription();
List<String> scenarioTags = gherkinToCucableConverter.convertGherkinTagsToCucableTags(scenarioOutline.getTags());
if (!scenarioShouldBeIncluded(featureTags, scenarioTags, includeScenarioTags, excludeScenarioTags)) {
return Collections.emptyList();
}
List<SingleScenario> outlineScenarios = new ArrayList<>();
List<Step> steps = gherkinToCucableConverter.convertGherkinStepsToCucableSteps(scenarioOutline.getSteps());
if (scenarioOutline.getExamples().isEmpty()) {
throw new CucablePluginException("Scenario outline without examples table!");
}
Examples exampleTable = scenarioOutline.getExamples().get(0);
Map<String, List<String>> exampleMap = gherkinToCucableConverter.convertGherkinExampleTableToCucableExampleMap(exampleTable);
String firstColumnHeader = (String) exampleMap.keySet().toArray()[0];
int rowCount = exampleMap.get(firstColumnHeader).size();
// For each example row, create a new single scenario
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
SingleScenario singleScenario = new SingleScenario(featureName, featureFilePath, featureLanguage, featureDescription, substituteScenarioNameExamplePlaceholders(scenarioName, exampleMap, rowIndex), scenarioDescription, featureTags, backgroundSteps);
List<Step> substitutedSteps = substituteStepExamplePlaceholders(steps, exampleMap, rowIndex);
singleScenario.setSteps(substitutedSteps);
singleScenario.setScenarioTags(scenarioTags);
outlineScenarios.add(singleScenario);
}
return outlineScenarios;
}
Aggregations