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;
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.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;
60 public class ArtifactValidationUtilsTest {
63 private ArtifactCassandraDao artifactCassandraDao;
65 private TopologyTemplateOperation topologyTemplateOperation;
67 private ArtifactValidationUtils testSubject;
70 private GraphVertex vertex;
72 private MapArtifactDataDefinition mapToscaDataDefinition;
74 private ArtifactDataDefinition artifactDataDefinition;
76 private ArtifactDataDefinition artifactDataDefinitionNotInCassandra;
78 private ArtifactDataDefinition artifactDataDefinitionDummy;
80 private TopologyTemplate topologyTemplate;
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";
88 private final static String resourcePath = new File("src/test/resources").getAbsolutePath();
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(txtReportFilePath);
110 public void clean() {
111 ReportManagerHelper.cleanReports(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 withTxtFile(txtReportFilePath, file -> {
143 ArtifactsVertexResult result =
144 testSubject.validateArtifactsAreInCassandra(report, vertex, TASK_NAME, artifacts, txtReportFilePath);
145 file.reportEndOfToolRun(report);
147 List<String> reportOutputFile = readFileAsList(txtReportFilePath);
150 assertFalse(result.getStatus());
151 assertEquals(1, result.notFoundArtifacts.size());
152 assertEquals(UNIQUE_ID, result.notFoundArtifacts.iterator().next());
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));
163 public void testIsArtifactsInCassandra() {
165 boolean notInCass = testSubject.isArtifactInCassandra(ES_ID_NOT_IN_CASS);
166 boolean inCass = testSubject.isArtifactInCassandra(ES_ID);
169 assertFalse(notInCass);
174 public void testAddRelevantArtifacts() {
176 Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
177 artifactsMap.put(ES_ID_NOT_IN_CASS, artifactDataDefinitionNotInCassandra);
178 artifactsMap.put(ES_ID, artifactDataDefinition);
181 List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
184 result.forEach(Assertions::assertNotNull);
188 public void testAddRelevantArtifactsWithNullEsId() {
190 Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
191 artifactsMap.put("", artifactDataDefinitionDummy);
194 List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
197 assertEquals(0, result.size());
201 public void testValidateTopologyTemplateArtifacts() {
203 Report report = Report.make();
204 Map<String, ArtifactDataDefinition> artifacts = new HashMap<>();
205 artifacts.put(ES_ID, artifactDataDefinition);
207 when(topologyTemplate.getDeploymentArtifacts()).thenReturn(artifacts);
208 when(topologyTemplate.getArtifacts()).thenReturn(artifacts);
209 when(topologyTemplate.getServiceApiArtifacts()).thenReturn(artifacts);
211 when(mapToscaDataDefinition.getMapToscaDataDefinition()).thenReturn(artifacts);
212 Map<String, MapArtifactDataDefinition> artifactsMap = new HashMap<>();
213 artifactsMap.put(ES_ID, mapToscaDataDefinition);
215 when(topologyTemplate.getInstanceArtifacts()).thenReturn(artifactsMap);
216 when(topologyTemplate.getInstDeploymentArtifacts()).thenReturn(artifactsMap);
218 when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
219 .thenReturn(Either.left(topologyTemplate));
222 ArtifactsVertexResult result =
223 testSubject.validateTopologyTemplateArtifacts(report, vertex, TASK_NAME, txtReportFilePath);
225 List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath);
228 assertTrue(result.getStatus());
229 assertEquals(0, result.notFoundArtifacts.size());
231 IntStream.range(2, reportOutputFile.size()).forEach(
232 i -> assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(i)));
236 public void testValidateTopologyTemplateArtifactsNotFoundToscaElement() {
238 Report report = Report.make();
239 when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
240 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
243 ArtifactsVertexResult result =
244 testSubject.validateTopologyTemplateArtifacts(report, vertex, TASK_NAME, txtReportFilePath);
247 assertFalse(result.getStatus());