Fix unit tests with conflicting folder
[sdc.git] / asdctool / src / test / java / org / openecomp / sdc / asdctool / impl / validator / tasks / artifacts / ArtifactValidationUtilsTest.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.tasks.artifacts;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.when;
30 import static org.openecomp.sdc.asdctool.impl.validator.report.ReportFileNioHelper.readFileAsList;
31 import static org.openecomp.sdc.asdctool.impl.validator.report.ReportFileNioHelper.withTxtFile;
32
33 import fj.data.Either;
34 import java.nio.file.Path;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.stream.IntStream;
40 import org.junit.jupiter.api.Assertions;
41 import org.junit.jupiter.api.BeforeAll;
42 import org.junit.jupiter.api.BeforeEach;
43 import org.junit.jupiter.api.Test;
44 import org.junit.jupiter.api.io.TempDir;
45 import org.mockito.InjectMocks;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager;
49 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
50 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
51 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
52 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
53 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
54 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
55 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
56 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
57 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
58
59 class ArtifactValidationUtilsTest {
60
61     private static final String ES_ID = "testEsInCassandra";
62     private static final String ES_ID_NOT_IN_CASS = "testEsNotInCassandra";
63     private static final String TASK_NAME = "testTaskName";
64     private static final String UNIQUE_ID = "4321";
65     private static final String UNIQUE_ID_VERTEX = "321";
66     private static String txtReportFilePath;
67
68     @InjectMocks
69     private ArtifactValidationUtils testSubject;
70
71     @Mock
72     private ArtifactCassandraDao artifactCassandraDao;
73     @Mock
74     private TopologyTemplateOperation topologyTemplateOperation;
75     @Mock
76     private GraphVertex vertex;
77     @Mock
78     private MapArtifactDataDefinition mapToscaDataDefinition;
79     @Mock
80     private ArtifactDataDefinition artifactDataDefinition;
81     @Mock
82     private ArtifactDataDefinition artifactDataDefinitionNotInCassandra;
83     @Mock
84     private ArtifactDataDefinition artifactDataDefinitionDummy;
85     @Mock
86     private TopologyTemplate topologyTemplate;
87
88     @TempDir
89     static Path reportOutputPath;
90
91     @BeforeAll
92     static void beforeAll() {
93         txtReportFilePath = ValidationConfigManager.txtReportFilePath(reportOutputPath.toString());
94     }
95
96     @BeforeEach
97     public void initMocks() {
98         MockitoAnnotations.openMocks(this);
99         when(artifactCassandraDao.getCountOfArtifactById(ES_ID)).thenReturn(Either.left(1L));
100         when(artifactCassandraDao.getCountOfArtifactById(ES_ID_NOT_IN_CASS)).thenReturn(Either.right(CassandraOperationStatus.NOT_FOUND));
101
102         when(artifactDataDefinition.getEsId()).thenReturn(ES_ID);
103         when(artifactDataDefinitionNotInCassandra.getEsId()).thenReturn(ES_ID_NOT_IN_CASS);
104
105         when(artifactDataDefinitionNotInCassandra.getUniqueId()).thenReturn(UNIQUE_ID);
106         when(vertex.getUniqueId()).thenReturn(UNIQUE_ID_VERTEX);
107     }
108
109     @Test
110     void testValidateArtifactsAreInCassandra() {
111         // given
112         Report report = Report.make();
113         List<ArtifactDataDefinition> artifacts = new ArrayList<>();
114         artifacts.add(artifactDataDefinition);
115
116         // when
117         withTxtFile(txtReportFilePath, file -> {
118             ArtifactsVertexResult result =
119                 testSubject.validateArtifactsAreInCassandra(report, vertex, TASK_NAME, artifacts, file);
120
121             List<String> reportOutputFile = readFileAsList(txtReportFilePath);
122
123             // then
124             assertTrue(result.getStatus());
125             assertEquals(0, result.notFoundArtifacts.size());
126             assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(2));
127         });
128     }
129
130     @Test
131     void testValidateArtifactsNotInCassandra() {
132         // given
133         Report report = Report.make();
134         List<ArtifactDataDefinition> artifacts = new ArrayList<>();
135         artifacts.add(artifactDataDefinition);
136         artifacts.add(artifactDataDefinitionNotInCassandra);
137
138         // when
139         withTxtFile(txtReportFilePath, file -> {
140             ArtifactsVertexResult result =
141                 testSubject.validateArtifactsAreInCassandra(report, vertex, TASK_NAME, artifacts, file);
142             file.reportEndOfToolRun(report);
143
144             List<String> reportOutputFile = readFileAsList(txtReportFilePath);
145
146             // then
147             assertFalse(result.getStatus());
148             assertEquals(1, result.notFoundArtifacts.size());
149             assertEquals(UNIQUE_ID, result.notFoundArtifacts.iterator().next());
150
151             assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(2));
152             assertEquals("Artifact " + ES_ID_NOT_IN_CASS + " doesn't exist in Cassandra",
153                 reportOutputFile.get(3));
154             assertEquals("Task: " + TASK_NAME, reportOutputFile.get(5));
155             assertEquals("FailedVertices: [" + UNIQUE_ID_VERTEX + "]", reportOutputFile.get(6));
156         });
157     }
158
159     @Test
160     void testIsArtifactsInCassandra() {
161         // when
162         boolean notInCass = testSubject.isArtifactInCassandra(ES_ID_NOT_IN_CASS);
163         boolean inCass = testSubject.isArtifactInCassandra(ES_ID);
164
165         // then
166         assertFalse(notInCass);
167         assertTrue(inCass);
168     }
169
170     @Test
171     void testAddRelevantArtifacts() {
172         // given
173         Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
174         artifactsMap.put(ES_ID_NOT_IN_CASS, artifactDataDefinitionNotInCassandra);
175         artifactsMap.put(ES_ID, artifactDataDefinition);
176
177         // when
178         List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
179
180         // then
181         result.forEach(Assertions::assertNotNull);
182     }
183
184     @Test
185     void testAddRelevantArtifactsWithNullEsId() {
186         // given
187         Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
188         artifactsMap.put("", artifactDataDefinitionDummy);
189
190         // when
191         List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
192
193         // then
194         assertEquals(0, result.size());
195     }
196
197     @Test
198     void testValidateTopologyTemplateArtifacts() {
199         // given
200         Report report = Report.make();
201         Map<String, ArtifactDataDefinition> artifacts = new HashMap<>();
202         artifacts.put(ES_ID, artifactDataDefinition);
203
204         when(topologyTemplate.getDeploymentArtifacts()).thenReturn(artifacts);
205         when(topologyTemplate.getArtifacts()).thenReturn(artifacts);
206         when(topologyTemplate.getServiceApiArtifacts()).thenReturn(artifacts);
207
208         when(mapToscaDataDefinition.getMapToscaDataDefinition()).thenReturn(artifacts);
209         Map<String, MapArtifactDataDefinition> artifactsMap = new HashMap<>();
210         artifactsMap.put(ES_ID, mapToscaDataDefinition);
211
212         when(topologyTemplate.getInstanceArtifacts()).thenReturn(artifactsMap);
213         when(topologyTemplate.getInstDeploymentArtifacts()).thenReturn(artifactsMap);
214
215         when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
216             .thenReturn(Either.left(topologyTemplate));
217
218         // when
219         withTxtFile(txtReportFilePath, file -> {
220             ArtifactsVertexResult result =
221                 testSubject.validateTopologyTemplateArtifacts(report, vertex, TASK_NAME, file);
222
223             List<String> reportOutputFile = readFileAsList(txtReportFilePath);
224
225             // then
226             assertTrue(result.getStatus());
227             assertEquals(0, result.notFoundArtifacts.size());
228
229             IntStream.range(2, reportOutputFile.size()).forEach(
230                 i -> assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(i)));
231         });
232     }
233
234     @Test
235     void testValidateTopologyTemplateArtifactsNotFoundToscaElement() {
236         // given
237         Report report = Report.make();
238         when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
239             .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
240
241         // when
242         withTxtFile(txtReportFilePath, file -> {
243             ArtifactsVertexResult result =
244                 testSubject.validateTopologyTemplateArtifacts(report, vertex, TASK_NAME, file);
245             // then
246             assertFalse(result.getStatus());
247         });
248     }
249 }