diff --git a/.travis.yml b/.travis.yml index f22fc326..11ff4ba1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,9 @@ -language: java -sudo: false # faster builds +language: + java -before_install: - - pip install --user codecov - -script: +before_script: - cd plugin-code - - mvn cobertura:cobertura + - pip install --user codecov after_success: - - bash <(curl -s https://codecov.io/bash) - - codecov - + - codecov \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index f6d64365..b605cd73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,10 +7,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. Back to [Readme](README.md). -## [2.0.0] - UNRELEASED +## [2.0.0] - 2019-06-14 + +### Added +* Ability to set custom parameters via a properties file through the `customParametersFile` property (#167, contributed by gazler22) + +### Fixed +* Custom parameters are now displayed in the order of definition (#157) +* Better logging and error handling ### Changed -* Major internal architecture change to ease extension +* Major internal architecture change to ease future extension ## [1.11.0] - 2019-05-21 diff --git a/README.md b/README.md index e1c489c9..64dc0de6 100644 --- a/README.md +++ b/README.md @@ -146,17 +146,6 @@ This points to the root directory of the generated Cluecumber HTML report. ## Optional Configuration Parameters -### skip - -The `skip` property is used to skip the report generation. The default value is `false` - -```xml - - true - ... - -``` - ### customParameters The `customParameters` block can be used to define custom information that should be displayed on the report start page. @@ -178,6 +167,42 @@ The property definitions above are shown in the report like this: ![custom parameters](documentation/img/custom_params.png) +### customParametersFile + +You can also set custom parameters by specifying the path to a `.properties` file in the `customParametersFile` property like this: + +```xml + + path/to/your/customParameters.properties + ... + +``` + +This file needs to have a format like this: + +```properties +Custom_Parameter=This is a test +Custom_URL=http://www.google.com +``` + +__Note:__ These custom parameters behave exactly like the ones defined by the `customParameters` property and will be added on top of already defined properties. +If a property has the same name as an existing one, its value will be overwritten! + +The property definitions above are shown in the report like this: + +![custom parameters](documentation/img/custom_params.png) + +### skip + +The `skip` property is used to skip the report generation. The default value is `false` + +```xml + + true + ... + +``` + ### failScenariosOnPendingOrUndefinedSteps This optional property can be set to `true` if you scenarios should be marked as `failed` when they contain `pending` or `skipped` steps. diff --git a/example-project/custom/custom.properties b/example-project/custom/custom.properties new file mode 100644 index 00000000..e69de29b diff --git a/example-project/pom.xml b/example-project/pom.xml index a3ab642f..1004b5f7 100644 --- a/example-project/pom.xml +++ b/example-project/pom.xml @@ -6,7 +6,7 @@ de.benjamin-bischoff cluecumber-test-project - 1.11.0 + 2.0.0 UTF-8 @@ -42,6 +42,9 @@ + + custom/custom.properties + diff --git a/plugin-code/pom.xml b/plugin-code/pom.xml index b4ffacbe..23204bd2 100644 --- a/plugin-code/pom.xml +++ b/plugin-code/pom.xml @@ -134,20 +134,28 @@ ${maven.compiler.source} ${maven.compiler.target} - UTF-8 + ${project.build.sourceEncoding} + - org.codehaus.mojo - cobertura-maven-plugin - ${cobertura.maven.plugin} - - - html - xml - - - + org.jacoco + jacoco-maven-plugin + 0.8.4 + + + + prepare-agent + + + + report + test + + report + + + org.apache.maven.plugins @@ -156,6 +164,7 @@ cluecumber-report false + ${project.build.sourceEncoding} diff --git a/plugin-code/src/main/java/com/trivago/cluecumber/CluecumberReportPlugin.java b/plugin-code/src/main/java/com/trivago/cluecumber/CluecumberReportPlugin.java index 65ff319d..b6a41db3 100644 --- a/plugin-code/src/main/java/com/trivago/cluecumber/CluecumberReportPlugin.java +++ b/plugin-code/src/main/java/com/trivago/cluecumber/CluecumberReportPlugin.java @@ -68,6 +68,15 @@ public final class CluecumberReportPlugin extends AbstractMojo { @Parameter(property = "reporting.customParameters") private LinkedHashMap customParameters = new LinkedHashMap<>(); + /** + * Path to a properties file. The included properties are converted to custom parameters. + *
+     *
+     * 
+ */ + @Parameter(property = "reporting.customParametersFile") + private String customParametersFile = ""; + /** * Mark scenarios as failed if they contain pending or undefined steps (default: false). */ @@ -141,12 +150,12 @@ public void execute() throws CluecumberPluginException { propertyManager.setSourceJsonReportDirectory(sourceJsonReportDirectory); propertyManager.setGeneratedHtmlReportDirectory(generatedHtmlReportDirectory); propertyManager.setCustomParameters(customParameters); + propertyManager.setCustomParametersFile(customParametersFile); propertyManager.setFailScenariosOnPendingOrUndefinedSteps(failScenariosOnPendingOrUndefinedSteps); propertyManager.setExpandBeforeAfterHooks(expandBeforeAfterHooks); propertyManager.setExpandStepHooks(expandStepHooks); propertyManager.setExpandDocStrings(expandDocStrings); - propertyManager.setCustomCss(customCss); - propertyManager.validateSettings(); + propertyManager.setCustomCssFile(customCss); logger.logSeparator(); logger.info(String.format(" Cluecumber Report Maven Plugin, version %s", getClass().getPackage().getImplementationVersion())); diff --git a/plugin-code/src/main/java/com/trivago/cluecumber/filesystem/FileIO.java b/plugin-code/src/main/java/com/trivago/cluecumber/filesystem/FileIO.java index 66b77253..a31d9403 100644 --- a/plugin-code/src/main/java/com/trivago/cluecumber/filesystem/FileIO.java +++ b/plugin-code/src/main/java/com/trivago/cluecumber/filesystem/FileIO.java @@ -20,6 +20,7 @@ import com.trivago.cluecumber.exceptions.filesystem.MissingFileException; import javax.inject.Singleton; +import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.nio.file.Files; @@ -79,4 +80,15 @@ public String readContentFromFile(final String filePath) throws MissingFileExcep throw new MissingFileException(filePath); } } + + /** + * Check if a file exists. + * + * @param filePath the complete path to the file. + * @return true if the file exists. + */ + public boolean isExistingFile(final String filePath) { + File file = new File(filePath); + return file.exists() && file.isFile(); + } } diff --git a/plugin-code/src/main/java/com/trivago/cluecumber/properties/LinkedProperties.java b/plugin-code/src/main/java/com/trivago/cluecumber/properties/LinkedProperties.java new file mode 100644 index 00000000..18879e38 --- /dev/null +++ b/plugin-code/src/main/java/com/trivago/cluecumber/properties/LinkedProperties.java @@ -0,0 +1,51 @@ +package com.trivago.cluecumber.properties; + +import java.util.Enumeration; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +public class LinkedProperties extends Properties { + + private static final long serialVersionUID = 1L; + + private Map linkMap = new LinkedHashMap<>(); + + @Override + public synchronized Object put(Object key, Object value) { + return linkMap.put(key, value); + } + + @Override + public synchronized boolean contains(Object value) { + return linkMap.containsValue(value); + } + + @Override + public boolean containsValue(Object value) { + return linkMap.containsValue(value); + } + + @Override + public synchronized Enumeration elements() { + throw new UnsupportedOperationException( + "Please use keySet() or entrySet() instead of Enumeration."); + } + + @Override + public Set> entrySet() { + return linkMap.entrySet(); + } + + @Override + public synchronized void clear() { + linkMap.clear(); + } + + @Override + public synchronized boolean containsKey(Object key) { + return linkMap.containsKey(key); + } + +} diff --git a/plugin-code/src/main/java/com/trivago/cluecumber/properties/PropertiesFileLoader.java b/plugin-code/src/main/java/com/trivago/cluecumber/properties/PropertiesFileLoader.java new file mode 100644 index 00000000..3726d6b5 --- /dev/null +++ b/plugin-code/src/main/java/com/trivago/cluecumber/properties/PropertiesFileLoader.java @@ -0,0 +1,37 @@ +package com.trivago.cluecumber.properties; + +import com.trivago.cluecumber.exceptions.CluecumberPluginException; +import com.trivago.cluecumber.filesystem.FileIO; + +import javax.inject.Inject; +import javax.inject.Singleton; +import java.io.IOException; +import java.io.StringReader; +import java.util.LinkedHashMap; +import java.util.Map; + +@Singleton +public class PropertiesFileLoader { + + private FileIO fileIO; + + @Inject + public PropertiesFileLoader(final FileIO fileIO) { + this.fileIO = fileIO; + } + + LinkedHashMap loadPropertiesMap(final String propertiesFilePath) throws CluecumberPluginException { + LinkedHashMap propertiesMap = new LinkedHashMap<>(); + String content = fileIO.readContentFromFile(propertiesFilePath); + LinkedProperties properties = new LinkedProperties(); + try { + properties.load(new StringReader(content)); + } catch (IOException e) { + throw new CluecumberPluginException("Could not parse properties file '" + "': " + e.getMessage()); + } + for (Map.Entry propertyEntry : properties.entrySet()) { + propertiesMap.put((String) propertyEntry.getKey(), (String) propertyEntry.getValue()); + } + return propertiesMap; + } +} diff --git a/plugin-code/src/main/java/com/trivago/cluecumber/properties/PropertyManager.java b/plugin-code/src/main/java/com/trivago/cluecumber/properties/PropertyManager.java index 61e78be8..3a7a213a 100644 --- a/plugin-code/src/main/java/com/trivago/cluecumber/properties/PropertyManager.java +++ b/plugin-code/src/main/java/com/trivago/cluecumber/properties/PropertyManager.java @@ -17,45 +17,65 @@ package com.trivago.cluecumber.properties; import com.trivago.cluecumber.exceptions.CluecumberPluginException; +import com.trivago.cluecumber.exceptions.filesystem.MissingFileException; import com.trivago.cluecumber.exceptions.properties.WrongOrMissingPropertyException; +import com.trivago.cluecumber.filesystem.FileIO; import com.trivago.cluecumber.logging.CluecumberLogger; import javax.inject.Inject; import javax.inject.Singleton; +import java.util.LinkedHashMap; import java.util.Map; @Singleton public class PropertyManager { private final CluecumberLogger logger; + private FileIO fileIO; + private PropertiesFileLoader propertiesFileLoader; private String sourceJsonReportDirectory; private String generatedHtmlReportDirectory; - private Map customParameters; + private Map customParameters = new LinkedHashMap<>(); private boolean failScenariosOnPendingOrUndefinedSteps; private boolean expandBeforeAfterHooks; private boolean expandStepHooks; private boolean expandDocStrings; - private String customCss; + private String customCssFile; + private String customParametersFile; @Inject - public PropertyManager(final CluecumberLogger logger) { + public PropertyManager( + final CluecumberLogger logger, + FileIO fileIO, + final PropertiesFileLoader propertiesFileLoader + ) { this.logger = logger; + this.fileIO = fileIO; + this.propertiesFileLoader = propertiesFileLoader; } public String getSourceJsonReportDirectory() { return sourceJsonReportDirectory; } - public void setSourceJsonReportDirectory(final String reportDirectory) { - this.sourceJsonReportDirectory = reportDirectory; + public void setSourceJsonReportDirectory(final String sourceJsonReportDirectory) + throws WrongOrMissingPropertyException { + + if (!isSet(sourceJsonReportDirectory)) { + throw new WrongOrMissingPropertyException("sourceJsonReportDirectory"); + } + this.sourceJsonReportDirectory = sourceJsonReportDirectory; } public String getGeneratedHtmlReportDirectory() { return generatedHtmlReportDirectory; } - public void setGeneratedHtmlReportDirectory(final String generatedHtmlReportDirectory) { + public void setGeneratedHtmlReportDirectory(final String generatedHtmlReportDirectory) throws WrongOrMissingPropertyException { + if (!isSet(generatedHtmlReportDirectory)) { + throw new WrongOrMissingPropertyException("generatedHtmlReportDirectory"); + } this.generatedHtmlReportDirectory = generatedHtmlReportDirectory; } @@ -64,7 +84,20 @@ public Map getCustomParameters() { } public void setCustomParameters(final Map customParameters) { - this.customParameters = customParameters; + this.customParameters.putAll(customParameters); + } + + String getCustomParametersFile() { + return customParametersFile; + } + + public void setCustomParametersFile(String customParametersFile) throws CluecumberPluginException { + if (isSet(customParametersFile) && !fileIO.isExistingFile(customParametersFile)) { + throw new MissingFileException(customParametersFile); + } + this.customParametersFile = customParametersFile; + Map customParameters = propertiesFileLoader.loadPropertiesMap(customParametersFile); + this.customParameters.putAll(customParameters); } public boolean isFailScenariosOnPendingOrUndefinedSteps() { @@ -99,39 +132,31 @@ public void setExpandDocStrings(final boolean expandDocStrings) { this.expandDocStrings = expandDocStrings; } - public String getCustomCss() { - return customCss; - } - - public void setCustomCss(final String customCss) { - this.customCss = customCss; + public String getCustomCssFile() { + return customCssFile; } - /** - * Checks the pom settings for the plugin. - * - * @throws CluecumberPluginException Thrown when a required setting - * is not specified in the pom. - */ - public void validateSettings() throws CluecumberPluginException { - String missingProperty = null; - if (sourceJsonReportDirectory == null || sourceJsonReportDirectory.equals("")) { - missingProperty = "sourceJsonReportDirectory"; - } else if (generatedHtmlReportDirectory == null || generatedHtmlReportDirectory.equals("")) { - missingProperty = "generatedHtmlReportDirectory"; - } - - if (missingProperty != null) { - throw new WrongOrMissingPropertyException(missingProperty); + public void setCustomCssFile(final String customCssFile) throws MissingFileException { + if (isSet(customCssFile) && !fileIO.isExistingFile(customCssFile)) { + throw new MissingFileException(customCssFile); } + this.customCssFile = customCssFile; } public void logProperties() { logger.info("- source JSON report directory : " + sourceJsonReportDirectory); logger.info("- generated HTML report directory : " + generatedHtmlReportDirectory); - if (customParameters != null && !customParameters.isEmpty()) { + boolean customParametersFileExists = customParametersFile != null && !customParametersFile.isEmpty(); + if (customParametersFileExists) { logger.logSeparator(); + logger.info("- custom parameters file : " + customParametersFile); + } + + if (customParameters != null && !customParameters.isEmpty()) { + if (!customParametersFileExists) { + logger.logSeparator(); + } for (Map.Entry entry : customParameters.entrySet()) { logger.info("- custom parameter : " + entry.getKey() + " -> " + entry.getValue()); @@ -145,10 +170,14 @@ public void logProperties() { logger.info("- expand step hooks : " + expandStepHooks); logger.info("- expand doc strings : " + expandDocStrings); - if (customCss != null && !customCss.isEmpty()) { - logger.info("- custom CSS : " + customCss); + if (customCssFile != null && !customCssFile.isEmpty()) { + logger.info("- custom CSS file : " + customCssFile); } logger.logSeparator(); } + + private boolean isSet(final String string) { + return string != null && !string.trim().isEmpty(); + } } diff --git a/plugin-code/src/main/java/com/trivago/cluecumber/rendering/ReportGenerator.java b/plugin-code/src/main/java/com/trivago/cluecumber/rendering/ReportGenerator.java index 19efe2b2..f2bfeaa3 100644 --- a/plugin-code/src/main/java/com/trivago/cluecumber/rendering/ReportGenerator.java +++ b/plugin-code/src/main/java/com/trivago/cluecumber/rendering/ReportGenerator.java @@ -94,7 +94,7 @@ private void copyReportAssets() throws CluecumberPluginException { copyFileFromJarToReportDirectory("/css/dataTables.bootstrap4.min.css"); // Either use the custom CSS or the dummy CSS files that comes with Cluecumber - String customCss = propertyManager.getCustomCss(); + String customCss = propertyManager.getCustomCssFile(); if (customCss != null && !customCss.isEmpty()) { fileSystemManager.copyResource(customCss, reportDirectory + "/css/cluecumber_custom.css"); } else { diff --git a/plugin-code/src/test/java/com/trivago/cluecumber/files/FileIOTest.java b/plugin-code/src/test/java/com/trivago/cluecumber/files/FileIOTest.java index 87e1611e..19a3b197 100644 --- a/plugin-code/src/test/java/com/trivago/cluecumber/files/FileIOTest.java +++ b/plugin-code/src/test/java/com/trivago/cluecumber/files/FileIOTest.java @@ -7,8 +7,8 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; -import static org.hamcrest.core.Is.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; public class FileIOTest { @Rule @@ -46,4 +46,14 @@ public void readFromMissingFileTest() throws Exception { String wrongPath = testFolder.getRoot().getPath().concat("/missing.tmp"); fileIO.readContentFromFile(wrongPath); } + + @Test + public void isExistingFileWrongFileTest() { + assertThat(fileIO.isExistingFile("nonexistent"), is(false)); + } + + @Test + public void isExistingFileExistingFileTest() { + assertThat(fileIO.isExistingFile("src/test/resources/test.ftl"), is(true)); + } } diff --git a/plugin-code/src/test/java/com/trivago/cluecumber/files/FileSystemManagerTest.java b/plugin-code/src/test/java/com/trivago/cluecumber/files/FileSystemManagerTest.java index fefd77c7..852cb911 100644 --- a/plugin-code/src/test/java/com/trivago/cluecumber/files/FileSystemManagerTest.java +++ b/plugin-code/src/test/java/com/trivago/cluecumber/files/FileSystemManagerTest.java @@ -8,6 +8,11 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; +import java.io.File; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + public class FileSystemManagerTest { @Rule public TemporaryFolder testFolder = new TemporaryFolder(); @@ -35,10 +40,17 @@ public void createInvalidDirectory() throws Exception { } @Test(expected = CluecumberPluginException.class) - public void copyResourceFromJarTest() throws Exception { + public void copyInvalidResourceFromJarTest() throws Exception { fileSystemManager.copyResourceFromJar("resource", ""); } + @Test + public void copyResourceFromJarTest() throws Exception { + String newLocation = testFolder.getRoot().getPath().concat("/test.ftl"); + fileSystemManager.copyResourceFromJar("/test.ftl", newLocation); + assertThat(new File(newLocation).isFile(), is(true)); + } + @Test(expected = CluecumberPluginException.class) public void copyResourceTest() throws Exception { fileSystemManager.copyResource("resource", ""); diff --git a/plugin-code/src/test/java/com/trivago/cluecumber/json/pojo/ElementTest.java b/plugin-code/src/test/java/com/trivago/cluecumber/json/pojo/ElementTest.java index 3cd43118..37414f03 100644 --- a/plugin-code/src/test/java/com/trivago/cluecumber/json/pojo/ElementTest.java +++ b/plugin-code/src/test/java/com/trivago/cluecumber/json/pojo/ElementTest.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import java.util.List; +import static org.hamcrest.CoreMatchers.endsWith; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsNull.nullValue; @@ -20,16 +21,9 @@ public class ElementTest { @Before public void setup() { - oldTimeZoneId = ZoneId.systemDefault().getId(); - System.setProperty("user.timezone", "EST"); element = new Element(); } - @After - public void tearDown() { - System.setProperty("user.timezone", oldTimeZoneId); - } - @Test public void getSkippedStatusInEmptyElementsTest() { Status status = element.getStatus(); @@ -397,7 +391,7 @@ public void getStartDateStringTest() { @Test public void getStartTimeStringTest() { element.setStartTimestamp("2019-04-11T08:00:23.668Z"); - assertThat(element.getStartTimeString(), is("10:00:23")); + assertThat(element.getStartTimeString(), endsWith(":00:23")); } @Test @@ -423,6 +417,6 @@ public void getEndTimeStringTest() { before.setResult(beforeResult); beforeSteps.add(before); element.setBefore(beforeSteps); - assertThat(element.getEndTimeString(), is("12:47:03")); + assertThat(element.getEndTimeString(), endsWith(":47:03")); } } diff --git a/plugin-code/src/test/java/com/trivago/cluecumber/properties/LinkedPropertiesTest.java b/plugin-code/src/test/java/com/trivago/cluecumber/properties/LinkedPropertiesTest.java new file mode 100644 index 00000000..d0711027 --- /dev/null +++ b/plugin-code/src/test/java/com/trivago/cluecumber/properties/LinkedPropertiesTest.java @@ -0,0 +1,63 @@ +package com.trivago.cluecumber.properties; + +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class LinkedPropertiesTest { + + private LinkedProperties linkedProperties; + + @Before + public void setup() { + linkedProperties = new LinkedProperties(); + } + + @Test + public void putTest() { + assertThat(linkedProperties.contains("value"), is(false)); + linkedProperties.put("key", "value"); + } + + @Test + public void containsTest() { + assertThat(linkedProperties.contains("value"), is(false)); + linkedProperties.put("key", "value"); + assertThat(linkedProperties.contains("value"), is(true)); + } + + @Test + public void containsValueTest() { + assertThat(linkedProperties.contains("value"), is(false)); + linkedProperties.put("key", "value"); + assertThat(linkedProperties.containsValue("value"), is(true)); + } + + @Test(expected = UnsupportedOperationException.class) + public void elementsTest() { + linkedProperties.elements(); + } + + @Test + public void entrySetTest() { + linkedProperties.put("key", "value"); + linkedProperties.put("key2", "value2"); + assertThat(linkedProperties.entrySet().size(), is(2)); + } + + @Test + public void clearTest() { + linkedProperties.put("key", "value"); + linkedProperties.clear(); + assertThat(linkedProperties.entrySet().size(), is(0)); + } + + @Test + public void containsKeyTest() { + assertThat(linkedProperties.containsKey("key"), is(false)); + linkedProperties.put("key", "value"); + assertThat(linkedProperties.containsKey("key"), is(true)); + } +} diff --git a/plugin-code/src/test/java/com/trivago/cluecumber/properties/PropertiesFileLoaderTest.java b/plugin-code/src/test/java/com/trivago/cluecumber/properties/PropertiesFileLoaderTest.java new file mode 100644 index 00000000..f1685050 --- /dev/null +++ b/plugin-code/src/test/java/com/trivago/cluecumber/properties/PropertiesFileLoaderTest.java @@ -0,0 +1,33 @@ +package com.trivago.cluecumber.properties; + +import com.trivago.cluecumber.exceptions.CluecumberPluginException; +import com.trivago.cluecumber.filesystem.FileIO; +import org.junit.Before; +import org.junit.Test; + +import java.util.LinkedHashMap; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +public class PropertiesFileLoaderTest { + + private PropertiesFileLoader propertiesFileLoader; + + @Before + public void setUp() { + FileIO fileIO = new FileIO(); + propertiesFileLoader = new PropertiesFileLoader(fileIO); + } + + @Test(expected = CluecumberPluginException.class) + public void loadInvalidPropertiesMapTest() throws CluecumberPluginException { + propertiesFileLoader.loadPropertiesMap("nonexistent"); + } + + @Test + public void loadPropertiesMapTest() throws CluecumberPluginException { + final LinkedHashMap propertiesMap = propertiesFileLoader.loadPropertiesMap("src/test/resources/test.properties"); + assertThat(propertiesMap.size(), is(2)); + } +} \ No newline at end of file diff --git a/plugin-code/src/test/java/com/trivago/cluecumber/properties/PropertyManagerTest.java b/plugin-code/src/test/java/com/trivago/cluecumber/properties/PropertyManagerTest.java index 5af3e2c7..534b8656 100644 --- a/plugin-code/src/test/java/com/trivago/cluecumber/properties/PropertyManagerTest.java +++ b/plugin-code/src/test/java/com/trivago/cluecumber/properties/PropertyManagerTest.java @@ -1,6 +1,9 @@ package com.trivago.cluecumber.properties; +import com.trivago.cluecumber.exceptions.CluecumberPluginException; +import com.trivago.cluecumber.exceptions.filesystem.MissingFileException; import com.trivago.cluecumber.exceptions.properties.WrongOrMissingPropertyException; +import com.trivago.cluecumber.filesystem.FileIO; import com.trivago.cluecumber.logging.CluecumberLogger; import org.junit.Before; import org.junit.Test; @@ -11,42 +14,42 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.*; public class PropertyManagerTest { private PropertyManager propertyManager; private CluecumberLogger logger; + private FileIO fileIO; + private PropertiesFileLoader propertiesFileLoader; @Before public void setup() { logger = mock(CluecumberLogger.class); - propertyManager = new PropertyManager(logger); + fileIO = mock(FileIO.class); + propertiesFileLoader = new PropertiesFileLoader(fileIO); + propertyManager = new PropertyManager(logger, fileIO, propertiesFileLoader); } @Test - public void generatedHtmlReportDirectoryTest() { + public void generatedHtmlReportDirectoryTest() throws WrongOrMissingPropertyException { propertyManager.setGeneratedHtmlReportDirectory("test"); assertThat(propertyManager.getGeneratedHtmlReportDirectory(), is("test")); } @Test - public void sourceJsonReportDirectoryTest() { + public void sourceJsonReportDirectoryTest() throws WrongOrMissingPropertyException { propertyManager.setSourceJsonReportDirectory("test"); assertThat(propertyManager.getSourceJsonReportDirectory(), is("test")); } @Test(expected = WrongOrMissingPropertyException.class) public void missingJsonReportDirectoryTest() throws Exception { - propertyManager.setGeneratedHtmlReportDirectory("test"); - propertyManager.validateSettings(); + propertyManager.setSourceJsonReportDirectory(""); } @Test(expected = WrongOrMissingPropertyException.class) public void missingHtmlReportDirectoryTest() throws Exception { - propertyManager.setSourceJsonReportDirectory("test"); - propertyManager.validateSettings(); + propertyManager.setGeneratedHtmlReportDirectory(""); } @Test @@ -64,6 +67,26 @@ public void customParametersTest() { assertThat(propertyManager.getCustomParameters().get("key"), is("value")); } + @Test(expected = MissingFileException.class) + public void setCustomParametersFileNonExistingTest() throws CluecumberPluginException { + String customParametersFile = "customParametersFile"; + when(fileIO.isExistingFile(customParametersFile)).thenReturn(false); + propertyManager.setCustomParametersFile(customParametersFile); + } + + @Test + public void setCustomParametersFileTest() throws CluecumberPluginException { + String customParametersFile = "src/test/resources/test.properties"; + when(fileIO.isExistingFile(customParametersFile)).thenCallRealMethod(); + when(fileIO.readContentFromFile(customParametersFile)).thenCallRealMethod(); + propertyManager.setCustomParametersFile(customParametersFile); + assertThat(propertyManager.getCustomParametersFile(), is(customParametersFile)); + final Map customParameters = propertyManager.getCustomParameters(); + assertThat(customParameters.size(), is(2)); + assertThat(customParameters.get("Test_Property"), is("some value")); + assertThat(customParameters.get("Test_Property2"), is("another value")); + } + @Test public void failOnPendingOrSkippedStepsTest() { assertThat(propertyManager.isFailScenariosOnPendingOrUndefinedSteps(), is(false)); @@ -105,20 +128,29 @@ public void expandDocStringsTest() { } @Test - public void customCssTest() { + public void customCssTest() throws MissingFileException { + String customCss = "MyCss"; + when(fileIO.isExistingFile(customCss)).thenReturn(true); + propertyManager.setCustomCssFile(customCss); + assertThat(propertyManager.getCustomCssFile(), is(customCss)); + } + + @Test(expected = MissingFileException.class) + public void customCssMissingFileTest() throws MissingFileException { String customCss = "MyCss"; - propertyManager.setCustomCss(customCss); - assertThat(propertyManager.getCustomCss(), is("MyCss")); + when(fileIO.isExistingFile(customCss)).thenReturn(false); + propertyManager.setCustomCssFile(customCss); } @Test - public void logFullPropertiesTest() { + public void logFullPropertiesTest() throws MissingFileException { Map customParameters = new HashMap<>(); customParameters.put("key1", "value1"); customParameters.put("key2", "value2"); propertyManager.setCustomParameters(customParameters); - propertyManager.setCustomCss("customCss"); + when(fileIO.isExistingFile("test")).thenReturn(true); + propertyManager.setCustomCssFile("test"); propertyManager.logProperties(); verify(logger, times(9)).info(anyString()); diff --git a/plugin-code/src/test/java/com/trivago/cluecumber/rendering/ReportGeneratorTest.java b/plugin-code/src/test/java/com/trivago/cluecumber/rendering/ReportGeneratorTest.java index dc2cfe79..4e6e9927 100644 --- a/plugin-code/src/test/java/com/trivago/cluecumber/rendering/ReportGeneratorTest.java +++ b/plugin-code/src/test/java/com/trivago/cluecumber/rendering/ReportGeneratorTest.java @@ -1,9 +1,11 @@ package com.trivago.cluecumber.rendering; +import com.trivago.cluecumber.filesystem.FileIO; import com.trivago.cluecumber.filesystem.FileSystemManager; import com.trivago.cluecumber.json.pojo.Element; import com.trivago.cluecumber.json.pojo.Report; import com.trivago.cluecumber.logging.CluecumberLogger; +import com.trivago.cluecumber.properties.PropertiesFileLoader; import com.trivago.cluecumber.properties.PropertyManager; import com.trivago.cluecumber.rendering.pages.pojos.pagecollections.AllScenariosPageCollection; import com.trivago.cluecumber.rendering.pages.visitors.FeatureVisitor; @@ -30,7 +32,9 @@ public class ReportGeneratorTest { public void setup() { fileSystemManager = mock(FileSystemManager.class); CluecumberLogger logger = mock(CluecumberLogger.class); - PropertyManager propertyManager = new PropertyManager(logger); + PropertiesFileLoader propertiesFileLoader = mock(PropertiesFileLoader.class); + FileIO fileIO = mock(FileIO.class); + PropertyManager propertyManager = new PropertyManager(logger, fileIO, propertiesFileLoader); ScenarioVisitor scenarioVisitor = mock(ScenarioVisitor.class); FeatureVisitor featureVisitor = mock(FeatureVisitor.class); TagVisitor tagVisitor = mock(TagVisitor.class); @@ -64,20 +68,9 @@ public void fileOperationsTest() throws Exception { Report[] reportList = {report1, report2}; allScenariosPageCollection.addReports(reportList); -// when(templateEngine.getRenderedScenarioSummaryPageContent(allScenariosPageCollection)).thenReturn("RENDERED_START_PAGE_CONTENT"); -// when(templateEngine.getRenderedScenarioDetailPageContent(any(ScenarioDetailsPageCollection.class))).thenReturn("RENDERED_DETAIL_PAGE_CONTENT"); -// when(templateEngine.getRenderedTagSummaryPageContent(any(AllTagsPageCollection.class))).thenReturn("RENDERED_TAG_PAGE_CONTENT"); -// when(templateEngine.getRenderedFeatureSummaryPageContent(any(AllFeaturesPageCollection.class))).thenReturn("RENDERED_FEATURE_PAGE_CONTENT"); -// when(templateEngine.getRenderedStepSummaryPageContent(any(AllStepsPageCollection.class))).thenReturn("RENDERED_STEPS_PAGE_CONTENT"); - reportGenerator.generateReport(allScenariosPageCollection); verify(fileSystemManager, times(8)).createDirectory(anyString()); verify(fileSystemManager, times(17)).copyResourceFromJar(anyString(), anyString()); -// verify(fileIO, times(1)).writeContentToFile(eq("RENDERED_START_PAGE_CONTENT"), anyString()); -// verify(fileIO, times(2)).writeContentToFile(eq("RENDERED_DETAIL_PAGE_CONTENT"), anyString()); -// verify(fileIO, times(1)).writeContentToFile(eq("RENDERED_TAG_PAGE_CONTENT"), anyString()); -// verify(fileIO, times(1)).writeContentToFile(eq("RENDERED_STEPS_PAGE_CONTENT"), anyString()); -// verify(fileIO, times(1)).writeContentToFile(eq("RENDERED_FEATURE_PAGE_CONTENT"), anyString()); } } diff --git a/plugin-code/src/test/resources/test.css b/plugin-code/src/test/resources/test.css new file mode 100644 index 00000000..e69de29b diff --git a/plugin-code/src/test/resources/test.properties b/plugin-code/src/test/resources/test.properties new file mode 100644 index 00000000..99655fda --- /dev/null +++ b/plugin-code/src/test/resources/test.properties @@ -0,0 +1,2 @@ +Test_Property=some value +Test_Property2=another value \ No newline at end of file