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 = ToscaRepresentation.make("value".getBytes());
240 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
242 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
244 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
245 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
247 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
248 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
249 } catch (Exception e) {
255 public void testPopulateZipWhenExportComponentIsRight() {
256 Component component = new Resource();
257 boolean getFromCS = false;
259 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
260 ArtifactDefinition artifact = new ArtifactDefinition();
261 artifact.setArtifactName("artifactName");
262 artifact.setEsId("esId");
263 artifact.setArtifactUUID("artifactUUID");
264 artifact.setArtifactType("YANG");
265 toscaArtifacts.put("assettoscatemplate", artifact);
267 component.setToscaArtifacts(toscaArtifacts);
268 component.setDeploymentArtifacts(toscaArtifacts);
269 component.setArtifacts(toscaArtifacts);
270 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
271 DAOArtifactData artifactData = new DAOArtifactData();
272 byte[] data = "value".getBytes();
273 ByteBuffer bufferData = ByteBuffer.wrap(data);
274 artifactData.setData(bufferData);
276 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
277 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
279 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
280 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
281 } catch (Exception e) {
287 public void testPopulateZipWhenComponentIsServiceAndCollectComponentCsarDefinitionIsRight() {
288 Component component = new Service();
289 boolean getFromCS = false;
291 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
292 ArtifactDefinition artifact = new ArtifactDefinition();
293 artifact.setArtifactName("artifactName");
294 artifact.setEsId("esId");
295 artifact.setArtifactUUID("artifactUUID");
296 artifact.setArtifactType("YANG");
297 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
298 artifact.setDescription("description");
299 artifact.setArtifactLabel("artifactLabel");
300 toscaArtifacts.put("assettoscatemplate", artifact);
302 component.setToscaArtifacts(toscaArtifacts);
303 component.setDeploymentArtifacts(toscaArtifacts);
304 component.setArtifacts(toscaArtifacts);
305 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
306 component.setVersion("1.0");
307 component.setLastUpdaterUserId("userId");
308 component.setUniqueId("uid");
309 DAOArtifactData artifactData = new DAOArtifactData();
310 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
311 artifactData.setData(bufferData);
313 List<SdcSchemaFilesData> filesData = new ArrayList<>();
314 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
315 filedata.setPayloadAsArray(contentData);
316 filesData.add(filedata);
318 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
319 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
320 Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
321 dependencies.add(triple);
322 toscaTemplate.setDependencies(dependencies);
324 ToscaRepresentation tosca = ToscaRepresentation.make("value".getBytes());
326 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
328 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
330 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
331 .thenReturn(Either.left(toscaTemplate));
334 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
335 .thenReturn(Either.left(filesData));
337 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
338 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
340 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(User.class))).thenReturn(new User());
343 Mockito.when(artifactsBusinessLogic.validateAndHandleArtifact(Mockito.any(String.class),
344 Mockito.any(ComponentTypeEnum.class), Mockito.any(ArtifactOperationInfo.class), Mockito.isNull(),
345 Mockito.any(ArtifactDefinition.class), Mockito.any(String.class), Mockito.any(String.class),
346 Mockito.isNull(), Mockito.isNull(), Mockito.any(User.class), Mockito.any(Component.class),
347 Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(Boolean.class)))
348 .thenReturn(Either.left(Mockito.any(ArtifactDefinition.class)));
350 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
351 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
352 } catch (Exception e) {
358 public void testPopulateZipWhenGetEntryDataIsRight() {
359 Component component = new Service();
360 boolean getFromCS = true;
362 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
363 ArtifactDefinition artifact = new ArtifactDefinition();
364 artifact.setArtifactName("artifactName");
365 artifact.setEsId("esId");
366 artifact.setArtifactUUID("artifactUUID");
367 artifact.setArtifactType("YANG");
368 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
369 artifact.setDescription("description");
370 artifact.setArtifactLabel("artifactLabel");
371 toscaArtifacts.put("assettoscatemplate", artifact);
373 component.setToscaArtifacts(toscaArtifacts);
374 component.setDeploymentArtifacts(toscaArtifacts);
375 component.setArtifacts(toscaArtifacts);
376 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
377 component.setVersion("1.0");
378 component.setLastUpdaterUserId("userId");
379 component.setUniqueId("uid");
380 DAOArtifactData artifactData = new DAOArtifactData();
381 byte[] data = "value".getBytes();
382 ByteBuffer bufferData = ByteBuffer.wrap(data);
383 artifactData.setData(bufferData);
385 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
386 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
387 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
388 dependencies.add(triple);
389 toscaTemplate.setDependencies(dependencies);
391 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
393 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
394 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
396 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
397 .thenReturn(Either.left(toscaTemplate));
399 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
400 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
401 } catch (Exception e) {
407 public void testPopulateZipWhenGetEntryDataOfInnerComponentIsRight() {
408 Component component = new Service();
409 boolean getFromCS = false;
411 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
412 ArtifactDefinition artifact = new ArtifactDefinition();
413 artifact.setArtifactName("artifactName");
414 artifact.setEsId("esId");
415 artifact.setArtifactUUID("artifactUUID");
416 artifact.setArtifactType("YANG");
417 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
418 artifact.setDescription("description");
419 artifact.setArtifactLabel("artifactLabel");
420 toscaArtifacts.put("assettoscatemplate", artifact);
422 component.setToscaArtifacts(toscaArtifacts);
423 component.setDeploymentArtifacts(toscaArtifacts);
424 component.setArtifacts(toscaArtifacts);
425 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
426 component.setVersion("1.0");
427 component.setLastUpdaterUserId("userId");
428 component.setUniqueId("uid");
429 DAOArtifactData artifactData = new DAOArtifactData();
430 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
431 artifactData.setData(bufferData);
433 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
434 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
435 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
436 dependencies.add(triple);
437 toscaTemplate.setDependencies(dependencies);
439 ToscaRepresentation tosca = ToscaRepresentation.make(contentData);
441 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
443 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca),
444 Either.left(tosca), Either.right(ToscaError.GENERAL_ERROR));
446 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
447 .thenReturn(Either.left(toscaTemplate));
449 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
450 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
451 } catch (Exception e) {
457 public void testPopulateZipWhenLatestSchemaFilesFromCassandraIsRight() {
458 Component component = new Service();
459 boolean getFromCS = false;
461 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
462 ArtifactDefinition artifact = new ArtifactDefinition();
463 artifact.setArtifactName("artifactName");
464 artifact.setEsId("esId");
465 artifact.setArtifactUUID("artifactUUID");
466 artifact.setArtifactType("YANG");
467 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
468 artifact.setDescription("description");
469 artifact.setArtifactLabel("artifactLabel");
470 toscaArtifacts.put("assettoscatemplate", artifact);
472 component.setToscaArtifacts(toscaArtifacts);
473 component.setDeploymentArtifacts(toscaArtifacts);
474 component.setArtifacts(toscaArtifacts);
475 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
476 component.setVersion("1.0");
477 component.setLastUpdaterUserId("userId");
478 component.setUniqueId("uid");
479 DAOArtifactData artifactData = new DAOArtifactData();
480 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
481 artifactData.setData(bufferData);
483 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
484 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
485 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
486 dependencies.add(triple);
487 toscaTemplate.setDependencies(dependencies);
489 ToscaRepresentation tosca = ToscaRepresentation.make(contentData);
491 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
493 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
495 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
496 .thenReturn(Either.left(toscaTemplate));
499 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
500 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
502 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
503 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
504 } catch (Exception e) {
510 public void testAddInnerComponentsToCache() {
511 ComponentCache componentCache = ComponentCache.overwritable(overwriteIfSameVersions());
512 Component childComponent = new Resource();
513 Component componentRI = new Service();
514 List<ComponentInstance> componentInstances = new ArrayList<>();
515 ComponentInstance instance = new ComponentInstance();
516 instance.setComponentUid("resourceUid");
517 componentInstances.add(instance);
518 childComponent.setComponentInstances(componentInstances);
520 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
521 ArtifactDefinition artifact = new ArtifactDefinition();
522 artifact.setArtifactName("artifactName");
523 artifact.setEsId("esId");
524 artifact.setArtifactUUID("artifactUUID");
525 artifact.setArtifactType("YANG");
526 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
527 artifact.setDescription("description");
528 artifact.setArtifactLabel("artifactLabel");
529 toscaArtifacts.put("assettoscatemplate", artifact);
531 componentRI.setToscaArtifacts(toscaArtifacts);
533 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
534 .thenReturn(Either.left(componentRI));
536 Deencapsulation.invoke(testSubject, "addInnerComponentsToCache", componentCache, childComponent);
538 io.vavr.collection.List<CacheEntry> expected = io.vavr.collection.List.of(entry("esId","artifactName",componentRI));
539 assertEquals(expected, componentCache.all().toList());
543 public void testAddInnerComponentsToCacheWhenGetToscaElementIsRight() {
544 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
545 Component childComponent = new Resource();
547 List<ComponentInstance> componentInstances = new ArrayList<>();
548 ComponentInstance instance = new ComponentInstance();
549 instance.setComponentUid("abc");
550 componentInstances.add(instance);
551 childComponent.setComponentInstances(componentInstances);
553 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
554 ArtifactDefinition artifact = new ArtifactDefinition();
555 artifact.setArtifactName("artifactName");
556 artifact.setEsId("esId");
557 artifact.setArtifactUUID("artifactUUID");
558 artifact.setArtifactType("YANG");
559 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
560 artifact.setDescription("description");
561 artifact.setArtifactLabel("artifactLabel");
562 toscaArtifacts.put("assettoscatemplate", artifact);
564 Component componentRI = new Service();
566 componentRI.setToscaArtifacts(toscaArtifacts);
568 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
569 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
572 assertTrue(componentCache.isEmpty());
576 public void testWriteComponentInterface() throws IOException {
577 String fileName = "name.hello";
578 ToscaRepresentation tosca = ToscaRepresentation.make("value".getBytes());
580 Mockito.when(toscaExportUtils.exportComponentInterface(Mockito.any(Component.class), Mockito.any(Boolean.class)))
581 .thenReturn(Either.left(tosca));
584 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
585 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "writeComponentInterface", new Resource(), zip, fileName, false);
587 assertNotNull(output);
588 assertTrue(output.isLeft());
593 public void testGetEntryData() {
594 String cassandraId = "id";
595 Component childComponent = new Resource();
597 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
598 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
600 Either<byte[], ActionStatus> output = Deencapsulation.invoke(testSubject, "getEntryData", cassandraId, childComponent);
602 assertNotNull(output);
603 assertTrue(output.isRight());
607 public void testGetLatestSchemaFilesFromCassandraWhenListOfSchemasIsEmpty() {
608 List<SdcSchemaFilesData> filesData = new ArrayList<>();
611 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
612 .thenReturn(Either.left(filesData));
614 Either<byte[], ResponseFormat> output = Deencapsulation.invoke(testSubject, "getLatestSchemaFilesFromCassandra");
616 assertNotNull(output);
617 assertTrue(output.isRight());
621 public void testExtractVfcsArtifactsFromCsar() {
622 String key = "Artifacts/org.openecomp.resource.some/Deployment/to/resource";
623 byte[] data = "value".getBytes();
625 Map<String, byte[]> csar = new HashMap<>();
628 Map<String, List<ArtifactDefinition>> output = CsarUtils.extractVfcsArtifactsFromCsar(csar);
630 assertNotNull(output);
631 assertTrue(output.containsKey("org.openecomp.resource.some"));
632 assertEquals(1, output.get("org.openecomp.resource.some").size());
636 public void testAddExtractedVfcArtifactWhenArtifactsContainsExtractedArtifactKey() {
637 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
638 "key", new ArtifactDefinition());
639 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
640 artifacts.put("key", new ArrayList<>());
642 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
644 assertEquals(1, artifacts.get("key").size());
648 public void testAddExtractedVfcArtifactWhenArtifactsDoesntContainsExtractedArtifactKey() {
649 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
650 "key", new ArtifactDefinition());
651 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
652 artifacts.put("key1", new ArrayList<>());
654 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
656 assertEquals(0, artifacts.get("key1").size());
657 assertEquals(1, artifacts.get("key").size());
658 assertEquals(2, artifacts.size());
662 public void testExtractVfcArtifact() {
663 String path = "path/to/informational/artificat";
664 Map<String, byte[]> map = new HashMap<>();
665 map.put(path, "value".getBytes());
666 Entry<String, byte[]> entry = map.entrySet().iterator().next();
668 Optional<ImmutablePair<String, ArtifactDefinition>> output =
669 Deencapsulation.invoke(testSubject, "extractVfcArtifact", entry, new HashMap<>());
671 if(output.isPresent()) {
672 assertEquals("to", output.get().left);
674 fail("`output` is empty!");
679 public void testDetectArtifactGroupTypeWithExceptionBeingCaught() {
680 Either<ArtifactGroupTypeEnum, Boolean> output = Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", Map.class);
682 assertNotNull(output);
683 assertTrue(output.isRight());
684 assertFalse(output.right().value());
688 public void testDetectArtifactGroupTypeWWhenCollectedWarningMessagesContainesKey() {
689 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
691 collectedWarningMessages.put("Warning - unrecognized artifact group type {} was received.", new HashSet<>());
692 Either<ArtifactGroupTypeEnum, Boolean> output = Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", collectedWarningMessages);
694 assertNotNull(output);
695 assertTrue(output.isRight());
696 assertFalse(output.right().value());
700 public void testNonMetaArtifactInfoCtor() {
701 createNonMetaArtifactInfoTestSubject();
705 public void testNonMetaArtifactInfoGetPath() {
706 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
708 testSubject.getPath();
712 public void testNonMetaArtifactInfoGetArtifactName() {
713 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
715 testSubject.getArtifactName();
719 public void testNonMetaArtifactInfoGetArtifactType() {
720 final NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
721 assertThat("The artifact type should be as expected",
722 testSubject.getArtifactType(), is(ArtifactTypeEnum.AAI_SERVICE_MODEL.getType()));
726 public void testNonMetaArtifactInfoGetDisplayName() {
727 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
729 testSubject.getDisplayName();
733 public void testNonMetaArtifactInfoGetArtifactGroupType() {
734 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
736 testSubject.getArtifactGroupType();
740 public void testNonMetaArtifactInfoGetArtifactLabel() {
741 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
743 testSubject.getArtifactLabel();
747 public void testNonMetaArtifactInfoGetIsFromCsar() {
748 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
750 testSubject.isFromCsar();
754 public void testNonMetaArtifactInfoGetPayloadData() {
755 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
757 testSubject.getPayloadData();
761 public void testNonMetaArtifactInfoGetArtifaactChecksum() {
762 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
764 testSubject.getArtifactChecksum();
768 public void testNonMetaArtifactInfoGetArtifactUniqueId() {
769 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
771 testSubject.getArtifactUniqueId();
775 public void testNonMetaArtifactInfosetArtifactUniqueId() {
776 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
778 testSubject.setArtifactUniqueId("artifactUniqueId");
782 public void testValidateNonMetaArtifactWithExceptionCaught() {
783 CsarUtils.validateNonMetaArtifact("", new byte[0], new HashMap<>());
787 public void testCollectComponentCsarDefinitionWhenComponentIsServiceAndGetToscaElementIsLeft() {
788 Component component = new Service();
789 component.setUniqueId("uniqueId");
790 List<ComponentInstance> resourceInstances = new ArrayList<>();
791 ComponentInstance instance = new ComponentInstance();
792 instance.setComponentUid("resourceUid");
793 instance.setOriginType(OriginTypeEnum.SERVICE);
794 resourceInstances.add(instance);
795 component.setComponentInstances(resourceInstances);
797 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
798 ArtifactDefinition artifact = new ArtifactDefinition();
799 artifact.setArtifactName("artifactName");
800 artifact.setEsId("esId");
801 artifact.setArtifactUUID("artifactUUID");
802 artifact.setArtifactType("YANG");
803 toscaArtifacts.put("assettoscatemplate", artifact);
805 component.setToscaArtifacts(toscaArtifacts);
806 component.setDeploymentArtifacts(toscaArtifacts);
807 component.setArtifacts(toscaArtifacts);
809 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
810 Either.right(StorageOperationStatus.BAD_REQUEST));
812 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
814 assertNotNull(output);
815 assertTrue(output.isRight());
819 public void testCollectComponentTypeArtifactsWhenFetchedComponentHasComponentInstances() {
820 Component component = new Service();
821 Component fetchedComponent = new Resource();
822 component.setUniqueId("uniqueId");
823 List<ComponentInstance> resourceInstances = new ArrayList<>();
824 ComponentInstance instance = new ComponentInstance();
825 instance.setComponentUid("resourceUid");
826 instance.setOriginType(OriginTypeEnum.SERVICE);
827 resourceInstances.add(instance);
828 component.setComponentInstances(resourceInstances);
829 fetchedComponent.setComponentInstances(resourceInstances);
831 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
832 ArtifactDefinition artifact = new ArtifactDefinition();
833 artifact.setArtifactName("artifactName");
834 artifact.setEsId("esId");
835 artifact.setArtifactUUID("artifactUUID");
836 artifact.setArtifactType("YANG");
837 toscaArtifacts.put("assettoscatemplate", artifact);
839 component.setToscaArtifacts(toscaArtifacts);
840 component.setDeploymentArtifacts(toscaArtifacts);
841 component.setArtifacts(toscaArtifacts);
843 fetchedComponent.setToscaArtifacts(toscaArtifacts);
844 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
845 fetchedComponent.setArtifacts(toscaArtifacts);
847 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
848 Either.left(fetchedComponent), Either.right(StorageOperationStatus.BAD_REQUEST));
850 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
852 assertNotNull(output);
853 assertTrue(output.isRight());
857 public void testCollectComponentTypeArtifactsWhenFetchedComponentDontHaveComponentInstances() {
858 Component component = new Service();
859 Component fetchedComponent = new Resource();
860 component.setUniqueId("uniqueId");
861 List<ComponentInstance> resourceInstances = new ArrayList<>();
862 ComponentInstance instance = new ComponentInstance();
863 instance.setComponentUid("resourceUid");
864 instance.setOriginType(OriginTypeEnum.SERVICE);
866 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
867 ArtifactDefinition artifact = new ArtifactDefinition();
868 artifact.setArtifactName("artifactName");
869 artifact.setEsId("esId");
870 artifact.setArtifactUUID("artifactUUID");
871 artifact.setArtifactType("PLAN");
872 toscaArtifacts.put("assettoscatemplate", artifact);
874 instance.setDeploymentArtifacts(toscaArtifacts);
876 resourceInstances.add(instance);
877 component.setComponentInstances(resourceInstances);
879 component.setToscaArtifacts(toscaArtifacts);
880 component.setDeploymentArtifacts(toscaArtifacts);
881 component.setArtifacts(toscaArtifacts);
883 fetchedComponent.setToscaArtifacts(toscaArtifacts);
884 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
885 fetchedComponent.setArtifacts(toscaArtifacts);
887 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
888 Either.left(fetchedComponent));
890 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
892 assertNotNull(output);
893 assertTrue(output.isLeft());
897 public void testValidateNonMetaArtifactHappyScenario() {
898 String artifactPath = "Artifacts/Deployment/YANG_XML/myYang.xml";
899 byte[] payloadData = "some payload data".getBytes();
900 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
901 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
902 payloadData, collectedWarningMessages);
903 assertTrue(eitherNonMetaArtifact.isLeft());
904 assertTrue(collectedWarningMessages.isEmpty());
906 artifactPath = "Artifacts/Informational/OTHER/someArtifact.xml";
907 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
908 assertTrue(eitherNonMetaArtifact.isLeft());
909 assertTrue(collectedWarningMessages.isEmpty());
913 public void testValidateNonMetaArtifactScenarioWithWarnnings() {
914 String artifactPath = "Artifacts/Deployment/Buga/myYang.xml";
915 byte[] payloadData = "some payload data".getBytes();
916 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
917 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
918 payloadData, collectedWarningMessages);
919 assertTrue(eitherNonMetaArtifact.isLeft());
921 artifactPath = "Artifacts/Informational/Buga2/someArtifact.xml";
922 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
923 assertTrue(eitherNonMetaArtifact.isLeft());
925 assertTrue(collectedWarningMessages.size() == 1);
926 assertTrue(collectedWarningMessages.values().iterator().next().size() == 2);
930 public void testValidateNonMetaArtifactUnhappyScenario() {
931 String artifactPath = "Artifacts/Buga/YANG_XML/myYang.xml";
932 byte[] payloadData = "some payload data".getBytes();
933 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
934 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
935 payloadData, collectedWarningMessages);
936 assertTrue(eitherNonMetaArtifact.isRight());
937 assertTrue(!collectedWarningMessages.isEmpty());
940 @Test(expected = IOException.class)
941 public void testAddSchemaFilesFromCassandraAddingDuplicatedEntry() throws IOException {
942 final String rootPath = System.getProperty("user.dir");
943 final Path path = Paths.get(rootPath + "/src/test/resources/sdc.zip");
945 final byte[] data = Files.readAllBytes(path);
946 try (final ByteArrayOutputStream out = new ByteArrayOutputStream();
947 final ZipOutputStream zip = new ZipOutputStream(out);) {
948 Deencapsulation.invoke(testSubject, "addSchemaFilesFromCassandra",
949 zip, data, nodesFromPackage);
950 zip.putNextEntry(new ZipEntry("Definitions/nodes.yml"));
953 } catch (final IOException e) {
954 Assert.assertTrue("duplicate entry: Definitions/nodes.yml".equals(e.getMessage()));
955 throw new IOException("Could not add Schema Files From Cassandra", e);
960 public 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);