2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts;
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;
46 import java.util.ArrayList;
47 import java.util.HashMap;
48 import java.util.List;
50 import java.util.stream.IntStream;
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;
59 public class ArtifactValidationUtilsTest {
62 private ArtifactCassandraDao artifactCassandraDao;
64 private TopologyTemplateOperation topologyTemplateOperation;
66 private ArtifactValidationUtils testSubject;
69 private GraphVertex vertex;
71 private MapArtifactDataDefinition mapToscaDataDefinition;
73 private ArtifactDataDefinition artifactDataDefinition;
75 private ArtifactDataDefinition artifactDataDefinitionNotInCassandra;
77 private ArtifactDataDefinition artifactDataDefinitionDummy;
79 private TopologyTemplate topologyTemplate;
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";
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);
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));
97 when(artifactDataDefinition.getEsId()).thenReturn(ES_ID);
98 when(artifactDataDefinitionNotInCassandra.getEsId()).thenReturn(ES_ID_NOT_IN_CASS);
100 when(artifactDataDefinitionNotInCassandra.getUniqueId()).thenReturn(UNIQUE_ID);
101 when(vertex.getUniqueId()).thenReturn(UNIQUE_ID_VERTEX);
105 public void setup() {
106 ReportManager.make(csvReportFilePath, txtReportFilePath);
110 public void clean() {
111 ReportManagerHelper.cleanReports(csvReportFilePath, txtReportFilePath);
115 public void testValidateArtifactsAreInCassandra() {
117 Report report = Report.make();
118 List<ArtifactDataDefinition> artifacts = new ArrayList<>();
119 artifacts.add(artifactDataDefinition);
122 ArtifactsVertexResult result =
123 testSubject.validateArtifactsAreInCassandra(report, vertex, TASK_NAME, artifacts, txtReportFilePath);
125 List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath);
128 assertTrue(result.getStatus());
129 assertEquals(0, result.notFoundArtifacts.size());
130 assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(2));
134 public void testValidateArtifactsNotInCassandra() {
136 Report report = Report.make();
137 List<ArtifactDataDefinition> artifacts = new ArrayList<>();
138 artifacts.add(artifactDataDefinition);
139 artifacts.add(artifactDataDefinitionNotInCassandra);
142 ArtifactsVertexResult result =
143 testSubject.validateArtifactsAreInCassandra(report, vertex, TASK_NAME, artifacts, txtReportFilePath);
144 ReportManager.reportEndOfToolRun(report, csvReportFilePath, txtReportFilePath);
146 List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath);
149 assertFalse(result.getStatus());
150 assertEquals(1, result.notFoundArtifacts.size());
151 assertEquals(UNIQUE_ID, result.notFoundArtifacts.iterator().next());
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));
161 public void testIsArtifactsInCassandra() {
163 boolean notInCass = testSubject.isArtifactInCassandra(ES_ID_NOT_IN_CASS);
164 boolean inCass = testSubject.isArtifactInCassandra(ES_ID);
167 assertFalse(notInCass);
172 public void testAddRelevantArtifacts() {
174 Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
175 artifactsMap.put(ES_ID_NOT_IN_CASS, artifactDataDefinitionNotInCassandra);
176 artifactsMap.put(ES_ID, artifactDataDefinition);
179 List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
182 result.forEach(Assertions::assertNotNull);
186 public void testAddRelevantArtifactsWithNullEsId() {
188 Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
189 artifactsMap.put("", artifactDataDefinitionDummy);
192 List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
195 assertEquals(0, result.size());
199 public void testValidateTopologyTemplateArtifacts() {
201 Report report = Report.make();
202 Map<String, ArtifactDataDefinition> artifacts = new HashMap<>();
203 artifacts.put(ES_ID, artifactDataDefinition);
205 when(topologyTemplate.getDeploymentArtifacts()).thenReturn(artifacts);
206 when(topologyTemplate.getArtifacts()).thenReturn(artifacts);
207 when(topologyTemplate.getServiceApiArtifacts()).thenReturn(artifacts);
209 when(mapToscaDataDefinition.getMapToscaDataDefinition()).thenReturn(artifacts);
210 Map<String, MapArtifactDataDefinition> artifactsMap = new HashMap<>();
211 artifactsMap.put(ES_ID, mapToscaDataDefinition);
213 when(topologyTemplate.getInstanceArtifacts()).thenReturn(artifactsMap);
214 when(topologyTemplate.getInstDeploymentArtifacts()).thenReturn(artifactsMap);
216 when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
217 .thenReturn(Either.left(topologyTemplate));
220 ArtifactsVertexResult result =
221 testSubject.validateTopologyTemplateArtifacts(report, vertex, TASK_NAME, txtReportFilePath);
223 List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath);
226 assertTrue(result.getStatus());
227 assertEquals(0, result.notFoundArtifacts.size());
229 IntStream.range(2, reportOutputFile.size()).forEach(
230 i -> assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(i)));
234 public void testValidateTopologyTemplateArtifactsNotFoundToscaElement() {
236 Report report = Report.make();
237 when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
238 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
241 ArtifactsVertexResult result =
242 testSubject.validateTopologyTemplateArtifacts(report, vertex, TASK_NAME, txtReportFilePath);
245 assertFalse(result.getStatus());