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.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
30 import static org.openecomp.sdc.be.tosca.ComponentCache.MergeStrategy.overwriteIfSameVersions;
31 import static org.openecomp.sdc.be.tosca.ComponentCache.entry;
33 import fj.data.Either;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.nio.ByteBuffer;
38 import java.nio.charset.StandardCharsets;
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.Assert;
62 import org.junit.Before;
63 import org.junit.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.BeConfDependentTest;
69 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
70 import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo;
71 import org.openecomp.sdc.be.config.ConfigurationManager;
72 import org.openecomp.sdc.be.dao.api.ActionStatus;
73 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
74 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
75 import org.openecomp.sdc.be.dao.cassandra.SdcSchemaFilesCassandraDao;
76 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
77 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
78 import org.openecomp.sdc.be.impl.ComponentsUtils;
79 import org.openecomp.sdc.be.model.ArtifactDefinition;
80 import org.openecomp.sdc.be.model.Component;
81 import org.openecomp.sdc.be.model.ComponentInstance;
82 import org.openecomp.sdc.be.model.LifecycleStateEnum;
83 import org.openecomp.sdc.be.model.Resource;
84 import org.openecomp.sdc.be.model.Service;
85 import org.openecomp.sdc.be.model.User;
86 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
87 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
88 import org.openecomp.sdc.be.resources.data.DAOArtifactData;
89 import org.openecomp.sdc.be.resources.data.SdcSchemaFilesData;
90 import org.openecomp.sdc.be.tosca.ComponentCache.CacheEntry;
91 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
92 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
93 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
94 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
95 import org.openecomp.sdc.common.api.ConfigurationSource;
96 import org.openecomp.sdc.common.impl.ExternalConfiguration;
97 import org.openecomp.sdc.common.impl.FSConfigurationSource;
98 import org.openecomp.sdc.exception.ResponseFormat;
100 public class CsarUtilsTest extends BeConfDependentTest {
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 public CsarUtilsTest() throws IOException {
127 public void setUpMock() throws Exception {
128 ExternalConfiguration.setAppName("catalog-be");
129 MockitoAnnotations.initMocks(this);
130 initConfigurationManager();
133 private static void initConfigurationManager() {
134 final String confPath = new File(Objects
136 CsarUtilsTest.class.getClassLoader().getResource("config/catalog-be/configuration.yaml"))
137 .getFile()).getParent();
138 final ConfigurationSource confSource =
139 new FSConfigurationSource(ExternalConfiguration.getChangeListener(), confPath);
140 new ConfigurationManager(confSource);
143 private final List<String> nodesFromPackage = Arrays.asList("tosca.nodes.Root", "tosca.nodes.Container.Application");
145 private final byte[] contentData = getFileResource("yamlValidation/resource-serviceTemplate.yml");
148 private NonMetaArtifactInfo createNonMetaArtifactInfoTestSubject() {
149 return new CsarUtils.NonMetaArtifactInfo("mock", "mock", ArtifactTypeEnum.AAI_SERVICE_MODEL.getType(),
150 ArtifactGroupTypeEnum.DEPLOYMENT, new byte[0], "mock", true);
154 public void testCreateCsar() {
155 Component component = new Resource();
156 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
157 ArtifactDefinition artifact = new ArtifactDefinition();
158 artifact.setArtifactName("artifactName");
159 artifact.setEsId("esId");
160 toscaArtifacts.put("assettoscatemplate", artifact);
162 component.setToscaArtifacts(toscaArtifacts);
164 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
165 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
167 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
168 .thenReturn(ActionStatus.GENERAL_ERROR);
170 testSubject.createCsar(component, true, true);
174 public void testCreateCsarWithGenerateCsarZipResponseIsLeft() {
175 Component component = new Resource();
176 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
177 ArtifactDefinition artifact = new ArtifactDefinition();
178 artifact.setArtifactName("artifactName");
179 artifact.setEsId("esId");
180 artifact.setArtifactUUID("artifactUUID");
181 artifact.setArtifactType("YANG");
182 toscaArtifacts.put("assettoscatemplate", artifact);
184 component.setToscaArtifacts(toscaArtifacts);
185 component.setDeploymentArtifacts(toscaArtifacts);
186 component.setArtifacts(toscaArtifacts);
187 DAOArtifactData artifactData = new DAOArtifactData();
188 byte[] data = "value".getBytes();
189 ByteBuffer bufferData = ByteBuffer.wrap(data);
190 artifactData.setData(bufferData);
192 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
193 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
194 toscaTemplate.setDependencies(dependencies);
196 List<SdcSchemaFilesData> filesData = new ArrayList<>();
197 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
198 filedata.setPayloadAsArray(data);
199 filesData.add(filedata);
201 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
203 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
204 .thenReturn(ActionStatus.GENERAL_ERROR);
206 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
207 .thenReturn(Either.left(toscaTemplate));
210 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
211 .thenReturn(Either.left(filesData));
213 testSubject.createCsar(component, false, true);
217 public void testPopulateZipWhenGetDependenciesIsRight() {
218 Component component = new Service();
219 boolean getFromCS = false;
221 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
222 ArtifactDefinition artifact = new ArtifactDefinition();
223 artifact.setArtifactName("artifactName");
224 artifact.setEsId("esId");
225 artifact.setArtifactUUID("artifactUUID");
226 artifact.setArtifactType("YANG");
227 toscaArtifacts.put("assettoscatemplate", artifact);
229 component.setToscaArtifacts(toscaArtifacts);
230 component.setDeploymentArtifacts(toscaArtifacts);
231 component.setArtifacts(toscaArtifacts);
232 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
233 DAOArtifactData artifactData = new DAOArtifactData();
234 byte[] data = "value".getBytes();
235 ByteBuffer bufferData = ByteBuffer.wrap(data);
236 artifactData.setData(bufferData);
238 ToscaRepresentation tosca = new ToscaRepresentation();
239 tosca.setMainYaml("value");
241 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
243 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
245 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
246 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
248 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
249 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
250 } catch (Exception e) {
256 public void testPopulateZipWhenExportComponentIsRight() {
257 Component component = new Resource();
258 boolean getFromCS = false;
260 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
261 ArtifactDefinition artifact = new ArtifactDefinition();
262 artifact.setArtifactName("artifactName");
263 artifact.setEsId("esId");
264 artifact.setArtifactUUID("artifactUUID");
265 artifact.setArtifactType("YANG");
266 toscaArtifacts.put("assettoscatemplate", artifact);
268 component.setToscaArtifacts(toscaArtifacts);
269 component.setDeploymentArtifacts(toscaArtifacts);
270 component.setArtifacts(toscaArtifacts);
271 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
272 DAOArtifactData artifactData = new DAOArtifactData();
273 byte[] data = "value".getBytes();
274 ByteBuffer bufferData = ByteBuffer.wrap(data);
275 artifactData.setData(bufferData);
277 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
278 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
280 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
281 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
282 } catch (Exception e) {
288 public void testPopulateZipWhenComponentIsServiceAndCollectComponentCsarDefinitionIsRight() {
289 Component component = new Service();
290 boolean getFromCS = false;
292 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
293 ArtifactDefinition artifact = new ArtifactDefinition();
294 artifact.setArtifactName("artifactName");
295 artifact.setEsId("esId");
296 artifact.setArtifactUUID("artifactUUID");
297 artifact.setArtifactType("YANG");
298 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
299 artifact.setDescription("description");
300 artifact.setArtifactLabel("artifactLabel");
301 toscaArtifacts.put("assettoscatemplate", artifact);
303 component.setToscaArtifacts(toscaArtifacts);
304 component.setDeploymentArtifacts(toscaArtifacts);
305 component.setArtifacts(toscaArtifacts);
306 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
307 component.setVersion("1.0");
308 component.setLastUpdaterUserId("userId");
309 component.setUniqueId("uid");
310 DAOArtifactData artifactData = new DAOArtifactData();
311 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
312 artifactData.setData(bufferData);
314 List<SdcSchemaFilesData> filesData = new ArrayList<>();
315 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
316 filedata.setPayloadAsArray(contentData);
317 filesData.add(filedata);
319 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
320 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
321 Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
322 dependencies.add(triple);
323 toscaTemplate.setDependencies(dependencies);
325 ToscaRepresentation tosca = new ToscaRepresentation();
326 tosca.setMainYaml("value");
328 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
330 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
332 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
333 .thenReturn(Either.left(toscaTemplate));
336 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
337 .thenReturn(Either.left(filesData));
339 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
340 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
342 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(User.class))).thenReturn(new User());
345 Mockito.when(artifactsBusinessLogic.validateAndHandleArtifact(Mockito.any(String.class),
346 Mockito.any(ComponentTypeEnum.class), Mockito.any(ArtifactOperationInfo.class), Mockito.isNull(),
347 Mockito.any(ArtifactDefinition.class), Mockito.any(String.class), Mockito.any(String.class),
348 Mockito.isNull(), Mockito.isNull(), Mockito.any(User.class), Mockito.any(Component.class),
349 Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(Boolean.class)))
350 .thenReturn(Either.left(Mockito.any(ArtifactDefinition.class)));
352 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
353 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
354 } catch (Exception e) {
360 public void testPopulateZipWhenGetEntryDataIsRight() {
361 Component component = new Service();
362 boolean getFromCS = true;
364 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
365 ArtifactDefinition artifact = new ArtifactDefinition();
366 artifact.setArtifactName("artifactName");
367 artifact.setEsId("esId");
368 artifact.setArtifactUUID("artifactUUID");
369 artifact.setArtifactType("YANG");
370 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
371 artifact.setDescription("description");
372 artifact.setArtifactLabel("artifactLabel");
373 toscaArtifacts.put("assettoscatemplate", artifact);
375 component.setToscaArtifacts(toscaArtifacts);
376 component.setDeploymentArtifacts(toscaArtifacts);
377 component.setArtifacts(toscaArtifacts);
378 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
379 component.setVersion("1.0");
380 component.setLastUpdaterUserId("userId");
381 component.setUniqueId("uid");
382 DAOArtifactData artifactData = new DAOArtifactData();
383 byte[] data = "value".getBytes();
384 ByteBuffer bufferData = ByteBuffer.wrap(data);
385 artifactData.setData(bufferData);
387 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
388 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
389 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
390 dependencies.add(triple);
391 toscaTemplate.setDependencies(dependencies);
393 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
395 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
396 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
398 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
399 .thenReturn(Either.left(toscaTemplate));
401 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
402 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
403 } catch (Exception e) {
409 public void testPopulateZipWhenGetEntryDataOfInnerComponentIsRight() {
410 Component component = new Service();
411 boolean getFromCS = false;
413 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
414 ArtifactDefinition artifact = new ArtifactDefinition();
415 artifact.setArtifactName("artifactName");
416 artifact.setEsId("esId");
417 artifact.setArtifactUUID("artifactUUID");
418 artifact.setArtifactType("YANG");
419 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
420 artifact.setDescription("description");
421 artifact.setArtifactLabel("artifactLabel");
422 toscaArtifacts.put("assettoscatemplate", artifact);
424 component.setToscaArtifacts(toscaArtifacts);
425 component.setDeploymentArtifacts(toscaArtifacts);
426 component.setArtifacts(toscaArtifacts);
427 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
428 component.setVersion("1.0");
429 component.setLastUpdaterUserId("userId");
430 component.setUniqueId("uid");
431 DAOArtifactData artifactData = new DAOArtifactData();
432 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
433 artifactData.setData(bufferData);
435 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
436 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
437 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
438 dependencies.add(triple);
439 toscaTemplate.setDependencies(dependencies);
441 ToscaRepresentation tosca = new ToscaRepresentation();
442 tosca.setMainYaml(new String(contentData, StandardCharsets.UTF_8));
444 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
446 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca),
447 Either.left(tosca), Either.right(ToscaError.GENERAL_ERROR));
449 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
450 .thenReturn(Either.left(toscaTemplate));
452 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
453 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
454 } catch (Exception e) {
460 public void testPopulateZipWhenLatestSchemaFilesFromCassandraIsRight() {
461 Component component = new Service();
462 boolean getFromCS = false;
464 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
465 ArtifactDefinition artifact = new ArtifactDefinition();
466 artifact.setArtifactName("artifactName");
467 artifact.setEsId("esId");
468 artifact.setArtifactUUID("artifactUUID");
469 artifact.setArtifactType("YANG");
470 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
471 artifact.setDescription("description");
472 artifact.setArtifactLabel("artifactLabel");
473 toscaArtifacts.put("assettoscatemplate", artifact);
475 component.setToscaArtifacts(toscaArtifacts);
476 component.setDeploymentArtifacts(toscaArtifacts);
477 component.setArtifacts(toscaArtifacts);
478 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
479 component.setVersion("1.0");
480 component.setLastUpdaterUserId("userId");
481 component.setUniqueId("uid");
482 DAOArtifactData artifactData = new DAOArtifactData();
483 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
484 artifactData.setData(bufferData);
486 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
487 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
488 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
489 dependencies.add(triple);
490 toscaTemplate.setDependencies(dependencies);
492 ToscaRepresentation tosca = new ToscaRepresentation();
493 tosca.setMainYaml(new String(contentData, StandardCharsets.UTF_8));
495 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
497 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
499 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
500 .thenReturn(Either.left(toscaTemplate));
503 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
504 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
506 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
507 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
508 } catch (Exception e) {
514 public void testAddInnerComponentsToCache() {
515 ComponentCache componentCache = ComponentCache.overwritable(overwriteIfSameVersions());
516 Component childComponent = new Resource();
517 Component componentRI = new Service();
518 List<ComponentInstance> componentInstances = new ArrayList<>();
519 ComponentInstance instance = new ComponentInstance();
520 instance.setComponentUid("resourceUid");
521 componentInstances.add(instance);
522 childComponent.setComponentInstances(componentInstances);
524 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
525 ArtifactDefinition artifact = new ArtifactDefinition();
526 artifact.setArtifactName("artifactName");
527 artifact.setEsId("esId");
528 artifact.setArtifactUUID("artifactUUID");
529 artifact.setArtifactType("YANG");
530 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
531 artifact.setDescription("description");
532 artifact.setArtifactLabel("artifactLabel");
533 toscaArtifacts.put("assettoscatemplate", artifact);
535 componentRI.setToscaArtifacts(toscaArtifacts);
537 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
538 .thenReturn(Either.left(componentRI));
540 Deencapsulation.invoke(testSubject, "addInnerComponentsToCache", componentCache, childComponent);
542 io.vavr.collection.List<CacheEntry> expected = io.vavr.collection.List.of(entry("esId","artifactName",componentRI));
543 assertEquals(expected, componentCache.all().toList());
547 public void testAddInnerComponentsToCacheWhenGetToscaElementIsRight() {
548 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
549 Component childComponent = new Resource();
551 List<ComponentInstance> componentInstances = new ArrayList<>();
552 ComponentInstance instance = new ComponentInstance();
553 instance.setComponentUid("abc");
554 componentInstances.add(instance);
555 childComponent.setComponentInstances(componentInstances);
557 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
558 ArtifactDefinition artifact = new ArtifactDefinition();
559 artifact.setArtifactName("artifactName");
560 artifact.setEsId("esId");
561 artifact.setArtifactUUID("artifactUUID");
562 artifact.setArtifactType("YANG");
563 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
564 artifact.setDescription("description");
565 artifact.setArtifactLabel("artifactLabel");
566 toscaArtifacts.put("assettoscatemplate", artifact);
568 Component componentRI = new Service();
570 componentRI.setToscaArtifacts(toscaArtifacts);
572 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
573 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
576 assertTrue(componentCache.isEmpty());
580 public void testWriteComponentInterface() throws IOException {
581 String fileName = "name.hello";
582 ToscaRepresentation tosca = new ToscaRepresentation();
583 tosca.setMainYaml("value");
585 Mockito.when(toscaExportUtils.exportComponentInterface(Mockito.any(Component.class), Mockito.any(Boolean.class)))
586 .thenReturn(Either.left(tosca));
589 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
590 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "writeComponentInterface", new Resource(), zip, fileName, false);
592 assertNotNull(output);
593 assertTrue(output.isLeft());
598 public 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 public 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 public 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 public 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 public 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 public 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 public 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 public 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 public void testNonMetaArtifactInfoCtor() {
706 createNonMetaArtifactInfoTestSubject();
710 public void testNonMetaArtifactInfoGetPath() {
711 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
713 testSubject.getPath();
717 public void testNonMetaArtifactInfoGetArtifactName() {
718 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
720 testSubject.getArtifactName();
724 public 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 public void testNonMetaArtifactInfoGetDisplayName() {
732 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
734 testSubject.getDisplayName();
738 public void testNonMetaArtifactInfoGetArtifactGroupType() {
739 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
741 testSubject.getArtifactGroupType();
745 public void testNonMetaArtifactInfoGetArtifactLabel() {
746 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
748 testSubject.getArtifactLabel();
752 public void testNonMetaArtifactInfoGetIsFromCsar() {
753 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
755 testSubject.isFromCsar();
759 public void testNonMetaArtifactInfoGetPayloadData() {
760 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
762 testSubject.getPayloadData();
766 public void testNonMetaArtifactInfoGetArtifaactChecksum() {
767 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
769 testSubject.getArtifactChecksum();
773 public void testNonMetaArtifactInfoGetArtifactUniqueId() {
774 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
776 testSubject.getArtifactUniqueId();
780 public void testNonMetaArtifactInfosetArtifactUniqueId() {
781 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
783 testSubject.setArtifactUniqueId("artifactUniqueId");
787 public void testValidateNonMetaArtifactWithExceptionCaught() {
788 CsarUtils.validateNonMetaArtifact("", new byte[0], new HashMap<>());
792 public 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 public 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 public 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 public 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 public 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 assertTrue(collectedWarningMessages.size() == 1);
931 assertTrue(collectedWarningMessages.values().iterator().next().size() == 2);
935 public 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 assertTrue(!collectedWarningMessages.isEmpty());
945 @Test(expected = IOException.class)
946 public void testAddSchemaFilesFromCassandraAddingDuplicatedEntry() throws IOException {
947 final String rootPath = System.getProperty("user.dir");
948 final Path path = Paths.get(rootPath + "/src/test/resources/sdc.zip");
950 final byte[] data = Files.readAllBytes(path);
951 try (final ByteArrayOutputStream out = new ByteArrayOutputStream();
952 final ZipOutputStream zip = new ZipOutputStream(out);) {
953 Deencapsulation.invoke(testSubject, "addSchemaFilesFromCassandra",
954 zip, data, nodesFromPackage);
955 zip.putNextEntry(new ZipEntry("Definitions/nodes.yml"));
958 } catch (final IOException e) {
959 Assert.assertTrue("duplicate entry: Definitions/nodes.yml".equals(e.getMessage()));
960 throw new IOException("Could not add Schema Files From Cassandra", e);
965 public void testFindNonRootNodesFromPackage() {
966 final Resource resource = new Resource();
967 resource.setDerivedList(nodesFromPackage);
968 final Component component = resource;
969 final List<Triple<String, String, Component>> dependencies = new ArrayList<>();
970 final Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
971 dependencies.add(triple);
972 final List<String> expectedResult = Arrays.asList("tosca.nodes.Container.Application");
973 final List<String> result = Deencapsulation.invoke(testSubject,
974 "findNonRootNodesFromPackage", dependencies);
975 assertTrue(CollectionUtils.isNotEmpty(result));
976 assertEquals(expectedResult, result);
979 private byte[] getFileResource(final String filePath) throws IOException {
980 try (final InputStream inputStream = getFileResourceAsInputStream(filePath)) {
981 return IOUtils.toByteArray(inputStream);
985 private InputStream getFileResourceAsInputStream(final String filePath) {
986 return Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);