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> toscaArtifacts = new HashMap<>();
164 ArtifactDefinition artifact = new ArtifactDefinition();
165 artifact.setArtifactName("artifactName");
166 artifact.setEsId("esId");
167 toscaArtifacts.put("assettoscatemplate", artifact);
169 component.setToscaArtifacts(toscaArtifacts);
171 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
172 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
174 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
175 .thenReturn(ActionStatus.GENERAL_ERROR);
177 testSubject.createCsar(component, true, true);
181 void testCreateCsarWithGenerateCsarZipResponseIsLeft() {
182 Component component = new Resource();
183 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
184 ArtifactDefinition artifact = new ArtifactDefinition();
185 artifact.setArtifactName("artifactName");
186 artifact.setEsId("esId");
187 artifact.setArtifactUUID("artifactUUID");
188 artifact.setArtifactType("YANG");
189 toscaArtifacts.put("assettoscatemplate", artifact);
191 component.setToscaArtifacts(toscaArtifacts);
192 component.setDeploymentArtifacts(toscaArtifacts);
193 component.setArtifacts(toscaArtifacts);
194 DAOArtifactData artifactData = new DAOArtifactData();
195 byte[] data = "value".getBytes();
196 ByteBuffer bufferData = ByteBuffer.wrap(data);
197 artifactData.setData(bufferData);
199 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
200 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
201 toscaTemplate.setDependencies(dependencies);
203 List<SdcSchemaFilesData> filesData = new ArrayList<>();
204 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
205 filedata.setPayloadAsArray(data);
206 filesData.add(filedata);
208 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
210 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
211 .thenReturn(ActionStatus.GENERAL_ERROR);
213 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
214 .thenReturn(Either.left(toscaTemplate));
217 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
218 .thenReturn(Either.left(filesData));
220 testSubject.createCsar(component, false, true);
224 void testPopulateZipWhenGetDependenciesIsRight() {
225 Component component = new Service();
226 boolean getFromCS = false;
228 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
229 ArtifactDefinition artifact = new ArtifactDefinition();
230 artifact.setArtifactName("artifactName");
231 artifact.setEsId("esId");
232 artifact.setArtifactUUID("artifactUUID");
233 artifact.setArtifactType("YANG");
234 toscaArtifacts.put("assettoscatemplate", artifact);
236 component.setToscaArtifacts(toscaArtifacts);
237 component.setDeploymentArtifacts(toscaArtifacts);
238 component.setArtifacts(toscaArtifacts);
239 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
240 DAOArtifactData artifactData = new DAOArtifactData();
241 byte[] data = "value".getBytes();
242 ByteBuffer bufferData = ByteBuffer.wrap(data);
243 artifactData.setData(bufferData);
245 ToscaRepresentation tosca = ToscaRepresentation.make("value".getBytes());
247 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
249 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
251 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
252 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
254 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
255 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
256 } catch (Exception e) {
262 void testPopulateZipWhenExportComponentIsRight() {
263 Component component = new Resource();
264 boolean getFromCS = false;
266 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
267 ArtifactDefinition artifact = new ArtifactDefinition();
268 artifact.setArtifactName("artifactName");
269 artifact.setEsId("esId");
270 artifact.setArtifactUUID("artifactUUID");
271 artifact.setArtifactType("YANG");
272 toscaArtifacts.put("assettoscatemplate", artifact);
274 component.setToscaArtifacts(toscaArtifacts);
275 component.setDeploymentArtifacts(toscaArtifacts);
276 component.setArtifacts(toscaArtifacts);
277 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
278 DAOArtifactData artifactData = new DAOArtifactData();
279 byte[] data = "value".getBytes();
280 ByteBuffer bufferData = ByteBuffer.wrap(data);
281 artifactData.setData(bufferData);
283 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
284 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
286 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
287 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
288 } catch (Exception e) {
294 void testPopulateZipWhenComponentIsServiceAndCollectComponentCsarDefinitionIsRight() {
295 Component component = new Service();
296 boolean getFromCS = false;
298 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
299 ArtifactDefinition artifact = new ArtifactDefinition();
300 artifact.setArtifactName("artifactName");
301 artifact.setEsId("esId");
302 artifact.setArtifactUUID("artifactUUID");
303 artifact.setArtifactType("YANG");
304 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
305 artifact.setDescription("description");
306 artifact.setArtifactLabel("artifactLabel");
307 toscaArtifacts.put("assettoscatemplate", artifact);
309 component.setToscaArtifacts(toscaArtifacts);
310 component.setDeploymentArtifacts(toscaArtifacts);
311 component.setArtifacts(toscaArtifacts);
312 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
313 component.setVersion("1.0");
314 component.setLastUpdaterUserId("userId");
315 component.setUniqueId("uid");
316 DAOArtifactData artifactData = new DAOArtifactData();
317 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
318 artifactData.setData(bufferData);
320 List<SdcSchemaFilesData> filesData = new ArrayList<>();
321 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
322 filedata.setPayloadAsArray(contentData);
323 filesData.add(filedata);
325 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
326 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
327 Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
328 dependencies.add(triple);
329 toscaTemplate.setDependencies(dependencies);
331 ToscaRepresentation tosca = ToscaRepresentation.make("value".getBytes());
333 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
335 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
337 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
338 .thenReturn(Either.left(toscaTemplate));
341 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
342 .thenReturn(Either.left(filesData));
344 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
345 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
347 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(User.class))).thenReturn(new User());
350 Mockito.when(artifactsBusinessLogic.validateAndHandleArtifact(Mockito.any(String.class),
351 Mockito.any(ComponentTypeEnum.class), Mockito.any(ArtifactOperationInfo.class), Mockito.isNull(),
352 Mockito.any(ArtifactDefinition.class), Mockito.any(String.class), Mockito.any(String.class),
353 Mockito.isNull(), Mockito.isNull(), Mockito.any(User.class), Mockito.any(Component.class),
354 Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(Boolean.class)))
355 .thenReturn(Either.left(Mockito.any(ArtifactDefinition.class)));
357 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
358 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
359 } catch (Exception e) {
365 void testPopulateZipWhenGetEntryDataIsRight() {
366 Component component = new Service();
367 boolean getFromCS = true;
369 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
370 ArtifactDefinition artifact = new ArtifactDefinition();
371 artifact.setArtifactName("artifactName");
372 artifact.setEsId("esId");
373 artifact.setArtifactUUID("artifactUUID");
374 artifact.setArtifactType("YANG");
375 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
376 artifact.setDescription("description");
377 artifact.setArtifactLabel("artifactLabel");
378 toscaArtifacts.put("assettoscatemplate", artifact);
380 component.setToscaArtifacts(toscaArtifacts);
381 component.setDeploymentArtifacts(toscaArtifacts);
382 component.setArtifacts(toscaArtifacts);
383 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
384 component.setVersion("1.0");
385 component.setLastUpdaterUserId("userId");
386 component.setUniqueId("uid");
387 DAOArtifactData artifactData = new DAOArtifactData();
388 byte[] data = "value".getBytes();
389 ByteBuffer bufferData = ByteBuffer.wrap(data);
390 artifactData.setData(bufferData);
392 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
393 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
394 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
395 dependencies.add(triple);
396 toscaTemplate.setDependencies(dependencies);
398 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
400 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
401 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
403 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
404 .thenReturn(Either.left(toscaTemplate));
406 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
407 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
408 } catch (Exception e) {
414 void testPopulateZipWhenGetEntryDataOfInnerComponentIsRight() {
415 Component component = new Service();
416 boolean getFromCS = false;
418 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
419 ArtifactDefinition artifact = new ArtifactDefinition();
420 artifact.setArtifactName("artifactName");
421 artifact.setEsId("esId");
422 artifact.setArtifactUUID("artifactUUID");
423 artifact.setArtifactType("YANG");
424 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
425 artifact.setDescription("description");
426 artifact.setArtifactLabel("artifactLabel");
427 toscaArtifacts.put("assettoscatemplate", artifact);
429 component.setToscaArtifacts(toscaArtifacts);
430 component.setDeploymentArtifacts(toscaArtifacts);
431 component.setArtifacts(toscaArtifacts);
432 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
433 component.setVersion("1.0");
434 component.setLastUpdaterUserId("userId");
435 component.setUniqueId("uid");
436 DAOArtifactData artifactData = new DAOArtifactData();
437 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
438 artifactData.setData(bufferData);
440 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
441 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
442 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
443 dependencies.add(triple);
444 toscaTemplate.setDependencies(dependencies);
446 ToscaRepresentation tosca = ToscaRepresentation.make(contentData);
448 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
450 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca),
451 Either.left(tosca), Either.right(ToscaError.GENERAL_ERROR));
453 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
454 .thenReturn(Either.left(toscaTemplate));
456 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
457 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
458 } catch (Exception e) {
464 void testPopulateZipWhenLatestSchemaFilesFromCassandraIsRight() {
465 Component component = new Service();
466 boolean getFromCS = false;
468 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
469 ArtifactDefinition artifact = new ArtifactDefinition();
470 artifact.setArtifactName("artifactName");
471 artifact.setEsId("esId");
472 artifact.setArtifactUUID("artifactUUID");
473 artifact.setArtifactType("YANG");
474 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
475 artifact.setDescription("description");
476 artifact.setArtifactLabel("artifactLabel");
477 toscaArtifacts.put("assettoscatemplate", artifact);
479 component.setToscaArtifacts(toscaArtifacts);
480 component.setDeploymentArtifacts(toscaArtifacts);
481 component.setArtifacts(toscaArtifacts);
482 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
483 component.setVersion("1.0");
484 component.setLastUpdaterUserId("userId");
485 component.setUniqueId("uid");
486 DAOArtifactData artifactData = new DAOArtifactData();
487 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
488 artifactData.setData(bufferData);
490 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
491 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
492 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
493 dependencies.add(triple);
494 toscaTemplate.setDependencies(dependencies);
496 ToscaRepresentation tosca = ToscaRepresentation.make(contentData);
498 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
500 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
502 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
503 .thenReturn(Either.left(toscaTemplate));
506 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
507 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
509 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
510 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
511 } catch (Exception e) {
517 void testAddInnerComponentsToCache() {
518 ComponentCache componentCache = ComponentCache.overwritable(overwriteIfSameVersions());
519 Component childComponent = new Resource();
520 Component componentRI = new Service();
521 List<ComponentInstance> componentInstances = new ArrayList<>();
522 ComponentInstance instance = new ComponentInstance();
523 instance.setComponentUid("resourceUid");
524 componentInstances.add(instance);
525 childComponent.setComponentInstances(componentInstances);
527 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
528 ArtifactDefinition artifact = new ArtifactDefinition();
529 artifact.setArtifactName("artifactName");
530 artifact.setEsId("esId");
531 artifact.setArtifactUUID("artifactUUID");
532 artifact.setArtifactType("YANG");
533 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
534 artifact.setDescription("description");
535 artifact.setArtifactLabel("artifactLabel");
536 toscaArtifacts.put("assettoscatemplate", artifact);
538 componentRI.setToscaArtifacts(toscaArtifacts);
540 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
541 .thenReturn(Either.left(componentRI));
543 Deencapsulation.invoke(testSubject, "addInnerComponentsToCache", componentCache, childComponent);
545 io.vavr.collection.List<CacheEntry> expected = io.vavr.collection.List.of(entry("esId","artifactName",componentRI));
546 assertEquals(expected, componentCache.all().toList());
550 void testAddInnerComponentsToCacheWhenGetToscaElementIsRight() {
551 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
552 Component childComponent = new Resource();
554 List<ComponentInstance> componentInstances = new ArrayList<>();
555 ComponentInstance instance = new ComponentInstance();
556 instance.setComponentUid("abc");
557 componentInstances.add(instance);
558 childComponent.setComponentInstances(componentInstances);
560 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
561 ArtifactDefinition artifact = new ArtifactDefinition();
562 artifact.setArtifactName("artifactName");
563 artifact.setEsId("esId");
564 artifact.setArtifactUUID("artifactUUID");
565 artifact.setArtifactType("YANG");
566 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
567 artifact.setDescription("description");
568 artifact.setArtifactLabel("artifactLabel");
569 toscaArtifacts.put("assettoscatemplate", artifact);
571 Component componentRI = new Service();
573 componentRI.setToscaArtifacts(toscaArtifacts);
575 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
576 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
579 assertTrue(componentCache.isEmpty());
583 void testWriteComponentInterface() throws IOException {
584 String fileName = "name.hello";
585 ToscaRepresentation tosca = ToscaRepresentation.make("value".getBytes());
587 Mockito.when(toscaExportUtils.exportComponentInterface(Mockito.any(Component.class), Mockito.any(Boolean.class)))
588 .thenReturn(Either.left(tosca));
591 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
592 List<Triple<String, String, Component>> output = Deencapsulation.invoke(testSubject, "writeComponentInterface", new Resource(), zip, fileName);
593 assertNotNull(output);
598 void testGetEntryData() {
599 String cassandraId = "id";
600 Component childComponent = new Resource();
602 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
603 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
605 Either<byte[], ActionStatus> output = Deencapsulation.invoke(testSubject, "getEntryData", cassandraId, childComponent);
607 assertNotNull(output);
608 assertTrue(output.isRight());
612 void testGetLatestSchemaFilesFromCassandraWhenListOfSchemasIsEmpty() {
613 List<SdcSchemaFilesData> filesData = new ArrayList<>();
616 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
617 .thenReturn(Either.left(filesData));
619 Either<byte[], ResponseFormat> output = Deencapsulation.invoke(testSubject, "getLatestSchemaFilesFromCassandra");
621 assertNotNull(output);
622 assertTrue(output.isRight());
626 void testExtractVfcsArtifactsFromCsar() {
627 String key = "Artifacts/org.openecomp.resource.some/Deployment/to/resource";
628 byte[] data = "value".getBytes();
630 Map<String, byte[]> csar = new HashMap<>();
633 Map<String, List<ArtifactDefinition>> output = CsarUtils.extractVfcsArtifactsFromCsar(csar);
635 assertNotNull(output);
636 assertTrue(output.containsKey("org.openecomp.resource.some"));
637 assertEquals(1, output.get("org.openecomp.resource.some").size());
641 void testAddExtractedVfcArtifactWhenArtifactsContainsExtractedArtifactKey() {
642 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
643 "key", new ArtifactDefinition());
644 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
645 artifacts.put("key", new ArrayList<>());
647 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
649 assertEquals(1, artifacts.get("key").size());
653 void testAddExtractedVfcArtifactWhenArtifactsDoesntContainsExtractedArtifactKey() {
654 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
655 "key", new ArtifactDefinition());
656 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
657 artifacts.put("key1", new ArrayList<>());
659 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
661 assertEquals(0, artifacts.get("key1").size());
662 assertEquals(1, artifacts.get("key").size());
663 assertEquals(2, artifacts.size());
667 void testExtractVfcArtifact() {
668 String path = "path/to/informational/artificat";
669 Map<String, byte[]> map = new HashMap<>();
670 map.put(path, "value".getBytes());
671 Entry<String, byte[]> entry = map.entrySet().iterator().next();
673 Optional<ImmutablePair<String, ArtifactDefinition>> output =
674 Deencapsulation.invoke(testSubject, "extractVfcArtifact", entry, new HashMap<>());
676 if(output.isPresent()) {
677 assertEquals("to", output.get().left);
679 fail("`output` is empty!");
684 void testDetectArtifactGroupTypeWithExceptionBeingCaught() {
685 Either<ArtifactGroupTypeEnum, Boolean> output = Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", Map.class);
687 assertNotNull(output);
688 assertTrue(output.isRight());
689 assertFalse(output.right().value());
693 void testDetectArtifactGroupTypeWWhenCollectedWarningMessagesContainesKey() {
694 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
696 collectedWarningMessages.put("Warning - unrecognized artifact group type {} was received.", new HashSet<>());
697 Either<ArtifactGroupTypeEnum, Boolean> output = Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", collectedWarningMessages);
699 assertNotNull(output);
700 assertTrue(output.isRight());
701 assertFalse(output.right().value());
705 void testNonMetaArtifactInfoCtor() {
706 createNonMetaArtifactInfoTestSubject();
710 void testNonMetaArtifactInfoGetPath() {
711 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
713 testSubject.getPath();
717 void testNonMetaArtifactInfoGetArtifactName() {
718 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
720 testSubject.getArtifactName();
724 void testNonMetaArtifactInfoGetArtifactType() {
725 final NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
726 assertThat("The artifact type should be as expected",
727 testSubject.getArtifactType(), is(ArtifactTypeEnum.AAI_SERVICE_MODEL.getType()));
731 void testNonMetaArtifactInfoGetDisplayName() {
732 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
734 testSubject.getDisplayName();
738 void testNonMetaArtifactInfoGetArtifactGroupType() {
739 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
741 testSubject.getArtifactGroupType();
745 void testNonMetaArtifactInfoGetArtifactLabel() {
746 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
748 testSubject.getArtifactLabel();
752 void testNonMetaArtifactInfoGetIsFromCsar() {
753 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
755 testSubject.isFromCsar();
759 void testNonMetaArtifactInfoGetPayloadData() {
760 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
762 testSubject.getPayloadData();
766 void testNonMetaArtifactInfoGetArtifaactChecksum() {
767 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
769 testSubject.getArtifactChecksum();
773 void testNonMetaArtifactInfoGetArtifactUniqueId() {
774 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
776 testSubject.getArtifactUniqueId();
780 void testNonMetaArtifactInfosetArtifactUniqueId() {
781 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
783 testSubject.setArtifactUniqueId("artifactUniqueId");
787 void testValidateNonMetaArtifactWithExceptionCaught() {
788 CsarUtils.validateNonMetaArtifact("", new byte[0], new HashMap<>());
792 void testCollectComponentCsarDefinitionWhenComponentIsServiceAndGetToscaElementIsLeft() {
793 Component component = new Service();
794 component.setUniqueId("uniqueId");
795 List<ComponentInstance> resourceInstances = new ArrayList<>();
796 ComponentInstance instance = new ComponentInstance();
797 instance.setComponentUid("resourceUid");
798 instance.setOriginType(OriginTypeEnum.SERVICE);
799 resourceInstances.add(instance);
800 component.setComponentInstances(resourceInstances);
802 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
803 ArtifactDefinition artifact = new ArtifactDefinition();
804 artifact.setArtifactName("artifactName");
805 artifact.setEsId("esId");
806 artifact.setArtifactUUID("artifactUUID");
807 artifact.setArtifactType("YANG");
808 toscaArtifacts.put("assettoscatemplate", artifact);
810 component.setToscaArtifacts(toscaArtifacts);
811 component.setDeploymentArtifacts(toscaArtifacts);
812 component.setArtifacts(toscaArtifacts);
814 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
815 Either.right(StorageOperationStatus.BAD_REQUEST));
817 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
819 assertNotNull(output);
820 assertTrue(output.isRight());
824 void testCollectComponentTypeArtifactsWhenFetchedComponentHasComponentInstances() {
825 Component component = new Service();
826 Component fetchedComponent = new Resource();
827 component.setUniqueId("uniqueId");
828 List<ComponentInstance> resourceInstances = new ArrayList<>();
829 ComponentInstance instance = new ComponentInstance();
830 instance.setComponentUid("resourceUid");
831 instance.setOriginType(OriginTypeEnum.SERVICE);
832 resourceInstances.add(instance);
833 component.setComponentInstances(resourceInstances);
834 fetchedComponent.setComponentInstances(resourceInstances);
836 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
837 ArtifactDefinition artifact = new ArtifactDefinition();
838 artifact.setArtifactName("artifactName");
839 artifact.setEsId("esId");
840 artifact.setArtifactUUID("artifactUUID");
841 artifact.setArtifactType("YANG");
842 toscaArtifacts.put("assettoscatemplate", artifact);
844 component.setToscaArtifacts(toscaArtifacts);
845 component.setDeploymentArtifacts(toscaArtifacts);
846 component.setArtifacts(toscaArtifacts);
848 fetchedComponent.setToscaArtifacts(toscaArtifacts);
849 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
850 fetchedComponent.setArtifacts(toscaArtifacts);
852 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
853 Either.left(fetchedComponent), Either.right(StorageOperationStatus.BAD_REQUEST));
855 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
857 assertNotNull(output);
858 assertTrue(output.isRight());
862 void testCollectComponentTypeArtifactsWhenFetchedComponentDontHaveComponentInstances() {
863 Component component = new Service();
864 Component fetchedComponent = new Resource();
865 component.setUniqueId("uniqueId");
866 List<ComponentInstance> resourceInstances = new ArrayList<>();
867 ComponentInstance instance = new ComponentInstance();
868 instance.setComponentUid("resourceUid");
869 instance.setOriginType(OriginTypeEnum.SERVICE);
871 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
872 ArtifactDefinition artifact = new ArtifactDefinition();
873 artifact.setArtifactName("artifactName");
874 artifact.setEsId("esId");
875 artifact.setArtifactUUID("artifactUUID");
876 artifact.setArtifactType("PLAN");
877 toscaArtifacts.put("assettoscatemplate", artifact);
879 instance.setDeploymentArtifacts(toscaArtifacts);
881 resourceInstances.add(instance);
882 component.setComponentInstances(resourceInstances);
884 component.setToscaArtifacts(toscaArtifacts);
885 component.setDeploymentArtifacts(toscaArtifacts);
886 component.setArtifacts(toscaArtifacts);
888 fetchedComponent.setToscaArtifacts(toscaArtifacts);
889 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
890 fetchedComponent.setArtifacts(toscaArtifacts);
892 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
893 Either.left(fetchedComponent));
895 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
897 assertNotNull(output);
898 assertTrue(output.isLeft());
902 void testValidateNonMetaArtifactHappyScenario() {
903 String artifactPath = "Artifacts/Deployment/YANG_XML/myYang.xml";
904 byte[] payloadData = "some payload data".getBytes();
905 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
906 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
907 payloadData, collectedWarningMessages);
908 assertTrue(eitherNonMetaArtifact.isLeft());
909 assertTrue(collectedWarningMessages.isEmpty());
911 artifactPath = "Artifacts/Informational/OTHER/someArtifact.xml";
912 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
913 assertTrue(eitherNonMetaArtifact.isLeft());
914 assertTrue(collectedWarningMessages.isEmpty());
918 void testValidateNonMetaArtifactScenarioWithWarnnings() {
919 String artifactPath = "Artifacts/Deployment/Buga/myYang.xml";
920 byte[] payloadData = "some payload data".getBytes();
921 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
922 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
923 payloadData, collectedWarningMessages);
924 assertTrue(eitherNonMetaArtifact.isLeft());
926 artifactPath = "Artifacts/Informational/Buga2/someArtifact.xml";
927 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
928 assertTrue(eitherNonMetaArtifact.isLeft());
930 assertEquals(1, collectedWarningMessages.size());
931 assertEquals(2, collectedWarningMessages.values().iterator().next().size());
935 void testValidateNonMetaArtifactUnhappyScenario() {
936 String artifactPath = "Artifacts/Buga/YANG_XML/myYang.xml";
937 byte[] payloadData = "some payload data".getBytes();
938 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
939 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
940 payloadData, collectedWarningMessages);
941 assertTrue(eitherNonMetaArtifact.isRight());
942 assertFalse(collectedWarningMessages.isEmpty());
946 void testAddSchemaFilesFromCassandraAddingDuplicatedEntry() throws IOException {
947 final String rootPath = System.getProperty("user.dir");
948 final Path path = Paths.get(rootPath + "/src/test/resources/sdc.zip");
949 final byte[] data = Files.readAllBytes(path);
950 try (final ByteArrayOutputStream out = new ByteArrayOutputStream(); final ZipOutputStream zip = new ZipOutputStream(out)) {
951 Deencapsulation.invoke(testSubject, "addSchemaFilesFromCassandra", zip, data, nodesFromPackage);
952 final IOException actualException = assertThrows(IOException.class, () -> zip.putNextEntry(new ZipEntry("Definitions/nodes.yml")));
953 assertEquals("duplicate entry: Definitions/nodes.yml", actualException.getMessage());
958 void testFindNonRootNodesFromPackage() {
959 final Resource resource = new Resource();
960 resource.setDerivedList(nodesFromPackage);
961 final Component component = resource;
962 final List<Triple<String, String, Component>> dependencies = new ArrayList<>();
963 final Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
964 dependencies.add(triple);
965 final List<String> expectedResult = Arrays.asList("tosca.nodes.Container.Application");
966 final List<String> result = Deencapsulation.invoke(testSubject,
967 "findNonRootNodesFromPackage", dependencies);
968 assertTrue(CollectionUtils.isNotEmpty(result));
969 assertEquals(expectedResult, result);
972 private byte[] getFileResource(final String filePath) throws IOException {
973 try (final InputStream inputStream = getFileResourceAsInputStream(filePath)) {
974 return IOUtils.toByteArray(inputStream);
978 private InputStream getFileResourceAsInputStream(final String filePath) {
979 return Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);