2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.tosca;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertThrows;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static org.junit.jupiter.api.Assertions.fail;
31 import static org.openecomp.sdc.be.tosca.ComponentCache.MergeStrategy.overwriteIfSameVersions;
32 import static org.openecomp.sdc.be.tosca.ComponentCache.entry;
34 import fj.data.Either;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.nio.ByteBuffer;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.HashMap;
45 import java.util.HashSet;
46 import java.util.List;
48 import java.util.Map.Entry;
49 import java.util.Objects;
50 import java.util.Optional;
52 import java.util.zip.ZipEntry;
53 import java.util.zip.ZipOutputStream;
54 import mockit.Deencapsulation;
55 import org.apache.commons.collections.CollectionUtils;
56 import org.apache.commons.io.IOUtils;
57 import org.apache.commons.io.output.ByteArrayOutputStream;
58 import org.apache.commons.lang3.tuple.ImmutablePair;
59 import org.apache.commons.lang3.tuple.ImmutableTriple;
60 import org.apache.commons.lang3.tuple.Triple;
61 import org.junit.jupiter.api.BeforeAll;
62 import org.junit.jupiter.api.BeforeEach;
63 import org.junit.jupiter.api.Test;
64 import org.mockito.InjectMocks;
65 import org.mockito.Mock;
66 import org.mockito.Mockito;
67 import org.mockito.MockitoAnnotations;
68 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
69 import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo;
70 import org.openecomp.sdc.be.config.ConfigurationManager;
71 import org.openecomp.sdc.be.dao.api.ActionStatus;
72 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
73 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
74 import org.openecomp.sdc.be.dao.cassandra.SdcSchemaFilesCassandraDao;
75 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
76 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
77 import org.openecomp.sdc.be.impl.ComponentsUtils;
78 import org.openecomp.sdc.be.model.ArtifactDefinition;
79 import org.openecomp.sdc.be.model.Component;
80 import org.openecomp.sdc.be.model.ComponentInstance;
81 import org.openecomp.sdc.be.model.LifecycleStateEnum;
82 import org.openecomp.sdc.be.model.Resource;
83 import org.openecomp.sdc.be.model.Service;
84 import org.openecomp.sdc.be.model.User;
85 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
86 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
87 import org.openecomp.sdc.be.resources.data.DAOArtifactData;
88 import org.openecomp.sdc.be.resources.data.SdcSchemaFilesData;
89 import org.openecomp.sdc.be.tosca.ComponentCache.CacheEntry;
90 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
91 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
92 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
93 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
94 import org.openecomp.sdc.common.api.ConfigurationSource;
95 import org.openecomp.sdc.common.impl.ExternalConfiguration;
96 import org.openecomp.sdc.common.impl.FSConfigurationSource;
97 import org.openecomp.sdc.common.test.BaseConfDependent;
98 import org.openecomp.sdc.exception.ResponseFormat;
100 class CsarUtilsTest extends BaseConfDependent {
103 CsarUtils testSubject;
106 private ArtifactCassandraDao artifactCassandraDao;
109 private ComponentsUtils componentsUtils;
112 private ToscaExportHandler toscaExportUtils;
115 private SdcSchemaFilesCassandraDao sdcSchemaFilesCassandraDao;
118 private ToscaOperationFacade toscaOperationFacade;
121 private ArtifactsBusinessLogic artifactsBusinessLogic;
123 private final List<String> nodesFromPackage = Arrays.asList("tosca.nodes.Root", "tosca.nodes.Container.Application");
125 private final byte[] contentData;
127 public CsarUtilsTest() throws IOException {
128 contentData = getFileResource("yamlValidation/resource-serviceTemplate.yml");
132 public static void setupBeforeClass() {
133 componentName = "catalog-be";
134 confPath = "src/test/resources/config";
139 public void setUpMock() {
140 ExternalConfiguration.setAppName("catalog-be");
141 MockitoAnnotations.openMocks(this);
142 initConfigurationManager();
145 private static void initConfigurationManager() {
146 final String confPath = new File(Objects
148 CsarUtilsTest.class.getClassLoader().getResource("config/catalog-be/configuration.yaml"))
149 .getFile()).getParent();
150 final ConfigurationSource confSource =
151 new FSConfigurationSource(ExternalConfiguration.getChangeListener(), confPath);
152 new ConfigurationManager(confSource);
155 private NonMetaArtifactInfo createNonMetaArtifactInfoTestSubject() {
156 return new CsarUtils.NonMetaArtifactInfo("mock", "mock", ArtifactTypeEnum.AAI_SERVICE_MODEL.getType(),
157 ArtifactGroupTypeEnum.DEPLOYMENT, new byte[0], "mock", true);
161 void testCreateCsar() {
162 Component component = new Resource();
163 Map<String, ArtifactDefinition> artifactDefinitionHashMap = new HashMap<>();
164 ArtifactDefinition artifact = new ArtifactDefinition();
165 artifact.setArtifactName("artifactName");
166 artifact.setEsId("esId");
167 artifactDefinitionHashMap.put("assettoscatemplate", artifact);
169 component.setToscaArtifacts(artifactDefinitionHashMap);
170 component.setArtifacts(artifactDefinitionHashMap);
171 component.setDeploymentArtifacts(artifactDefinitionHashMap);
173 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
174 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
176 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
177 .thenReturn(ActionStatus.GENERAL_ERROR);
179 testSubject.createCsar(component, true, true);
183 void testCreateCsarWithGenerateCsarZipResponseIsLeft() {
184 Component component = new Resource();
185 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
186 ArtifactDefinition artifact = new ArtifactDefinition();
187 artifact.setArtifactName("artifactName");
188 artifact.setEsId("esId");
189 artifact.setArtifactUUID("artifactUUID");
190 artifact.setArtifactType("YANG");
191 toscaArtifacts.put("assettoscatemplate", artifact);
193 component.setToscaArtifacts(toscaArtifacts);
194 component.setDeploymentArtifacts(toscaArtifacts);
195 component.setArtifacts(toscaArtifacts);
196 DAOArtifactData artifactData = new DAOArtifactData();
197 byte[] data = "value".getBytes();
198 ByteBuffer bufferData = ByteBuffer.wrap(data);
199 artifactData.setData(bufferData);
201 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
202 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
203 toscaTemplate.setDependencies(dependencies);
205 List<SdcSchemaFilesData> filesData = new ArrayList<>();
206 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
207 filedata.setPayloadAsArray(data);
208 filesData.add(filedata);
210 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
212 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
213 .thenReturn(ActionStatus.GENERAL_ERROR);
215 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
216 .thenReturn(Either.left(toscaTemplate));
219 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
220 .thenReturn(Either.left(filesData));
222 testSubject.createCsar(component, false, true);
226 void testPopulateZipWhenGetDependenciesIsRight() {
227 Component component = new Service();
228 boolean getFromCS = false;
230 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
231 ArtifactDefinition artifact = new ArtifactDefinition();
232 artifact.setArtifactName("artifactName");
233 artifact.setEsId("esId");
234 artifact.setArtifactUUID("artifactUUID");
235 artifact.setArtifactType("YANG");
236 toscaArtifacts.put("assettoscatemplate", artifact);
238 component.setToscaArtifacts(toscaArtifacts);
239 component.setDeploymentArtifacts(toscaArtifacts);
240 component.setArtifacts(toscaArtifacts);
241 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
242 DAOArtifactData artifactData = new DAOArtifactData();
243 byte[] data = "value".getBytes();
244 ByteBuffer bufferData = ByteBuffer.wrap(data);
245 artifactData.setData(bufferData);
247 ToscaRepresentation tosca = ToscaRepresentation.make("value".getBytes());
249 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
251 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
253 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
254 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
256 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
257 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
258 } catch (Exception e) {
264 void testPopulateZipWhenExportComponentIsRight() {
265 Component component = new Resource();
266 boolean getFromCS = false;
268 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
269 ArtifactDefinition artifact = new ArtifactDefinition();
270 artifact.setArtifactName("artifactName");
271 artifact.setEsId("esId");
272 artifact.setArtifactUUID("artifactUUID");
273 artifact.setArtifactType("YANG");
274 toscaArtifacts.put("assettoscatemplate", artifact);
276 component.setToscaArtifacts(toscaArtifacts);
277 component.setDeploymentArtifacts(toscaArtifacts);
278 component.setArtifacts(toscaArtifacts);
279 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
280 DAOArtifactData artifactData = new DAOArtifactData();
281 byte[] data = "value".getBytes();
282 ByteBuffer bufferData = ByteBuffer.wrap(data);
283 artifactData.setData(bufferData);
285 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
286 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
288 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
289 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
290 } catch (Exception e) {
296 void testPopulateZipWhenComponentIsServiceAndCollectComponentCsarDefinitionIsRight() {
297 Component component = new Service();
298 boolean getFromCS = false;
300 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
301 ArtifactDefinition artifact = new ArtifactDefinition();
302 artifact.setArtifactName("artifactName");
303 artifact.setEsId("esId");
304 artifact.setArtifactUUID("artifactUUID");
305 artifact.setArtifactType("YANG");
306 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
307 artifact.setDescription("description");
308 artifact.setArtifactLabel("artifactLabel");
309 toscaArtifacts.put("assettoscatemplate", artifact);
311 component.setToscaArtifacts(toscaArtifacts);
312 component.setDeploymentArtifacts(toscaArtifacts);
313 component.setArtifacts(toscaArtifacts);
314 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
315 component.setVersion("1.0");
316 component.setLastUpdaterUserId("userId");
317 component.setUniqueId("uid");
318 DAOArtifactData artifactData = new DAOArtifactData();
319 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
320 artifactData.setData(bufferData);
322 List<SdcSchemaFilesData> filesData = new ArrayList<>();
323 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
324 filedata.setPayloadAsArray(contentData);
325 filesData.add(filedata);
327 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
328 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
329 Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
330 dependencies.add(triple);
331 toscaTemplate.setDependencies(dependencies);
333 ToscaRepresentation tosca = ToscaRepresentation.make("value".getBytes());
335 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
337 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
339 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
340 .thenReturn(Either.left(toscaTemplate));
343 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
344 .thenReturn(Either.left(filesData));
346 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
347 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
349 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(User.class))).thenReturn(new User());
352 Mockito.when(artifactsBusinessLogic.validateAndHandleArtifact(Mockito.any(String.class),
353 Mockito.any(ComponentTypeEnum.class), Mockito.any(ArtifactOperationInfo.class), Mockito.isNull(),
354 Mockito.any(ArtifactDefinition.class), Mockito.any(String.class), Mockito.any(String.class),
355 Mockito.isNull(), Mockito.isNull(), Mockito.any(User.class), Mockito.any(Component.class),
356 Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(Boolean.class)))
357 .thenReturn(Either.left(Mockito.any(ArtifactDefinition.class)));
359 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
360 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
361 } catch (Exception e) {
367 void testPopulateZipWhenGetEntryDataIsRight() {
368 Component component = new Service();
369 boolean getFromCS = true;
371 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
372 ArtifactDefinition artifact = new ArtifactDefinition();
373 artifact.setArtifactName("artifactName");
374 artifact.setEsId("esId");
375 artifact.setArtifactUUID("artifactUUID");
376 artifact.setArtifactType("YANG");
377 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
378 artifact.setDescription("description");
379 artifact.setArtifactLabel("artifactLabel");
380 toscaArtifacts.put("assettoscatemplate", artifact);
382 component.setToscaArtifacts(toscaArtifacts);
383 component.setDeploymentArtifacts(toscaArtifacts);
384 component.setArtifacts(toscaArtifacts);
385 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
386 component.setVersion("1.0");
387 component.setLastUpdaterUserId("userId");
388 component.setUniqueId("uid");
389 DAOArtifactData artifactData = new DAOArtifactData();
390 byte[] data = "value".getBytes();
391 ByteBuffer bufferData = ByteBuffer.wrap(data);
392 artifactData.setData(bufferData);
394 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
395 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
396 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
397 dependencies.add(triple);
398 toscaTemplate.setDependencies(dependencies);
400 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
402 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
403 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
405 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
406 .thenReturn(Either.left(toscaTemplate));
408 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
409 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
410 } catch (Exception e) {
416 void testPopulateZipWhenGetEntryDataOfInnerComponentIsRight() {
417 Component component = new Service();
418 boolean getFromCS = false;
420 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
421 ArtifactDefinition artifact = new ArtifactDefinition();
422 artifact.setArtifactName("artifactName");
423 artifact.setEsId("esId");
424 artifact.setArtifactUUID("artifactUUID");
425 artifact.setArtifactType("YANG");
426 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
427 artifact.setDescription("description");
428 artifact.setArtifactLabel("artifactLabel");
429 toscaArtifacts.put("assettoscatemplate", artifact);
431 component.setToscaArtifacts(toscaArtifacts);
432 component.setDeploymentArtifacts(toscaArtifacts);
433 component.setArtifacts(toscaArtifacts);
434 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
435 component.setVersion("1.0");
436 component.setLastUpdaterUserId("userId");
437 component.setUniqueId("uid");
438 DAOArtifactData artifactData = new DAOArtifactData();
439 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
440 artifactData.setData(bufferData);
442 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
443 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
444 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
445 dependencies.add(triple);
446 toscaTemplate.setDependencies(dependencies);
448 ToscaRepresentation tosca = ToscaRepresentation.make(contentData);
450 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
452 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca),
453 Either.left(tosca), Either.right(ToscaError.GENERAL_ERROR));
455 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
456 .thenReturn(Either.left(toscaTemplate));
458 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
459 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
460 } catch (Exception e) {
466 void testPopulateZipWhenLatestSchemaFilesFromCassandraIsRight() {
467 Component component = new Service();
468 boolean getFromCS = false;
470 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
471 ArtifactDefinition artifact = new ArtifactDefinition();
472 artifact.setArtifactName("artifactName");
473 artifact.setEsId("esId");
474 artifact.setArtifactUUID("artifactUUID");
475 artifact.setArtifactType("YANG");
476 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
477 artifact.setDescription("description");
478 artifact.setArtifactLabel("artifactLabel");
479 toscaArtifacts.put("assettoscatemplate", artifact);
481 component.setToscaArtifacts(toscaArtifacts);
482 component.setDeploymentArtifacts(toscaArtifacts);
483 component.setArtifacts(toscaArtifacts);
484 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
485 component.setVersion("1.0");
486 component.setLastUpdaterUserId("userId");
487 component.setUniqueId("uid");
488 DAOArtifactData artifactData = new DAOArtifactData();
489 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
490 artifactData.setData(bufferData);
492 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
493 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
494 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
495 dependencies.add(triple);
496 toscaTemplate.setDependencies(dependencies);
498 ToscaRepresentation tosca = ToscaRepresentation.make(contentData);
500 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
502 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
504 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
505 .thenReturn(Either.left(toscaTemplate));
508 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
509 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
511 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
512 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
513 } catch (Exception e) {
519 void testAddInnerComponentsToCache() {
520 ComponentCache componentCache = ComponentCache.overwritable(overwriteIfSameVersions());
521 Component childComponent = new Resource();
522 Component componentRI = new Service();
523 List<ComponentInstance> componentInstances = new ArrayList<>();
524 ComponentInstance instance = new ComponentInstance();
525 instance.setComponentUid("resourceUid");
526 componentInstances.add(instance);
527 childComponent.setComponentInstances(componentInstances);
529 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
530 ArtifactDefinition artifact = new ArtifactDefinition();
531 artifact.setArtifactName("artifactName");
532 artifact.setEsId("esId");
533 artifact.setArtifactUUID("artifactUUID");
534 artifact.setArtifactType("YANG");
535 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
536 artifact.setDescription("description");
537 artifact.setArtifactLabel("artifactLabel");
538 toscaArtifacts.put("assettoscatemplate", artifact);
540 componentRI.setToscaArtifacts(toscaArtifacts);
542 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
543 .thenReturn(Either.left(componentRI));
545 Deencapsulation.invoke(testSubject, "addInnerComponentsToCache", componentCache, childComponent);
547 io.vavr.collection.List<CacheEntry> expected = io.vavr.collection.List.of(entry("esId","artifactName",componentRI));
548 assertEquals(expected, componentCache.all().toList());
552 void testAddInnerComponentsToCacheWhenGetToscaElementIsRight() {
553 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
554 Component childComponent = new Resource();
556 List<ComponentInstance> componentInstances = new ArrayList<>();
557 ComponentInstance instance = new ComponentInstance();
558 instance.setComponentUid("abc");
559 componentInstances.add(instance);
560 childComponent.setComponentInstances(componentInstances);
562 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
563 ArtifactDefinition artifact = new ArtifactDefinition();
564 artifact.setArtifactName("artifactName");
565 artifact.setEsId("esId");
566 artifact.setArtifactUUID("artifactUUID");
567 artifact.setArtifactType("YANG");
568 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
569 artifact.setDescription("description");
570 artifact.setArtifactLabel("artifactLabel");
571 toscaArtifacts.put("assettoscatemplate", artifact);
573 Component componentRI = new Service();
575 componentRI.setToscaArtifacts(toscaArtifacts);
577 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
578 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
581 assertTrue(componentCache.isEmpty());
585 void testWriteComponentInterface() throws IOException {
586 String fileName = "name.hello";
587 ToscaRepresentation tosca = ToscaRepresentation.make("value".getBytes());
589 Mockito.when(toscaExportUtils.exportComponentInterface(Mockito.any(Component.class), Mockito.any(Boolean.class)))
590 .thenReturn(Either.left(tosca));
593 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
594 List<Triple<String, String, Component>> output = Deencapsulation.invoke(testSubject, "writeComponentInterface", new Resource(), zip, fileName);
595 assertNotNull(output);
600 void testGetEntryData() {
601 String cassandraId = "id";
602 Component childComponent = new Resource();
604 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
605 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
607 Either<byte[], ActionStatus> output = Deencapsulation.invoke(testSubject, "getEntryData", cassandraId, childComponent);
609 assertNotNull(output);
610 assertTrue(output.isRight());
614 void testGetLatestSchemaFilesFromCassandraWhenListOfSchemasIsEmpty() {
615 List<SdcSchemaFilesData> filesData = new ArrayList<>();
618 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
619 .thenReturn(Either.left(filesData));
621 Either<byte[], ResponseFormat> output = Deencapsulation.invoke(testSubject, "getLatestSchemaFilesFromCassandra");
623 assertNotNull(output);
624 assertTrue(output.isRight());
628 void testExtractVfcsArtifactsFromCsar() {
629 String key = "Artifacts/org.openecomp.resource.some/Deployment/to/resource";
630 byte[] data = "value".getBytes();
632 Map<String, byte[]> csar = new HashMap<>();
635 Map<String, List<ArtifactDefinition>> output = CsarUtils.extractVfcsArtifactsFromCsar(csar);
637 assertNotNull(output);
638 assertTrue(output.containsKey("org.openecomp.resource.some"));
639 assertEquals(1, output.get("org.openecomp.resource.some").size());
643 void testAddExtractedVfcArtifactWhenArtifactsContainsExtractedArtifactKey() {
644 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
645 "key", new ArtifactDefinition());
646 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
647 artifacts.put("key", new ArrayList<>());
649 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
651 assertEquals(1, artifacts.get("key").size());
655 void testAddExtractedVfcArtifactWhenArtifactsDoesntContainsExtractedArtifactKey() {
656 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
657 "key", new ArtifactDefinition());
658 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
659 artifacts.put("key1", new ArrayList<>());
661 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
663 assertEquals(0, artifacts.get("key1").size());
664 assertEquals(1, artifacts.get("key").size());
665 assertEquals(2, artifacts.size());
669 void testExtractVfcArtifact() {
670 String path = "path/to/informational/artificat";
671 Map<String, byte[]> map = new HashMap<>();
672 map.put(path, "value".getBytes());
673 Entry<String, byte[]> entry = map.entrySet().iterator().next();
675 Optional<ImmutablePair<String, ArtifactDefinition>> output =
676 Deencapsulation.invoke(testSubject, "extractVfcArtifact", entry, new HashMap<>());
678 if(output.isPresent()) {
679 assertEquals("to", output.get().left);
681 fail("`output` is empty!");
686 void testDetectArtifactGroupTypeWithExceptionBeingCaught() {
687 Either<ArtifactGroupTypeEnum, Boolean> output = Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", Map.class);
689 assertNotNull(output);
690 assertTrue(output.isRight());
691 assertFalse(output.right().value());
695 void testDetectArtifactGroupTypeWWhenCollectedWarningMessagesContainesKey() {
696 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
698 collectedWarningMessages.put("Warning - unrecognized artifact group type {} was received.", new HashSet<>());
699 Either<ArtifactGroupTypeEnum, Boolean> output = Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", collectedWarningMessages);
701 assertNotNull(output);
702 assertTrue(output.isRight());
703 assertFalse(output.right().value());
707 void testNonMetaArtifactInfoCtor() {
708 createNonMetaArtifactInfoTestSubject();
712 void testNonMetaArtifactInfoGetPath() {
713 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
715 testSubject.getPath();
719 void testNonMetaArtifactInfoGetArtifactName() {
720 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
722 testSubject.getArtifactName();
726 void testNonMetaArtifactInfoGetArtifactType() {
727 final NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
728 assertThat("The artifact type should be as expected",
729 testSubject.getArtifactType(), is(ArtifactTypeEnum.AAI_SERVICE_MODEL.getType()));
733 void testNonMetaArtifactInfoGetDisplayName() {
734 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
736 testSubject.getDisplayName();
740 void testNonMetaArtifactInfoGetArtifactGroupType() {
741 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
743 testSubject.getArtifactGroupType();
747 void testNonMetaArtifactInfoGetArtifactLabel() {
748 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
750 testSubject.getArtifactLabel();
754 void testNonMetaArtifactInfoGetIsFromCsar() {
755 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
757 testSubject.isFromCsar();
761 void testNonMetaArtifactInfoGetPayloadData() {
762 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
764 testSubject.getPayloadData();
768 void testNonMetaArtifactInfoGetArtifaactChecksum() {
769 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
771 testSubject.getArtifactChecksum();
775 void testNonMetaArtifactInfoGetArtifactUniqueId() {
776 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
778 testSubject.getArtifactUniqueId();
782 void testNonMetaArtifactInfosetArtifactUniqueId() {
783 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
785 testSubject.setArtifactUniqueId("artifactUniqueId");
789 void testValidateNonMetaArtifactWithExceptionCaught() {
790 CsarUtils.validateNonMetaArtifact("", new byte[0], new HashMap<>());
794 void testCollectComponentCsarDefinitionWhenComponentIsServiceAndGetToscaElementIsLeft() {
795 Component component = new Service();
796 component.setUniqueId("uniqueId");
797 List<ComponentInstance> resourceInstances = new ArrayList<>();
798 ComponentInstance instance = new ComponentInstance();
799 instance.setComponentUid("resourceUid");
800 instance.setOriginType(OriginTypeEnum.SERVICE);
801 resourceInstances.add(instance);
802 component.setComponentInstances(resourceInstances);
804 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
805 ArtifactDefinition artifact = new ArtifactDefinition();
806 artifact.setArtifactName("artifactName");
807 artifact.setEsId("esId");
808 artifact.setArtifactUUID("artifactUUID");
809 artifact.setArtifactType("YANG");
810 toscaArtifacts.put("assettoscatemplate", artifact);
812 component.setToscaArtifacts(toscaArtifacts);
813 component.setDeploymentArtifacts(toscaArtifacts);
814 component.setArtifacts(toscaArtifacts);
816 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
817 Either.right(StorageOperationStatus.BAD_REQUEST));
819 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
821 assertNotNull(output);
822 assertTrue(output.isRight());
826 void testCollectComponentTypeArtifactsWhenFetchedComponentHasComponentInstances() {
827 Component component = new Service();
828 Component fetchedComponent = new Resource();
829 component.setUniqueId("uniqueId");
830 List<ComponentInstance> resourceInstances = new ArrayList<>();
831 ComponentInstance instance = new ComponentInstance();
832 instance.setComponentUid("resourceUid");
833 instance.setOriginType(OriginTypeEnum.SERVICE);
834 resourceInstances.add(instance);
835 component.setComponentInstances(resourceInstances);
836 fetchedComponent.setComponentInstances(resourceInstances);
838 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
839 ArtifactDefinition artifact = new ArtifactDefinition();
840 artifact.setArtifactName("artifactName");
841 artifact.setEsId("esId");
842 artifact.setArtifactUUID("artifactUUID");
843 artifact.setArtifactType("YANG");
844 toscaArtifacts.put("assettoscatemplate", artifact);
846 component.setToscaArtifacts(toscaArtifacts);
847 component.setDeploymentArtifacts(toscaArtifacts);
848 component.setArtifacts(toscaArtifacts);
850 fetchedComponent.setToscaArtifacts(toscaArtifacts);
851 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
852 fetchedComponent.setArtifacts(toscaArtifacts);
854 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
855 Either.left(fetchedComponent), Either.right(StorageOperationStatus.BAD_REQUEST));
857 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
859 assertNotNull(output);
860 assertTrue(output.isRight());
864 void testCollectComponentTypeArtifactsWhenFetchedComponentDontHaveComponentInstances() {
865 Component component = new Service();
866 Component fetchedComponent = new Resource();
867 component.setUniqueId("uniqueId");
868 List<ComponentInstance> resourceInstances = new ArrayList<>();
869 ComponentInstance instance = new ComponentInstance();
870 instance.setComponentUid("resourceUid");
871 instance.setOriginType(OriginTypeEnum.SERVICE);
873 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
874 ArtifactDefinition artifact = new ArtifactDefinition();
875 artifact.setArtifactName("artifactName");
876 artifact.setEsId("esId");
877 artifact.setArtifactUUID("artifactUUID");
878 artifact.setArtifactType("PLAN");
879 toscaArtifacts.put("assettoscatemplate", artifact);
881 instance.setDeploymentArtifacts(toscaArtifacts);
883 resourceInstances.add(instance);
884 component.setComponentInstances(resourceInstances);
886 component.setToscaArtifacts(toscaArtifacts);
887 component.setDeploymentArtifacts(toscaArtifacts);
888 component.setArtifacts(toscaArtifacts);
890 fetchedComponent.setToscaArtifacts(toscaArtifacts);
891 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
892 fetchedComponent.setArtifacts(toscaArtifacts);
894 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
895 Either.left(fetchedComponent));
897 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
899 assertNotNull(output);
900 assertTrue(output.isLeft());
904 void testValidateNonMetaArtifactHappyScenario() {
905 String artifactPath = "Artifacts/Deployment/YANG_XML/myYang.xml";
906 byte[] payloadData = "some payload data".getBytes();
907 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
908 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
909 payloadData, collectedWarningMessages);
910 assertTrue(eitherNonMetaArtifact.isLeft());
911 assertTrue(collectedWarningMessages.isEmpty());
913 artifactPath = "Artifacts/Informational/OTHER/someArtifact.xml";
914 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
915 assertTrue(eitherNonMetaArtifact.isLeft());
916 assertTrue(collectedWarningMessages.isEmpty());
920 void testValidateNonMetaArtifactScenarioWithWarnnings() {
921 String artifactPath = "Artifacts/Deployment/Buga/myYang.xml";
922 byte[] payloadData = "some payload data".getBytes();
923 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
924 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
925 payloadData, collectedWarningMessages);
926 assertTrue(eitherNonMetaArtifact.isLeft());
928 artifactPath = "Artifacts/Informational/Buga2/someArtifact.xml";
929 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
930 assertTrue(eitherNonMetaArtifact.isLeft());
932 assertEquals(1, collectedWarningMessages.size());
933 assertEquals(2, collectedWarningMessages.values().iterator().next().size());
937 void testValidateNonMetaArtifactUnhappyScenario() {
938 String artifactPath = "Artifacts/Buga/YANG_XML/myYang.xml";
939 byte[] payloadData = "some payload data".getBytes();
940 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
941 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
942 payloadData, collectedWarningMessages);
943 assertTrue(eitherNonMetaArtifact.isRight());
944 assertFalse(collectedWarningMessages.isEmpty());
948 void testAddSchemaFilesFromCassandraAddingDuplicatedEntry() throws IOException {
949 final String rootPath = System.getProperty("user.dir");
950 final Path path = Paths.get(rootPath + "/src/test/resources/sdc.zip");
951 final byte[] data = Files.readAllBytes(path);
952 try (final ByteArrayOutputStream out = new ByteArrayOutputStream(); final ZipOutputStream zip = new ZipOutputStream(out)) {
953 Deencapsulation.invoke(testSubject, "addSchemaFilesFromCassandra", zip, data, nodesFromPackage);
954 final IOException actualException = assertThrows(IOException.class, () -> zip.putNextEntry(new ZipEntry("Definitions/nodes.yml")));
955 assertEquals("duplicate entry: Definitions/nodes.yml", actualException.getMessage());
960 void testFindNonRootNodesFromPackage() {
961 final Resource resource = new Resource();
962 resource.setDerivedList(nodesFromPackage);
963 final Component component = resource;
964 final List<Triple<String, String, Component>> dependencies = new ArrayList<>();
965 final Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
966 dependencies.add(triple);
967 final List<String> expectedResult = Arrays.asList("tosca.nodes.Container.Application");
968 final List<String> result = Deencapsulation.invoke(testSubject,
969 "findNonRootNodesFromPackage", dependencies);
970 assertTrue(CollectionUtils.isNotEmpty(result));
971 assertEquals(expectedResult, result);
974 private byte[] getFileResource(final String filePath) throws IOException {
975 try (final InputStream inputStream = getFileResourceAsInputStream(filePath)) {
976 return IOUtils.toByteArray(inputStream);
980 private InputStream getFileResourceAsInputStream(final String filePath) {
981 return Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);