af056103ffd9a7bd88c7a8aa6111253d2a132955
[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.io.File;
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.Test;
34 import org.mockito.Mockito;
35 import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager;
36 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
37 import org.openecomp.sdc.asdctool.impl.validator.report.ReportFileNioHelper;
38 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
39
40 public class ReportManagerTest {
41
42     private static final String VERTEX_1_ID = "testID1";
43     private static final String TASK_1_FAILED_NAME = "testFailedTask1";
44     private static final String TASK_1_NAME = "testTask1";
45     private static final String VERTEX_2_ID = "testID2";
46     private static final String TASK_2_NAME = "testTask2";
47     private static final String TASK_2_FAILED_NAME = "testFailedTask2";
48     private static final String UNIQUE_ID = "uniqueID";
49     private static final String DUMMY_MESSAGE = "dummyMessage";
50     private static final String VALIDATOR_NAME = "testValidatorNamed";
51     private static final int COMPONENT_SUM = 0;
52
53     private static final String EXPECTED_CSV_HEADER =
54         "Vertex ID,Task Name,Success,Result Details,Result Description";
55     private static final String EXPECTED_OUTPUT_FILE_HEADER =
56         "-----------------------Validation Tool Results:-------------------------";
57     private static final String EXPECTED_OUTPUT_FILE_SUMMARY =
58         "-----------------------------------Validator Tool Summary-----------------------------------";
59
60     private final SortedSet<String> failedTasksNames =
61         new TreeSet<>(Arrays.asList(TASK_1_FAILED_NAME, TASK_2_FAILED_NAME));
62     private final SortedSet<String> successTasksNames =
63         new TreeSet<>(Arrays.asList(TASK_1_NAME, TASK_2_NAME));
64
65     private final VertexResult successResult = new VertexResult();
66
67     private final static String resourcePath = new File("src/test/resources").getAbsolutePath();
68     private final static String csvReportFilePath = ValidationConfigManager
69         .csvReportFilePath(resourcePath, System::currentTimeMillis);
70     private final static String txtReportFilePath = ValidationConfigManager.txtReportFilePath(resourcePath);
71
72     private final GraphVertex vertexScanned = Mockito.mock(GraphVertex.class);
73
74     @Test
75     public void testReportTaskEnd() {
76         // when
77         Report report = Report.make();
78         report.addSuccess(VERTEX_1_ID, TASK_1_NAME, successResult);
79         report.addSuccess(VERTEX_2_ID, TASK_2_NAME, successResult);
80
81         List<String> reportCsvFile = ReportFileNioHelper.withCsvFile(csvReportFilePath, file -> {
82             file.printAllResults(report);
83             return ReportManagerHelper.readFileAsList(csvReportFilePath);
84         });
85
86         // then
87         assertNotNull(reportCsvFile);
88         assertEquals(EXPECTED_CSV_HEADER, reportCsvFile.get(0));
89         assertEquals(getCsvExpectedResult(VERTEX_1_ID, TASK_1_NAME), reportCsvFile.get(1));
90         assertEquals(getCsvExpectedResult(VERTEX_2_ID, TASK_2_NAME), reportCsvFile.get(2));
91     }
92
93     @Test
94     public void testAddFailedVertex() {
95         // when
96         Report report = Report.make();
97         report.addFailure(TASK_1_NAME, VERTEX_1_ID);
98
99         List<String> reportTxtFile = ReportFileNioHelper.withTxtFile(txtReportFilePath, file -> {
100             file.reportEndOfToolRun(report);
101             return ReportFileNioHelper.readFileAsList(txtReportFilePath);
102         });
103
104         // then
105         assertNotNull(reportTxtFile);
106         assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportTxtFile.get(0));
107         assertEquals(EXPECTED_OUTPUT_FILE_SUMMARY, reportTxtFile.get(2));
108         assertEquals("Task: " + TASK_1_NAME, reportTxtFile.get(3));
109         assertEquals("FailedVertices: [" + VERTEX_1_ID + "]", reportTxtFile.get(4));
110     }
111
112     @Test
113     public void testPrintValidationTaskStatus() {
114         // given
115         when(vertexScanned.getUniqueId()).thenReturn(UNIQUE_ID);
116
117         // when
118         List<String> reportTxtFile = ReportFileNioHelper.withTxtFile(txtReportFilePath, file -> {
119             file.printValidationTaskStatus(vertexScanned, TASK_1_NAME, false);
120             return ReportFileNioHelper.readFileAsList(txtReportFilePath);
121         });
122
123         // then
124         assertNotNull(reportTxtFile);
125         assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportTxtFile.get(0));
126         assertEquals("-----------------------Vertex: " + UNIQUE_ID + ", Task " + TASK_1_NAME
127              + " failed-----------------------", reportTxtFile.get(2));
128     }
129
130     @Test
131     public void testWriteReportLineToFile() {
132         // when
133         List<String> reportTxtFile = ReportFileNioHelper.withTxtFile(txtReportFilePath, file -> {
134             file.writeReportLineToFile(DUMMY_MESSAGE);
135             return ReportFileNioHelper.readFileAsList(txtReportFilePath);
136         });
137
138         // then
139         assertNotNull(reportTxtFile);
140         assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportTxtFile.get(0));
141         assertEquals(DUMMY_MESSAGE, reportTxtFile.get(2));
142     }
143
144     @Test
145     public void testReportValidatorTypeSummary() {
146         // when
147         List<String> reportTxtFile = ReportFileNioHelper.withTxtFile(txtReportFilePath, file -> {
148             file.reportValidatorTypeSummary(VALIDATOR_NAME, failedTasksNames, successTasksNames);
149             return ReportFileNioHelper.readFileAsList(txtReportFilePath);
150         });
151
152         // then
153         assertNotNull(reportTxtFile);
154         assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportTxtFile.get(0));
155         assertEquals("-----------------------ValidatorExecuter " + VALIDATOR_NAME
156                         + " Validation Summary-----------------------", reportTxtFile.get(2));
157         assertEquals("Failed tasks: [" + TASK_1_FAILED_NAME + ", " + TASK_2_FAILED_NAME + "]", reportTxtFile.get(3));
158         assertEquals("Success tasks: [" + TASK_1_NAME + ", " + TASK_2_NAME + "]", reportTxtFile.get(4));
159     }
160
161     @Test
162     public void testReportStartValidatorRun() {
163         // when
164         List<String> reportTxtFile = ReportFileNioHelper.withTxtFile(txtReportFilePath, file -> {
165             file.reportStartValidatorRun(VALIDATOR_NAME, COMPONENT_SUM);
166             return ReportFileNioHelper.readFileAsList(txtReportFilePath);
167         });
168
169         // then
170         assertNotNull(reportTxtFile);
171         assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportTxtFile.get(0));
172         assertEquals("------ValidatorExecuter " + VALIDATOR_NAME + " Validation Started, on "
173             + COMPONENT_SUM + " components---------", reportTxtFile.get(2));
174     }
175
176     @Test
177     public void testReportStartTaskRun() {
178         // given
179         when(vertexScanned.getUniqueId()).thenReturn(UNIQUE_ID);
180
181         // when
182         List<String> reportTxtFile = ReportFileNioHelper.withTxtFile(txtReportFilePath, file -> {
183             file.reportStartTaskRun(vertexScanned, TASK_1_NAME);
184             return ReportFileNioHelper.readFileAsList(txtReportFilePath);
185         });
186
187         // then
188         assertNotNull(reportTxtFile);
189         assertEquals(EXPECTED_OUTPUT_FILE_HEADER, reportTxtFile.get(0));
190         assertEquals("-----------------------Vertex: " + UNIQUE_ID + ", Task " + TASK_1_NAME
191             + " Started-----------------------", reportTxtFile.get(2));
192     }
193
194     private String getCsvExpectedResult(String vertexID, String taskID) {
195         return String.join(",", new String[]{vertexID, taskID,
196             String.valueOf(successResult.getStatus()), successResult.getResult()});
197     }
198 }