Decouple TXT Report file writing and formatting logic (1/6) 02/109302/6
authorFrancis Toth <francis.toth@yoppworks.com>
Thu, 18 Jun 2020 11:37:20 +0000 (07:37 -0400)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Fri, 19 Jun 2020 10:56:47 +0000 (10:56 +0000)
This commit is the first of a series aiming to decouple the writing and formatting logic of text report files in the asdc tool. Unfortunately because of a significant amount of technical debt, a small hack had to be done so that the header of the file is not written twice. This will be removed in a future change.

Signed-off-by: Francis Toth <francis.toth@yoppworks.com>
Change-Id: I8d2a7c1353d59562d8026088824c26ae0daa36e9
Issue-ID: SDC-2499

15 files changed:
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/ValidationToolBL.java
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ServiceValidatorExecuter.java
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/TopologyTemplateValidatorExecuter.java
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ValidatorExecuter.java
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/VfValidatorExecuter.java
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/FileType.java
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFile.java
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManager.java
asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ValidationTool.java
asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/ValidationToolBLTest.java
asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/executers/ServiceValidatorExecuterTest.java
asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/executers/VfValidatorExecuterTest.java
asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFileNioHelper.java
asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFileWriterTestFactory.java
asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManagerTest.java

index 6347e34..61b3640 100644 (file)
@@ -22,6 +22,7 @@ package org.openecomp.sdc.asdctool.impl.validator;
 
 import org.openecomp.sdc.asdctool.impl.validator.executers.ValidatorExecuter;
 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
+import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile;
 import org.openecomp.sdc.common.log.wrappers.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -31,7 +32,7 @@ import java.util.List;
 @Component
 public class ValidationToolBL {
 
-    private static Logger log = Logger.getLogger(ValidationToolBL.class.getName());
+    private static final Logger log = Logger.getLogger(ValidationToolBL.class);
 
     protected List<ValidatorExecuter> validators;
 
@@ -43,10 +44,10 @@ public class ValidationToolBL {
         this.validators = validators;
     }
 
-    public boolean validateAll(Report report, String outputFilePath) {
+    public boolean validateAll(Report report, ReportFile.TXTFile textFile, String outputFilePath) {
         for (ValidatorExecuter validatorExec: validators) {
             log.debug("ValidatorExecuter "+validatorExec.getName()+" started");
-            if (!validatorExec.executeValidations(report, outputFilePath)) {
+            if (!validatorExec.executeValidations(report, textFile, outputFilePath)) {
                 allValid = false;
                 log.debug("ValidatorExecuter "+validatorExec.getName()+" finished with warnings");
             }
index 4bcdeaf..af9cb36 100644 (file)
@@ -21,6 +21,7 @@
 package org.openecomp.sdc.asdctool.impl.validator.executers;
 
 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
+import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.TXTFile;
 import org.openecomp.sdc.asdctool.impl.validator.tasks.ServiceValidationTask;
 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
@@ -43,13 +44,8 @@ public class ServiceValidatorExecuter extends TopologyTemplateValidatorExecuter
     }
 
     @Override
-    public boolean executeValidations(Report report, String outputFilePath) {
+    public boolean executeValidations(Report report, TXTFile reportFile, String outputFilePath) {
         List<GraphVertex> vertices = getVerticesToValidate(ComponentTypeEnum.SERVICE);
-        return validate(report, tasks, vertices, outputFilePath);
-    }
-
-    @Override
-    public String getName() {
-        return super.getName();
+        return validate(report, tasks, vertices, reportFile, outputFilePath);
     }
 }
index 0c1e3be..d6a5d12 100644 (file)
@@ -22,6 +22,7 @@ package org.openecomp.sdc.asdctool.impl.validator.executers;
 
 import fj.data.Either;
 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
+import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.TXTFile;
 import org.openecomp.sdc.asdctool.impl.validator.tasks.TopologyTemplateValidationTask;
 import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager;
 import org.openecomp.sdc.asdctool.impl.validator.utils.VertexResult;
@@ -44,7 +45,7 @@ import java.util.Set;
 
 public class TopologyTemplateValidatorExecuter {
 
-    private static Logger log = Logger.getLogger(VfValidatorExecuter.class.getName());
+    private static final Logger log = Logger.getLogger(VfValidatorExecuter.class);
 
     protected JanusGraphDao janusGraphDao;
 
@@ -80,8 +81,13 @@ public class TopologyTemplateValidatorExecuter {
         return results.left().value();
     }
 
-    protected boolean validate(Report report, List<? extends TopologyTemplateValidationTask> tasks, List<GraphVertex> vertices,
-        String outputFilePath) {
+    protected boolean validate(
+        Report report,
+        List<? extends TopologyTemplateValidationTask> tasks,
+        List<GraphVertex> vertices,
+        TXTFile reportFile,
+        String outputFilePath
+    ) {
         ReportManager.reportStartValidatorRun(getName(), vertices.size(), outputFilePath);
         Set<String> failedTasks = new HashSet<>();
         Set<String> successTasks = new HashSet<>();
@@ -93,7 +99,7 @@ public class TopologyTemplateValidatorExecuter {
             vertexNum++;
             boolean successAllTasks = true;
             for (TopologyTemplateValidationTask task : tasks) {
-                ReportManager.reportStartTaskRun(vertex, task.getTaskName(), outputFilePath);
+                reportFile.reportStartTaskRun(vertex, task.getTaskName());
                 VertexResult result = task.validate(report, vertex, outputFilePath);
                 if (!result.getStatus()) {
                     failedTasks.add(task.getTaskName());
index 7d5314c..88b7c0c 100644 (file)
 package org.openecomp.sdc.asdctool.impl.validator.executers;
 
 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
+import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.TXTFile;
 
 public interface ValidatorExecuter {
 
-    boolean executeValidations(Report report, String outputFilePath);
+    boolean executeValidations(Report report, TXTFile textFile, String outputFilePath);
     String getName();
 }
index 5c42637..3484bbf 100644 (file)
@@ -21,6 +21,7 @@
 package org.openecomp.sdc.asdctool.impl.validator.executers;
 
 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
+import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.TXTFile;
 import org.openecomp.sdc.asdctool.impl.validator.tasks.VfValidationTask;
 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
@@ -41,13 +42,8 @@ public class VfValidatorExecuter extends TopologyTemplateValidatorExecuter imple
     }
 
     @Override
-    public boolean executeValidations(Report report, String outputFilePath) {
+    public boolean executeValidations(Report report, TXTFile reportFile, String outputFilePath) {
         List<GraphVertex> vertices = getVerticesToValidate(ComponentTypeEnum.RESOURCE);
-        return validate(report, tasks, vertices, outputFilePath);
-    }
-
-    @Override
-    public String getName() {
-        return super.getName();
+        return validate(report, tasks, vertices, reportFile, outputFilePath);
     }
 }
index 7598ae1..6ce0791 100644 (file)
@@ -25,5 +25,7 @@ package org.openecomp.sdc.asdctool.impl.validator.report;
  * a {@link org.openecomp.sdc.asdctool.impl.validator.report.ReportFileWriter }.
  */
 public interface FileType {
+    interface TXT extends FileType { }
+
     interface CSV extends FileType { }
 }
index a0703f4..8b76766 100644 (file)
 
 package org.openecomp.sdc.asdctool.impl.validator.report;
 
+import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
+
 /**
  * Provides business logic in regards to file writing required by the validation tools
  */
 public class ReportFile {
 
+    // TODO: Delete this function once all the report file business logic has been moved to ReportFile
+    static public TXTFile makeAppendableTxtFile(ReportFileWriter<FileType.TXT> writer) {
+        return new TXTFile(writer);
+    }
+
+    static public TXTFile makeTxtFile(ReportFileWriter<FileType.TXT> writer) {
+        writer.writeln("-----------------------Validation Tool Results:-------------------------");
+        return new TXTFile(writer);
+    }
+
     static public CSVFile makeCsvFile(ReportFileWriter<FileType.CSV> writer) {
         writer.writeln("Vertex ID,Task Name,Success,Result Details,Result Description");
         return new CSVFile(writer);
     }
 
+    /**
+     * Provides csv writing business logic related to {@link org.openecomp.sdc.asdctool.main.ValidationTool}
+     */
+    public static final class TXTFile extends ReportFile {
+
+        private final ReportFileWriter<FileType.TXT> writer;
+
+        private TXTFile(ReportFileWriter<FileType.TXT> writer) {
+            this.writer = writer;
+        }
+
+        public void reportStartTaskRun(GraphVertex vertex, String taskName) {
+            writer.writeln("");
+            writer.writeln("-----------------------Vertex: " + vertex.getUniqueId() +
+                ", Task " + taskName + " Started-----------------------");
+        }
+    }
+
     /**
      * Provides csv writing business logic related to {@link org.openecomp.sdc.asdctool.main.ValidationTool}
      */
index 5b78df8..6e8e2de 100644 (file)
@@ -23,6 +23,7 @@ package org.openecomp.sdc.asdctool.impl.validator.utils;
 
 import java.io.IOException;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.StandardOpenOption;
 import java.util.Set;
@@ -49,9 +50,13 @@ public class ReportManager {
     }
 
     private void initReportFile(String txtReportFilePath) throws IOException {
-        StrBuilder sb = new StrBuilder();
-        sb.appendln("-----------------------Validation Tool Results:-------------------------");
-        Files.write(Paths.get(txtReportFilePath), sb.toString().getBytes());
+        Path path = Paths.get(txtReportFilePath);
+        // TODO: Remove this once all the report file business logic has been moved to ReportFile
+        if(Files.notExists(path)) {
+            StrBuilder sb = new StrBuilder();
+            sb.appendln("-----------------------Validation Tool Results:-------------------------");
+            Files.write(path, sb.toString().getBytes());
+        }
     }
 
     public static void printValidationTaskStatus(GraphVertex vertexScanned, String taskName, boolean success,
@@ -92,13 +97,6 @@ public class ReportManager {
         writeReportLineToFile(sb.toString(), outputFilePath);
     }
 
-    public static void reportStartTaskRun(GraphVertex vertex, String taskName, String outputFilePath) {
-        StrBuilder sb = new StrBuilder();
-        sb.appendln("-----------------------Vertex: " + vertex.getUniqueId() + ", Task " + taskName
-            + " Started-----------------------");
-        writeReportLineToFile(sb.toString(), outputFilePath);
-    }
-
     public static void reportEndOfToolRun(Report report, String outputFilePath) {
         StrBuilder sb = new StrBuilder();
         sb.appendln("-----------------------------------Validator Tool Summary-----------------------------------");
index 754d163..d84f0a5 100644 (file)
@@ -29,6 +29,7 @@ import org.openecomp.sdc.asdctool.impl.validator.report.FileType;
 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
 import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile;
 import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.CSVFile;
+import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.TXTFile;
 import org.openecomp.sdc.asdctool.impl.validator.report.ReportFileWriter;
 import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager;
 import org.openecomp.sdc.be.config.ConfigurationManager;
@@ -49,6 +50,7 @@ public class ValidationTool {
         String csvReportFilePath = ValidationConfigManager.csvReportFilePath(outputPath, System::currentTimeMillis);
 
         CSVFile csvFile = ReportFile.makeCsvFile(makeNioWriter(Paths.get(csvReportFilePath)));
+        TXTFile textFile = ReportFile.makeTxtFile(makeNioWriter(Paths.get(txtReportFilePath)));
 
         String appConfigDir = args[1];
         AnnotationConfigApplicationContext context = initContext(appConfigDir);
@@ -56,7 +58,7 @@ public class ValidationTool {
 
         log.info("Start Validation Tool");
         Report report = Report.make();
-        boolean result = validationToolBL.validateAll(report, txtReportFilePath);
+        boolean result = validationToolBL.validateAll(report, textFile, txtReportFilePath);
         ReportManager.reportEndOfToolRun(report, txtReportFilePath);
         csvFile.printAllResults(report);
         if (result) {
index 3db011b..8c2dffc 100644 (file)
@@ -25,6 +25,9 @@ import org.openecomp.sdc.asdctool.impl.validator.executers.ServiceValidatorExecu
 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
 
+import static org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.makeTxtFile;
+import static org.openecomp.sdc.asdctool.impl.validator.report.ReportFileWriterTestFactory.makeConsoleWriter;
+
 import java.util.ArrayList;
 import java.util.LinkedList;
 
@@ -45,6 +48,6 @@ public class ValidationToolBLTest {
         Report report = Report.make();
         // Initially no outputFilePath was passed to this function (hence it is set to null)
         // TODO: Fix this null and see if the argument is used by this function
-        testSubject.validateAll(report, null);
+        testSubject.validateAll(report, makeTxtFile(makeConsoleWriter()), null);
     }
 }
index 98b8b25..2f9f11b 100644 (file)
 
 package org.openecomp.sdc.asdctool.impl.validator.executers;
 
+import static org.mockito.Mockito.mock;
+import static org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.makeTxtFile;
+import static org.openecomp.sdc.asdctool.impl.validator.report.ReportFileWriterTestFactory.makeConsoleWriter;
+
 import org.junit.Test;
 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
 
-import static org.mockito.Mockito.mock;
-
 public class ServiceValidatorExecuterTest {
 
     private ServiceValidatorExecuter createTestSubject() {
@@ -43,6 +45,6 @@ public class ServiceValidatorExecuterTest {
         Report report = Report.make();
         // Initially no outputFilePath was passed to this function (hence it is set to null)
         // TODO: Fix this null and see if the argument is used by this function
-        createTestSubject().executeValidations(report, null);
+        createTestSubject().executeValidations(report, makeTxtFile(makeConsoleWriter()), null);
     }
 }
index 8ea87b1..3760a57 100644 (file)
 
 package org.openecomp.sdc.asdctool.impl.validator.executers;
 
+import static org.mockito.Mockito.mock;
+import static org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.makeTxtFile;
+import static org.openecomp.sdc.asdctool.impl.validator.report.ReportFileWriterTestFactory.makeConsoleWriter;
+
+import java.util.ArrayList;
+import java.util.List;
 import org.junit.Test;
 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
 import org.openecomp.sdc.asdctool.impl.validator.tasks.VfValidationTask;
 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
 
-import java.util.ArrayList;
-import java.util.List;
-
-import static org.mockito.Mockito.mock;
-
 public class VfValidatorExecuterTest {
 
     private VfValidatorExecuter createTestSubject() {
@@ -49,6 +50,6 @@ public class VfValidatorExecuterTest {
         Report report = Report.make();
         // Initially no outputFilePath was passed to this function (hence it is set to null)
         // TODO: Fix this null and see if the argument is used by this function
-        createTestSubject().executeValidations(report, null);
+        createTestSubject().executeValidations(report, makeTxtFile(makeConsoleWriter()), null);
     }
 }
index 7a9bb0c..d6d03a9 100644 (file)
 
 package org.openecomp.sdc.asdctool.impl.validator.report;
 
+import static org.openecomp.sdc.asdctool.impl.validator.report.ReportFileWriterTestFactory.makeNioWriter;
+
+import java.io.BufferedReader;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
+import java.util.List;
 import java.util.function.Function;
+import java.util.stream.Collectors;
 
 /**
  * Provides facilities to for writing report files when testing
@@ -49,4 +54,36 @@ public class ReportFileNioHelper {
             throw new RuntimeException(ex);
         }
     }
+
+   /**
+     * Provides a transactional context for TXT report file writing
+     *
+     * @param txtReportFilePath The resulting file path
+     * @param f                 The function to write in a TXT file
+     * @param <A>               The type returned by the function consuming the file
+     */
+    public static <A> A withTxtFile(String txtReportFilePath, Function<ReportFile.TXTFile, A> f) {
+        // TODO: Switch to makeTxtFile once all the report file business logic has been moved to
+        // ReportFile
+        ReportFile.TXTFile file = ReportFile.makeAppendableTxtFile(makeNioWriter(txtReportFilePath));
+        A result = f.apply(file);
+        try {
+            Files.delete(Paths.get(txtReportFilePath));
+            return result;
+        } catch (IOException ex) {
+            throw new RuntimeException(ex);
+        }
+    }
+
+    /**
+     * Reads the content of a file and store each line in a list
+     * @param filePath The path to the file
+     */
+    public static List<String> readFileAsList(String filePath) {
+        try (BufferedReader br = Files.newBufferedReader(Paths.get(filePath))) {
+            return br.lines().collect(Collectors.toList());
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
 }
index 364c080..b28cb7a 100644 (file)
@@ -51,4 +51,17 @@ public class ReportFileWriterTestFactory {
     public static <A extends FileType> ReportFileWriter<A> makeNioWriter(Path path) {
         return ReportFileWriter.makeNioWriter(path, ex -> fail(ex.getMessage()));
     }
+
+    /**
+     * Creates a writer rendering the data written in the console
+     * @param <A> a Phantom type used only for type-safety
+     */
+    public static <A extends FileType> ReportFileWriter<A> makeConsoleWriter() {
+        return new ReportFileWriter<A>() {
+            @Override
+            public void write(String line) {
+                System.out.print(line);
+            }
+        };
+    }
 }
index f28ca5d..53e5668 100644 (file)
@@ -193,15 +193,16 @@ public class ReportManagerTest {
         when(vertexScanned.getUniqueId()).thenReturn(UNIQUE_ID);
 
         // when
-        ReportManager.reportStartTaskRun(vertexScanned, TASK_1_NAME, txtReportFilePath);
-
-        List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath);
+        List<String> reportTxtFile = ReportFileNioHelper.withTxtFile(txtReportFilePath, file -> {
+            file.reportStartTaskRun(vertexScanned, TASK_1_NAME);
+            return ReportFileNioHelper.readFileAsList(txtReportFilePath);
+        });
 
         // then
-        assertNotNull(reportOutputFile);
-        assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportOutputFile.get(0));
+        assertNotNull(reportTxtFile);
+        assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportTxtFile.get(0));
         assertEquals("-----------------------Vertex: " + UNIQUE_ID + ", Task " + TASK_1_NAME
-            + " Started-----------------------", reportOutputFile.get(2));
+            + " Started-----------------------", reportTxtFile.get(2));
     }
 
     private String getCsvExpectedResult(String vertexID, String taskID) {