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