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