io.aubay.fase.maven.plugin.ExamplesReplacer Maven / Gradle / Ivy
package io.aubay.fase.maven.plugin;
/*-
* #%L
* fase-maven-plugin Maven Plugin
* %%
* Copyright (C) 2018 - 2019 Aubay
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import gherkin.ast.Examples;
import gherkin.ast.Feature;
import gherkin.ast.ScenarioOutline;
import gherkin.events.CucumberEvent;
import gherkin.events.SourceEvent;
import io.aubay.fase.maven.plugin.util.CucumberUtils;
import io.aubay.fase.maven.plugin.util.GherkinUtils;
/**
*
* Classe permettant d'effectuer le remplacement d'un Examples Gherkin
* contenant des references a FASe en une Datatable contenant les donnees provenant
* d'un transformateur
*
* Exemple :
*
* | faseId | prefix | dataFile | dataFileFormat | dataFileTransformer | dataFileTransformerParams |
* | testExamples | examples | src/test/resources/testing-good-file.csv | CSV | CSV | , |
*
*
* devient
*
* | examples.id | examples.numeroDossier | examples.description |
* | 1 | 12345 | Description de mon dossier |
*
*
* avec un fichier src/test/resources/testing-good-file.csv contenant :
*
* id,numeroDossier,description
* 1,12345,Description de mon dossier
*
* @author elegeleux
*
*/
public class ExamplesReplacer {
private static final Logger LOG = LogManager.getLogger();
private String[] featuresPaths;
private File outputDirectory;
private String featureExtension;
/**
* Cette methode :
* - filtre les featuresPaths par rapport à la propriete featureExtension.
* Si featureExtension==null alors l'extension par défaut est prise, à savoir CucumberUtils.FEATURE_FASE_EXTENSION
* - verifie la presence de featuresPaths contenant des features a traiter
* - execute le remplacement de l'Examples de type FASe par une Datatable avec les donnees provenant de la transformation
* - ecrit les features resultante dans le outputDirectory
*
*/
public void executeReplacement() {
// Filter Feature files
List paths = Arrays.stream(featuresPaths).filter(fp -> fp.endsWith("." + getFeatureExtension()))
.collect(Collectors.toList());
// Check if there is features
if (paths.isEmpty()) {
String featuresPathsTemp = String.join(",", this.featuresPaths);
LOG.error("featuresPaths={} DOES NOT CONTAINS feature with extension={}", featuresPathsTemp, this.getFeatureExtension());
throw new IllegalArgumentException("featuresPaths DOES NOT CONTAINS ANY feature");
}
// Execute replacement for each path
LOG.info("Starting replacement for {} elements", paths.size());
paths.stream().forEach(this::executeReplacementOnePath);
}
/**
* Cette methode execute le remplacement pour un unique fichier definit par le parametre "path"
* Renvoie une IllegalArgumentException si le path==null
*
* @param path : chemin de la feature a transformer
*/
public void executeReplacementOnePath(String path) {
if (path == null) {
throw new IllegalArgumentException("path parameter CANNOT BE NULL");
}
List paths = new ArrayList<>();
paths.add(path);
SourceEvent sourceEvent = GherkinUtils.getSourceEvent(path);
LOG.info("sourceEvent={}", sourceEvent);
LOG.info("sourceEvent.data={}", sourceEvent.data);
// Loading CucumberEvents
List cucumberEvents = GherkinUtils.getCucumberEvents(paths, true, true, false);
LOG.info("cucumberEvents.size()={}", cucumberEvents.size());
// Retrieve Examples by Feature
Map>> examplesByFeatureAndScenarioOutline = GherkinUtils
.getExamplesByFeatureAndScenarioOutline(cucumberEvents);
LOG.info("examplesByFeatureAndScenarioOutline.size()={}", examplesByFeatureAndScenarioOutline.size());
// Transform Examples to DataTable
examplesByFeatureAndScenarioOutline.entrySet().stream().forEach(featureEntrySet -> {
Feature feature = featureEntrySet.getKey();
LOG.info("feature={}", feature);
Map> examplesByScenarioOutline = featureEntrySet.getValue();
LOG.info("examplesByScenarioOutline={}", examplesByScenarioOutline);
examplesByScenarioOutline.entrySet().stream().forEach(scenarioOutlineEntrySet -> {
ScenarioOutline scenarioOutline = scenarioOutlineEntrySet.getKey();
LOG.info("scenarioOutline={}", scenarioOutline);
List examples = scenarioOutlineEntrySet.getValue();
LOG.info("examples={}", examples);
final SourceData sourceData = new SourceData(sourceEvent);
examples.stream().forEach(sourceData::loadExamples);
sourceData.saveNewData(outputDirectory);
});
});
}
/**
* Affecte les featuresPaths a traiter
*
* @param featuresPaths : featuresPaths a analyser pour le remplacement
*/
public void setFeaturesPaths(String[] featuresPaths) {
Arrays.stream(featuresPaths).forEach(LOG::info);
this.featuresPaths = featuresPaths;
}
/**
* Affecte le repertoire de sortie des remplacements
*
* @param outputDirectory : chemin de sortie des fichiers modifies
*/
public void setOutputDirectory(File outputDirectory) {
this.outputDirectory = outputDirectory;
}
/**
* Affecte l'extension a rechercher pour le remplacement
*
* @param featureExtension : extension a rechercher pour le remplacement
*/
public void setFeatureExtension(String featureExtension) {
this.featureExtension = featureExtension;
}
/**
* Retourne l'extension a rechercher pour le remplacement
* Renvoie CucumberUtils.FEATURE_FASE_EXTENSION si featureExtension==null
*
* @return : l'extension a rechercher pour le remplacement
*/
public String getFeatureExtension() {
if (this.featureExtension == null) {
return CucumberUtils.FEATURE_FASE_EXTENSION;
}
return this.featureExtension;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy