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