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.assertTrue;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
31 import java.util.Map.Entry;
33 import java.util.zip.ZipEntry;
34 import java.util.zip.ZipOutputStream;
36 import org.apache.commons.io.output.ByteArrayOutputStream;
37 import org.apache.commons.lang3.tuple.ImmutablePair;
38 import org.apache.commons.lang3.tuple.ImmutableTriple;
39 import org.apache.commons.lang3.tuple.Triple;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.MockitoAnnotations;
46 import org.openecomp.sdc.be.components.BeConfDependentTest;
47 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
48 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationInfo;
49 import org.openecomp.sdc.be.dao.api.ActionStatus;
50 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
51 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
52 import org.openecomp.sdc.be.dao.cassandra.SdcSchemaFilesCassandraDao;
53 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
54 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
55 import org.openecomp.sdc.be.impl.ComponentsUtils;
56 import org.openecomp.sdc.be.model.ArtifactDefinition;
57 import org.openecomp.sdc.be.model.Component;
58 import org.openecomp.sdc.be.model.ComponentInstance;
59 import org.openecomp.sdc.be.model.LifecycleStateEnum;
60 import org.openecomp.sdc.be.model.Resource;
61 import org.openecomp.sdc.be.model.Service;
62 import org.openecomp.sdc.be.model.User;
63 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
64 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
65 import org.openecomp.sdc.be.resources.data.ESArtifactData;
66 import org.openecomp.sdc.be.resources.data.SdcSchemaFilesData;
67 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
68 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
69 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
70 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
72 import fj.data.Either;
73 import mockit.Deencapsulation;
75 public class CsarUtilsTest extends BeConfDependentTest {
78 CsarUtils testSubject;
81 private ArtifactCassandraDao artifactCassandraDao;
84 private ComponentsUtils componentsUtils;
87 private ToscaExportHandler toscaExportUtils;
90 private SdcSchemaFilesCassandraDao sdcSchemaFilesCassandraDao;
93 private ToscaOperationFacade toscaOperationFacade;
96 private ArtifactsBusinessLogic artifactsBusinessLogic;
99 public void setUpMock() throws Exception {
100 MockitoAnnotations.initMocks(this);
104 private NonMetaArtifactInfo createNonMetaArtifactInfoTestSubject() {
105 return new CsarUtils.NonMetaArtifactInfo("mock", "mock", ArtifactTypeEnum.AAI_SERVICE_MODEL,
106 ArtifactGroupTypeEnum.DEPLOYMENT, new byte[0], "mock", true);
110 public void testCreateCsar() {
111 Component component = new Resource();
112 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
113 ArtifactDefinition artifact = new ArtifactDefinition();
114 artifact.setArtifactName("artifactName");
115 artifact.setEsId("esId");
116 toscaArtifacts.put("assettoscatemplate", artifact);
118 component.setToscaArtifacts(toscaArtifacts);
120 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
121 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
123 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
124 .thenReturn(ActionStatus.GENERAL_ERROR);
126 testSubject.createCsar(component, true, true);
130 public void testCreateCsarWithGenerateCsarZipResponseIsLeft() {
131 Component component = new Resource();
132 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
133 ArtifactDefinition artifact = new ArtifactDefinition();
134 artifact.setArtifactName("artifactName");
135 artifact.setEsId("esId");
136 artifact.setArtifactUUID("artifactUUID");
137 artifact.setArtifactType("YANG");
138 toscaArtifacts.put("assettoscatemplate", artifact);
140 component.setToscaArtifacts(toscaArtifacts);
141 component.setDeploymentArtifacts(toscaArtifacts);
142 component.setArtifacts(toscaArtifacts);
143 ESArtifactData artifactData = new ESArtifactData();
144 byte[] data = "value".getBytes();
145 artifactData.setDataAsArray(data);
147 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
148 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
149 toscaTemplate.setDependencies(dependencies);
151 List<SdcSchemaFilesData> filesData = new ArrayList<>();
152 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
153 filedata.setPayloadAsArray(data);
154 filesData.add(filedata);
156 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
158 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
159 .thenReturn(ActionStatus.GENERAL_ERROR);
161 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
162 .thenReturn(Either.left(toscaTemplate));
165 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
166 .thenReturn(Either.left(filesData));
168 testSubject.createCsar(component, false, true);
172 public void testGenerateCsarZipThrowsIOException() {
174 Deencapsulation.invoke(testSubject, "generateCsarZip", new byte[]{},new byte[]{}, new Resource(), true, false);
178 public void testPopulateZipWhenGetDependenciesIsRight() {
179 Component component = new Service();
180 boolean getFromCS = false;
182 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
183 ArtifactDefinition artifact = new ArtifactDefinition();
184 artifact.setArtifactName("artifactName");
185 artifact.setEsId("esId");
186 artifact.setArtifactUUID("artifactUUID");
187 artifact.setArtifactType("YANG");
188 toscaArtifacts.put("assettoscatemplate", artifact);
190 component.setToscaArtifacts(toscaArtifacts);
191 component.setDeploymentArtifacts(toscaArtifacts);
192 component.setArtifacts(toscaArtifacts);
193 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
194 ESArtifactData artifactData = new ESArtifactData();
195 byte[] data = "value".getBytes();
196 artifactData.setDataAsArray(data);
198 ToscaRepresentation tosca = new ToscaRepresentation();
199 tosca.setMainYaml("value");
201 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
203 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
205 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
206 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
208 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
209 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
210 } catch (Exception e) {
216 public void testPopulateZipWhenExportComponentIsRight() {
217 Component component = new Resource();
218 boolean getFromCS = false;
220 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
221 ArtifactDefinition artifact = new ArtifactDefinition();
222 artifact.setArtifactName("artifactName");
223 artifact.setEsId("esId");
224 artifact.setArtifactUUID("artifactUUID");
225 artifact.setArtifactType("YANG");
226 toscaArtifacts.put("assettoscatemplate", artifact);
228 component.setToscaArtifacts(toscaArtifacts);
229 component.setDeploymentArtifacts(toscaArtifacts);
230 component.setArtifacts(toscaArtifacts);
231 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
232 ESArtifactData artifactData = new ESArtifactData();
233 byte[] data = "value".getBytes();
234 artifactData.setDataAsArray(data);
236 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
237 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
239 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
240 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
241 } catch (Exception e) {
247 public void testPopulateZipWhenComponentIsServiceAndCollectComponentCsarDefinitionIsRight() {
248 Component component = new Service();
249 boolean getFromCS = false;
251 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
252 ArtifactDefinition artifact = new ArtifactDefinition();
253 artifact.setArtifactName("artifactName");
254 artifact.setEsId("esId");
255 artifact.setArtifactUUID("artifactUUID");
256 artifact.setArtifactType("YANG");
257 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
258 artifact.setDescription("description");
259 artifact.setArtifactLabel("artifactLabel");
260 toscaArtifacts.put("assettoscatemplate", artifact);
262 component.setToscaArtifacts(toscaArtifacts);
263 component.setDeploymentArtifacts(toscaArtifacts);
264 component.setArtifacts(toscaArtifacts);
265 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
266 component.setVersion("1.0");
267 component.setLastUpdaterUserId("userId");
268 component.setUniqueId("uid");
269 ESArtifactData artifactData = new ESArtifactData();
270 byte[] data = "value".getBytes();
271 artifactData.setDataAsArray(data);
273 List<SdcSchemaFilesData> filesData = new ArrayList<>();
274 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
275 filedata.setPayloadAsArray(data);
276 filesData.add(filedata);
278 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
279 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
280 Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
281 dependencies.add(triple);
282 toscaTemplate.setDependencies(dependencies);
284 ToscaRepresentation tosca = new ToscaRepresentation();
285 tosca.setMainYaml("value");
287 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
289 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
291 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
292 .thenReturn(Either.left(toscaTemplate));
295 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
296 .thenReturn(Either.left(filesData));
298 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
299 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
301 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(String.class), Mockito.any(String.class),
302 Mockito.any(Boolean.class))).thenReturn(new User());
304 Mockito.when(artifactsBusinessLogic.validateAndHandleArtifact(Mockito.any(String.class),
305 Mockito.any(ComponentTypeEnum.class), Mockito.any(ArtifactOperationInfo.class), Mockito.isNull(),
306 Mockito.any(ArtifactDefinition.class), Mockito.any(String.class), Mockito.any(String.class),
307 Mockito.isNull(), Mockito.isNull(), Mockito.any(User.class), Mockito.any(Component.class),
308 Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(Boolean.class)))
309 .thenReturn(Either.left(Mockito.any(Either.class)));
311 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
312 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
313 } catch (Exception e) {
319 public void testPopulateZipWhenGetEntryDataIsRight() {
320 Component component = new Service();
321 boolean getFromCS = true;
323 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
324 ArtifactDefinition artifact = new ArtifactDefinition();
325 artifact.setArtifactName("artifactName");
326 artifact.setEsId("esId");
327 artifact.setArtifactUUID("artifactUUID");
328 artifact.setArtifactType("YANG");
329 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
330 artifact.setDescription("description");
331 artifact.setArtifactLabel("artifactLabel");
332 toscaArtifacts.put("assettoscatemplate", artifact);
334 component.setToscaArtifacts(toscaArtifacts);
335 component.setDeploymentArtifacts(toscaArtifacts);
336 component.setArtifacts(toscaArtifacts);
337 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
338 component.setVersion("1.0");
339 component.setLastUpdaterUserId("userId");
340 component.setUniqueId("uid");
341 ESArtifactData artifactData = new ESArtifactData();
342 byte[] data = "value".getBytes();
343 artifactData.setDataAsArray(data);
345 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
346 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
347 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
348 dependencies.add(triple);
349 toscaTemplate.setDependencies(dependencies);
351 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
353 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
354 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
356 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
357 .thenReturn(Either.left(toscaTemplate));
359 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
360 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
361 } catch (Exception e) {
367 public void testPopulateZipWhenGetEntryDataOfInnerComponentIsRight() {
368 Component component = new Service();
369 boolean getFromCS = false;
371 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
372 ArtifactDefinition artifact = new ArtifactDefinition();
373 artifact.setArtifactName("artifactName");
374 artifact.setEsId("esId");
375 artifact.setArtifactUUID("artifactUUID");
376 artifact.setArtifactType("YANG");
377 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
378 artifact.setDescription("description");
379 artifact.setArtifactLabel("artifactLabel");
380 toscaArtifacts.put("assettoscatemplate", artifact);
382 component.setToscaArtifacts(toscaArtifacts);
383 component.setDeploymentArtifacts(toscaArtifacts);
384 component.setArtifacts(toscaArtifacts);
385 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
386 component.setVersion("1.0");
387 component.setLastUpdaterUserId("userId");
388 component.setUniqueId("uid");
389 ESArtifactData artifactData = new ESArtifactData();
390 byte[] data = "value".getBytes();
391 artifactData.setDataAsArray(data);
393 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
394 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
395 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
396 dependencies.add(triple);
397 toscaTemplate.setDependencies(dependencies);
399 ToscaRepresentation tosca = new ToscaRepresentation();
400 tosca.setMainYaml("value");
402 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
404 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca),
405 Either.left(tosca), Either.right(ToscaError.GENERAL_ERROR));
407 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
408 .thenReturn(Either.left(toscaTemplate));
410 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
411 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
412 } catch (Exception e) {
418 public void testPopulateZipWhenLatestSchemaFilesFromCassandraIsRight() {
419 Component component = new Service();
420 boolean getFromCS = false;
422 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
423 ArtifactDefinition artifact = new ArtifactDefinition();
424 artifact.setArtifactName("artifactName");
425 artifact.setEsId("esId");
426 artifact.setArtifactUUID("artifactUUID");
427 artifact.setArtifactType("YANG");
428 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
429 artifact.setDescription("description");
430 artifact.setArtifactLabel("artifactLabel");
431 toscaArtifacts.put("assettoscatemplate", artifact);
433 component.setToscaArtifacts(toscaArtifacts);
434 component.setDeploymentArtifacts(toscaArtifacts);
435 component.setArtifacts(toscaArtifacts);
436 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
437 component.setVersion("1.0");
438 component.setLastUpdaterUserId("userId");
439 component.setUniqueId("uid");
440 ESArtifactData artifactData = new ESArtifactData();
441 byte[] data = "value".getBytes();
442 artifactData.setDataAsArray(data);
444 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
445 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
446 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
447 dependencies.add(triple);
448 toscaTemplate.setDependencies(dependencies);
450 ToscaRepresentation tosca = new ToscaRepresentation();
451 tosca.setMainYaml("value");
453 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
455 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
457 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
458 .thenReturn(Either.left(toscaTemplate));
461 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
462 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
464 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
465 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
466 } catch (Exception e) {
472 public void testPopulateZipWhenAddSchemaFilesFromCassandraIsRight() {
473 Component component = new Service();
474 boolean getFromCS = false;
476 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
477 ArtifactDefinition artifact = new ArtifactDefinition();
478 artifact.setArtifactName("artifactName");
479 artifact.setEsId("esId");
480 artifact.setArtifactUUID("artifactUUID");
481 artifact.setArtifactType("YANG");
482 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
483 artifact.setDescription("description");
484 artifact.setArtifactLabel("artifactLabel");
485 toscaArtifacts.put("assettoscatemplate", artifact);
487 component.setToscaArtifacts(toscaArtifacts);
488 component.setDeploymentArtifacts(toscaArtifacts);
489 component.setArtifacts(toscaArtifacts);
490 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
491 component.setVersion("1.0");
492 component.setLastUpdaterUserId("userId");
493 component.setUniqueId("uid");
494 ESArtifactData artifactData = new ESArtifactData();
495 byte[] data = "value".getBytes();
496 artifactData.setDataAsArray(data);
498 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
499 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
500 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
501 dependencies.add(triple);
502 toscaTemplate.setDependencies(dependencies);
504 ToscaRepresentation tosca = new ToscaRepresentation();
505 tosca.setMainYaml("value");
507 List<SdcSchemaFilesData> schemaList = new ArrayList<>();
508 SdcSchemaFilesData schemaData = new SdcSchemaFilesData();
509 schemaData.setPayloadAsArray(null);
510 schemaList.add(schemaData);
512 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
514 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
516 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
517 .thenReturn(Either.left(toscaTemplate));
520 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
521 .thenReturn(Either.left(schemaList));
523 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
524 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
525 } catch (Exception e) {
531 public void testPopulateZipWhenHandleAllAAIArtifactsInDataModelIsRight() {
532 Component component = new Service();
533 boolean getFromCS = false;
535 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
536 ArtifactDefinition artifact = new ArtifactDefinition();
537 artifact.setArtifactName("artifactName");
538 artifact.setEsId("esId");
539 artifact.setArtifactUUID("artifactUUID");
540 artifact.setArtifactType("YANG");
541 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
542 artifact.setDescription("description");
543 artifact.setArtifactLabel("artifactLabel");
544 toscaArtifacts.put("assettoscatemplate", artifact);
546 component.setToscaArtifacts(toscaArtifacts);
547 component.setDeploymentArtifacts(toscaArtifacts);
548 component.setArtifacts(toscaArtifacts);
549 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
550 component.setVersion("1.0");
551 component.setLastUpdaterUserId("userId");
552 component.setUniqueId("uid");
553 ESArtifactData artifactData = new ESArtifactData();
554 byte[] data = "value".getBytes();
555 artifactData.setDataAsArray(data);
557 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
558 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
559 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
560 dependencies.add(triple);
561 toscaTemplate.setDependencies(dependencies);
563 ToscaRepresentation tosca = new ToscaRepresentation();
564 tosca.setMainYaml("value");
566 List<SdcSchemaFilesData> schemaList = new ArrayList<>();
567 SdcSchemaFilesData schemaData = new SdcSchemaFilesData();
568 schemaData.setPayloadAsArray(data);
569 schemaList.add(schemaData);
571 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
573 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
575 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
576 .thenReturn(Either.left(toscaTemplate));
579 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
580 .thenReturn(Either.left(schemaList));
582 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(String.class), Mockito.any(String.class),
583 Mockito.any(Boolean.class))).thenReturn(new User());
585 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
586 Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
587 } catch (Exception e) {
593 public void testAddSchemaFilesFromCassandra() {
594 try (ByteArrayOutputStream out = new ByteArrayOutputStream();
595 ZipOutputStream zip = new ZipOutputStream(out);
596 ByteArrayOutputStream outMockStream = new ByteArrayOutputStream();
597 ZipOutputStream outMock = new ZipOutputStream(outMockStream);) {
599 outMock.putNextEntry(new ZipEntry("mock1"));
600 outMock.write(new byte[1]);
601 outMock.putNextEntry(new ZipEntry("mock2"));
602 outMock.write(new byte[3]);
604 byte[] byteArray = outMockStream.toByteArray();
605 Deencapsulation.invoke(testSubject, "addSchemaFilesFromCassandra", zip, byteArray);
606 } catch (Exception e) {
613 public void testAddInnerComponentsToCache() {
614 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
615 Component childComponent = new Resource();
616 Component componentRI = new Service();
617 List<ComponentInstance> componentInstances = new ArrayList<>();
618 ComponentInstance instance = new ComponentInstance();
619 instance.setComponentUid("resourceUid");
620 componentInstances.add(instance);
621 childComponent.setComponentInstances(componentInstances);
623 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
624 ArtifactDefinition artifact = new ArtifactDefinition();
625 artifact.setArtifactName("artifactName");
626 artifact.setEsId("esId");
627 artifact.setArtifactUUID("artifactUUID");
628 artifact.setArtifactType("YANG");
629 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
630 artifact.setDescription("description");
631 artifact.setArtifactLabel("artifactLabel");
632 toscaArtifacts.put("assettoscatemplate", artifact);
634 componentRI.setToscaArtifacts(toscaArtifacts);
636 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
637 .thenReturn(Either.left(componentRI));
639 Deencapsulation.invoke(testSubject, "addInnerComponentsToCache", componentCache, childComponent);
643 public void testAddInnerComponentsToCacheWhenGetToscaElementIsRight() {
644 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
645 Component childComponent = new Resource();
647 List<ComponentInstance> componentInstances = new ArrayList<>();
648 ComponentInstance instance = new ComponentInstance();
649 componentInstances.add(instance);
650 childComponent.setComponentInstances(componentInstances);
652 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
653 ArtifactDefinition artifact = new ArtifactDefinition();
654 artifact.setArtifactName("artifactName");
655 artifact.setEsId("esId");
656 artifact.setArtifactUUID("artifactUUID");
657 artifact.setArtifactType("YANG");
658 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
659 artifact.setDescription("description");
660 artifact.setArtifactLabel("artifactLabel");
661 toscaArtifacts.put("assettoscatemplate", artifact);
663 Component componentRI = new Service();
665 componentRI.setToscaArtifacts(toscaArtifacts);
667 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
668 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
670 Deencapsulation.invoke(testSubject, "addInnerComponentsToCache", componentCache, childComponent);
674 public void testAddComponentToCache() {
675 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
677 String fileName = "fileName";
678 Component component = new Resource();
679 component.setInvariantUUID("key");
680 component.setVersion("1.0");
682 Component cachedComponent = new Resource();
683 cachedComponent.setVersion("0.3");
685 componentCache.put("key", new ImmutableTriple<String, String, Component>(id, fileName, cachedComponent));
687 Deencapsulation.invoke(testSubject, "addComponentToCache", componentCache, id, fileName, component);
691 public void testWriteComponentInterface() {
692 String fileName = "name.hello";
693 ToscaRepresentation tosca = new ToscaRepresentation();
694 tosca.setMainYaml("value");
696 Mockito.when(toscaExportUtils.exportComponentInterface(Mockito.any(Component.class), Mockito.any(Boolean.class)))
697 .thenReturn(Either.left(tosca));
699 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
700 Deencapsulation.invoke(testSubject, "writeComponentInterface", new Resource(), zip, fileName, false);
701 } catch (IOException e) {
708 public void testGetEntryData() {
709 String cassandraId = "id";
710 Component childComponent = new Resource();
712 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
713 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
715 Deencapsulation.invoke(testSubject, "getEntryData", cassandraId, childComponent);
719 public void testGetLatestSchemaFilesFromCassandraWhenListOfSchemasIsEmpty() {
720 List<SdcSchemaFilesData> filesData = new ArrayList<>();
723 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
724 .thenReturn(Either.left(filesData));
725 Deencapsulation.invoke(testSubject, "getLatestSchemaFilesFromCassandra");
729 public void testExtractVfcsArtifactsFromCsar() {
730 String key = "Artifacts/org.openecomp.resource.some/path/to/resource";
731 byte[] data = "value".getBytes();
733 Map<String, byte[]> csar = new HashMap<>();
736 CsarUtils.extractVfcsArtifactsFromCsar(csar);
740 public void testAddExtractedVfcArtifactWhenArtifactsContainsExtractedArtifactKey() {
741 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
742 "key", new ArtifactDefinition());
743 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
744 artifacts.put("key", new ArrayList<>());
746 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
750 public void testAddExtractedVfcArtifactWhenArtifactsDoesntContainsExtractedArtifactKey() {
751 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
752 "key", new ArtifactDefinition());
753 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
754 artifacts.put("key1", new ArrayList<>());
756 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
760 public void testExtractVfcArtifact() {
761 String path = "path/to/informational/artificat";
762 Map<String, byte[]> map = new HashMap<>();
763 map.put(path, "value".getBytes());
764 Entry<String, byte[]> entry = map.entrySet().iterator().next();
766 Deencapsulation.invoke(testSubject, "extractVfcArtifact", entry, new HashMap<>());
770 public void testDetectArtifactGroupTypeWithExceptionBeingCaught() {
771 Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", Map.class);
775 public void testDetectArtifactGroupTypeWWhenCollectedWarningMessagesContainesKey() {
776 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
778 collectedWarningMessages.put("Warning - unrecognized artifact group type {} was received.", new HashSet<>());
779 Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", collectedWarningMessages);
783 public void testNonMetaArtifactInfoCtor() {
784 createNonMetaArtifactInfoTestSubject();
788 public void testNonMetaArtifactInfoGetPath() {
789 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
791 testSubject.getPath();
795 public void testNonMetaArtifactInfoGetArtifactName() {
796 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
798 testSubject.getArtifactName();
802 public void testNonMetaArtifactInfoGetArtifactType() {
803 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
805 testSubject.getArtifactType();
809 public void testNonMetaArtifactInfoGetDisplayName() {
810 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
812 testSubject.getDisplayName();
816 public void testNonMetaArtifactInfoGetArtifactGroupType() {
817 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
819 testSubject.getArtifactGroupType();
823 public void testNonMetaArtifactInfoGetArtifactLabel() {
824 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
826 testSubject.getArtifactLabel();
830 public void testNonMetaArtifactInfoGetIsFromCsar() {
831 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
833 testSubject.isFromCsar();
837 public void testNonMetaArtifactInfoGetPayloadData() {
838 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
840 testSubject.getPayloadData();
844 public void testNonMetaArtifactInfoGetArtifaactChecksum() {
845 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
847 testSubject.getArtifactChecksum();
851 public void testNonMetaArtifactInfoGetArtifactUniqueId() {
852 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
854 testSubject.getArtifactUniqueId();
858 public void testNonMetaArtifactInfosetArtifactUniqueId() {
859 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
861 testSubject.setArtifactUniqueId("artifactUniqueId");
865 public void testValidateNonMetaArtifactWithExceptionCaught() {
866 CsarUtils.validateNonMetaArtifact("", new byte[0], new HashMap<>());
870 public void testWriteArtifactDefinition() {
871 Component component = new Service();
872 List<ArtifactDefinition> artifactDefinitionList = new ArrayList<>();
873 String artifactPathAndFolder = "";
875 ArtifactDefinition artifact = new ArtifactDefinition();
876 artifact.setArtifactType(ArtifactTypeEnum.HEAT_ENV.getType());
877 artifactDefinitionList.add(artifact);
879 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
880 Deencapsulation.invoke(testSubject, "writeArtifactDefinition", component, zip, artifactDefinitionList,
881 artifactPathAndFolder, false);
882 } catch (Exception e) {
888 public void testCollectComponentCsarDefinitionWhenComponentIsServiceAndGetToscaElementIsLeft() {
889 Component component = new Service();
890 component.setUniqueId("uniqueId");
891 List<ComponentInstance> resourceInstances = new ArrayList<>();
892 ComponentInstance instance = new ComponentInstance();
893 instance.setComponentUid("resourceUid");
894 instance.setOriginType(OriginTypeEnum.SERVICE);
895 resourceInstances.add(instance);
896 component.setComponentInstances(resourceInstances);
898 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
899 ArtifactDefinition artifact = new ArtifactDefinition();
900 artifact.setArtifactName("artifactName");
901 artifact.setEsId("esId");
902 artifact.setArtifactUUID("artifactUUID");
903 artifact.setArtifactType("YANG");
904 toscaArtifacts.put("assettoscatemplate", artifact);
906 component.setToscaArtifacts(toscaArtifacts);
907 component.setDeploymentArtifacts(toscaArtifacts);
908 component.setArtifacts(toscaArtifacts);
910 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
911 Either.right(StorageOperationStatus.BAD_REQUEST));
913 Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
918 public void testCollectComponentTypeArtifactsWhenFetchedComponentHasComponentInstances() {
919 Component component = new Service();
920 Component fetchedComponent = new Resource();
921 component.setUniqueId("uniqueId");
922 List<ComponentInstance> resourceInstances = new ArrayList<>();
923 ComponentInstance instance = new ComponentInstance();
924 instance.setComponentUid("resourceUid");
925 instance.setOriginType(OriginTypeEnum.SERVICE);
926 resourceInstances.add(instance);
927 component.setComponentInstances(resourceInstances);
928 fetchedComponent.setComponentInstances(resourceInstances);
930 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
931 ArtifactDefinition artifact = new ArtifactDefinition();
932 artifact.setArtifactName("artifactName");
933 artifact.setEsId("esId");
934 artifact.setArtifactUUID("artifactUUID");
935 artifact.setArtifactType("YANG");
936 toscaArtifacts.put("assettoscatemplate", artifact);
938 component.setToscaArtifacts(toscaArtifacts);
939 component.setDeploymentArtifacts(toscaArtifacts);
940 component.setArtifacts(toscaArtifacts);
942 fetchedComponent.setToscaArtifacts(toscaArtifacts);
943 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
944 fetchedComponent.setArtifacts(toscaArtifacts);
946 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
947 Either.left(fetchedComponent), Either.right(StorageOperationStatus.BAD_REQUEST));
949 Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
953 public void testCollectComponentTypeArtifactsWhenFetchedComponentDontHaveComponentInstances() {
954 Component component = new Service();
955 Component fetchedComponent = new Resource();
956 component.setUniqueId("uniqueId");
957 List<ComponentInstance> resourceInstances = new ArrayList<>();
958 ComponentInstance instance = new ComponentInstance();
959 instance.setComponentUid("resourceUid");
960 instance.setOriginType(OriginTypeEnum.SERVICE);
962 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
963 ArtifactDefinition artifact = new ArtifactDefinition();
964 artifact.setArtifactName("artifactName");
965 artifact.setEsId("esId");
966 artifact.setArtifactUUID("artifactUUID");
967 artifact.setArtifactType("PLAN");
968 toscaArtifacts.put("assettoscatemplate", artifact);
970 instance.setDeploymentArtifacts(toscaArtifacts);
972 resourceInstances.add(instance);
973 component.setComponentInstances(resourceInstances);
975 component.setToscaArtifacts(toscaArtifacts);
976 component.setDeploymentArtifacts(toscaArtifacts);
977 component.setArtifacts(toscaArtifacts);
979 fetchedComponent.setToscaArtifacts(toscaArtifacts);
980 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
981 fetchedComponent.setArtifacts(toscaArtifacts);
983 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
984 Either.left(fetchedComponent));
986 Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
990 public void testValidateNonMetaArtifactHappyScenario() {
991 String artifactPath = "Artifacts/Deployment/YANG_XML/myYang.xml";
992 byte[] payloadData = "some payload data".getBytes();
993 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
994 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
995 payloadData, collectedWarningMessages);
996 assertTrue(eitherNonMetaArtifact.isLeft());
997 assertTrue(collectedWarningMessages.isEmpty());
999 artifactPath = "Artifacts/Informational/OTHER/someArtifact.xml";
1000 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
1001 assertTrue(eitherNonMetaArtifact.isLeft());
1002 assertTrue(collectedWarningMessages.isEmpty());
1006 public void testValidateNonMetaArtifactScenarioWithWarnnings() {
1007 String artifactPath = "Artifacts/Deployment/Buga/myYang.xml";
1008 byte[] payloadData = "some payload data".getBytes();
1009 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
1010 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
1011 payloadData, collectedWarningMessages);
1012 assertTrue(eitherNonMetaArtifact.isLeft());
1014 artifactPath = "Artifacts/Informational/Buga2/someArtifact.xml";
1015 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
1016 assertTrue(eitherNonMetaArtifact.isLeft());
1018 assertTrue(collectedWarningMessages.size() == 1);
1019 assertTrue(collectedWarningMessages.values().iterator().next().size() == 2);
1023 public void testValidateNonMetaArtifactUnhappyScenario() {
1024 String artifactPath = "Artifacts/Buga/YANG_XML/myYang.xml";
1025 byte[] payloadData = "some payload data".getBytes();
1026 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
1027 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
1028 payloadData, collectedWarningMessages);
1029 assertTrue(eitherNonMetaArtifact.isRight());
1030 assertTrue(!collectedWarningMessages.isEmpty());