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 fj.data.Either;
24 import mockit.Deencapsulation;
25 import org.apache.commons.io.output.ByteArrayOutputStream;
26 import org.apache.commons.lang3.tuple.ImmutablePair;
27 import org.apache.commons.lang3.tuple.ImmutableTriple;
28 import org.apache.commons.lang3.tuple.Triple;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.MockitoAnnotations;
35 import org.openecomp.sdc.be.components.BeConfDependentTest;
36 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationInfo;
38 import org.openecomp.sdc.be.dao.api.ActionStatus;
39 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
40 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
41 import org.openecomp.sdc.be.dao.cassandra.SdcSchemaFilesCassandraDao;
42 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
43 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
44 import org.openecomp.sdc.be.impl.ComponentsUtils;
45 import org.openecomp.sdc.be.model.ArtifactDefinition;
46 import org.openecomp.sdc.be.model.Component;
47 import org.openecomp.sdc.be.model.ComponentInstance;
48 import org.openecomp.sdc.be.model.LifecycleStateEnum;
49 import org.openecomp.sdc.be.model.Resource;
50 import org.openecomp.sdc.be.model.Service;
51 import org.openecomp.sdc.be.model.User;
52 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
53 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
54 import org.openecomp.sdc.be.resources.data.DAOArtifactData;
55 import org.openecomp.sdc.be.resources.data.SdcSchemaFilesData;
56 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
57 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
58 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
59 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
60 import org.openecomp.sdc.common.impl.ExternalConfiguration;
61 import org.openecomp.sdc.exception.ResponseFormat;
63 import java.io.IOException;
64 import java.nio.ByteBuffer;
65 import java.util.ArrayList;
66 import java.util.HashMap;
67 import java.util.HashSet;
68 import java.util.List;
70 import java.util.Map.Entry;
72 import java.util.zip.ZipEntry;
73 import java.util.zip.ZipOutputStream;
75 import static org.junit.Assert.assertEquals;
76 import static org.junit.Assert.assertFalse;
77 import static org.junit.Assert.assertNotNull;
78 import static org.junit.Assert.assertTrue;
80 public class CsarUtilsTest extends BeConfDependentTest {
83 CsarUtils testSubject;
86 private ArtifactCassandraDao artifactCassandraDao;
89 private ComponentsUtils componentsUtils;
92 private ToscaExportHandler toscaExportUtils;
95 private SdcSchemaFilesCassandraDao sdcSchemaFilesCassandraDao;
98 private ToscaOperationFacade toscaOperationFacade;
101 private ArtifactsBusinessLogic artifactsBusinessLogic;
104 public void setUpMock() throws Exception {
105 ExternalConfiguration.setAppName("catalog-be");
106 MockitoAnnotations.initMocks(this);
110 private NonMetaArtifactInfo createNonMetaArtifactInfoTestSubject() {
111 return new CsarUtils.NonMetaArtifactInfo("mock", "mock", ArtifactTypeEnum.AAI_SERVICE_MODEL,
112 ArtifactGroupTypeEnum.DEPLOYMENT, new byte[0], "mock", true);
116 public void testCreateCsar() {
117 Component component = new Resource();
118 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
119 ArtifactDefinition artifact = new ArtifactDefinition();
120 artifact.setArtifactName("artifactName");
121 artifact.setEsId("esId");
122 toscaArtifacts.put("assettoscatemplate", artifact);
124 component.setToscaArtifacts(toscaArtifacts);
126 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
127 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
129 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
130 .thenReturn(ActionStatus.GENERAL_ERROR);
132 testSubject.createCsar(component, true, true);
136 public void testCreateCsarWithGenerateCsarZipResponseIsLeft() {
137 Component component = new Resource();
138 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
139 ArtifactDefinition artifact = new ArtifactDefinition();
140 artifact.setArtifactName("artifactName");
141 artifact.setEsId("esId");
142 artifact.setArtifactUUID("artifactUUID");
143 artifact.setArtifactType("YANG");
144 toscaArtifacts.put("assettoscatemplate", artifact);
146 component.setToscaArtifacts(toscaArtifacts);
147 component.setDeploymentArtifacts(toscaArtifacts);
148 component.setArtifacts(toscaArtifacts);
149 DAOArtifactData artifactData = new DAOArtifactData();
150 byte[] data = "value".getBytes();
151 ByteBuffer bufferData = ByteBuffer.wrap(data);
152 artifactData.setData(bufferData);
154 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
155 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
156 toscaTemplate.setDependencies(dependencies);
158 List<SdcSchemaFilesData> filesData = new ArrayList<>();
159 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
160 filedata.setPayloadAsArray(data);
161 filesData.add(filedata);
163 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
165 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
166 .thenReturn(ActionStatus.GENERAL_ERROR);
168 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
169 .thenReturn(Either.left(toscaTemplate));
172 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
173 .thenReturn(Either.left(filesData));
175 testSubject.createCsar(component, false, true);
179 public void testPopulateZipWhenGetDependenciesIsRight() {
180 Component component = new Service();
181 boolean getFromCS = false;
183 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
184 ArtifactDefinition artifact = new ArtifactDefinition();
185 artifact.setArtifactName("artifactName");
186 artifact.setEsId("esId");
187 artifact.setArtifactUUID("artifactUUID");
188 artifact.setArtifactType("YANG");
189 toscaArtifacts.put("assettoscatemplate", artifact);
191 component.setToscaArtifacts(toscaArtifacts);
192 component.setDeploymentArtifacts(toscaArtifacts);
193 component.setArtifacts(toscaArtifacts);
194 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
195 DAOArtifactData artifactData = new DAOArtifactData();
196 byte[] data = "value".getBytes();
197 ByteBuffer bufferData = ByteBuffer.wrap(data);
198 artifactData.setData(bufferData);
200 ToscaRepresentation tosca = new ToscaRepresentation();
201 tosca.setMainYaml("value");
203 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
205 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
207 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
208 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
210 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
211 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
212 } catch (Exception e) {
218 public void testPopulateZipWhenExportComponentIsRight() {
219 Component component = new Resource();
220 boolean getFromCS = false;
222 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
223 ArtifactDefinition artifact = new ArtifactDefinition();
224 artifact.setArtifactName("artifactName");
225 artifact.setEsId("esId");
226 artifact.setArtifactUUID("artifactUUID");
227 artifact.setArtifactType("YANG");
228 toscaArtifacts.put("assettoscatemplate", artifact);
230 component.setToscaArtifacts(toscaArtifacts);
231 component.setDeploymentArtifacts(toscaArtifacts);
232 component.setArtifacts(toscaArtifacts);
233 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
234 DAOArtifactData artifactData = new DAOArtifactData();
235 byte[] data = "value".getBytes();
236 ByteBuffer bufferData = ByteBuffer.wrap(data);
237 artifactData.setData(bufferData);
239 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
240 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
242 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
243 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
244 } catch (Exception e) {
250 public void testPopulateZipWhenComponentIsServiceAndCollectComponentCsarDefinitionIsRight() {
251 Component component = new Service();
252 boolean getFromCS = false;
254 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
255 ArtifactDefinition artifact = new ArtifactDefinition();
256 artifact.setArtifactName("artifactName");
257 artifact.setEsId("esId");
258 artifact.setArtifactUUID("artifactUUID");
259 artifact.setArtifactType("YANG");
260 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
261 artifact.setDescription("description");
262 artifact.setArtifactLabel("artifactLabel");
263 toscaArtifacts.put("assettoscatemplate", artifact);
265 component.setToscaArtifacts(toscaArtifacts);
266 component.setDeploymentArtifacts(toscaArtifacts);
267 component.setArtifacts(toscaArtifacts);
268 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
269 component.setVersion("1.0");
270 component.setLastUpdaterUserId("userId");
271 component.setUniqueId("uid");
272 DAOArtifactData artifactData = new DAOArtifactData();
273 byte[] data = "value".getBytes();
274 ByteBuffer bufferData = ByteBuffer.wrap(data);
275 artifactData.setData(bufferData);
277 List<SdcSchemaFilesData> filesData = new ArrayList<>();
278 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
279 filedata.setPayloadAsArray(data);
280 filesData.add(filedata);
282 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
283 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
284 Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
285 dependencies.add(triple);
286 toscaTemplate.setDependencies(dependencies);
288 ToscaRepresentation tosca = new ToscaRepresentation();
289 tosca.setMainYaml("value");
291 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
293 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
295 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
296 .thenReturn(Either.left(toscaTemplate));
299 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
300 .thenReturn(Either.left(filesData));
302 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
303 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
305 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(User.class))).thenReturn(new User());
308 Mockito.when(artifactsBusinessLogic.validateAndHandleArtifact(Mockito.any(String.class),
309 Mockito.any(ComponentTypeEnum.class), Mockito.any(ArtifactOperationInfo.class), Mockito.isNull(),
310 Mockito.any(ArtifactDefinition.class), Mockito.any(String.class), Mockito.any(String.class),
311 Mockito.isNull(), Mockito.isNull(), Mockito.any(User.class), Mockito.any(Component.class),
312 Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(Boolean.class)))
313 .thenReturn(Either.left(Mockito.any(ArtifactDefinition.class)));
315 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
316 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
317 } catch (Exception e) {
323 public void testPopulateZipWhenGetEntryDataIsRight() {
324 Component component = new Service();
325 boolean getFromCS = true;
327 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
328 ArtifactDefinition artifact = new ArtifactDefinition();
329 artifact.setArtifactName("artifactName");
330 artifact.setEsId("esId");
331 artifact.setArtifactUUID("artifactUUID");
332 artifact.setArtifactType("YANG");
333 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
334 artifact.setDescription("description");
335 artifact.setArtifactLabel("artifactLabel");
336 toscaArtifacts.put("assettoscatemplate", artifact);
338 component.setToscaArtifacts(toscaArtifacts);
339 component.setDeploymentArtifacts(toscaArtifacts);
340 component.setArtifacts(toscaArtifacts);
341 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
342 component.setVersion("1.0");
343 component.setLastUpdaterUserId("userId");
344 component.setUniqueId("uid");
345 DAOArtifactData artifactData = new DAOArtifactData();
346 byte[] data = "value".getBytes();
347 ByteBuffer bufferData = ByteBuffer.wrap(data);
348 artifactData.setData(bufferData);
350 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
351 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
352 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
353 dependencies.add(triple);
354 toscaTemplate.setDependencies(dependencies);
356 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
358 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
359 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
361 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
362 .thenReturn(Either.left(toscaTemplate));
364 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
365 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
366 } catch (Exception e) {
372 public void testPopulateZipWhenGetEntryDataOfInnerComponentIsRight() {
373 Component component = new Service();
374 boolean getFromCS = false;
376 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
377 ArtifactDefinition artifact = new ArtifactDefinition();
378 artifact.setArtifactName("artifactName");
379 artifact.setEsId("esId");
380 artifact.setArtifactUUID("artifactUUID");
381 artifact.setArtifactType("YANG");
382 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
383 artifact.setDescription("description");
384 artifact.setArtifactLabel("artifactLabel");
385 toscaArtifacts.put("assettoscatemplate", artifact);
387 component.setToscaArtifacts(toscaArtifacts);
388 component.setDeploymentArtifacts(toscaArtifacts);
389 component.setArtifacts(toscaArtifacts);
390 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
391 component.setVersion("1.0");
392 component.setLastUpdaterUserId("userId");
393 component.setUniqueId("uid");
394 DAOArtifactData artifactData = new DAOArtifactData();
395 byte[] data = "value".getBytes();
396 ByteBuffer bufferData = ByteBuffer.wrap(data);
397 artifactData.setData(bufferData);
399 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
400 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
401 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
402 dependencies.add(triple);
403 toscaTemplate.setDependencies(dependencies);
405 ToscaRepresentation tosca = new ToscaRepresentation();
406 tosca.setMainYaml("value");
408 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
410 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca),
411 Either.left(tosca), Either.right(ToscaError.GENERAL_ERROR));
413 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
414 .thenReturn(Either.left(toscaTemplate));
416 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
417 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
418 } catch (Exception e) {
424 public void testPopulateZipWhenLatestSchemaFilesFromCassandraIsRight() {
425 Component component = new Service();
426 boolean getFromCS = false;
428 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
429 ArtifactDefinition artifact = new ArtifactDefinition();
430 artifact.setArtifactName("artifactName");
431 artifact.setEsId("esId");
432 artifact.setArtifactUUID("artifactUUID");
433 artifact.setArtifactType("YANG");
434 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
435 artifact.setDescription("description");
436 artifact.setArtifactLabel("artifactLabel");
437 toscaArtifacts.put("assettoscatemplate", artifact);
439 component.setToscaArtifacts(toscaArtifacts);
440 component.setDeploymentArtifacts(toscaArtifacts);
441 component.setArtifacts(toscaArtifacts);
442 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
443 component.setVersion("1.0");
444 component.setLastUpdaterUserId("userId");
445 component.setUniqueId("uid");
446 DAOArtifactData artifactData = new DAOArtifactData();
447 byte[] data = "value".getBytes();
448 ByteBuffer bufferData = ByteBuffer.wrap(data);
449 artifactData.setData(bufferData);
451 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
452 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
453 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
454 dependencies.add(triple);
455 toscaTemplate.setDependencies(dependencies);
457 ToscaRepresentation tosca = new ToscaRepresentation();
458 tosca.setMainYaml("value");
460 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
462 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
464 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
465 .thenReturn(Either.left(toscaTemplate));
468 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
469 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
471 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
472 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
473 } catch (Exception e) {
480 public void testAddSchemaFilesFromCassandra() throws IOException {
481 try (ByteArrayOutputStream out = new ByteArrayOutputStream();
482 ZipOutputStream zip = new ZipOutputStream(out);
483 ByteArrayOutputStream outMockStream = new ByteArrayOutputStream();
484 ZipOutputStream outMock = new ZipOutputStream(outMockStream);) {
486 outMock.putNextEntry(new ZipEntry("mock1"));
487 outMock.write(new byte[1]);
488 outMock.putNextEntry(new ZipEntry("mock2"));
489 outMock.write(new byte[3]);
491 byte[] byteArray = outMockStream.toByteArray();
492 Deencapsulation.invoke(testSubject, "addSchemaFilesFromCassandra", zip, byteArray);
493 } catch (Exception e) {
500 public void testAddInnerComponentsToCache() {
501 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
502 Component childComponent = new Resource();
503 Component componentRI = new Service();
504 List<ComponentInstance> componentInstances = new ArrayList<>();
505 ComponentInstance instance = new ComponentInstance();
506 instance.setComponentUid("resourceUid");
507 componentInstances.add(instance);
508 childComponent.setComponentInstances(componentInstances);
510 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
511 ArtifactDefinition artifact = new ArtifactDefinition();
512 artifact.setArtifactName("artifactName");
513 artifact.setEsId("esId");
514 artifact.setArtifactUUID("artifactUUID");
515 artifact.setArtifactType("YANG");
516 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
517 artifact.setDescription("description");
518 artifact.setArtifactLabel("artifactLabel");
519 toscaArtifacts.put("assettoscatemplate", artifact);
521 componentRI.setToscaArtifacts(toscaArtifacts);
523 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
524 .thenReturn(Either.left(componentRI));
526 Deencapsulation.invoke(testSubject, "addInnerComponentsToCache", componentCache, childComponent);
528 assertTrue(componentCache.containsValue(ImmutableTriple.of("esId","artifactName",componentRI)));
532 public void testAddInnerComponentsToCacheWhenGetToscaElementIsRight() {
533 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
534 Component childComponent = new Resource();
536 List<ComponentInstance> componentInstances = new ArrayList<>();
537 ComponentInstance instance = new ComponentInstance();
538 instance.setComponentUid("abc");
539 componentInstances.add(instance);
540 childComponent.setComponentInstances(componentInstances);
542 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
543 ArtifactDefinition artifact = new ArtifactDefinition();
544 artifact.setArtifactName("artifactName");
545 artifact.setEsId("esId");
546 artifact.setArtifactUUID("artifactUUID");
547 artifact.setArtifactType("YANG");
548 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
549 artifact.setDescription("description");
550 artifact.setArtifactLabel("artifactLabel");
551 toscaArtifacts.put("assettoscatemplate", artifact);
553 Component componentRI = new Service();
555 componentRI.setToscaArtifacts(toscaArtifacts);
557 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
558 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
561 assertTrue(componentCache.isEmpty());
565 public void testAddComponentToCache() {
566 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
568 String fileName = "fileName";
569 Component component = new Resource();
570 component.setInvariantUUID("key");
571 component.setVersion("1.0");
573 Component cachedComponent = new Resource();
574 cachedComponent.setVersion("0.3");
576 componentCache.put("key", new ImmutableTriple<String, String, Component>(id, fileName, cachedComponent));
578 Deencapsulation.invoke(testSubject, "addComponentToCache", componentCache, id, fileName, component);
582 public void testWriteComponentInterface() throws IOException {
583 String fileName = "name.hello";
584 ToscaRepresentation tosca = new ToscaRepresentation();
585 tosca.setMainYaml("value");
587 Mockito.when(toscaExportUtils.exportComponentInterface(Mockito.any(Component.class), Mockito.any(Boolean.class)))
588 .thenReturn(Either.left(tosca));
591 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
592 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "writeComponentInterface", new Resource(), zip, fileName, false);
594 assertNotNull(output);
595 assertTrue(output.isLeft());
600 public void testGetEntryData() {
601 String cassandraId = "id";
602 Component childComponent = new Resource();
604 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
605 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
607 Either<byte[], ActionStatus> output = Deencapsulation.invoke(testSubject, "getEntryData", cassandraId, childComponent);
609 assertNotNull(output);
610 assertTrue(output.isRight());
614 public void testGetLatestSchemaFilesFromCassandraWhenListOfSchemasIsEmpty() {
615 List<SdcSchemaFilesData> filesData = new ArrayList<>();
618 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
619 .thenReturn(Either.left(filesData));
621 Either<byte[], ResponseFormat> output = Deencapsulation.invoke(testSubject, "getLatestSchemaFilesFromCassandra");
623 assertNotNull(output);
624 assertTrue(output.isRight());
628 public void testExtractVfcsArtifactsFromCsar() {
629 String key = "Artifacts/org.openecomp.resource.some/Deployment/to/resource";
630 byte[] data = "value".getBytes();
632 Map<String, byte[]> csar = new HashMap<>();
635 Map<String, List<ArtifactDefinition>> output = CsarUtils.extractVfcsArtifactsFromCsar(csar);
637 assertNotNull(output);
638 assertTrue(output.containsKey("org.openecomp.resource.some"));
639 assertEquals(1, output.get("org.openecomp.resource.some").size());
643 public void testAddExtractedVfcArtifactWhenArtifactsContainsExtractedArtifactKey() {
644 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
645 "key", new ArtifactDefinition());
646 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
647 artifacts.put("key", new ArrayList<>());
649 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
651 assertEquals(1, artifacts.get("key").size());
655 public void testAddExtractedVfcArtifactWhenArtifactsDoesntContainsExtractedArtifactKey() {
656 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
657 "key", new ArtifactDefinition());
658 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
659 artifacts.put("key1", new ArrayList<>());
661 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
663 assertEquals(0, artifacts.get("key1").size());
664 assertEquals(1, artifacts.get("key").size());
665 assertEquals(2, artifacts.size());
669 public void testExtractVfcArtifact() {
670 String path = "path/to/informational/artificat";
671 Map<String, byte[]> map = new HashMap<>();
672 map.put(path, "value".getBytes());
673 Entry<String, byte[]> entry = map.entrySet().iterator().next();
675 ImmutablePair<String, ArtifactDefinition> output = Deencapsulation.invoke(testSubject, "extractVfcArtifact", entry, new HashMap<>());
677 assertNotNull(output);
678 assertEquals("to",output.left);
682 public void testDetectArtifactGroupTypeWithExceptionBeingCaught() {
683 Either<ArtifactGroupTypeEnum, Boolean> output = Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", Map.class);
685 assertNotNull(output);
686 assertTrue(output.isRight());
687 assertFalse(output.right().value());
691 public void testDetectArtifactGroupTypeWWhenCollectedWarningMessagesContainesKey() {
692 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
694 collectedWarningMessages.put("Warning - unrecognized artifact group type {} was received.", new HashSet<>());
695 Either<ArtifactGroupTypeEnum, Boolean> output = Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", collectedWarningMessages);
697 assertNotNull(output);
698 assertTrue(output.isRight());
699 assertFalse(output.right().value());
703 public void testNonMetaArtifactInfoCtor() {
704 createNonMetaArtifactInfoTestSubject();
708 public void testNonMetaArtifactInfoGetPath() {
709 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
711 testSubject.getPath();
715 public void testNonMetaArtifactInfoGetArtifactName() {
716 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
718 testSubject.getArtifactName();
722 public void testNonMetaArtifactInfoGetArtifactType() {
723 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
725 testSubject.getArtifactType();
729 public void testNonMetaArtifactInfoGetDisplayName() {
730 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
732 testSubject.getDisplayName();
736 public void testNonMetaArtifactInfoGetArtifactGroupType() {
737 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
739 testSubject.getArtifactGroupType();
743 public void testNonMetaArtifactInfoGetArtifactLabel() {
744 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
746 testSubject.getArtifactLabel();
750 public void testNonMetaArtifactInfoGetIsFromCsar() {
751 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
753 testSubject.isFromCsar();
757 public void testNonMetaArtifactInfoGetPayloadData() {
758 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
760 testSubject.getPayloadData();
764 public void testNonMetaArtifactInfoGetArtifaactChecksum() {
765 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
767 testSubject.getArtifactChecksum();
771 public void testNonMetaArtifactInfoGetArtifactUniqueId() {
772 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
774 testSubject.getArtifactUniqueId();
778 public void testNonMetaArtifactInfosetArtifactUniqueId() {
779 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
781 testSubject.setArtifactUniqueId("artifactUniqueId");
785 public void testValidateNonMetaArtifactWithExceptionCaught() {
786 CsarUtils.validateNonMetaArtifact("", new byte[0], new HashMap<>());
790 public void testWriteArtifactDefinition() throws IOException {
791 Component component = new Service();
792 List<ArtifactDefinition> artifactDefinitionList = new ArrayList<>();
793 String artifactPathAndFolder = "";
795 ArtifactDefinition artifact = new ArtifactDefinition();
796 artifact.setArtifactType(ArtifactTypeEnum.HEAT_ENV.getType());
797 artifactDefinitionList.add(artifact);
799 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
800 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "writeArtifactDefinition", component, zip, artifactDefinitionList, artifactPathAndFolder, false);
802 assertNotNull(output);
803 assertTrue(output.isLeft());
808 public void testCollectComponentCsarDefinitionWhenComponentIsServiceAndGetToscaElementIsLeft() {
809 Component component = new Service();
810 component.setUniqueId("uniqueId");
811 List<ComponentInstance> resourceInstances = new ArrayList<>();
812 ComponentInstance instance = new ComponentInstance();
813 instance.setComponentUid("resourceUid");
814 instance.setOriginType(OriginTypeEnum.SERVICE);
815 resourceInstances.add(instance);
816 component.setComponentInstances(resourceInstances);
818 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
819 ArtifactDefinition artifact = new ArtifactDefinition();
820 artifact.setArtifactName("artifactName");
821 artifact.setEsId("esId");
822 artifact.setArtifactUUID("artifactUUID");
823 artifact.setArtifactType("YANG");
824 toscaArtifacts.put("assettoscatemplate", artifact);
826 component.setToscaArtifacts(toscaArtifacts);
827 component.setDeploymentArtifacts(toscaArtifacts);
828 component.setArtifacts(toscaArtifacts);
830 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
831 Either.right(StorageOperationStatus.BAD_REQUEST));
833 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
835 assertNotNull(output);
836 assertTrue(output.isRight());
840 public void testCollectComponentTypeArtifactsWhenFetchedComponentHasComponentInstances() {
841 Component component = new Service();
842 Component fetchedComponent = new Resource();
843 component.setUniqueId("uniqueId");
844 List<ComponentInstance> resourceInstances = new ArrayList<>();
845 ComponentInstance instance = new ComponentInstance();
846 instance.setComponentUid("resourceUid");
847 instance.setOriginType(OriginTypeEnum.SERVICE);
848 resourceInstances.add(instance);
849 component.setComponentInstances(resourceInstances);
850 fetchedComponent.setComponentInstances(resourceInstances);
852 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
853 ArtifactDefinition artifact = new ArtifactDefinition();
854 artifact.setArtifactName("artifactName");
855 artifact.setEsId("esId");
856 artifact.setArtifactUUID("artifactUUID");
857 artifact.setArtifactType("YANG");
858 toscaArtifacts.put("assettoscatemplate", artifact);
860 component.setToscaArtifacts(toscaArtifacts);
861 component.setDeploymentArtifacts(toscaArtifacts);
862 component.setArtifacts(toscaArtifacts);
864 fetchedComponent.setToscaArtifacts(toscaArtifacts);
865 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
866 fetchedComponent.setArtifacts(toscaArtifacts);
868 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
869 Either.left(fetchedComponent), Either.right(StorageOperationStatus.BAD_REQUEST));
871 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
873 assertNotNull(output);
874 assertTrue(output.isRight());
878 public void testCollectComponentTypeArtifactsWhenFetchedComponentDontHaveComponentInstances() {
879 Component component = new Service();
880 Component fetchedComponent = new Resource();
881 component.setUniqueId("uniqueId");
882 List<ComponentInstance> resourceInstances = new ArrayList<>();
883 ComponentInstance instance = new ComponentInstance();
884 instance.setComponentUid("resourceUid");
885 instance.setOriginType(OriginTypeEnum.SERVICE);
887 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
888 ArtifactDefinition artifact = new ArtifactDefinition();
889 artifact.setArtifactName("artifactName");
890 artifact.setEsId("esId");
891 artifact.setArtifactUUID("artifactUUID");
892 artifact.setArtifactType("PLAN");
893 toscaArtifacts.put("assettoscatemplate", artifact);
895 instance.setDeploymentArtifacts(toscaArtifacts);
897 resourceInstances.add(instance);
898 component.setComponentInstances(resourceInstances);
900 component.setToscaArtifacts(toscaArtifacts);
901 component.setDeploymentArtifacts(toscaArtifacts);
902 component.setArtifacts(toscaArtifacts);
904 fetchedComponent.setToscaArtifacts(toscaArtifacts);
905 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
906 fetchedComponent.setArtifacts(toscaArtifacts);
908 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
909 Either.left(fetchedComponent));
911 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
913 assertNotNull(output);
914 assertTrue(output.isLeft());
918 public void testValidateNonMetaArtifactHappyScenario() {
919 String artifactPath = "Artifacts/Deployment/YANG_XML/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());
925 assertTrue(collectedWarningMessages.isEmpty());
927 artifactPath = "Artifacts/Informational/OTHER/someArtifact.xml";
928 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
929 assertTrue(eitherNonMetaArtifact.isLeft());
930 assertTrue(collectedWarningMessages.isEmpty());
934 public void testValidateNonMetaArtifactScenarioWithWarnnings() {
935 String artifactPath = "Artifacts/Deployment/Buga/myYang.xml";
936 byte[] payloadData = "some payload data".getBytes();
937 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
938 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
939 payloadData, collectedWarningMessages);
940 assertTrue(eitherNonMetaArtifact.isLeft());
942 artifactPath = "Artifacts/Informational/Buga2/someArtifact.xml";
943 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
944 assertTrue(eitherNonMetaArtifact.isLeft());
946 assertTrue(collectedWarningMessages.size() == 1);
947 assertTrue(collectedWarningMessages.values().iterator().next().size() == 2);
951 public void testValidateNonMetaArtifactUnhappyScenario() {
952 String artifactPath = "Artifacts/Buga/YANG_XML/myYang.xml";
953 byte[] payloadData = "some payload data".getBytes();
954 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
955 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
956 payloadData, collectedWarningMessages);
957 assertTrue(eitherNonMetaArtifact.isRight());
958 assertTrue(!collectedWarningMessages.isEmpty());