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 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;
33 import fj.data.Either;
34 import java.nio.file.Path;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
39 import java.util.stream.IntStream;
40 import org.junit.jupiter.api.Assertions;
41 import org.junit.jupiter.api.BeforeAll;
42 import org.junit.jupiter.api.BeforeEach;
43 import org.junit.jupiter.api.Test;
44 import org.junit.jupiter.api.io.TempDir;
45 import org.mockito.InjectMocks;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager;
49 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
50 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
51 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
52 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
53 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
54 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
55 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
56 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
57 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
59 class ArtifactValidationUtilsTest {
61 private static final String ES_ID = "testEsInCassandra";
62 private static final String ES_ID_NOT_IN_CASS = "testEsNotInCassandra";
63 private static final String TASK_NAME = "testTaskName";
64 private static final String UNIQUE_ID = "4321";
65 private static final String UNIQUE_ID_VERTEX = "321";
66 private static String txtReportFilePath;
69 private ArtifactValidationUtils testSubject;
72 private ArtifactCassandraDao artifactCassandraDao;
74 private TopologyTemplateOperation topologyTemplateOperation;
76 private GraphVertex vertex;
78 private MapArtifactDataDefinition mapToscaDataDefinition;
80 private ArtifactDataDefinition artifactDataDefinition;
82 private ArtifactDataDefinition artifactDataDefinitionNotInCassandra;
84 private ArtifactDataDefinition artifactDataDefinitionDummy;
86 private TopologyTemplate topologyTemplate;
89 static Path reportOutputPath;
92 static void beforeAll() {
93 txtReportFilePath = ValidationConfigManager.txtReportFilePath(reportOutputPath.toString());
97 public void initMocks() {
98 MockitoAnnotations.openMocks(this);
99 when(artifactCassandraDao.getCountOfArtifactById(ES_ID)).thenReturn(Either.left(1L));
100 when(artifactCassandraDao.getCountOfArtifactById(ES_ID_NOT_IN_CASS)).thenReturn(Either.right(CassandraOperationStatus.NOT_FOUND));
102 when(artifactDataDefinition.getEsId()).thenReturn(ES_ID);
103 when(artifactDataDefinitionNotInCassandra.getEsId()).thenReturn(ES_ID_NOT_IN_CASS);
105 when(artifactDataDefinitionNotInCassandra.getUniqueId()).thenReturn(UNIQUE_ID);
106 when(vertex.getUniqueId()).thenReturn(UNIQUE_ID_VERTEX);
110 void testValidateArtifactsAreInCassandra() {
112 Report report = Report.make();
113 List<ArtifactDataDefinition> artifacts = new ArrayList<>();
114 artifacts.add(artifactDataDefinition);
117 withTxtFile(txtReportFilePath, file -> {
118 ArtifactsVertexResult result =
119 testSubject.validateArtifactsAreInCassandra(report, vertex, TASK_NAME, artifacts, file);
121 List<String> reportOutputFile = readFileAsList(txtReportFilePath);
124 assertTrue(result.getStatus());
125 assertEquals(0, result.notFoundArtifacts.size());
126 assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(2));
131 void testValidateArtifactsNotInCassandra() {
133 Report report = Report.make();
134 List<ArtifactDataDefinition> artifacts = new ArrayList<>();
135 artifacts.add(artifactDataDefinition);
136 artifacts.add(artifactDataDefinitionNotInCassandra);
139 withTxtFile(txtReportFilePath, file -> {
140 ArtifactsVertexResult result =
141 testSubject.validateArtifactsAreInCassandra(report, vertex, TASK_NAME, artifacts, file);
142 file.reportEndOfToolRun(report);
144 List<String> reportOutputFile = readFileAsList(txtReportFilePath);
147 assertFalse(result.getStatus());
148 assertEquals(1, result.notFoundArtifacts.size());
149 assertEquals(UNIQUE_ID, result.notFoundArtifacts.iterator().next());
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));
160 void testIsArtifactsInCassandra() {
162 boolean notInCass = testSubject.isArtifactInCassandra(ES_ID_NOT_IN_CASS);
163 boolean inCass = testSubject.isArtifactInCassandra(ES_ID);
166 assertFalse(notInCass);
171 void testAddRelevantArtifacts() {
173 Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
174 artifactsMap.put(ES_ID_NOT_IN_CASS, artifactDataDefinitionNotInCassandra);
175 artifactsMap.put(ES_ID, artifactDataDefinition);
178 List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
181 result.forEach(Assertions::assertNotNull);
185 void testAddRelevantArtifactsWithNullEsId() {
187 Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
188 artifactsMap.put("", artifactDataDefinitionDummy);
191 List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
194 assertEquals(0, result.size());
198 void testValidateTopologyTemplateArtifacts() {
200 Report report = Report.make();
201 Map<String, ArtifactDataDefinition> artifacts = new HashMap<>();
202 artifacts.put(ES_ID, artifactDataDefinition);
204 when(topologyTemplate.getDeploymentArtifacts()).thenReturn(artifacts);
205 when(topologyTemplate.getArtifacts()).thenReturn(artifacts);
206 when(topologyTemplate.getServiceApiArtifacts()).thenReturn(artifacts);
208 when(mapToscaDataDefinition.getMapToscaDataDefinition()).thenReturn(artifacts);
209 Map<String, MapArtifactDataDefinition> artifactsMap = new HashMap<>();
210 artifactsMap.put(ES_ID, mapToscaDataDefinition);
212 when(topologyTemplate.getInstanceArtifacts()).thenReturn(artifactsMap);
213 when(topologyTemplate.getInstDeploymentArtifacts()).thenReturn(artifactsMap);
215 when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
216 .thenReturn(Either.left(topologyTemplate));
219 withTxtFile(txtReportFilePath, file -> {
220 ArtifactsVertexResult result =
221 testSubject.validateTopologyTemplateArtifacts(report, vertex, TASK_NAME, file);
223 List<String> reportOutputFile = readFileAsList(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)));
235 void testValidateTopologyTemplateArtifactsNotFoundToscaElement() {
237 Report report = Report.make();
238 when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
239 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
242 withTxtFile(txtReportFilePath, file -> {
243 ArtifactsVertexResult result =
244 testSubject.validateTopologyTemplateArtifacts(report, vertex, TASK_NAME, file);
246 assertFalse(result.getStatus());