0399dbed3852441046e28de226cb4cd5122d8446
[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.utils.ReportManager;
34 import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManagerHelper;
35 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
36 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
37 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
38 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
39 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
40 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
41 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
42 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
43
44 import java.io.File;
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.stream.IntStream;
50
51 import static org.junit.jupiter.api.Assertions.assertEquals;
52 import static org.junit.jupiter.api.Assertions.assertFalse;
53 import static org.junit.jupiter.api.Assertions.assertTrue;
54 import static org.mockito.ArgumentMatchers.any;
55 import static org.mockito.ArgumentMatchers.eq;
56 import static org.mockito.Mockito.when;
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 csvReportFilePath = ValidationConfigManager.DEFAULT_CSV_PATH;
88     private final static String txtReportFilePath = ValidationConfigManager.txtReportFilePath(resourcePath);
89
90     ArtifactValidationUtilsTest () {
91         MockitoAnnotations.initMocks(this);
92         when(artifactCassandraDao.getCountOfArtifactById(ES_ID)).thenReturn(Either.left(1L));
93         when(artifactCassandraDao.getCountOfArtifactById(ES_ID_NOT_IN_CASS))
94             .thenReturn(Either.right(CassandraOperationStatus.NOT_FOUND));
95
96         when(artifactDataDefinition.getEsId()).thenReturn(ES_ID);
97         when(artifactDataDefinitionNotInCassandra.getEsId()).thenReturn(ES_ID_NOT_IN_CASS);
98
99         when(artifactDataDefinitionNotInCassandra.getUniqueId()).thenReturn(UNIQUE_ID);
100         when(vertex.getUniqueId()).thenReturn(UNIQUE_ID_VERTEX);
101     }
102
103     @BeforeEach
104     public void setup() {
105         ReportManager.make(csvReportFilePath, txtReportFilePath);
106     }
107
108     @AfterEach
109     public void clean() {
110         ReportManagerHelper.cleanReports(csvReportFilePath, txtReportFilePath);
111     }
112
113     @Test
114     public void testValidateArtifactsAreInCassandra() {
115         // given
116         List<ArtifactDataDefinition> artifacts = new ArrayList<>();
117         artifacts.add(artifactDataDefinition);
118
119         // when
120         ArtifactsVertexResult result =
121             testSubject.validateArtifactsAreInCassandra(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         List<ArtifactDataDefinition> artifacts = new ArrayList<>();
135         artifacts.add(artifactDataDefinition);
136         artifacts.add(artifactDataDefinitionNotInCassandra);
137
138         // when
139         ArtifactsVertexResult result =
140             testSubject.validateArtifactsAreInCassandra(vertex, TASK_NAME, artifacts, txtReportFilePath);
141         ReportManager.reportEndOfToolRun(csvReportFilePath, txtReportFilePath);
142
143         List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath);
144
145         // then
146         assertFalse(result.getStatus());
147         assertEquals(1, result.notFoundArtifacts.size());
148         assertEquals(UNIQUE_ID, result.notFoundArtifacts.iterator().next());
149
150         assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(2));
151         assertEquals("Artifact " + ES_ID_NOT_IN_CASS + " doesn't exist in Cassandra",
152             reportOutputFile.get(3));
153         assertEquals("Task: " + TASK_NAME, reportOutputFile.get(5));
154         assertEquals("FailedVertices: [" + UNIQUE_ID_VERTEX + "]", reportOutputFile.get(6));
155     }
156
157     @Test
158     public void testIsArtifactsInCassandra() {
159         // when
160         boolean notInCass = testSubject.isArtifactInCassandra(ES_ID_NOT_IN_CASS);
161         boolean inCass = testSubject.isArtifactInCassandra(ES_ID);
162
163         // then
164         assertFalse(notInCass);
165         assertTrue(inCass);
166     }
167
168     @Test
169     public void testAddRelevantArtifacts() {
170         // given
171         Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
172         artifactsMap.put(ES_ID_NOT_IN_CASS, artifactDataDefinitionNotInCassandra);
173         artifactsMap.put(ES_ID, artifactDataDefinition);
174
175         // when
176         List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
177
178         // then
179         result.forEach(Assertions::assertNotNull);
180     }
181
182     @Test
183     public void testAddRelevantArtifactsWithNullEsId() {
184         // given
185         Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
186         artifactsMap.put("", artifactDataDefinitionDummy);
187
188         // when
189         List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
190
191         // then
192         assertEquals(0, result.size());
193     }
194
195     @Test
196     public void testValidateTopologyTemplateArtifacts() {
197         // given
198         Map<String, ArtifactDataDefinition> artifacts = new HashMap<>();
199         artifacts.put(ES_ID, artifactDataDefinition);
200
201         when(topologyTemplate.getDeploymentArtifacts()).thenReturn(artifacts);
202         when(topologyTemplate.getArtifacts()).thenReturn(artifacts);
203         when(topologyTemplate.getServiceApiArtifacts()).thenReturn(artifacts);
204
205         when(mapToscaDataDefinition.getMapToscaDataDefinition()).thenReturn(artifacts);
206         Map<String, MapArtifactDataDefinition> artifactsMap = new HashMap<>();
207         artifactsMap.put(ES_ID, mapToscaDataDefinition);
208
209         when(topologyTemplate.getInstanceArtifacts()).thenReturn(artifactsMap);
210         when(topologyTemplate.getInstDeploymentArtifacts()).thenReturn(artifactsMap);
211
212         when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
213             .thenReturn(Either.left(topologyTemplate));
214
215         // when
216         ArtifactsVertexResult result =
217             testSubject.validateTopologyTemplateArtifacts(vertex, TASK_NAME, txtReportFilePath);
218
219         List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath);
220
221         // then
222         assertTrue(result.getStatus());
223         assertEquals(0, result.notFoundArtifacts.size());
224
225         IntStream.range(2, reportOutputFile.size()).forEach(
226             i -> assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(i)));
227     }
228
229     @Test
230     public void testValidateTopologyTemplateArtifactsNotFoundToscaElement() {
231         // given
232         when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
233             .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
234
235         // when
236         ArtifactsVertexResult result =
237             testSubject.validateTopologyTemplateArtifacts(vertex, TASK_NAME, txtReportFilePath);
238
239         // then
240         assertFalse(result.getStatus());
241     }
242 }