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