diff options
author | Francis Toth <francis.toth@yoppworks.com> | 2020-04-20 19:16:47 -0400 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2020-06-18 05:40:40 +0000 |
commit | 841d054f6bf5231c96abf1e91e1715c02809046a (patch) | |
tree | 66b681561778e6aa72470ee049927f5e1db05430 /asdctool/src/test | |
parent | bc9783d71b65e0ab70dcd5bd6fcb8ff0e09eaf4b (diff) |
Decouple CSV Report file writing and formatting logic
Signed-off-by: Francis Toth <francis.toth@yoppworks.com>
Change-Id: I6168b71bb54192a806ff6aa437788bedad933c76
Issue-ID: SDC-2499
Diffstat (limited to 'asdctool/src/test')
5 files changed, 133 insertions, 32 deletions
diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFileNioHelper.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFileNioHelper.java new file mode 100644 index 0000000000..7a9bb0cf4d --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFileNioHelper.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 Bell. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.asdctool.impl.validator.report; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.function.Function; + +/** + * Provides facilities to for writing report files when testing + */ +public class ReportFileNioHelper { + private ReportFileNioHelper() { + } + + /** + * Provides a transactional context for CSV report file writing + * + * @param csvReportFilePath The resulting file path + * @param f The function to write in a CSV file + * @param <A> A Phantom type only required for type-safety + */ + public static <A> A withCsvFile(String csvReportFilePath, Function<ReportFile.CSVFile, A> f) { + ReportFile.CSVFile file = ReportFile.makeCsvFile(ReportFileWriterTestFactory.makeNioWriter(csvReportFilePath)); + A result = f.apply(file); + try { + Files.delete(Paths.get(csvReportFilePath)); + return result; + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFileWriterTestFactory.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFileWriterTestFactory.java new file mode 100644 index 0000000000..364c08014e --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFileWriterTestFactory.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 Bell Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.asdctool.impl.validator.report; + +import static org.junit.Assert.fail; + +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Provides facilities to for creating report file writers when testing + */ +public class ReportFileWriterTestFactory { + private ReportFileWriterTestFactory() { + } + + /** + * Alias for {@link org.openecomp.sdc.asdctool.impl.validator.report.ReportFileWriterTestFactory#makeNioWriter(Path)} + * + * @param path The resulting file path + * @param <A> a Phantom type used only for type-safety + */ + public static <A extends FileType> ReportFileWriter<A> makeNioWriter(String path) { + Path p = Paths.get(path); + return makeNioWriter(p); + } + + /** + * Creates a NIO writer storing the data written into a file on disk + * @param path The resulting file path + * @param <A> a Phantom type used only for type-safety + */ + public static <A extends FileType> ReportFileWriter<A> makeNioWriter(Path path) { + return ReportFileWriter.makeNioWriter(path, ex -> fail(ex.getMessage())); + } +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ArtifactValidationUtilsTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ArtifactValidationUtilsTest.java index 354714b0c2..7b35373768 100644 --- a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ArtifactValidationUtilsTest.java +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ArtifactValidationUtilsTest.java @@ -21,11 +21,24 @@ package org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + import fj.data.Either; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.IntStream; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Assertions; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -42,20 +55,6 @@ import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.IntStream; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.when; - public class ArtifactValidationUtilsTest { @Mock @@ -85,7 +84,6 @@ public class ArtifactValidationUtilsTest { private static final String UNIQUE_ID_VERTEX = "321"; private final static String resourcePath = new File("src/test/resources").getAbsolutePath(); - private final static String csvReportFilePath = ValidationConfigManager.DEFAULT_CSV_PATH; private final static String txtReportFilePath = ValidationConfigManager.txtReportFilePath(resourcePath); ArtifactValidationUtilsTest () { @@ -103,12 +101,12 @@ public class ArtifactValidationUtilsTest { @BeforeEach public void setup() { - ReportManager.make(csvReportFilePath, txtReportFilePath); + ReportManager.make(txtReportFilePath); } @AfterEach public void clean() { - ReportManagerHelper.cleanReports(csvReportFilePath, txtReportFilePath); + ReportManagerHelper.cleanReports(txtReportFilePath); } @Test @@ -141,7 +139,7 @@ public class ArtifactValidationUtilsTest { // when ArtifactsVertexResult result = testSubject.validateArtifactsAreInCassandra(report, vertex, TASK_NAME, artifacts, txtReportFilePath); - ReportManager.reportEndOfToolRun(report, csvReportFilePath, txtReportFilePath); + ReportManager.reportEndOfToolRun(report, txtReportFilePath); List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath); diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManagerHelper.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManagerHelper.java index 938b2c2202..5a8a36a88a 100644 --- a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManagerHelper.java +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManagerHelper.java @@ -36,16 +36,11 @@ public class ReportManagerHelper { return readFileAsList(txtReportFilePath); } - public static List<String> getReportCsvFileAsList(String csvReportFilePath) { - return readFileAsList(csvReportFilePath); - } - - public static void cleanReports(String csvReportFilePath, String txtReportFilePath) { - cleanFile(csvReportFilePath); + public static void cleanReports(String txtReportFilePath) { cleanFile(txtReportFilePath); } - private static List<String> readFileAsList(String filePath) { + public static List<String> readFileAsList(String filePath) { try (BufferedReader br = Files.newBufferedReader(Paths.get(filePath))) { return br.lines().collect(Collectors.toList()); } catch (IOException e) { diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManagerTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManagerTest.java index c231ca3677..f28ca5dc41 100644 --- a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManagerTest.java +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManagerTest.java @@ -27,6 +27,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager; import org.openecomp.sdc.asdctool.impl.validator.report.Report; +import org.openecomp.sdc.asdctool.impl.validator.report.ReportFileNioHelper; import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; import java.io.File; @@ -75,13 +76,13 @@ public class ReportManagerTest { @BeforeEach public void setup() { - ReportManager.make(csvReportFilePath, txtReportFilePath); + ReportManager.make(txtReportFilePath); successResult.setStatus(true); } @AfterEach public void clean() { - ReportManagerHelper.cleanReports(csvReportFilePath, txtReportFilePath); + ReportManagerHelper.cleanReports(txtReportFilePath); } @Test @@ -90,9 +91,11 @@ public class ReportManagerTest { Report report = Report.make(); report.addSuccess(VERTEX_1_ID, TASK_1_NAME, successResult); report.addSuccess(VERTEX_2_ID, TASK_2_NAME, successResult); - ReportManager.printAllResults(report, csvReportFilePath); - List<String> reportCsvFile = ReportManagerHelper.getReportCsvFileAsList(csvReportFilePath); + List<String> reportCsvFile = ReportFileNioHelper.withCsvFile(csvReportFilePath, file -> { + file.printAllResults(report); + return ReportManagerHelper.readFileAsList(csvReportFilePath); + }); // then assertNotNull(reportCsvFile); @@ -106,13 +109,12 @@ public class ReportManagerTest { // when Report report = Report.make(); report.addFailure(TASK_1_NAME, VERTEX_1_ID); - ReportManager.reportEndOfToolRun(report, csvReportFilePath, txtReportFilePath); + ReportManager.reportEndOfToolRun(report, txtReportFilePath); List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath); // then assertNotNull(reportOutputFile); - assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportOutputFile.get(0)); assertEquals(EXPECTED_OUTPUT_FILE_SUMMARY, reportOutputFile.get(2)); assertEquals("Task: " + TASK_1_NAME, reportOutputFile.get(3)); |