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;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.List;
30 import java.util.Map.Entry;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipOutputStream;
35 import org.apache.commons.io.output.ByteArrayOutputStream;
36 import org.apache.commons.lang3.tuple.ImmutablePair;
37 import org.apache.commons.lang3.tuple.ImmutableTriple;
38 import org.apache.commons.lang3.tuple.Triple;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.InjectMocks;
42 import org.mockito.Mock;
43 import org.mockito.Mockito;
44 import org.mockito.MockitoAnnotations;
45 import org.openecomp.sdc.be.components.BeConfDependentTest;
46 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
47 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationInfo;
48 import org.openecomp.sdc.be.dao.api.ActionStatus;
49 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
50 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
51 import org.openecomp.sdc.be.dao.cassandra.SdcSchemaFilesCassandraDao;
52 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
53 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
54 import org.openecomp.sdc.be.impl.ComponentsUtils;
55 import org.openecomp.sdc.be.model.ArtifactDefinition;
56 import org.openecomp.sdc.be.model.Component;
57 import org.openecomp.sdc.be.model.ComponentInstance;
58 import org.openecomp.sdc.be.model.LifecycleStateEnum;
59 import org.openecomp.sdc.be.model.Resource;
60 import org.openecomp.sdc.be.model.Service;
61 import org.openecomp.sdc.be.model.User;
62 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
63 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
64 import org.openecomp.sdc.be.resources.data.ESArtifactData;
65 import org.openecomp.sdc.be.resources.data.SdcSchemaFilesData;
66 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
67 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
68 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
69 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
71 import fj.data.Either;
72 import mockit.Deencapsulation;
74 import static junit.framework.Assert.assertFalse;
75 import static junit.framework.Assert.assertNotNull;
76 import static junit.framework.TestCase.assertEquals;
77 import static junit.framework.TestCase.assertSame;
78 import static junit.framework.TestCase.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 MockitoAnnotations.initMocks(this);
109 private NonMetaArtifactInfo createNonMetaArtifactInfoTestSubject() {
110 return new CsarUtils.NonMetaArtifactInfo("mock", "mock", ArtifactTypeEnum.AAI_SERVICE_MODEL,
111 ArtifactGroupTypeEnum.DEPLOYMENT, new byte[0], "mock", true);
115 public void testCreateCsar() {
116 Component component = new Resource();
117 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
118 ArtifactDefinition artifact = new ArtifactDefinition();
119 artifact.setArtifactName("artifactName");
120 artifact.setEsId("esId");
121 toscaArtifacts.put("assettoscatemplate", artifact);
123 component.setToscaArtifacts(toscaArtifacts);
125 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
126 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
128 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
129 .thenReturn(ActionStatus.GENERAL_ERROR);
131 Either<byte[], ResponseFormat> csar = testSubject.createCsar(component, true, true);
134 assertTrue(csar.isRight());
138 public void testCreateCsarWithGenerateCsarZipResponseIsLeft() {
139 Component component = new Resource();
140 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
141 ArtifactDefinition artifact = new ArtifactDefinition();
142 artifact.setArtifactName("artifactName");
143 artifact.setEsId("esId");
144 artifact.setArtifactUUID("artifactUUID");
145 artifact.setArtifactType("YANG");
146 toscaArtifacts.put("assettoscatemplate", artifact);
148 component.setToscaArtifacts(toscaArtifacts);
149 component.setDeploymentArtifacts(toscaArtifacts);
150 component.setArtifacts(toscaArtifacts);
151 ESArtifactData artifactData = new ESArtifactData();
152 byte[] data = "value".getBytes();
153 artifactData.setDataAsArray(data);
155 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
156 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
157 toscaTemplate.setDependencies(dependencies);
159 List<SdcSchemaFilesData> filesData = new ArrayList<>();
160 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
161 filedata.setPayloadAsArray(data);
162 filesData.add(filedata);
164 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
166 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
167 .thenReturn(ActionStatus.GENERAL_ERROR);
169 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
170 .thenReturn(Either.left(toscaTemplate));
173 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
174 .thenReturn(Either.left(filesData));
176 Either<byte[], ResponseFormat> csar = testSubject.createCsar(component, false, true);
179 assertTrue(csar.isLeft());
183 public void testGenerateCsarZipThrowsIOException() {
185 Deencapsulation.invoke(testSubject, "generateCsarZip", new byte[]{},new byte[]{}, new Resource(), true, false);
189 public void testPopulateZipWhenGetDependenciesIsRight() throws IOException {
190 Component component = new Service();
191 boolean getFromCS = false;
193 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
194 ArtifactDefinition artifact = new ArtifactDefinition();
195 artifact.setArtifactName("artifactName");
196 artifact.setEsId("esId");
197 artifact.setArtifactUUID("artifactUUID");
198 artifact.setArtifactType("YANG");
199 toscaArtifacts.put("assettoscatemplate", artifact);
201 component.setToscaArtifacts(toscaArtifacts);
202 component.setDeploymentArtifacts(toscaArtifacts);
203 component.setArtifacts(toscaArtifacts);
204 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
205 ESArtifactData artifactData = new ESArtifactData();
206 byte[] data = "value".getBytes();
207 artifactData.setDataAsArray(data);
209 ToscaRepresentation tosca = new ToscaRepresentation();
210 tosca.setMainYaml("value");
212 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
214 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
216 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
217 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
219 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
220 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
222 assertNotNull(output);
223 assertTrue(output.isRight());
228 public void testPopulateZipWhenExportComponentIsRight() throws IOException {
229 Component component = new Resource();
230 boolean getFromCS = false;
232 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
233 ArtifactDefinition artifact = new ArtifactDefinition();
234 artifact.setArtifactName("artifactName");
235 artifact.setEsId("esId");
236 artifact.setArtifactUUID("artifactUUID");
237 artifact.setArtifactType("YANG");
238 toscaArtifacts.put("assettoscatemplate", artifact);
240 component.setToscaArtifacts(toscaArtifacts);
241 component.setDeploymentArtifacts(toscaArtifacts);
242 component.setArtifacts(toscaArtifacts);
243 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
244 ESArtifactData artifactData = new ESArtifactData();
245 byte[] data = "value".getBytes();
246 artifactData.setDataAsArray(data);
248 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
249 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
251 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
252 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
254 assertNotNull(output);
255 assertTrue(output.isRight());
260 public void testPopulateZipWhenComponentIsServiceAndCollectComponentCsarDefinitionIsRight() throws IOException {
261 Component component = new Service();
262 boolean getFromCS = false;
264 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
265 ArtifactDefinition artifact = new ArtifactDefinition();
266 artifact.setArtifactName("artifactName");
267 artifact.setEsId("esId");
268 artifact.setArtifactUUID("artifactUUID");
269 artifact.setArtifactType("YANG");
270 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
271 artifact.setDescription("description");
272 artifact.setArtifactLabel("artifactLabel");
273 toscaArtifacts.put("assettoscatemplate", artifact);
275 component.setToscaArtifacts(toscaArtifacts);
276 component.setDeploymentArtifacts(toscaArtifacts);
277 component.setArtifacts(toscaArtifacts);
278 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
279 component.setVersion("1.0");
280 component.setLastUpdaterUserId("userId");
281 component.setUniqueId("uid");
282 ESArtifactData artifactData = new ESArtifactData();
283 byte[] data = "value".getBytes();
284 artifactData.setDataAsArray(data);
286 List<SdcSchemaFilesData> filesData = new ArrayList<>();
287 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
288 filedata.setPayloadAsArray(data);
289 filesData.add(filedata);
291 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
292 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
293 Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
294 dependencies.add(triple);
295 toscaTemplate.setDependencies(dependencies);
297 ToscaRepresentation tosca = new ToscaRepresentation();
298 tosca.setMainYaml("value");
300 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
302 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
304 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
305 .thenReturn(Either.left(toscaTemplate));
308 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
309 .thenReturn(Either.left(filesData));
311 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
312 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
314 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(String.class), Mockito.any(String.class),
315 Mockito.any(Boolean.class))).thenReturn(new User());
317 Mockito.when(artifactsBusinessLogic.validateAndHandleArtifact(Mockito.any(String.class),
318 Mockito.any(ComponentTypeEnum.class), Mockito.any(ArtifactOperationInfo.class), Mockito.isNull(),
319 Mockito.any(ArtifactDefinition.class), Mockito.any(String.class), Mockito.any(String.class),
320 Mockito.isNull(), Mockito.isNull(), Mockito.any(User.class), Mockito.any(Component.class),
321 Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(Boolean.class)))
322 .thenReturn(Either.left(Mockito.any(Either.class)));
324 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
325 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
327 assertNotNull(output);
328 assertTrue(output.isRight());
333 public void testPopulateZipWhenGetEntryDataIsRight() throws IOException {
334 Component component = new Service();
335 boolean getFromCS = true;
337 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
338 ArtifactDefinition artifact = new ArtifactDefinition();
339 artifact.setArtifactName("artifactName");
340 artifact.setEsId("esId");
341 artifact.setArtifactUUID("artifactUUID");
342 artifact.setArtifactType("YANG");
343 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
344 artifact.setDescription("description");
345 artifact.setArtifactLabel("artifactLabel");
346 toscaArtifacts.put("assettoscatemplate", artifact);
348 component.setToscaArtifacts(toscaArtifacts);
349 component.setDeploymentArtifacts(toscaArtifacts);
350 component.setArtifacts(toscaArtifacts);
351 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
352 component.setVersion("1.0");
353 component.setLastUpdaterUserId("userId");
354 component.setUniqueId("uid");
355 ESArtifactData artifactData = new ESArtifactData();
356 byte[] data = "value".getBytes();
357 artifactData.setDataAsArray(data);
359 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
360 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
361 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
362 dependencies.add(triple);
363 toscaTemplate.setDependencies(dependencies);
365 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
367 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
368 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
370 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
371 .thenReturn(Either.left(toscaTemplate));
373 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
374 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
376 assertNotNull(output);
377 assertTrue(output.isRight());
382 public void testPopulateZipWhenGetEntryDataOfInnerComponentIsRight() throws IOException {
383 Component component = new Service();
384 boolean getFromCS = false;
386 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
387 ArtifactDefinition artifact = new ArtifactDefinition();
388 artifact.setArtifactName("artifactName");
389 artifact.setEsId("esId");
390 artifact.setArtifactUUID("artifactUUID");
391 artifact.setArtifactType("YANG");
392 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
393 artifact.setDescription("description");
394 artifact.setArtifactLabel("artifactLabel");
395 toscaArtifacts.put("assettoscatemplate", artifact);
397 component.setToscaArtifacts(toscaArtifacts);
398 component.setDeploymentArtifacts(toscaArtifacts);
399 component.setArtifacts(toscaArtifacts);
400 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
401 component.setVersion("1.0");
402 component.setLastUpdaterUserId("userId");
403 component.setUniqueId("uid");
404 ESArtifactData artifactData = new ESArtifactData();
405 byte[] data = "value".getBytes();
406 artifactData.setDataAsArray(data);
408 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
409 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
410 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
411 dependencies.add(triple);
412 toscaTemplate.setDependencies(dependencies);
414 ToscaRepresentation tosca = new ToscaRepresentation();
415 tosca.setMainYaml("value");
417 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
419 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca),
420 Either.left(tosca), Either.right(ToscaError.GENERAL_ERROR));
422 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
423 .thenReturn(Either.left(toscaTemplate));
425 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
426 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
428 assertNotNull(output);
429 assertTrue(output.isRight());
434 public void testPopulateZipWhenLatestSchemaFilesFromCassandraIsRight() throws IOException {
435 Component component = new Service();
436 boolean getFromCS = false;
438 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
439 ArtifactDefinition artifact = new ArtifactDefinition();
440 artifact.setArtifactName("artifactName");
441 artifact.setEsId("esId");
442 artifact.setArtifactUUID("artifactUUID");
443 artifact.setArtifactType("YANG");
444 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
445 artifact.setDescription("description");
446 artifact.setArtifactLabel("artifactLabel");
447 toscaArtifacts.put("assettoscatemplate", artifact);
449 component.setToscaArtifacts(toscaArtifacts);
450 component.setDeploymentArtifacts(toscaArtifacts);
451 component.setArtifacts(toscaArtifacts);
452 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
453 component.setVersion("1.0");
454 component.setLastUpdaterUserId("userId");
455 component.setUniqueId("uid");
456 ESArtifactData artifactData = new ESArtifactData();
457 byte[] data = "value".getBytes();
458 artifactData.setDataAsArray(data);
460 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
461 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
462 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
463 dependencies.add(triple);
464 toscaTemplate.setDependencies(dependencies);
466 ToscaRepresentation tosca = new ToscaRepresentation();
467 tosca.setMainYaml("value");
469 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
471 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
473 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
474 .thenReturn(Either.left(toscaTemplate));
477 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
478 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
480 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
481 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
483 assertNotNull(output);
484 assertTrue(output.isRight());
489 public void testPopulateZipWhenAddSchemaFilesFromCassandraIsRight() throws IOException {
490 Component component = new Service();
491 boolean getFromCS = false;
493 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
494 ArtifactDefinition artifact = new ArtifactDefinition();
495 artifact.setArtifactName("artifactName");
496 artifact.setEsId("esId");
497 artifact.setArtifactUUID("artifactUUID");
498 artifact.setArtifactType("YANG");
499 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
500 artifact.setDescription("description");
501 artifact.setArtifactLabel("artifactLabel");
502 toscaArtifacts.put("assettoscatemplate", artifact);
504 component.setToscaArtifacts(toscaArtifacts);
505 component.setDeploymentArtifacts(toscaArtifacts);
506 component.setArtifacts(toscaArtifacts);
507 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
508 component.setVersion("1.0");
509 component.setLastUpdaterUserId("userId");
510 component.setUniqueId("uid");
511 ESArtifactData artifactData = new ESArtifactData();
512 byte[] data = "value".getBytes();
513 artifactData.setDataAsArray(data);
515 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
516 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
517 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
518 dependencies.add(triple);
519 toscaTemplate.setDependencies(dependencies);
521 ToscaRepresentation tosca = new ToscaRepresentation();
522 tosca.setMainYaml("value");
524 List<SdcSchemaFilesData> schemaList = new ArrayList<>();
525 SdcSchemaFilesData schemaData = new SdcSchemaFilesData();
526 schemaData.setPayloadAsArray(null);
527 schemaList.add(schemaData);
529 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
531 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
533 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
534 .thenReturn(Either.left(toscaTemplate));
537 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
538 .thenReturn(Either.left(schemaList));
540 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
541 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
543 assertNotNull(output);
544 assertTrue(output.isRight());
549 public void testPopulateZipWhenHandleAllAAIArtifactsInDataModelIsRight() throws IOException {
550 Component component = new Service();
551 boolean getFromCS = false;
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.setToscaArtifacts(toscaArtifacts);
565 component.setDeploymentArtifacts(toscaArtifacts);
566 component.setArtifacts(toscaArtifacts);
567 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
568 component.setVersion("1.0");
569 component.setLastUpdaterUserId("userId");
570 component.setUniqueId("uid");
571 ESArtifactData artifactData = new ESArtifactData();
572 byte[] data = "value".getBytes();
573 artifactData.setDataAsArray(data);
575 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
576 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
577 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
578 dependencies.add(triple);
579 toscaTemplate.setDependencies(dependencies);
581 ToscaRepresentation tosca = new ToscaRepresentation();
582 tosca.setMainYaml("value");
584 List<SdcSchemaFilesData> schemaList = new ArrayList<>();
585 SdcSchemaFilesData schemaData = new SdcSchemaFilesData();
586 schemaData.setPayloadAsArray(data);
587 schemaList.add(schemaData);
589 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
591 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
593 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
595 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
596 .thenReturn(Either.left(toscaTemplate));
599 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
600 .thenReturn(Either.left(schemaList));
602 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(String.class), Mockito.any(String.class),
603 Mockito.any(Boolean.class))).thenReturn(new User());
606 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
607 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
609 assertNotNull(output);
610 assertTrue(output.isRight());
615 public void testAddSchemaFilesFromCassandra() throws IOException {
616 try (ByteArrayOutputStream out = new ByteArrayOutputStream();
617 ZipOutputStream zip = new ZipOutputStream(out);
618 ByteArrayOutputStream outMockStream = new ByteArrayOutputStream();
619 ZipOutputStream outMock = new ZipOutputStream(outMockStream);) {
621 outMock.putNextEntry(new ZipEntry("mock1"));
622 outMock.write(new byte[1]);
623 outMock.putNextEntry(new ZipEntry("mock2"));
624 outMock.write(new byte[3]);
626 byte[] byteArray = outMockStream.toByteArray();
627 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "addSchemaFilesFromCassandra", zip, byteArray);
629 assertNotNull(output);
630 assertTrue(output.isLeft());
636 public void testAddInnerComponentsToCache() {
637 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
638 Component childComponent = new Resource();
639 Component componentRI = new Service();
640 List<ComponentInstance> componentInstances = new ArrayList<>();
641 ComponentInstance instance = new ComponentInstance();
642 instance.setComponentUid("resourceUid");
643 componentInstances.add(instance);
644 childComponent.setComponentInstances(componentInstances);
646 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
647 ArtifactDefinition artifact = new ArtifactDefinition();
648 artifact.setArtifactName("artifactName");
649 artifact.setEsId("esId");
650 artifact.setArtifactUUID("artifactUUID");
651 artifact.setArtifactType("YANG");
652 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
653 artifact.setDescription("description");
654 artifact.setArtifactLabel("artifactLabel");
655 toscaArtifacts.put("assettoscatemplate", artifact);
657 componentRI.setToscaArtifacts(toscaArtifacts);
659 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
660 .thenReturn(Either.left(componentRI));
662 Deencapsulation.invoke(testSubject, "addInnerComponentsToCache", componentCache, childComponent);
664 assertTrue(componentCache.containsValue(ImmutableTriple.of("esId","artifactName",componentRI)));
668 public void testAddInnerComponentsToCacheWhenGetToscaElementIsRight() {
669 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
670 Component childComponent = new Resource();
672 List<ComponentInstance> componentInstances = new ArrayList<>();
673 ComponentInstance instance = new ComponentInstance();
674 componentInstances.add(instance);
675 childComponent.setComponentInstances(componentInstances);
677 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
678 ArtifactDefinition artifact = new ArtifactDefinition();
679 artifact.setArtifactName("artifactName");
680 artifact.setEsId("esId");
681 artifact.setArtifactUUID("artifactUUID");
682 artifact.setArtifactType("YANG");
683 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
684 artifact.setDescription("description");
685 artifact.setArtifactLabel("artifactLabel");
686 toscaArtifacts.put("assettoscatemplate", artifact);
688 Component componentRI = new Service();
690 componentRI.setToscaArtifacts(toscaArtifacts);
692 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
693 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
695 Deencapsulation.invoke(testSubject, "addInnerComponentsToCache", componentCache, childComponent);
697 assertTrue(componentCache.isEmpty());
701 public void testAddComponentToCache() {
702 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
704 String fileName = "fileName";
705 Component component = new Resource();
706 component.setInvariantUUID("key");
707 component.setVersion("1.0");
709 Component cachedComponent = new Resource();
710 cachedComponent.setVersion("0.3");
712 componentCache.put("key", new ImmutableTriple<String, String, Component>(id, fileName, cachedComponent));
714 Deencapsulation.invoke(testSubject, "addComponentToCache", componentCache, id, fileName, component);
716 assertSame("id", componentCache.get("key").left);
717 assertSame("fileName", componentCache.get("key").middle);
718 assertSame(componentCache.get("key").right, component);
722 public void testWriteComponentInterface() throws IOException {
723 String fileName = "name.hello";
724 ToscaRepresentation tosca = new ToscaRepresentation();
725 tosca.setMainYaml("value");
727 Mockito.when(toscaExportUtils.exportComponentInterface(Mockito.any(Component.class), Mockito.any(Boolean.class)))
728 .thenReturn(Either.left(tosca));
731 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
732 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "writeComponentInterface", new Resource(), zip, fileName, false);
734 assertNotNull(output);
735 assertTrue(output.isLeft());
740 public void testGetEntryData() {
741 String cassandraId = "id";
742 Component childComponent = new Resource();
744 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
745 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
747 Either<byte[], ActionStatus> output = Deencapsulation.invoke(testSubject, "getEntryData", cassandraId, childComponent);
749 assertNotNull(output);
750 assertTrue(output.isRight());
754 public void testGetLatestSchemaFilesFromCassandraWhenListOfSchemasIsEmpty() {
755 List<SdcSchemaFilesData> filesData = new ArrayList<>();
758 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
759 .thenReturn(Either.left(filesData));
761 Either<byte[], ResponseFormat> output = Deencapsulation.invoke(testSubject, "getLatestSchemaFilesFromCassandra");
763 assertNotNull(output);
764 assertTrue(output.isRight());
768 public void testExtractVfcsArtifactsFromCsar() {
769 String key = "Artifacts/org.openecomp.resource.some/Deployment/to/resource";
770 byte[] data = "value".getBytes();
772 Map<String, byte[]> csar = new HashMap<>();
775 Map<String, List<ArtifactDefinition>> output = CsarUtils.extractVfcsArtifactsFromCsar(csar);
777 assertNotNull(output);
778 assertTrue(output.containsKey("org.openecomp.resource.some"));
779 assertEquals(1, output.get("org.openecomp.resource.some").size());
783 public void testAddExtractedVfcArtifactWhenArtifactsContainsExtractedArtifactKey() {
784 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
785 "key", new ArtifactDefinition());
786 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
787 artifacts.put("key", new ArrayList<>());
789 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
791 assertEquals(1, artifacts.get("key").size());
795 public void testAddExtractedVfcArtifactWhenArtifactsDoesntContainsExtractedArtifactKey() {
796 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
797 "key", new ArtifactDefinition());
798 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
799 artifacts.put("key1", new ArrayList<>());
801 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
803 assertEquals(0, artifacts.get("key1").size());
804 assertEquals(1, artifacts.get("key").size());
805 assertEquals(2, artifacts.size());
809 public void testExtractVfcArtifact() {
810 String path = "path/to/informational/artificat";
811 Map<String, byte[]> map = new HashMap<>();
812 map.put(path, "value".getBytes());
813 Entry<String, byte[]> entry = map.entrySet().iterator().next();
815 ImmutablePair<String, ArtifactDefinition> output = Deencapsulation.invoke(testSubject, "extractVfcArtifact", entry, new HashMap<>());
817 assertNotNull(output);
818 assertEquals("to",output.left);
822 public void testDetectArtifactGroupTypeWithExceptionBeingCaught() {
823 Either<ArtifactGroupTypeEnum, Boolean> output = Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", Map.class);
825 assertNotNull(output);
826 assertTrue(output.isRight());
827 assertFalse(output.right().value());
831 public void testDetectArtifactGroupTypeWWhenCollectedWarningMessagesContainesKey() {
832 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
834 collectedWarningMessages.put("Warning - unrecognized artifact group type {} was received.", new HashSet<>());
835 Either<ArtifactGroupTypeEnum, Boolean> output = Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", collectedWarningMessages);
837 assertNotNull(output);
838 assertTrue(output.isRight());
839 assertFalse(output.right().value());
843 public void testNonMetaArtifactInfoCtor() {
844 createNonMetaArtifactInfoTestSubject();
848 public void testNonMetaArtifactInfoGetPath() {
849 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
851 testSubject.getPath();
855 public void testNonMetaArtifactInfoGetArtifactName() {
856 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
858 testSubject.getArtifactName();
862 public void testNonMetaArtifactInfoGetArtifactType() {
863 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
865 testSubject.getArtifactType();
869 public void testNonMetaArtifactInfoGetDisplayName() {
870 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
872 testSubject.getDisplayName();
876 public void testNonMetaArtifactInfoGetArtifactGroupType() {
877 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
879 testSubject.getArtifactGroupType();
883 public void testNonMetaArtifactInfoGetArtifactLabel() {
884 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
886 testSubject.getArtifactLabel();
890 public void testNonMetaArtifactInfoGetIsFromCsar() {
891 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
893 testSubject.isFromCsar();
897 public void testNonMetaArtifactInfoGetPayloadData() {
898 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
900 testSubject.getPayloadData();
904 public void testNonMetaArtifactInfoGetArtifaactChecksum() {
905 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
907 testSubject.getArtifactChecksum();
911 public void testNonMetaArtifactInfoGetArtifactUniqueId() {
912 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
914 testSubject.getArtifactUniqueId();
918 public void testNonMetaArtifactInfosetArtifactUniqueId() {
919 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
921 testSubject.setArtifactUniqueId("artifactUniqueId");
925 public void testValidateNonMetaArtifactWithExceptionCaught() {
926 CsarUtils.validateNonMetaArtifact("", new byte[0], new HashMap<>());
930 public void testWriteArtifactDefinition() throws IOException {
931 Component component = new Service();
932 List<ArtifactDefinition> artifactDefinitionList = new ArrayList<>();
933 String artifactPathAndFolder = "";
935 ArtifactDefinition artifact = new ArtifactDefinition();
936 artifact.setArtifactType(ArtifactTypeEnum.HEAT_ENV.getType());
937 artifactDefinitionList.add(artifact);
939 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
940 Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "writeArtifactDefinition", component, zip, artifactDefinitionList, artifactPathAndFolder, false);
942 assertNotNull(output);
943 assertTrue(output.isLeft());
948 public void testCollectComponentCsarDefinitionWhenComponentIsServiceAndGetToscaElementIsLeft() {
949 Component component = new Service();
950 component.setUniqueId("uniqueId");
951 List<ComponentInstance> resourceInstances = new ArrayList<>();
952 ComponentInstance instance = new ComponentInstance();
953 instance.setComponentUid("resourceUid");
954 instance.setOriginType(OriginTypeEnum.SERVICE);
955 resourceInstances.add(instance);
956 component.setComponentInstances(resourceInstances);
958 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
959 ArtifactDefinition artifact = new ArtifactDefinition();
960 artifact.setArtifactName("artifactName");
961 artifact.setEsId("esId");
962 artifact.setArtifactUUID("artifactUUID");
963 artifact.setArtifactType("YANG");
964 toscaArtifacts.put("assettoscatemplate", artifact);
966 component.setToscaArtifacts(toscaArtifacts);
967 component.setDeploymentArtifacts(toscaArtifacts);
968 component.setArtifacts(toscaArtifacts);
970 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
971 Either.right(StorageOperationStatus.BAD_REQUEST));
973 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
975 assertNotNull(output);
976 assertTrue(output.isRight());
980 public void testCollectComponentTypeArtifactsWhenFetchedComponentHasComponentInstances() {
981 Component component = new Service();
982 Component fetchedComponent = new Resource();
983 component.setUniqueId("uniqueId");
984 List<ComponentInstance> resourceInstances = new ArrayList<>();
985 ComponentInstance instance = new ComponentInstance();
986 instance.setComponentUid("resourceUid");
987 instance.setOriginType(OriginTypeEnum.SERVICE);
988 resourceInstances.add(instance);
989 component.setComponentInstances(resourceInstances);
990 fetchedComponent.setComponentInstances(resourceInstances);
992 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
993 ArtifactDefinition artifact = new ArtifactDefinition();
994 artifact.setArtifactName("artifactName");
995 artifact.setEsId("esId");
996 artifact.setArtifactUUID("artifactUUID");
997 artifact.setArtifactType("YANG");
998 toscaArtifacts.put("assettoscatemplate", artifact);
1000 component.setToscaArtifacts(toscaArtifacts);
1001 component.setDeploymentArtifacts(toscaArtifacts);
1002 component.setArtifacts(toscaArtifacts);
1004 fetchedComponent.setToscaArtifacts(toscaArtifacts);
1005 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
1006 fetchedComponent.setArtifacts(toscaArtifacts);
1008 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
1009 Either.left(fetchedComponent), Either.right(StorageOperationStatus.BAD_REQUEST));
1011 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
1013 assertNotNull(output);
1014 assertTrue(output.isRight());
1018 public void testCollectComponentTypeArtifactsWhenFetchedComponentDontHaveComponentInstances() {
1019 Component component = new Service();
1020 Component fetchedComponent = new Resource();
1021 component.setUniqueId("uniqueId");
1022 List<ComponentInstance> resourceInstances = new ArrayList<>();
1023 ComponentInstance instance = new ComponentInstance();
1024 instance.setComponentUid("resourceUid");
1025 instance.setOriginType(OriginTypeEnum.SERVICE);
1027 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
1028 ArtifactDefinition artifact = new ArtifactDefinition();
1029 artifact.setArtifactName("artifactName");
1030 artifact.setEsId("esId");
1031 artifact.setArtifactUUID("artifactUUID");
1032 artifact.setArtifactType("PLAN");
1033 toscaArtifacts.put("assettoscatemplate", artifact);
1035 instance.setDeploymentArtifacts(toscaArtifacts);
1037 resourceInstances.add(instance);
1038 component.setComponentInstances(resourceInstances);
1040 component.setToscaArtifacts(toscaArtifacts);
1041 component.setDeploymentArtifacts(toscaArtifacts);
1042 component.setArtifacts(toscaArtifacts);
1044 fetchedComponent.setToscaArtifacts(toscaArtifacts);
1045 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
1046 fetchedComponent.setArtifacts(toscaArtifacts);
1048 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
1049 Either.left(fetchedComponent));
1051 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
1053 assertNotNull(output);
1054 assertTrue(output.isLeft());
1058 public void testValidateNonMetaArtifactHappyScenario() {
1059 String artifactPath = "Artifacts/Deployment/YANG_XML/myYang.xml";
1060 byte[] payloadData = "some payload data".getBytes();
1061 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
1062 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
1063 payloadData, collectedWarningMessages);
1064 assertTrue(eitherNonMetaArtifact.isLeft());
1065 assertTrue(collectedWarningMessages.isEmpty());
1067 artifactPath = "Artifacts/Informational/OTHER/someArtifact.xml";
1068 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
1069 assertTrue(eitherNonMetaArtifact.isLeft());
1070 assertTrue(collectedWarningMessages.isEmpty());
1074 public void testValidateNonMetaArtifactScenarioWithWarnnings() {
1075 String artifactPath = "Artifacts/Deployment/Buga/myYang.xml";
1076 byte[] payloadData = "some payload data".getBytes();
1077 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
1078 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
1079 payloadData, collectedWarningMessages);
1080 assertTrue(eitherNonMetaArtifact.isLeft());
1082 artifactPath = "Artifacts/Informational/Buga2/someArtifact.xml";
1083 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
1084 assertTrue(eitherNonMetaArtifact.isLeft());
1086 assertTrue(collectedWarningMessages.size() == 1);
1087 assertTrue(collectedWarningMessages.values().iterator().next().size() == 2);
1091 public void testValidateNonMetaArtifactUnhappyScenario() {
1092 String artifactPath = "Artifacts/Buga/YANG_XML/myYang.xml";
1093 byte[] payloadData = "some payload data".getBytes();
1094 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
1095 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
1096 payloadData, collectedWarningMessages);
1097 assertTrue(eitherNonMetaArtifact.isRight());
1098 assertTrue(!collectedWarningMessages.isEmpty());