Include derived_from types in generated csar
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / CsarUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.tosca;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import fj.data.Either;
29 import java.io.IOException;
30 import java.nio.ByteBuffer;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Map.Entry;
41 import java.util.Set;
42 import java.util.zip.ZipEntry;
43 import java.util.zip.ZipOutputStream;
44 import mockit.Deencapsulation;
45 import org.apache.commons.collections.CollectionUtils;
46 import org.apache.commons.io.output.ByteArrayOutputStream;
47 import org.apache.commons.lang3.tuple.ImmutablePair;
48 import org.apache.commons.lang3.tuple.ImmutableTriple;
49 import org.apache.commons.lang3.tuple.Triple;
50 import org.junit.Assert;
51 import org.junit.Before;
52 import org.junit.Test;
53 import org.mockito.InjectMocks;
54 import org.mockito.Mock;
55 import org.mockito.Mockito;
56 import org.mockito.MockitoAnnotations;
57 import org.openecomp.sdc.be.components.BeConfDependentTest;
58 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
59 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationInfo;
60 import org.openecomp.sdc.be.dao.api.ActionStatus;
61 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
62 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
63 import org.openecomp.sdc.be.dao.cassandra.SdcSchemaFilesCassandraDao;
64 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
65 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
66 import org.openecomp.sdc.be.impl.ComponentsUtils;
67 import org.openecomp.sdc.be.model.ArtifactDefinition;
68 import org.openecomp.sdc.be.model.Component;
69 import org.openecomp.sdc.be.model.ComponentInstance;
70 import org.openecomp.sdc.be.model.LifecycleStateEnum;
71 import org.openecomp.sdc.be.model.Resource;
72 import org.openecomp.sdc.be.model.Service;
73 import org.openecomp.sdc.be.model.User;
74 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
75 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
76 import org.openecomp.sdc.be.resources.data.DAOArtifactData;
77 import org.openecomp.sdc.be.resources.data.SdcSchemaFilesData;
78 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
79 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
80 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
81 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
82 import org.openecomp.sdc.common.impl.ExternalConfiguration;
83 import org.openecomp.sdc.exception.ResponseFormat;
84
85 public class CsarUtilsTest extends BeConfDependentTest {
86
87         @InjectMocks
88         CsarUtils testSubject;
89
90         @Mock
91         private ArtifactCassandraDao artifactCassandraDao;
92
93         @Mock
94         private ComponentsUtils componentsUtils;
95
96         @Mock
97         private ToscaExportHandler toscaExportUtils;
98
99         @Mock
100         private SdcSchemaFilesCassandraDao sdcSchemaFilesCassandraDao;
101
102         @Mock
103         private ToscaOperationFacade toscaOperationFacade;
104
105         @Mock
106         private ArtifactsBusinessLogic artifactsBusinessLogic;
107
108         @Before
109         public void setUpMock() throws Exception {
110                 ExternalConfiguration.setAppName("catalog-be");
111                 MockitoAnnotations.initMocks(this);
112                 
113         }
114
115         private final List<String> nodesFromPackage = Arrays.asList("tosca.nodes.Root", "tosca.nodes.Container.Application");
116
117         private NonMetaArtifactInfo createNonMetaArtifactInfoTestSubject() {
118                 return new CsarUtils.NonMetaArtifactInfo("mock", "mock", ArtifactTypeEnum.AAI_SERVICE_MODEL,
119                                 ArtifactGroupTypeEnum.DEPLOYMENT, new byte[0], "mock", true);
120         }
121
122         @Test
123         public void testCreateCsar() {
124                 Component component = new Resource();
125                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
126                 ArtifactDefinition artifact = new ArtifactDefinition();
127                 artifact.setArtifactName("artifactName");
128                 artifact.setEsId("esId");
129                 toscaArtifacts.put("assettoscatemplate", artifact);
130
131                 component.setToscaArtifacts(toscaArtifacts);
132
133                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
134                                 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
135
136                 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
137                                 .thenReturn(ActionStatus.GENERAL_ERROR);
138
139                 testSubject.createCsar(component, true, true);
140         }
141
142         @Test
143         public void testCreateCsarWithGenerateCsarZipResponseIsLeft() {
144                 Component component = new Resource();
145                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
146                 ArtifactDefinition artifact = new ArtifactDefinition();
147                 artifact.setArtifactName("artifactName");
148                 artifact.setEsId("esId");
149                 artifact.setArtifactUUID("artifactUUID");
150                 artifact.setArtifactType("YANG");
151                 toscaArtifacts.put("assettoscatemplate", artifact);
152
153                 component.setToscaArtifacts(toscaArtifacts);
154                 component.setDeploymentArtifacts(toscaArtifacts);
155                 component.setArtifacts(toscaArtifacts);
156                 DAOArtifactData artifactData = new DAOArtifactData();
157                 byte[] data = "value".getBytes();
158                 ByteBuffer bufferData = ByteBuffer.wrap(data);
159                 artifactData.setData(bufferData);
160
161                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
162                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
163                 toscaTemplate.setDependencies(dependencies);
164
165                 List<SdcSchemaFilesData> filesData = new ArrayList<>();
166                 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
167                 filedata.setPayloadAsArray(data);
168                 filesData.add(filedata);
169
170                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
171
172                 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
173                                 .thenReturn(ActionStatus.GENERAL_ERROR);
174
175                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
176                                 .thenReturn(Either.left(toscaTemplate));
177
178                 Mockito.when(
179                                 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
180                                 .thenReturn(Either.left(filesData));
181
182                 testSubject.createCsar(component, false, true);
183         }
184
185         @Test
186         public void testPopulateZipWhenGetDependenciesIsRight() {
187                 Component component = new Service();
188                 boolean getFromCS = false;
189
190                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
191                 ArtifactDefinition artifact = new ArtifactDefinition();
192                 artifact.setArtifactName("artifactName");
193                 artifact.setEsId("esId");
194                 artifact.setArtifactUUID("artifactUUID");
195                 artifact.setArtifactType("YANG");
196                 toscaArtifacts.put("assettoscatemplate", artifact);
197
198                 component.setToscaArtifacts(toscaArtifacts);
199                 component.setDeploymentArtifacts(toscaArtifacts);
200                 component.setArtifacts(toscaArtifacts);
201                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
202                 DAOArtifactData artifactData = new DAOArtifactData();
203                 byte[] data = "value".getBytes();
204                 ByteBuffer bufferData = ByteBuffer.wrap(data);
205                 artifactData.setData(bufferData);
206
207                 ToscaRepresentation tosca = new ToscaRepresentation();
208                 tosca.setMainYaml("value");
209
210                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
211
212                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
213
214                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
215                                 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
216
217                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
218                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
219                 } catch (Exception e) {
220                         e.printStackTrace();
221                 }
222         }
223
224         @Test
225         public void testPopulateZipWhenExportComponentIsRight() {
226                 Component component = new Resource();
227                 boolean getFromCS = false;
228
229                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
230                 ArtifactDefinition artifact = new ArtifactDefinition();
231                 artifact.setArtifactName("artifactName");
232                 artifact.setEsId("esId");
233                 artifact.setArtifactUUID("artifactUUID");
234                 artifact.setArtifactType("YANG");
235                 toscaArtifacts.put("assettoscatemplate", artifact);
236
237                 component.setToscaArtifacts(toscaArtifacts);
238                 component.setDeploymentArtifacts(toscaArtifacts);
239                 component.setArtifacts(toscaArtifacts);
240                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
241                 DAOArtifactData artifactData = new DAOArtifactData();
242                 byte[] data = "value".getBytes();
243                 ByteBuffer bufferData = ByteBuffer.wrap(data);
244                 artifactData.setData(bufferData);
245
246                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
247                                 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
248
249                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
250                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
251                 } catch (Exception e) {
252                         e.printStackTrace();
253                 }
254         }
255
256         @Test
257         public void testPopulateZipWhenComponentIsServiceAndCollectComponentCsarDefinitionIsRight() {
258                 Component component = new Service();
259                 boolean getFromCS = false;
260
261                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
262                 ArtifactDefinition artifact = new ArtifactDefinition();
263                 artifact.setArtifactName("artifactName");
264                 artifact.setEsId("esId");
265                 artifact.setArtifactUUID("artifactUUID");
266                 artifact.setArtifactType("YANG");
267                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
268                 artifact.setDescription("description");
269                 artifact.setArtifactLabel("artifactLabel");
270                 toscaArtifacts.put("assettoscatemplate", artifact);
271
272                 component.setToscaArtifacts(toscaArtifacts);
273                 component.setDeploymentArtifacts(toscaArtifacts);
274                 component.setArtifacts(toscaArtifacts);
275                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
276                 component.setVersion("1.0");
277                 component.setLastUpdaterUserId("userId");
278                 component.setUniqueId("uid");
279                 DAOArtifactData artifactData = new DAOArtifactData();
280                 byte[] data = "value".getBytes();
281                 ByteBuffer bufferData = ByteBuffer.wrap(data);
282                 artifactData.setData(bufferData);
283
284                 List<SdcSchemaFilesData> filesData = new ArrayList<>();
285                 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
286                 filedata.setPayloadAsArray(data);
287                 filesData.add(filedata);
288
289                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
290                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
291                 Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
292                 dependencies.add(triple);
293                 toscaTemplate.setDependencies(dependencies);
294
295                 ToscaRepresentation tosca = new ToscaRepresentation();
296                 tosca.setMainYaml("value");
297
298                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
299
300                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
301
302                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
303                                 .thenReturn(Either.left(toscaTemplate));
304
305                 Mockito.when(
306                                 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
307                                 .thenReturn(Either.left(filesData));
308
309                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
310                                 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
311
312                 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(User.class))).thenReturn(new User());
313
314
315                 Mockito.when(artifactsBusinessLogic.validateAndHandleArtifact(Mockito.any(String.class),
316                                 Mockito.any(ComponentTypeEnum.class), Mockito.any(ArtifactOperationInfo.class), Mockito.isNull(),
317                                 Mockito.any(ArtifactDefinition.class), Mockito.any(String.class), Mockito.any(String.class),
318                                 Mockito.isNull(), Mockito.isNull(), Mockito.any(User.class), Mockito.any(Component.class),
319                                 Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(Boolean.class)))
320                                 .thenReturn(Either.left(Mockito.any(ArtifactDefinition.class)));
321
322                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
323                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
324                 } catch (Exception e) {
325                         e.printStackTrace();
326                 }
327         }
328
329         @Test
330         public void testPopulateZipWhenGetEntryDataIsRight() {
331                 Component component = new Service();
332                 boolean getFromCS = true;
333
334                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
335                 ArtifactDefinition artifact = new ArtifactDefinition();
336                 artifact.setArtifactName("artifactName");
337                 artifact.setEsId("esId");
338                 artifact.setArtifactUUID("artifactUUID");
339                 artifact.setArtifactType("YANG");
340                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
341                 artifact.setDescription("description");
342                 artifact.setArtifactLabel("artifactLabel");
343                 toscaArtifacts.put("assettoscatemplate", artifact);
344
345                 component.setToscaArtifacts(toscaArtifacts);
346                 component.setDeploymentArtifacts(toscaArtifacts);
347                 component.setArtifacts(toscaArtifacts);
348                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
349                 component.setVersion("1.0");
350                 component.setLastUpdaterUserId("userId");
351                 component.setUniqueId("uid");
352                 DAOArtifactData artifactData = new DAOArtifactData();
353                 byte[] data = "value".getBytes();
354                 ByteBuffer bufferData = ByteBuffer.wrap(data);
355                 artifactData.setData(bufferData);
356
357                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
358                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
359                 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
360                 dependencies.add(triple);
361                 toscaTemplate.setDependencies(dependencies);
362
363                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
364
365                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
366                                 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
367
368                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
369                                 .thenReturn(Either.left(toscaTemplate));
370
371                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
372                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
373                 } catch (Exception e) {
374                         e.printStackTrace();
375                 }
376         }
377
378         @Test
379         public void testPopulateZipWhenGetEntryDataOfInnerComponentIsRight() {
380                 Component component = new Service();
381                 boolean getFromCS = false;
382
383                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
384                 ArtifactDefinition artifact = new ArtifactDefinition();
385                 artifact.setArtifactName("artifactName");
386                 artifact.setEsId("esId");
387                 artifact.setArtifactUUID("artifactUUID");
388                 artifact.setArtifactType("YANG");
389                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
390                 artifact.setDescription("description");
391                 artifact.setArtifactLabel("artifactLabel");
392                 toscaArtifacts.put("assettoscatemplate", artifact);
393
394                 component.setToscaArtifacts(toscaArtifacts);
395                 component.setDeploymentArtifacts(toscaArtifacts);
396                 component.setArtifacts(toscaArtifacts);
397                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
398                 component.setVersion("1.0");
399                 component.setLastUpdaterUserId("userId");
400                 component.setUniqueId("uid");
401                 DAOArtifactData artifactData = new DAOArtifactData();
402                 byte[] data = "value".getBytes();
403                 ByteBuffer bufferData = ByteBuffer.wrap(data);
404                 artifactData.setData(bufferData);
405
406                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
407                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
408                 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
409                 dependencies.add(triple);
410                 toscaTemplate.setDependencies(dependencies);
411
412                 ToscaRepresentation tosca = new ToscaRepresentation();
413                 tosca.setMainYaml("value");
414
415                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
416
417                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca),
418                                 Either.left(tosca), Either.right(ToscaError.GENERAL_ERROR));
419
420                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
421                                 .thenReturn(Either.left(toscaTemplate));
422
423                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
424                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
425                 } catch (Exception e) {
426                         e.printStackTrace();
427                 }
428         }
429
430         @Test
431         public void testPopulateZipWhenLatestSchemaFilesFromCassandraIsRight() {
432                 Component component = new Service();
433                 boolean getFromCS = false;
434
435                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
436                 ArtifactDefinition artifact = new ArtifactDefinition();
437                 artifact.setArtifactName("artifactName");
438                 artifact.setEsId("esId");
439                 artifact.setArtifactUUID("artifactUUID");
440                 artifact.setArtifactType("YANG");
441                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
442                 artifact.setDescription("description");
443                 artifact.setArtifactLabel("artifactLabel");
444                 toscaArtifacts.put("assettoscatemplate", artifact);
445
446                 component.setToscaArtifacts(toscaArtifacts);
447                 component.setDeploymentArtifacts(toscaArtifacts);
448                 component.setArtifacts(toscaArtifacts);
449                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
450                 component.setVersion("1.0");
451                 component.setLastUpdaterUserId("userId");
452                 component.setUniqueId("uid");
453                 DAOArtifactData artifactData = new DAOArtifactData();
454                 byte[] data = "value".getBytes();
455                 ByteBuffer bufferData = ByteBuffer.wrap(data);
456                 artifactData.setData(bufferData);
457
458                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
459                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
460                 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
461                 dependencies.add(triple);
462                 toscaTemplate.setDependencies(dependencies);
463
464                 ToscaRepresentation tosca = new ToscaRepresentation();
465                 tosca.setMainYaml("value");
466
467                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
468
469                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
470
471                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
472                                 .thenReturn(Either.left(toscaTemplate));
473
474                 Mockito.when(
475                                 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
476                                 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
477
478                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
479                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
480                 } catch (Exception e) {
481                         e.printStackTrace();
482                 }
483         }
484
485         @Test
486         public void testAddInnerComponentsToCache() {
487                 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
488                 Component childComponent = new Resource();
489                 Component componentRI = new Service();
490                 List<ComponentInstance> componentInstances = new ArrayList<>();
491                 ComponentInstance instance = new ComponentInstance();
492                 instance.setComponentUid("resourceUid");
493                 componentInstances.add(instance);
494                 childComponent.setComponentInstances(componentInstances);
495
496                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
497                 ArtifactDefinition artifact = new ArtifactDefinition();
498                 artifact.setArtifactName("artifactName");
499                 artifact.setEsId("esId");
500                 artifact.setArtifactUUID("artifactUUID");
501                 artifact.setArtifactType("YANG");
502                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
503                 artifact.setDescription("description");
504                 artifact.setArtifactLabel("artifactLabel");
505                 toscaArtifacts.put("assettoscatemplate", artifact);
506
507                 componentRI.setToscaArtifacts(toscaArtifacts);
508
509                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
510                                 .thenReturn(Either.left(componentRI));
511
512                 Deencapsulation.invoke(testSubject, "addInnerComponentsToCache", componentCache, childComponent);
513
514                 assertTrue(componentCache.containsValue(ImmutableTriple.of("esId","artifactName",componentRI)));
515         }
516
517         @Test
518         public void testAddInnerComponentsToCacheWhenGetToscaElementIsRight() {
519                 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
520                 Component childComponent = new Resource();
521
522                 List<ComponentInstance> componentInstances = new ArrayList<>();
523                 ComponentInstance instance = new ComponentInstance();
524                 instance.setComponentUid("abc");
525                 componentInstances.add(instance);
526                 childComponent.setComponentInstances(componentInstances);
527
528                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
529                 ArtifactDefinition artifact = new ArtifactDefinition();
530                 artifact.setArtifactName("artifactName");
531                 artifact.setEsId("esId");
532                 artifact.setArtifactUUID("artifactUUID");
533                 artifact.setArtifactType("YANG");
534                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
535                 artifact.setDescription("description");
536                 artifact.setArtifactLabel("artifactLabel");
537                 toscaArtifacts.put("assettoscatemplate", artifact);
538
539                 Component componentRI = new Service();
540
541                 componentRI.setToscaArtifacts(toscaArtifacts);
542
543                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
544                                 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
545
546
547                 assertTrue(componentCache.isEmpty());
548         }
549
550         @Test
551         public void testAddComponentToCache() {
552                 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
553                 String id = "id";
554                 String fileName = "fileName";
555                 Component component = new Resource();
556                 component.setInvariantUUID("key");
557                 component.setVersion("1.0");
558
559                 Component cachedComponent = new Resource();
560                 cachedComponent.setVersion("0.3");
561
562                 componentCache.put("key", new ImmutableTriple<String, String, Component>(id, fileName, cachedComponent));
563
564                 Deencapsulation.invoke(testSubject, "addComponentToCache", componentCache, id, fileName, component);
565         }
566
567         @Test
568         public void testWriteComponentInterface() throws IOException {
569                 String fileName = "name.hello";
570                 ToscaRepresentation tosca = new ToscaRepresentation();
571                 tosca.setMainYaml("value");
572
573                 Mockito.when(toscaExportUtils.exportComponentInterface(Mockito.any(Component.class), Mockito.any(Boolean.class)))
574                                 .thenReturn(Either.left(tosca));
575
576
577                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
578                         Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "writeComponentInterface", new Resource(), zip, fileName, false);
579
580                         assertNotNull(output);
581                         assertTrue(output.isLeft());
582                 }
583         }
584
585         @Test
586         public void testGetEntryData() {
587                 String cassandraId = "id";
588                 Component childComponent = new Resource();
589
590                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
591                                 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
592
593                 Either<byte[], ActionStatus> output = Deencapsulation.invoke(testSubject, "getEntryData", cassandraId, childComponent);
594
595                 assertNotNull(output);
596                 assertTrue(output.isRight());
597         }
598
599         @Test
600         public void testGetLatestSchemaFilesFromCassandraWhenListOfSchemasIsEmpty() {
601                 List<SdcSchemaFilesData> filesData = new ArrayList<>();
602
603                 Mockito.when(
604                                 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
605                                 .thenReturn(Either.left(filesData));
606
607                 Either<byte[], ResponseFormat> output = Deencapsulation.invoke(testSubject, "getLatestSchemaFilesFromCassandra");
608
609                 assertNotNull(output);
610                 assertTrue(output.isRight());
611         }
612
613         @Test
614         public void testExtractVfcsArtifactsFromCsar() {
615                 String key = "Artifacts/org.openecomp.resource.some/Deployment/to/resource";
616                 byte[] data = "value".getBytes();
617
618                 Map<String, byte[]> csar = new HashMap<>();
619                 csar.put(key, data);
620
621                 Map<String, List<ArtifactDefinition>> output = CsarUtils.extractVfcsArtifactsFromCsar(csar);
622
623                 assertNotNull(output);
624                 assertTrue(output.containsKey("org.openecomp.resource.some"));
625                 assertEquals(1, output.get("org.openecomp.resource.some").size());
626         }
627
628         @Test
629         public void testAddExtractedVfcArtifactWhenArtifactsContainsExtractedArtifactKey() {
630                 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
631                                 "key", new ArtifactDefinition());
632                 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
633                 artifacts.put("key", new ArrayList<>());
634
635                 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
636
637                 assertEquals(1, artifacts.get("key").size());
638         }
639
640         @Test
641         public void testAddExtractedVfcArtifactWhenArtifactsDoesntContainsExtractedArtifactKey() {
642                 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
643                                 "key", new ArtifactDefinition());
644                 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
645                 artifacts.put("key1", new ArrayList<>());
646
647                 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
648
649                 assertEquals(0, artifacts.get("key1").size());
650                 assertEquals(1, artifacts.get("key").size());
651                 assertEquals(2, artifacts.size());
652         }
653
654         @Test
655         public void testExtractVfcArtifact() {
656                 String path = "path/to/informational/artificat";
657                 Map<String, byte[]> map = new HashMap<>();
658                 map.put(path, "value".getBytes());
659                 Entry<String, byte[]> entry = map.entrySet().iterator().next();
660
661                 ImmutablePair<String, ArtifactDefinition> output = Deencapsulation.invoke(testSubject, "extractVfcArtifact", entry, new HashMap<>());
662
663                 assertNotNull(output);
664                 assertEquals("to",output.left);
665         }
666
667         @Test
668         public void testDetectArtifactGroupTypeWithExceptionBeingCaught() {
669                 Either<ArtifactGroupTypeEnum, Boolean> output = Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", Map.class);
670
671                 assertNotNull(output);
672                 assertTrue(output.isRight());
673                 assertFalse(output.right().value());
674         }
675
676         @Test
677         public void testDetectArtifactGroupTypeWWhenCollectedWarningMessagesContainesKey() {
678                 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
679
680                 collectedWarningMessages.put("Warning - unrecognized artifact group type {} was received.", new HashSet<>());
681                 Either<ArtifactGroupTypeEnum, Boolean> output = Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", collectedWarningMessages);
682
683                 assertNotNull(output);
684                 assertTrue(output.isRight());
685                 assertFalse(output.right().value());
686         }
687
688         @Test
689         public void testNonMetaArtifactInfoCtor() {
690                 createNonMetaArtifactInfoTestSubject();
691         }
692
693         @Test
694         public void testNonMetaArtifactInfoGetPath() {
695                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
696
697                 testSubject.getPath();
698         }
699
700         @Test
701         public void testNonMetaArtifactInfoGetArtifactName() {
702                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
703
704                 testSubject.getArtifactName();
705         }
706
707         @Test
708         public void testNonMetaArtifactInfoGetArtifactType() {
709                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
710
711                 testSubject.getArtifactType();
712         }
713
714         @Test
715         public void testNonMetaArtifactInfoGetDisplayName() {
716                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
717
718                 testSubject.getDisplayName();
719         }
720
721         @Test
722         public void testNonMetaArtifactInfoGetArtifactGroupType() {
723                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
724
725                 testSubject.getArtifactGroupType();
726         }
727
728         @Test
729         public void testNonMetaArtifactInfoGetArtifactLabel() {
730                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
731
732                 testSubject.getArtifactLabel();
733         }
734
735         @Test
736         public void testNonMetaArtifactInfoGetIsFromCsar() {
737                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
738
739                 testSubject.isFromCsar();
740         }
741
742         @Test
743         public void testNonMetaArtifactInfoGetPayloadData() {
744                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
745
746                 testSubject.getPayloadData();
747         }
748
749         @Test
750         public void testNonMetaArtifactInfoGetArtifaactChecksum() {
751                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
752
753                 testSubject.getArtifactChecksum();
754         }
755
756         @Test
757         public void testNonMetaArtifactInfoGetArtifactUniqueId() {
758                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
759
760                 testSubject.getArtifactUniqueId();
761         }
762
763         @Test
764         public void testNonMetaArtifactInfosetArtifactUniqueId() {
765                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
766
767                 testSubject.setArtifactUniqueId("artifactUniqueId");
768         }
769
770         @Test
771         public void testValidateNonMetaArtifactWithExceptionCaught() {
772                 CsarUtils.validateNonMetaArtifact("", new byte[0], new HashMap<>());
773         }
774
775         @Test
776         public void testWriteArtifactDefinition() throws IOException {
777                 Component component = new Service();
778                 List<ArtifactDefinition> artifactDefinitionList = new ArrayList<>();
779                 String artifactPathAndFolder = "";
780
781                 ArtifactDefinition artifact = new ArtifactDefinition();
782                 artifact.setArtifactType(ArtifactTypeEnum.HEAT_ENV.getType());
783                 artifactDefinitionList.add(artifact);
784
785                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
786                         Either<ZipOutputStream, ResponseFormat> output = Deencapsulation.invoke(testSubject, "writeArtifactDefinition", component, zip, artifactDefinitionList, artifactPathAndFolder, false);
787
788                         assertNotNull(output);
789                         assertTrue(output.isLeft());
790                 }
791         }
792
793         @Test
794         public void testCollectComponentCsarDefinitionWhenComponentIsServiceAndGetToscaElementIsLeft() {
795                 Component component = new Service();
796                 component.setUniqueId("uniqueId");
797                 List<ComponentInstance> resourceInstances = new ArrayList<>();
798                 ComponentInstance instance = new ComponentInstance();
799                 instance.setComponentUid("resourceUid");
800                 instance.setOriginType(OriginTypeEnum.SERVICE);
801                 resourceInstances.add(instance);
802                 component.setComponentInstances(resourceInstances);
803
804                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
805                 ArtifactDefinition artifact = new ArtifactDefinition();
806                 artifact.setArtifactName("artifactName");
807                 artifact.setEsId("esId");
808                 artifact.setArtifactUUID("artifactUUID");
809                 artifact.setArtifactType("YANG");
810                 toscaArtifacts.put("assettoscatemplate", artifact);
811
812                 component.setToscaArtifacts(toscaArtifacts);
813                 component.setDeploymentArtifacts(toscaArtifacts);
814                 component.setArtifacts(toscaArtifacts);
815
816                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
817                                 Either.right(StorageOperationStatus.BAD_REQUEST));
818
819                 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
820
821                 assertNotNull(output);
822                 assertTrue(output.isRight());
823         }
824
825         @Test
826         public void testCollectComponentTypeArtifactsWhenFetchedComponentHasComponentInstances() {
827                 Component component = new Service();
828                 Component fetchedComponent = new Resource();
829                 component.setUniqueId("uniqueId");
830                 List<ComponentInstance> resourceInstances = new ArrayList<>();
831                 ComponentInstance instance = new ComponentInstance();
832                 instance.setComponentUid("resourceUid");
833                 instance.setOriginType(OriginTypeEnum.SERVICE);
834                 resourceInstances.add(instance);
835                 component.setComponentInstances(resourceInstances);
836                 fetchedComponent.setComponentInstances(resourceInstances);
837
838                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
839                 ArtifactDefinition artifact = new ArtifactDefinition();
840                 artifact.setArtifactName("artifactName");
841                 artifact.setEsId("esId");
842                 artifact.setArtifactUUID("artifactUUID");
843                 artifact.setArtifactType("YANG");
844                 toscaArtifacts.put("assettoscatemplate", artifact);
845
846                 component.setToscaArtifacts(toscaArtifacts);
847                 component.setDeploymentArtifacts(toscaArtifacts);
848                 component.setArtifacts(toscaArtifacts);
849
850                 fetchedComponent.setToscaArtifacts(toscaArtifacts);
851                 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
852                 fetchedComponent.setArtifacts(toscaArtifacts);
853
854                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
855                                 Either.left(fetchedComponent), Either.right(StorageOperationStatus.BAD_REQUEST));
856
857                 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
858
859                 assertNotNull(output);
860                 assertTrue(output.isRight());
861         }
862
863         @Test
864         public void testCollectComponentTypeArtifactsWhenFetchedComponentDontHaveComponentInstances() {
865                 Component component = new Service();
866                 Component fetchedComponent = new Resource();
867                 component.setUniqueId("uniqueId");
868                 List<ComponentInstance> resourceInstances = new ArrayList<>();
869                 ComponentInstance instance = new ComponentInstance();
870                 instance.setComponentUid("resourceUid");
871                 instance.setOriginType(OriginTypeEnum.SERVICE);
872
873                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
874                 ArtifactDefinition artifact = new ArtifactDefinition();
875                 artifact.setArtifactName("artifactName");
876                 artifact.setEsId("esId");
877                 artifact.setArtifactUUID("artifactUUID");
878                 artifact.setArtifactType("PLAN");
879                 toscaArtifacts.put("assettoscatemplate", artifact);
880
881                 instance.setDeploymentArtifacts(toscaArtifacts);
882
883                 resourceInstances.add(instance);
884                 component.setComponentInstances(resourceInstances);
885
886                 component.setToscaArtifacts(toscaArtifacts);
887                 component.setDeploymentArtifacts(toscaArtifacts);
888                 component.setArtifacts(toscaArtifacts);
889
890                 fetchedComponent.setToscaArtifacts(toscaArtifacts);
891                 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
892                 fetchedComponent.setArtifacts(toscaArtifacts);
893
894                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
895                                 Either.left(fetchedComponent));
896
897                 Either<Object, ResponseFormat> output = Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
898
899                 assertNotNull(output);
900                 assertTrue(output.isLeft());
901         }
902
903         @Test
904         public void testValidateNonMetaArtifactHappyScenario() {
905                 String artifactPath = "Artifacts/Deployment/YANG_XML/myYang.xml";
906                 byte[] payloadData = "some payload data".getBytes();
907                 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
908                 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
909                                 payloadData, collectedWarningMessages);
910                 assertTrue(eitherNonMetaArtifact.isLeft());
911                 assertTrue(collectedWarningMessages.isEmpty());
912
913                 artifactPath = "Artifacts/Informational/OTHER/someArtifact.xml";
914                 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
915                 assertTrue(eitherNonMetaArtifact.isLeft());
916                 assertTrue(collectedWarningMessages.isEmpty());
917         }
918
919         @Test
920         public void testValidateNonMetaArtifactScenarioWithWarnnings() {
921                 String artifactPath = "Artifacts/Deployment/Buga/myYang.xml";
922                 byte[] payloadData = "some payload data".getBytes();
923                 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
924                 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
925                                 payloadData, collectedWarningMessages);
926                 assertTrue(eitherNonMetaArtifact.isLeft());
927
928                 artifactPath = "Artifacts/Informational/Buga2/someArtifact.xml";
929                 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
930                 assertTrue(eitherNonMetaArtifact.isLeft());
931
932                 assertTrue(collectedWarningMessages.size() == 1);
933                 assertTrue(collectedWarningMessages.values().iterator().next().size() == 2);
934         }
935
936         @Test
937         public void testValidateNonMetaArtifactUnhappyScenario() {
938                 String artifactPath = "Artifacts/Buga/YANG_XML/myYang.xml";
939                 byte[] payloadData = "some payload data".getBytes();
940                 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
941                 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
942                                 payloadData, collectedWarningMessages);
943                 assertTrue(eitherNonMetaArtifact.isRight());
944                 assertTrue(!collectedWarningMessages.isEmpty());
945         }
946
947         @Test(expected = IOException.class)
948         public void testAddSchemaFilesFromCassandraAddingDuplicatedEntry() throws IOException {
949                 final String rootPath = System.getProperty("user.dir");
950                 final Path path = Paths.get(rootPath + "/src/test/resources/sdc.zip");
951                 try {
952                         final byte[] data = Files.readAllBytes(path);
953                         try (final ByteArrayOutputStream out = new ByteArrayOutputStream();
954                                 final ZipOutputStream zip = new ZipOutputStream(out);) {
955                                 Deencapsulation.invoke(testSubject, "addSchemaFilesFromCassandra",
956                                         zip, data, nodesFromPackage);
957                                 zip.putNextEntry(new ZipEntry("Definitions/nodes.yml"));
958                                 zip.finish();
959                         }
960                 } catch (final IOException e) {
961                         Assert.assertTrue("duplicate entry: Definitions/nodes.yml".equals(e.getMessage()));
962                         throw new IOException("Could not add Schema Files From Cassandra", e);
963                 }
964         }
965
966         @Test
967         public void testFindNonRootNodesFromPackage() {
968                 final Resource resource = new Resource();
969                 resource.setDerivedList(nodesFromPackage);
970                 final Component component = resource;
971                 final List<Triple<String, String, Component>> dependencies = new ArrayList<>();
972                 final Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
973                 dependencies.add(triple);
974                 final List<String> expectedResult = Arrays.asList("tosca.nodes.Container.Application");
975                 final List<String> result = Deencapsulation.invoke(testSubject,
976                         "findNonRootNodesFromPackage", dependencies);
977                 assertTrue(CollectionUtils.isNotEmpty(result));
978                 assertEquals(expectedResult, result);
979         }
980
981 }