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