Search in sources :

Example 1 with CucablePluginException

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;
}
Also used : AstBuilder(gherkin.AstBuilder) ParserException(gherkin.ParserException) CucablePluginException(com.trivago.rta.exceptions.CucablePluginException) GherkinDocument(gherkin.ast.GherkinDocument) Parser(gherkin.Parser)

Example 2 with CucablePluginException

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;
}
Also used : SingleScenarioRunner(com.trivago.rta.vo.SingleScenarioRunner) SingleScenario(com.trivago.rta.vo.SingleScenario) FeatureFileParseException(com.trivago.rta.exceptions.filesystem.FeatureFileParseException) CucablePluginException(com.trivago.rta.exceptions.CucablePluginException) MissingFileException(com.trivago.rta.exceptions.filesystem.MissingFileException)

Example 3 with CucablePluginException

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;
}
Also used : Path(java.nio.file.Path) Files(java.nio.file.Files) IOException(java.io.IOException) PathCreationException(com.trivago.rta.exceptions.filesystem.PathCreationException) Singleton(javax.inject.Singleton) Collectors(java.util.stream.Collectors) File(java.io.File) ArrayList(java.util.ArrayList) CucablePluginException(com.trivago.rta.exceptions.CucablePluginException) Inject(javax.inject.Inject) FileDeletionException(com.trivago.rta.exceptions.filesystem.FileDeletionException) List(java.util.List) Paths(java.nio.file.Paths) PropertyManager(com.trivago.rta.properties.PropertyManager) Path(java.nio.file.Path) CucablePluginException(com.trivago.rta.exceptions.CucablePluginException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Files(java.nio.file.Files) File(java.io.File)

Example 4 with CucablePluginException

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;
}
Also used : SingleScenario(com.trivago.rta.vo.SingleScenario) CucablePluginException(com.trivago.rta.exceptions.CucablePluginException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Step(com.trivago.rta.vo.Step) Examples(gherkin.ast.Examples)

Aggregations

CucablePluginException (com.trivago.rta.exceptions.CucablePluginException)4 SingleScenario (com.trivago.rta.vo.SingleScenario)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 FeatureFileParseException (com.trivago.rta.exceptions.filesystem.FeatureFileParseException)1 FileDeletionException (com.trivago.rta.exceptions.filesystem.FileDeletionException)1 MissingFileException (com.trivago.rta.exceptions.filesystem.MissingFileException)1 PathCreationException (com.trivago.rta.exceptions.filesystem.PathCreationException)1 PropertyManager (com.trivago.rta.properties.PropertyManager)1 SingleScenarioRunner (com.trivago.rta.vo.SingleScenarioRunner)1 Step (com.trivago.rta.vo.Step)1 AstBuilder (gherkin.AstBuilder)1 Parser (gherkin.Parser)1 ParserException (gherkin.ParserException)1 Examples (gherkin.ast.Examples)1 GherkinDocument (gherkin.ast.GherkinDocument)1 File (java.io.File)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1