Repository: authorjapps/zerocode
Pull request #20 (2018-01-31 17:11:25)

ISSUE-19 # Target report folder existence validated


Patch:
diff --git a/src/main/java/org/jsmart/zerocode/core/report/ZeroCodeReportGeneratorImpl.java b/src/main/java/org/jsmart/zerocode/core/report/ZeroCodeReportGeneratorImpl.java
index 0ed671af..f1b3557d 100644
--- a/src/main/java/org/jsmart/zerocode/core/report/ZeroCodeReportGeneratorImpl.java
+++ b/src/main/java/org/jsmart/zerocode/core/report/ZeroCodeReportGeneratorImpl.java
@@ -6,11 +6,11 @@ import com.fasterxml.jackson.dataformat.csv.CsvMapper;
 import com.fasterxml.jackson.dataformat.csv.CsvParser;
 import com.fasterxml.jackson.dataformat.csv.CsvSchema;
 import com.google.inject.Inject;
-import org.jsmart.zerocode.core.domain.reports.ZeroCodeReport;
 import org.jsmart.zerocode.core.domain.builders.HighChartColumnHtmlBuilder;
 import org.jsmart.zerocode.core.domain.builders.ZeroCodeChartKeyValueArrayBuilder;
 import org.jsmart.zerocode.core.domain.builders.ZeroCodeChartKeyValueBuilder;
 import org.jsmart.zerocode.core.domain.builders.ZeroCodeCsvReportBuilder;
+import org.jsmart.zerocode.core.domain.reports.ZeroCodeReport;
 import org.jsmart.zerocode.core.domain.reports.chart.HighChartColumnHtml;
 import org.jsmart.zerocode.core.domain.reports.csv.ZeroCodeCsvReport;
 
@@ -28,7 +28,7 @@ import static org.jsmart.zerocode.core.domain.reports.ZeroCodeReportProperties.T
 import static org.jsmart.zerocode.core.domain.reports.ZeroCodeReportProperties.TARGET_FULL_REPORT_DIR;
 import static org.jsmart.zerocode.core.domain.reports.ZeroCodeReportProperties.TARGET_REPORT_DIR;
 
-public class ZeroCodeReportGeneratorImpl implements ZeroCodeReportGenerator{
+public class ZeroCodeReportGeneratorImpl implements ZeroCodeReportGenerator {
 
     @Inject
     private ObjectMapper mapper;
@@ -168,6 +168,8 @@ public class ZeroCodeReportGeneratorImpl implements ZeroCodeReportGenerator{
 
     public List<ZeroCodeReport> readZeroCodeReportsByPath(String reportsFolder) {
 
+        validateReportsFolderAndTheFilesExists(reportsFolder);
+
         List<String> allEndPointFiles = getAllEndPointFilesFrom(reportsFolder);
 
         List<ZeroCodeReport> scenarioReports = allEndPointFiles.stream()
@@ -201,4 +203,28 @@ public class ZeroCodeReportGeneratorImpl implements ZeroCodeReportGenerator{
 
         return fileNames;
     }
+
+    protected void validateReportsFolderAndTheFilesExists(String reportsFolder) {
+
+        try {
+            File[] files = new File(reportsFolder).listFiles((dir, fileName) -> fileName.endsWith(".json"));
+
+            ofNullable(files).orElseThrow(() -> new RuntimeException("Somehow the '" + reportsFolder + "' has got no files."));
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            final String message = "\n----------------------------------------------------------------------------------------\n" +
+                    "Somehow the '" + reportsFolder + "' is not present or has no report JSON files. \n" +
+                    "Possible reasons- \n" +
+                    "   1) No tests were activated or made to run via ZeroCode runner. -or- \n" +
+                    "   2) You have simply used @RunWith(...) and ignored all tests -or- \n" +
+                    "   3) Permission issue to create/write folder/files \n" +
+                    "   4) Please fix it by adding/activating at least one test case or fix the file permission issue\n" +
+                    "----------------------------------------------------------------------------------------\n";
+
+            throw new RuntimeException(message);
+        }
+
+    }
+
 }


Test patch:
diff --git a/src/test/java/org/jsmart/zerocode/core/report/ZeroCodeReportGeneratorImplTest.java b/src/test/java/org/jsmart/zerocode/core/report/ZeroCodeReportGeneratorImplTest.java
new file mode 100644
index 00000000..1699708a
--- /dev/null
+++ b/src/test/java/org/jsmart/zerocode/core/report/ZeroCodeReportGeneratorImplTest.java
@@ -0,0 +1,45 @@
+package org.jsmart.zerocode.core.report;
+
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+public class ZeroCodeReportGeneratorImplTest {
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+
+    private ZeroCodeReportGeneratorImpl zeroCodeReportGenerator;
+
+
+    @Before
+    public void setItUp() throws Exception {
+
+        zeroCodeReportGenerator = new ZeroCodeReportGeneratorImpl();
+
+    }
+
+    @Test
+    public void testReportFolderNotPresentInTarget_validation() throws Exception {
+        final String reportsFolder = "/target/helloooo";
+
+        expectedException.expect(RuntimeException.class);
+        expectedException.expectMessage("Somehow the '/target/helloooo' is not present or has no report JSON files");
+        expectedException.expectMessage("1) No tests were activated or made to run via ZeroCode runner.");
+        zeroCodeReportGenerator.readZeroCodeReportsByPath(reportsFolder);
+
+    }
+
+    @Ignore("mvn clean install - removes target folder. So this passes when run without 'clean'" +
+            "To fix it create a temp folder, assign to reportsFolder variable and run")
+    @Test
+    public void testReportFolderPresentInTargetNormalFlow() throws Exception {
+        final String reportsFolder = "target/zerocode-test-reports";
+
+        zeroCodeReportGenerator.validateReportsFolderAndTheFilesExists(reportsFolder);
+
+    }
+}
\ No newline at end of file