Fix unit tests with conflicting folder
[sdc.git] / asdctool / src / test / java / org / openecomp / sdc / asdctool / impl / validator / utils / ReportManagerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (c) 2019 Samsung
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.asdctool.impl.validator.utils;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.mockito.Mockito.when;
27
28 import java.nio.file.Path;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.SortedSet;
32 import java.util.TreeSet;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.io.TempDir;
36 import org.mockito.Mockito;
37 import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager;
38 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
39 import org.openecomp.sdc.asdctool.impl.validator.report.ReportFileNioHelper;
40 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
41
42 class ReportManagerTest {
43
44     private static final String VERTEX_1_ID = "testID1";
45     private static final String TASK_1_FAILED_NAME = "testFailedTask1";
46     private static final String TASK_1_NAME = "testTask1";
47     private static final String VERTEX_2_ID = "testID2";
48     private static final String TASK_2_NAME = "testTask2";
49     private static final String TASK_2_FAILED_NAME = "testFailedTask2";
50     private static final String UNIQUE_ID = "uniqueID";
51     private static final String DUMMY_MESSAGE = "dummyMessage";
52     private static final String VALIDATOR_NAME = "testValidatorNamed";
53     private static final int COMPONENT_SUM = 0;
54
55     private static final String EXPECTED_CSV_HEADER =
56         "Vertex ID,Task Name,Success,Result Details,Result Description";
57     private static final String EXPECTED_OUTPUT_FILE_HEADER =
58         "-----------------------Validation Tool Results:-------------------------";
59     private static final String EXPECTED_OUTPUT_FILE_SUMMARY =
60         "-----------------------------------Validator Tool Summary-----------------------------------";
61
62     private final SortedSet<String> failedTasksNames =
63         new TreeSet<>(Arrays.asList(TASK_1_FAILED_NAME, TASK_2_FAILED_NAME));
64     private final SortedSet<String> successTasksNames =
65         new TreeSet<>(Arrays.asList(TASK_1_NAME, TASK_2_NAME));
66
67     private final VertexResult successResult = new VertexResult();
68
69     private static String csvReportFilePath;
70     private static String txtReportFilePath;
71
72     private final GraphVertex vertexScanned = Mockito.mock(GraphVertex.class);
73
74     @TempDir
75     static Path reportOutputPath;
76
77     @BeforeAll
78     static void beforeAll() {
79         csvReportFilePath = ValidationConfigManager.csvReportFilePath(reportOutputPath.toString(), System::currentTimeMillis);
80         txtReportFilePath = ValidationConfigManager.txtReportFilePath(reportOutputPath.toString());
81     }
82
83     @Test
84     void testReportTaskEnd() {
85         // when
86         Report report = Report.make();
87         report.addSuccess(VERTEX_1_ID, TASK_1_NAME, successResult);
88         report.addSuccess(VERTEX_2_ID, TASK_2_NAME, successResult);
89
90         List<String> reportCsvFile = ReportFileNioHelper.withCsvFile(csvReportFilePath, file -> {
91             file.printAllResults(report);
92             return ReportManagerHelper.readFileAsList(csvReportFilePath);
93         });
94
95         // then
96         assertNotNull(reportCsvFile);
97         assertEquals(EXPECTED_CSV_HEADER, reportCsvFile.get(0));
98         assertEquals(getCsvExpectedResult(VERTEX_1_ID, TASK_1_NAME), reportCsvFile.get(1));
99         assertEquals(getCsvExpectedResult(VERTEX_2_ID, TASK_2_NAME), reportCsvFile.get(2));
100     }
101
102     @Test
103     void testAddFailedVertex() {
104         // when
105         Report report = Report.make();
106         report.addFailure(TASK_1_NAME, VERTEX_1_ID);
107
108         List<String> reportTxtFile = ReportFileNioHelper.withTxtFile(txtReportFilePath, file -> {
109             file.reportEndOfToolRun(report);
110             return ReportFileNioHelper.readFileAsList(txtReportFilePath);
111         });
112
113         // then
114         assertNotNull(reportTxtFile);
115         assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportTxtFile.get(0));
116         assertEquals(EXPECTED_OUTPUT_FILE_SUMMARY, reportTxtFile.get(2));
117         assertEquals("Task: " + TASK_1_NAME, reportTxtFile.get(3));
118         assertEquals("FailedVertices: [" + VERTEX_1_ID + "]", reportTxtFile.get(4));
119     }
120
121     @Test
122     void testPrintValidationTaskStatus() {
123         // given
124         when(vertexScanned.getUniqueId()).thenReturn(UNIQUE_ID);
125
126         // when
127         List<String> reportTxtFile = ReportFileNioHelper.withTxtFile(txtReportFilePath, file -> {
128             file.printValidationTaskStatus(vertexScanned, TASK_1_NAME, false);
129             return ReportFileNioHelper.readFileAsList(txtReportFilePath);
130         });
131
132         // then
133         assertNotNull(reportTxtFile);
134         assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportTxtFile.get(0));
135         assertEquals("-----------------------Vertex: " + UNIQUE_ID + ", Task " + TASK_1_NAME
136              + " failed-----------------------", reportTxtFile.get(2));
137     }
138
139     @Test
140     void testWriteReportLineToFile() {
141         // when
142         List<String> reportTxtFile = ReportFileNioHelper.withTxtFile(txtReportFilePath, file -> {
143             file.writeReportLineToFile(DUMMY_MESSAGE);
144             return ReportFileNioHelper.readFileAsList(txtReportFilePath);
145         });
146
147         // then
148         assertNotNull(reportTxtFile);
149         assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportTxtFile.get(0));
150         assertEquals(DUMMY_MESSAGE, reportTxtFile.get(2));
151     }
152
153     @Test
154     void testReportValidatorTypeSummary() {
155         // when
156         List<String> reportTxtFile = ReportFileNioHelper.withTxtFile(txtReportFilePath, file -> {
157             file.reportValidatorTypeSummary(VALIDATOR_NAME, failedTasksNames, successTasksNames);
158             return ReportFileNioHelper.readFileAsList(txtReportFilePath);
159         });
160
161         // then
162         assertNotNull(reportTxtFile);
163         assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportTxtFile.get(0));
164         assertEquals("-----------------------ValidatorExecuter " + VALIDATOR_NAME
165                         + " Validation Summary-----------------------", reportTxtFile.get(2));
166         assertEquals("Failed tasks: [" + TASK_1_FAILED_NAME + ", " + TASK_2_FAILED_NAME + "]", reportTxtFile.get(3));
167         assertEquals("Success tasks: [" + TASK_1_NAME + ", " + TASK_2_NAME + "]", reportTxtFile.get(4));
168     }
169
170     @Test
171     void testReportStartValidatorRun() {
172         // when
173         List<String> reportTxtFile = ReportFileNioHelper.withTxtFile(txtReportFilePath, file -> {
174             file.reportStartValidatorRun(VALIDATOR_NAME, COMPONENT_SUM);
175             return ReportFileNioHelper.readFileAsList(txtReportFilePath);
176         });
177
178         // then
179         assertNotNull(reportTxtFile);
180         assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportTxtFile.get(0));
181         assertEquals("------ValidatorExecuter " + VALIDATOR_NAME + " Validation Started, on "
182             + COMPONENT_SUM + " components---------", reportTxtFile.get(2));
183     }
184
185     @Test
186     void testReportStartTaskRun() {
187         // given
188         when(vertexScanned.getUniqueId()).thenReturn(UNIQUE_ID);
189
190         // when
191         List<String> reportTxtFile = ReportFileNioHelper.withTxtFile(txtReportFilePath, file -> {
192             file.reportStartTaskRun(vertexScanned, TASK_1_NAME);
193             return ReportFileNioHelper.readFileAsList(txtReportFilePath);
194         });
195
196         // then
197         assertNotNull(reportTxtFile);
198         assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportTxtFile.get(0));
199         assertEquals("-----------------------Vertex: " + UNIQUE_ID + ", Task " + TASK_1_NAME
200             + " Started-----------------------", reportTxtFile.get(2));
201     }
202
203     private String getCsvExpectedResult(String vertexID, String taskID) {
204         return String.join(",", new String[]{vertexID, taskID,
205             String.valueOf(successResult.getStatus()), successResult.getResult()});
206     }
207 }