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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
28 import fj.data.Either;
29 import java.io.IOException;
30 import java.nio.ByteBuffer;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.List;
40 import java.util.Map.Entry;
42 import java.util.zip.ZipEntry;
43 import java.util.zip.ZipOutputStream;
44 import mockit.Deencapsulation;
45 import org.apache.commons.collections.CollectionUtils;
46 import org.apache.commons.io.output.ByteArrayOutputStream;
47 import org.apache.commons.lang3.tuple.ImmutablePair;
48 import org.apache.commons.lang3.tuple.ImmutableTriple;
49 import org.apache.commons.lang3.tuple.Triple;
50 import org.junit.Assert;
51 import org.junit.Before;
52 import org.junit.Test;
53 import org.mockito.InjectMocks;
54 import org.mockito.Mock;
55 import org.mockito.Mockito;
56 import org.mockito.MockitoAnnotations;
57 import org.openecomp.sdc.be.components.BeConfDependentTest;
58 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
59 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationInfo;
60 import org.openecomp.sdc.be.dao.api.ActionStatus;
61 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
62 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
63 import org.openecomp.sdc.be.dao.cassandra.SdcSchemaFilesCassandraDao;
64 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
65 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
66 import org.openecomp.sdc.be.impl.ComponentsUtils;
67 import org.openecomp.sdc.be.model.ArtifactDefinition;
68 import org.openecomp.sdc.be.model.Component;
69 import org.openecomp.sdc.be.model.ComponentInstance;
70 import org.openecomp.sdc.be.model.LifecycleStateEnum;
71 import org.openecomp.sdc.be.model.Resource;
72 import org.openecomp.sdc.be.model.Service;
73 import org.openecomp.sdc.be.model.User;
74 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
75 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
76 import org.openecomp.sdc.be.resources.data.DAOArtifactData;
77 import org.openecomp.sdc.be.resources.data.SdcSchemaFilesData;
78 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
79 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
80 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
81 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
82 import org.openecomp.sdc.common.impl.ExternalConfiguration;
83 import org.openecomp.sdc.exception.ResponseFormat;
85 public class CsarUtilsTest extends BeConfDependentTest {
88 CsarUtils testSubject;
91 private ArtifactCassandraDao artifactCassandraDao;
94 private ComponentsUtils componentsUtils;
97 private ToscaExportHandler toscaExportUtils;
100 private SdcSchemaFilesCassandraDao sdcSchemaFilesCassandraDao;
103 private ToscaOperationFacade toscaOperationFacade;
106 private ArtifactsBusinessLogic artifactsBusinessLogic;
109 public void setUpMock() throws Exception {
110 ExternalConfiguration.setAppName("catalog-be");
111 MockitoAnnotations.initMocks(this);
115 private final List<String> nodesFromPackage = Arrays.asList("tosca.nodes.Root", "tosca.nodes.Container.Application");
117 private NonMetaArtifactInfo createNonMetaArtifactInfoTestSubject() {
118 return new CsarUtils.NonMetaArtifactInfo("mock", "mock", ArtifactTypeEnum.AAI_SERVICE_MODEL,
119 ArtifactGroupTypeEnum.DEPLOYMENT, new byte[0], "mock", true);
123 public void testCreateCsar() {
124 Component component = new Resource();
125 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
126 ArtifactDefinition artifact = new ArtifactDefinition();
127 artifact.setArtifactName("artifactName");
128 artifact.setEsId("esId");
129 toscaArtifacts.put("assettoscatemplate", artifact);
131 component.setToscaArtifacts(toscaArtifacts);
133 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
134 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
136 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
137 .thenReturn(ActionStatus.GENERAL_ERROR);
139 testSubject.createCsar(component, true, true);
143 public void testCreateCsarWithGenerateCsarZipResponseIsLeft() {
144 Component component = new Resource();
145 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
146 ArtifactDefinition artifact = new ArtifactDefinition();
147 artifact.setArtifactName("artifactName");
148 artifact.setEsId("esId");
149 artifact.setArtifactUUID("artifactUUID");
150 artifact.setArtifactType("YANG");
151 toscaArtifacts.put("assettoscatemplate", artifact);
153 component.setToscaArtifacts(toscaArtifacts);
154 component.setDeploymentArtifacts(toscaArtifacts);
155 component.setArtifacts(toscaArtifacts);
156 DAOArtifactData artifactData = new DAOArtifactData();
157 byte[] data = "value".getBytes();
158 ByteBuffer bufferData = ByteBuffer.wrap(data);
159 artifactData.setData(bufferData);
161 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
162 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
163 toscaTemplate.setDependencies(dependencies);
165 List<SdcSchemaFilesData> filesData = new ArrayList<>();
166 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
167 filedata.setPayloadAsArray(data);
168 filesData.add(filedata);
170 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
172 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
173 .thenReturn(ActionStatus.GENERAL_ERROR);
175 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
176 .thenReturn(Either.left(toscaTemplate));
179 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
180 .thenReturn(Either.left(filesData));
182 testSubject.createCsar(component, false, true);
186 public void testPopulateZipWhenGetDependenciesIsRight() {
187 Component component = new Service();
188 boolean getFromCS = false;
190 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
191 ArtifactDefinition artifact = new ArtifactDefinition();
192 artifact.setArtifactName("artifactName");
193 artifact.setEsId("esId");
194 artifact.setArtifactUUID("artifactUUID");
195 artifact.setArtifactType("YANG");
196 toscaArtifacts.put("assettoscatemplate", artifact);
198 component.setToscaArtifacts(toscaArtifacts);
199 component.setDeploymentArtifacts(toscaArtifacts);
200 component.setArtifacts(toscaArtifacts);
201 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
202 DAOArtifactData artifactData = new DAOArtifactData();
203 byte[] data = "value".getBytes();
204 ByteBuffer bufferData = ByteBuffer.wrap(data);
205 artifactData.setData(bufferData);
207 ToscaRepresentation tosca = new ToscaRepresentation();
208 tosca.setMainYaml("value");
210 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
212 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
214 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
215 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
217 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
218 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
219 } catch (Exception e) {
225 public void testPopulateZipWhenExportComponentIsRight() {
226 Component component = new Resource();
227 boolean getFromCS = false;
229 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
230 ArtifactDefinition artifact = new ArtifactDefinition();
231 artifact.setArtifactName("artifactName");
232 artifact.setEsId("esId");
233 artifact.setArtifactUUID("artifactUUID");
234 artifact.setArtifactType("YANG");
235 toscaArtifacts.put("assettoscatemplate", artifact);
237 component.setToscaArtifacts(toscaArtifacts);
238 component.setDeploymentArtifacts(toscaArtifacts);
239 component.setArtifacts(toscaArtifacts);
240 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
241 DAOArtifactData artifactData = new DAOArtifactData();
242 byte[] data = "value".getBytes();
243 ByteBuffer bufferData = ByteBuffer.wrap(data);
244 artifactData.setData(bufferData);
246 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
247 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
249 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
250 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
251 } catch (Exception e) {
257 public void testPopulateZipWhenComponentIsServiceAndCollectComponentCsarDefinitionIsRight() {
258 Component component = new Service();
259 boolean getFromCS = false;
261 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
262 ArtifactDefinition artifact = new ArtifactDefinition();
263 artifact.setArtifactName("artifactName");
264 artifact.setEsId("esId");
265 artifact.setArtifactUUID("artifactUUID");
266 artifact.setArtifactType("YANG");
267 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
268 artifact.setDescription("description");
269 artifact.setArtifactLabel("artifactLabel");
270 toscaArtifacts.put("assettoscatemplate", artifact);
272 component.setToscaArtifacts(toscaArtifacts);
273 component.setDeploymentArtifacts(toscaArtifacts);
274 component.setArtifacts(toscaArtifacts);
275 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
276 component.setVersion("1.0");
277 component.setLastUpdaterUserId("userId");
278 component.setUniqueId("uid");
279 DAOArtifactData artifactData = new DAOArtifactData();
280 byte[] data = "value".getBytes();
281 ByteBuffer bufferData = ByteBuffer.wrap(data);
282 artifactData.setData(bufferData);
284 List<SdcSchemaFilesData> filesData = new ArrayList<>();
285 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
286 filedata.setPayloadAsArray(data);
287 filesData.add(filedata);
289 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
290 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
291 Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
292 dependencies.add(triple);
293 toscaTemplate.setDependencies(dependencies);
295 ToscaRepresentation tosca = new ToscaRepresentation();
296 tosca.setMainYaml("value");
298 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
300 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
302 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
303 .thenReturn(Either.left(toscaTemplate));
306 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
307 .thenReturn(Either.left(filesData));
309 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
310 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
312 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(User.class))).thenReturn(new User());
315 Mockito.when(artifactsBusinessLogic.validateAndHandleArtifact(Mockito.any(String.class),
316 Mockito.any(ComponentTypeEnum.class), Mockito.any(ArtifactOperationInfo.class), Mockito.isNull(),
317 Mockito.any(ArtifactDefinition.class), Mockito.any(String.class), Mockito.any(String.class),
318 Mockito.isNull(), Mockito.isNull(), Mockito.any(User.class), Mockito.any(Component.class),
319 Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(Boolean.class)))
320 .thenReturn(Either.left(Mockito.any(ArtifactDefinition.class)));
322 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
323 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
324 } catch (Exception e) {
330 public void testPopulateZipWhenGetEntryDataIsRight() {
331 Component component = new Service();
332 boolean getFromCS = true;
334 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
335 ArtifactDefinition artifact = new ArtifactDefinition();
336 artifact.setArtifactName("artifactName");
337 artifact.setEsId("esId");
338 artifact.setArtifactUUID("artifactUUID");
339 artifact.setArtifactType("YANG");
340 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
341 artifact.setDescription("description");
342 artifact.setArtifactLabel("artifactLabel");
343 toscaArtifacts.put("assettoscatemplate", artifact);
345 component.setToscaArtifacts(toscaArtifacts);
346 component.setDeploymentArtifacts(toscaArtifacts);
347 component.setArtifacts(toscaArtifacts);
348 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
349 component.setVersion("1.0");
350 component.setLastUpdaterUserId("userId");
351 component.setUniqueId("uid");
352 DAOArtifactData artifactData = new DAOArtifactData();
353 byte[] data = "value".getBytes();
354 ByteBuffer bufferData = ByteBuffer.wrap(data);
355 artifactData.setData(bufferData);
357 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
358 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
359 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
360 dependencies.add(triple);
361 toscaTemplate.setDependencies(dependencies);
363 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
365 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
366 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
368 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
369 .thenReturn(Either.left(toscaTemplate));
371 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
372 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
373 } catch (Exception e) {
379 public void testPopulateZipWhenGetEntryDataOfInnerComponentIsRight() {
380 Component component = new Service();
381 boolean getFromCS = false;
383 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
384 ArtifactDefinition artifact = new ArtifactDefinition();
385 artifact.setArtifactName("artifactName");
386 artifact.setEsId("esId");
387 artifact.setArtifactUUID("artifactUUID");
388 artifact.setArtifactType("YANG");
389 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
390 artifact.setDescription("description");
391 artifact.setArtifactLabel("artifactLabel");
392 toscaArtifacts.put("assettoscatemplate", artifact);
394 component.setToscaArtifacts(toscaArtifacts);
395 component.setDeploymentArtifacts(toscaArtifacts);
396 component.setArtifacts(toscaArtifacts);
397 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
398 component.setVersion("1.0");
399 component.setLastUpdaterUserId("userId");
400 component.setUniqueId("uid");
401 DAOArtifactData artifactData = new DAOArtifactData();
402 byte[] data = "value".getBytes();
403 ByteBuffer bufferData = ByteBuffer.wrap(data);
404 artifactData.setData(bufferData);
406 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
407 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
408 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
409 dependencies.add(triple);
410 toscaTemplate.setDependencies(dependencies);
412 ToscaRepresentation tosca = new ToscaRepresentation();
413 tosca.setMainYaml("value");
415 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
417 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca),
418 Either.left(tosca), Either.right(ToscaError.GENERAL_ERROR));
420 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
421 .thenReturn(Either.left(toscaTemplate));
423 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
424 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
425 } catch (Exception e) {
431 public void testPopulateZipWhenLatestSchemaFilesFromCassandraIsRight() {
432 Component component = new Service();
433 boolean getFromCS = false;
435 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
436 ArtifactDefinition artifact = new ArtifactDefinition();
437 artifact.setArtifactName("artifactName");
438 artifact.setEsId("esId");
439 artifact.setArtifactUUID("artifactUUID");
440 artifact.setArtifactType("YANG");
441 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
442 artifact.setDescription("description");
443 artifact.setArtifactLabel("artifactLabel");
444 toscaArtifacts.put("assettoscatemplate", artifact);
446 component.setToscaArtifacts(toscaArtifacts);
447 component.setDeploymentArtifacts(toscaArtifacts);
448 component.setArtifacts(toscaArtifacts);
449 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
450 component.setVersion("1.0");
451 component.setLastUpdaterUserId("userId");
452 component.setUniqueId("uid");
453 DAOArtifactData artifactData = new DAOArtifactData();
454 byte[] data = "value".getBytes();
455 ByteBuffer bufferData = ByteBuffer.wrap(data);
456 artifactData.setData(bufferData);
458 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
459 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
460 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
461 dependencies.add(triple);
462 toscaTemplate.setDependencies(dependencies);
464 ToscaRepresentation tosca = new ToscaRepresentation();
465 tosca.setMainYaml("value");
467 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
469 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
471 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
472 .thenReturn(Either.left(toscaTemplate));
475 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
476 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
478 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
479 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
480 } catch (Exception e) {
486 public void testAddInnerComponentsToCache() {
487 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
488 Component childComponent = new Resource();
489 Component componentRI = new Service();
490 List<ComponentInstance> componentInstances = new ArrayList<>();
491 ComponentInstance instance = new ComponentInstance();
492 instance.setComponentUid("resourceUid");
493 componentInstances.add(instance);
494 childComponent.setComponentInstances(componentInstances);
496 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
497 ArtifactDefinition artifact = new ArtifactDefinition();
498 artifact.setArtifactName("artifactName");
499 artifact.setEsId("esId");
500 artifact.setArtifactUUID("artifactUUID");
501 artifact.setArtifactType("YANG");
502 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
503 artifact.setDescription("description");
504 artifact.setArtifactLabel("artifactLabel");
505 toscaArtifacts.put("assettoscatemplate", artifact);
507 componentRI.setToscaArtifacts(toscaArtifacts);
509 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
510 .thenReturn(Either.left(componentRI));
512 Deencapsulation.invoke(testSubject, "addInnerComponentsToCache", componentCache, childComponent);
514 assertTrue(componentCache.containsValue(ImmutableTriple.of("esId","artifactName",componentRI)));
518 public void testAddInnerComponentsToCacheWhenGetToscaElementIsRight() {
519 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
520 Component childComponent = new Resource();
522 List<ComponentInstance> componentInstances = new ArrayList<>();
523 ComponentInstance instance = new ComponentInstance();
524 instance.setComponentUid("abc");
525 componentInstances.add(instance);
526 childComponent.setComponentInstances(componentInstances);
528 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
529 ArtifactDefinition artifact = new ArtifactDefinition();
530 artifact.setArtifactName("artifactName");
531 artifact.setEsId("esId");
532 artifact.setArtifactUUID("artifactUUID");
533 artifact.setArtifactType("YANG");
534 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
535 artifact.setDescription("description");
536 artifact.setArtifactLabel("artifactLabel");
537 toscaArtifacts.put("assettoscatemplate", artifact);
539 Component componentRI = new Service();
541 componentRI.setToscaArtifacts(toscaArtifacts);
543 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
544 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
547 assertTrue(componentCache.isEmpty());
551 public void testAddComponentToCache() {
552 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
554 String fileName = "fileName";
555 Component component = new Resource();
556 component.setInvariantUUID("key");
557 component.setVersion("1.0");
559 Component cachedComponent = new Resource();
560 cachedComponent.setVersion("0.3");
562 componentCache.put("key", new ImmutableTriple<String, String, Component>(id, fileName, cachedComponent));
564 Deencapsulation.invoke(testSubject, "addComponentToCache", componentCache, id, fileName, component);
568 public void testWriteComponentInterface() throws IOException {
569 String fileName = "name.hello";
570 ToscaRepresentation tosca = new ToscaRepresentation();
571 tosca.setMainYaml("value");
573 Mockito.when(toscaExportUtils.exportComponentInterface(Mockito.any(Component.class), Mockito.any(Boolean.class)))
574 .thenReturn(Either.left(tosca));
577 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
578 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "writeComponentInterface", new Resource(), zip, fileName, false);
580 assertNotNull(output);
581 assertTrue(output.isLeft());
586 public void testGetEntryData() {
587 String cassandraId = "id";
588 Component childComponent = new Resource();
590 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
591 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
593 Either<byte[], ActionStatus> output = Deencapsulation.invoke(testSubject, "getEntryData", cassandraId, childComponent);
595 assertNotNull(output);
596 assertTrue(output.isRight());
600 public void testGetLatestSchemaFilesFromCassandraWhenListOfSchemasIsEmpty() {
601 List<SdcSchemaFilesData> filesData = new ArrayList<>();
604 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
605 .thenReturn(Either.left(filesData));
607 Either<byte[], ResponseFormat> output = Deencapsulation.invoke(testSubject, "getLatestSchemaFilesFromCassandra");
609 assertNotNull(output);
610 assertTrue(output.isRight());
614 public void testExtractVfcsArtifactsFromCsar() {
615 String key = "Artifacts/org.openecomp.resource.some/Deployment/to/resource";
616 byte[] data = "value".getBytes();
618 Map<String, byte[]> csar = new HashMap<>();
621 Map<String, List<ArtifactDefinition>> output = CsarUtils.extractVfcsArtifactsFromCsar(csar);
623 assertNotNull(output);
624 assertTrue(output.containsKey("org.openecomp.resource.some"));
625 assertEquals(1, output.get("org.openecomp.resource.some").size());
629 public void testAddExtractedVfcArtifactWhenArtifactsContainsExtractedArtifactKey() {
630 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
631 "key", new ArtifactDefinition());
632 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
633 artifacts.put("key", new ArrayList<>());
635 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
637 assertEquals(1, artifacts.get("key").size());
641 public void testAddExtractedVfcArtifactWhenArtifactsDoesntContainsExtractedArtifactKey() {
642 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
643 "key", new ArtifactDefinition());
644 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
645 artifacts.put("key1", new ArrayList<>());
647 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
649 assertEquals(0, artifacts.get("key1").size());
650 assertEquals(1, artifacts.get("key").size());
651 assertEquals(2, artifacts.size());
655 public void testExtractVfcArtifact() {
656 String path = "path/to/informational/artificat";
657 Map<String, byte[]> map = new HashMap<>();
658 map.put(path, "value".getBytes());
659 Entry<String, byte[]> entry = map.entrySet().iterator().next();
661 ImmutablePair<String, ArtifactDefinition> output = Deencapsulation.invoke(testSubject, "extractVfcArtifact", entry, new HashMap<>());
663 assertNotNull(output);
664 assertEquals("to",output.left);
668 public void testDetectArtifactGroupTypeWithExceptionBeingCaught() {
669 Either<ArtifactGroupTypeEnum, Boolean> output = Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", Map.class);
671 assertNotNull(output);
672 assertTrue(output.isRight());
673 assertFalse(output.right().value());
677 public void testDetectArtifactGroupTypeWWhenCollectedWarningMessagesContainesKey() {
678 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
680 collectedWarningMessages.put("Warning - unrecognized artifact group type {} was received.", new HashSet<>());
681 Either<ArtifactGroupTypeEnum, Boolean> output = Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", collectedWarningMessages);
683 assertNotNull(output);
684 assertTrue(output.isRight());
685 assertFalse(output.right().value());
689 public void testNonMetaArtifactInfoCtor() {
690 createNonMetaArtifactInfoTestSubject();
694 public void testNonMetaArtifactInfoGetPath() {
695 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
697 testSubject.getPath();
701 public void testNonMetaArtifactInfoGetArtifactName() {
702 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
704 testSubject.getArtifactName();
708 public void testNonMetaArtifactInfoGetArtifactType() {
709 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
711 testSubject.getArtifactType();
715 public void testNonMetaArtifactInfoGetDisplayName() {
716 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
718 testSubject.getDisplayName();
722 public void testNonMetaArtifactInfoGetArtifactGroupType() {
723 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
725 testSubject.getArtifactGroupType();
729 public void testNonMetaArtifactInfoGetArtifactLabel() {
730 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
732 testSubject.getArtifactLabel();
736 public void testNonMetaArtifactInfoGetIsFromCsar() {
737 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
739 testSubject.isFromCsar();
743 public void testNonMetaArtifactInfoGetPayloadData() {
744 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
746 testSubject.getPayloadData();
750 public void testNonMetaArtifactInfoGetArtifaactChecksum() {
751 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
753 testSubject.getArtifactChecksum();
757 public void testNonMetaArtifactInfoGetArtifactUniqueId() {
758 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
760 testSubject.getArtifactUniqueId();
764 public void testNonMetaArtifactInfosetArtifactUniqueId() {
765 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
767 testSubject.setArtifactUniqueId("artifactUniqueId");
771 public void testValidateNonMetaArtifactWithExceptionCaught() {
772 CsarUtils.validateNonMetaArtifact("", new byte[0], new HashMap<>());
776 public void testWriteArtifactDefinition() throws IOException {
777 Component component = new Service();
778 List<ArtifactDefinition> artifactDefinitionList = new ArrayList<>();
779 String artifactPathAndFolder = "";
781 ArtifactDefinition artifact = new ArtifactDefinition();
782 artifact.setArtifactType(ArtifactTypeEnum.HEAT_ENV.getType());
783 artifactDefinitionList.add(artifact);
785 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
786 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "writeArtifactDefinition", component, zip, artifactDefinitionList, artifactPathAndFolder, false);
788 assertNotNull(output);
789 assertTrue(output.isLeft());
794 public void testCollectComponentCsarDefinitionWhenComponentIsServiceAndGetToscaElementIsLeft() {
795 Component component = new Service();
796 component.setUniqueId("uniqueId");
797 List<ComponentInstance> resourceInstances = new ArrayList<>();
798 ComponentInstance instance = new ComponentInstance();
799 instance.setComponentUid("resourceUid");
800 instance.setOriginType(OriginTypeEnum.SERVICE);
801 resourceInstances.add(instance);
802 component.setComponentInstances(resourceInstances);
804 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
805 ArtifactDefinition artifact = new ArtifactDefinition();
806 artifact.setArtifactName("artifactName");
807 artifact.setEsId("esId");
808 artifact.setArtifactUUID("artifactUUID");
809 artifact.setArtifactType("YANG");
810 toscaArtifacts.put("assettoscatemplate", artifact);
812 component.setToscaArtifacts(toscaArtifacts);
813 component.setDeploymentArtifacts(toscaArtifacts);
814 component.setArtifacts(toscaArtifacts);
816 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
817 Either.right(StorageOperationStatus.BAD_REQUEST));
819 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
821 assertNotNull(output);
822 assertTrue(output.isRight());
826 public void testCollectComponentTypeArtifactsWhenFetchedComponentHasComponentInstances() {
827 Component component = new Service();
828 Component fetchedComponent = new Resource();
829 component.setUniqueId("uniqueId");
830 List<ComponentInstance> resourceInstances = new ArrayList<>();
831 ComponentInstance instance = new ComponentInstance();
832 instance.setComponentUid("resourceUid");
833 instance.setOriginType(OriginTypeEnum.SERVICE);
834 resourceInstances.add(instance);
835 component.setComponentInstances(resourceInstances);
836 fetchedComponent.setComponentInstances(resourceInstances);
838 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
839 ArtifactDefinition artifact = new ArtifactDefinition();
840 artifact.setArtifactName("artifactName");
841 artifact.setEsId("esId");
842 artifact.setArtifactUUID("artifactUUID");
843 artifact.setArtifactType("YANG");
844 toscaArtifacts.put("assettoscatemplate", artifact);
846 component.setToscaArtifacts(toscaArtifacts);
847 component.setDeploymentArtifacts(toscaArtifacts);
848 component.setArtifacts(toscaArtifacts);
850 fetchedComponent.setToscaArtifacts(toscaArtifacts);
851 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
852 fetchedComponent.setArtifacts(toscaArtifacts);
854 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
855 Either.left(fetchedComponent), Either.right(StorageOperationStatus.BAD_REQUEST));
857 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
859 assertNotNull(output);
860 assertTrue(output.isRight());
864 public void testCollectComponentTypeArtifactsWhenFetchedComponentDontHaveComponentInstances() {
865 Component component = new Service();
866 Component fetchedComponent = new Resource();
867 component.setUniqueId("uniqueId");
868 List<ComponentInstance> resourceInstances = new ArrayList<>();
869 ComponentInstance instance = new ComponentInstance();
870 instance.setComponentUid("resourceUid");
871 instance.setOriginType(OriginTypeEnum.SERVICE);
873 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
874 ArtifactDefinition artifact = new ArtifactDefinition();
875 artifact.setArtifactName("artifactName");
876 artifact.setEsId("esId");
877 artifact.setArtifactUUID("artifactUUID");
878 artifact.setArtifactType("PLAN");
879 toscaArtifacts.put("assettoscatemplate", artifact);
881 instance.setDeploymentArtifacts(toscaArtifacts);
883 resourceInstances.add(instance);
884 component.setComponentInstances(resourceInstances);
886 component.setToscaArtifacts(toscaArtifacts);
887 component.setDeploymentArtifacts(toscaArtifacts);
888 component.setArtifacts(toscaArtifacts);
890 fetchedComponent.setToscaArtifacts(toscaArtifacts);
891 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
892 fetchedComponent.setArtifacts(toscaArtifacts);
894 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
895 Either.left(fetchedComponent));
897 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
899 assertNotNull(output);
900 assertTrue(output.isLeft());
904 public void testValidateNonMetaArtifactHappyScenario() {
905 String artifactPath = "Artifacts/Deployment/YANG_XML/myYang.xml";
906 byte[] payloadData = "some payload data".getBytes();
907 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
908 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
909 payloadData, collectedWarningMessages);
910 assertTrue(eitherNonMetaArtifact.isLeft());
911 assertTrue(collectedWarningMessages.isEmpty());
913 artifactPath = "Artifacts/Informational/OTHER/someArtifact.xml";
914 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
915 assertTrue(eitherNonMetaArtifact.isLeft());
916 assertTrue(collectedWarningMessages.isEmpty());
920 public void testValidateNonMetaArtifactScenarioWithWarnnings() {
921 String artifactPath = "Artifacts/Deployment/Buga/myYang.xml";
922 byte[] payloadData = "some payload data".getBytes();
923 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
924 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
925 payloadData, collectedWarningMessages);
926 assertTrue(eitherNonMetaArtifact.isLeft());
928 artifactPath = "Artifacts/Informational/Buga2/someArtifact.xml";
929 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
930 assertTrue(eitherNonMetaArtifact.isLeft());
932 assertTrue(collectedWarningMessages.size() == 1);
933 assertTrue(collectedWarningMessages.values().iterator().next().size() == 2);
937 public void testValidateNonMetaArtifactUnhappyScenario() {
938 String artifactPath = "Artifacts/Buga/YANG_XML/myYang.xml";
939 byte[] payloadData = "some payload data".getBytes();
940 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
941 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
942 payloadData, collectedWarningMessages);
943 assertTrue(eitherNonMetaArtifact.isRight());
944 assertTrue(!collectedWarningMessages.isEmpty());
947 @Test(expected = IOException.class)
948 public void testAddSchemaFilesFromCassandraAddingDuplicatedEntry() throws IOException {
949 final String rootPath = System.getProperty("user.dir");
950 final Path path = Paths.get(rootPath + "/src/test/resources/sdc.zip");
952 final byte[] data = Files.readAllBytes(path);
953 try (final ByteArrayOutputStream out = new ByteArrayOutputStream();
954 final ZipOutputStream zip = new ZipOutputStream(out);) {
955 Deencapsulation.invoke(testSubject, "addSchemaFilesFromCassandra",
956 zip, data, nodesFromPackage);
957 zip.putNextEntry(new ZipEntry("Definitions/nodes.yml"));
960 } catch (final IOException e) {
961 Assert.assertTrue("duplicate entry: Definitions/nodes.yml".equals(e.getMessage()));
962 throw new IOException("Could not add Schema Files From Cassandra", e);
967 public void testFindNonRootNodesFromPackage() {
968 final Resource resource = new Resource();
969 resource.setDerivedList(nodesFromPackage);
970 final Component component = resource;
971 final List<Triple<String, String, Component>> dependencies = new ArrayList<>();
972 final Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
973 dependencies.add(triple);
974 final List<String> expectedResult = Arrays.asList("tosca.nodes.Container.Application");
975 final List<String> result = Deencapsulation.invoke(testSubject,
976 "findNonRootNodesFromPackage", dependencies);
977 assertTrue(CollectionUtils.isNotEmpty(result));
978 assertEquals(expectedResult, result);