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.After;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager;
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;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
46 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
49 import java.util.ArrayList;
50 import java.util.HashMap;
51 import java.util.List;
53 import java.util.stream.IntStream;
55 import static org.junit.Assert.assertEquals;
56 import static org.junit.Assert.assertFalse;
57 import static org.junit.Assert.assertTrue;
58 import static org.mockito.ArgumentMatchers.any;
59 import static org.mockito.ArgumentMatchers.eq;
60 import static org.mockito.Mockito.when;
62 @RunWith(PowerMockRunner.class)
63 @PowerMockRunnerDelegate(MockitoJUnitRunner.class)
64 @PrepareForTest({ReportManager.class})
65 public class ArtifactValidationUtilsTest {
68 private ArtifactCassandraDao artifactCassandraDao;
70 private TopologyTemplateOperation topologyTemplateOperation;
72 private ArtifactValidationUtils testSubject;
75 private GraphVertex vertex;
77 private MapArtifactDataDefinition mapToscaDataDefinition;
79 private ArtifactDataDefinition artifactDataDefinition;
81 private ArtifactDataDefinition artifactDataDefinitionNotInCassandra;
83 private ArtifactDataDefinition artifactDataDefinitionDummy;
85 private TopologyTemplate topologyTemplate;
87 private static final String ES_ID = "testEsInCassandra";
88 private static final String ES_ID_NOT_IN_CASS = "testEsNotInCassandra";
89 private static final String TASK_NAME = "testTaskName";
90 private static final String UNIQUE_ID = "4321";
91 private static final String UNIQUE_ID_VERTEX = "321";
93 private final static String resourcePath = new File("src/test/resources").getAbsolutePath();
94 private final static String csvReportFilePath = ValidationConfigManager.DEFAULT_CSV_PATH;
95 private final static String txtReportFilePath = ValidationConfigManager.txtReportFilePath(resourcePath);
97 public void initReportManager() {
98 ReportManager.make(csvReportFilePath, txtReportFilePath);
102 public void setup() {
104 when(artifactCassandraDao.getCountOfArtifactById(ES_ID)).thenReturn(Either.left(1L));
105 when(artifactCassandraDao.getCountOfArtifactById(ES_ID_NOT_IN_CASS))
106 .thenReturn(Either.right(CassandraOperationStatus.NOT_FOUND));
108 when(artifactDataDefinition.getEsId()).thenReturn(ES_ID);
109 when(artifactDataDefinitionNotInCassandra.getEsId()).thenReturn(ES_ID_NOT_IN_CASS);
111 when(artifactDataDefinitionNotInCassandra.getUniqueId()).thenReturn(UNIQUE_ID);
112 when(vertex.getUniqueId()).thenReturn(UNIQUE_ID_VERTEX);
116 public void clean() {
117 ReportManagerHelper.cleanReports(csvReportFilePath, txtReportFilePath);
121 public void testValidateArtifactsAreInCassandra() {
123 List<ArtifactDataDefinition> artifacts = new ArrayList<>();
124 artifacts.add(artifactDataDefinition);
127 ArtifactsVertexResult result =
128 testSubject.validateArtifactsAreInCassandra(vertex, TASK_NAME, artifacts, txtReportFilePath);
130 List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath);
133 assertTrue(result.getStatus());
134 assertEquals(0, result.notFoundArtifacts.size());
135 assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(2));
139 public void testValidateArtifactsNotInCassandra() {
141 List<ArtifactDataDefinition> artifacts = new ArrayList<>();
142 artifacts.add(artifactDataDefinition);
143 artifacts.add(artifactDataDefinitionNotInCassandra);
146 ArtifactsVertexResult result =
147 testSubject.validateArtifactsAreInCassandra(vertex, TASK_NAME, artifacts, txtReportFilePath);
148 ReportManager.reportEndOfToolRun(csvReportFilePath, txtReportFilePath);
150 List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath);
153 assertFalse(result.getStatus());
154 assertEquals(1, result.notFoundArtifacts.size());
155 assertEquals(UNIQUE_ID, result.notFoundArtifacts.iterator().next());
157 assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(2));
158 assertEquals("Artifact " + ES_ID_NOT_IN_CASS + " doesn't exist in Cassandra",
159 reportOutputFile.get(3));
160 assertEquals("Task: " + TASK_NAME, reportOutputFile.get(5));
161 assertEquals("FailedVertices: [" + UNIQUE_ID_VERTEX + "]", reportOutputFile.get(6));
165 public void testIsArtifactsInCassandra() {
167 boolean notInCass = testSubject.isArtifactInCassandra(ES_ID_NOT_IN_CASS);
168 boolean inCass = testSubject.isArtifactInCassandra(ES_ID);
171 assertFalse(notInCass);
176 public void testAddRelevantArtifacts() {
178 Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
179 artifactsMap.put(ES_ID_NOT_IN_CASS, artifactDataDefinitionNotInCassandra);
180 artifactsMap.put(ES_ID, artifactDataDefinition);
183 List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
186 result.forEach(Assert::assertNotNull);
190 public void testAddRelevantArtifactsWithNullEsId() {
192 Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
193 artifactsMap.put("", artifactDataDefinitionDummy);
196 List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
199 assertEquals(0, result.size());
203 public void testValidateTopologyTemplateArtifacts() {
205 Map<String, ArtifactDataDefinition> artifacts = new HashMap<>();
206 artifacts.put(ES_ID, artifactDataDefinition);
208 when(topologyTemplate.getDeploymentArtifacts()).thenReturn(artifacts);
209 when(topologyTemplate.getArtifacts()).thenReturn(artifacts);
210 when(topologyTemplate.getServiceApiArtifacts()).thenReturn(artifacts);
212 when(mapToscaDataDefinition.getMapToscaDataDefinition()).thenReturn(artifacts);
213 Map<String, MapArtifactDataDefinition> artifactsMap = new HashMap<>();
214 artifactsMap.put(ES_ID, mapToscaDataDefinition);
216 when(topologyTemplate.getInstanceArtifacts()).thenReturn(artifactsMap);
217 when(topologyTemplate.getInstDeploymentArtifacts()).thenReturn(artifactsMap);
219 when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
220 .thenReturn(Either.left(topologyTemplate));
223 ArtifactsVertexResult result =
224 testSubject.validateTopologyTemplateArtifacts(vertex, TASK_NAME, txtReportFilePath);
226 List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath);
229 assertTrue(result.getStatus());
230 assertEquals(0, result.notFoundArtifacts.size());
232 IntStream.range(2, reportOutputFile.size()).forEach(
233 i -> assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(i)));
237 public void testValidateTopologyTemplateArtifactsNotFoundToscaElement() {
239 when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
240 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
243 ArtifactsVertexResult result =
244 testSubject.validateTopologyTemplateArtifacts(vertex, TASK_NAME, txtReportFilePath);
247 assertFalse(result.getStatus());