Make Service base type optional
[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.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertThrows;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static org.junit.jupiter.api.Assertions.fail;
31 import static org.openecomp.sdc.be.tosca.ComponentCache.MergeStrategy.overwriteIfSameVersions;
32 import static org.openecomp.sdc.be.tosca.ComponentCache.entry;
33
34 import fj.data.Either;
35 import java.io.File;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.nio.ByteBuffer;
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.jupiter.api.BeforeAll;
62 import org.junit.jupiter.api.BeforeEach;
63 import org.junit.jupiter.api.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.impl.ArtifactsBusinessLogic;
69 import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo;
70 import org.openecomp.sdc.be.config.ConfigurationManager;
71 import org.openecomp.sdc.be.dao.api.ActionStatus;
72 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
73 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
74 import org.openecomp.sdc.be.dao.cassandra.SdcSchemaFilesCassandraDao;
75 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
76 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
77 import org.openecomp.sdc.be.impl.ComponentsUtils;
78 import org.openecomp.sdc.be.model.ArtifactDefinition;
79 import org.openecomp.sdc.be.model.Component;
80 import org.openecomp.sdc.be.model.ComponentInstance;
81 import org.openecomp.sdc.be.model.LifecycleStateEnum;
82 import org.openecomp.sdc.be.model.Resource;
83 import org.openecomp.sdc.be.model.Service;
84 import org.openecomp.sdc.be.model.User;
85 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
86 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
87 import org.openecomp.sdc.be.resources.data.DAOArtifactData;
88 import org.openecomp.sdc.be.resources.data.SdcSchemaFilesData;
89 import org.openecomp.sdc.be.tosca.ComponentCache.CacheEntry;
90 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
91 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
92 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
93 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
94 import org.openecomp.sdc.common.api.ConfigurationSource;
95 import org.openecomp.sdc.common.impl.ExternalConfiguration;
96 import org.openecomp.sdc.common.impl.FSConfigurationSource;
97 import org.openecomp.sdc.common.test.BaseConfDependent;
98 import org.openecomp.sdc.exception.ResponseFormat;
99
100 class CsarUtilsTest extends BaseConfDependent {
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         private final List<String> nodesFromPackage = Arrays.asList("tosca.nodes.Root", "tosca.nodes.Container.Application");
124
125         private final byte[] contentData;
126
127         public CsarUtilsTest() throws IOException {
128                 contentData = getFileResource("yamlValidation/resource-serviceTemplate.yml");
129         }
130
131         @BeforeAll
132         public static void setupBeforeClass() {
133                 componentName = "catalog-be";
134                 confPath = "src/test/resources/config";
135                 setUp();
136         }
137
138         @BeforeEach
139         public void setUpMock() {
140                 ExternalConfiguration.setAppName("catalog-be");
141                 MockitoAnnotations.openMocks(this);
142                 initConfigurationManager();
143         }
144
145         private static void initConfigurationManager() {
146                 final String confPath = new File(Objects
147                         .requireNonNull(
148                                 CsarUtilsTest.class.getClassLoader().getResource("config/catalog-be/configuration.yaml"))
149                         .getFile()).getParent();
150                 final ConfigurationSource confSource =
151                         new FSConfigurationSource(ExternalConfiguration.getChangeListener(), confPath);
152                 new ConfigurationManager(confSource);
153         }
154
155         private NonMetaArtifactInfo createNonMetaArtifactInfoTestSubject() {
156                 return new CsarUtils.NonMetaArtifactInfo("mock", "mock", ArtifactTypeEnum.AAI_SERVICE_MODEL.getType(),
157                                 ArtifactGroupTypeEnum.DEPLOYMENT, new byte[0], "mock", true);
158         }
159
160         @Test
161         void testCreateCsar() {
162                 Component component = new Resource();
163                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
164                 ArtifactDefinition artifact = new ArtifactDefinition();
165                 artifact.setArtifactName("artifactName");
166                 artifact.setEsId("esId");
167                 toscaArtifacts.put("assettoscatemplate", artifact);
168
169                 component.setToscaArtifacts(toscaArtifacts);
170
171                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
172                                 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
173
174                 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
175                                 .thenReturn(ActionStatus.GENERAL_ERROR);
176
177                 testSubject.createCsar(component, true, true);
178         }
179
180         @Test
181         void testCreateCsarWithGenerateCsarZipResponseIsLeft() {
182                 Component component = new Resource();
183                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
184                 ArtifactDefinition artifact = new ArtifactDefinition();
185                 artifact.setArtifactName("artifactName");
186                 artifact.setEsId("esId");
187                 artifact.setArtifactUUID("artifactUUID");
188                 artifact.setArtifactType("YANG");
189                 toscaArtifacts.put("assettoscatemplate", artifact);
190
191                 component.setToscaArtifacts(toscaArtifacts);
192                 component.setDeploymentArtifacts(toscaArtifacts);
193                 component.setArtifacts(toscaArtifacts);
194                 DAOArtifactData artifactData = new DAOArtifactData();
195                 byte[] data = "value".getBytes();
196                 ByteBuffer bufferData = ByteBuffer.wrap(data);
197                 artifactData.setData(bufferData);
198
199                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
200                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
201                 toscaTemplate.setDependencies(dependencies);
202
203                 List<SdcSchemaFilesData> filesData = new ArrayList<>();
204                 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
205                 filedata.setPayloadAsArray(data);
206                 filesData.add(filedata);
207
208                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
209
210                 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
211                                 .thenReturn(ActionStatus.GENERAL_ERROR);
212
213                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
214                                 .thenReturn(Either.left(toscaTemplate));
215
216                 Mockito.when(
217                                 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
218                                 .thenReturn(Either.left(filesData));
219
220                 testSubject.createCsar(component, false, true);
221         }
222
223         @Test
224         void testPopulateZipWhenGetDependenciesIsRight() {
225                 Component component = new Service();
226                 boolean getFromCS = false;
227
228                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
229                 ArtifactDefinition artifact = new ArtifactDefinition();
230                 artifact.setArtifactName("artifactName");
231                 artifact.setEsId("esId");
232                 artifact.setArtifactUUID("artifactUUID");
233                 artifact.setArtifactType("YANG");
234                 toscaArtifacts.put("assettoscatemplate", artifact);
235
236                 component.setToscaArtifacts(toscaArtifacts);
237                 component.setDeploymentArtifacts(toscaArtifacts);
238                 component.setArtifacts(toscaArtifacts);
239                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
240                 DAOArtifactData artifactData = new DAOArtifactData();
241                 byte[] data = "value".getBytes();
242                 ByteBuffer bufferData = ByteBuffer.wrap(data);
243                 artifactData.setData(bufferData);
244
245                 ToscaRepresentation tosca = ToscaRepresentation.make("value".getBytes());
246
247                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
248
249                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
250
251                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
252                                 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
253
254                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
255                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
256                 } catch (Exception e) {
257                         e.printStackTrace();
258                 }
259         }
260
261         @Test
262         void testPopulateZipWhenExportComponentIsRight() {
263                 Component component = new Resource();
264                 boolean getFromCS = false;
265
266                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
267                 ArtifactDefinition artifact = new ArtifactDefinition();
268                 artifact.setArtifactName("artifactName");
269                 artifact.setEsId("esId");
270                 artifact.setArtifactUUID("artifactUUID");
271                 artifact.setArtifactType("YANG");
272                 toscaArtifacts.put("assettoscatemplate", artifact);
273
274                 component.setToscaArtifacts(toscaArtifacts);
275                 component.setDeploymentArtifacts(toscaArtifacts);
276                 component.setArtifacts(toscaArtifacts);
277                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
278                 DAOArtifactData artifactData = new DAOArtifactData();
279                 byte[] data = "value".getBytes();
280                 ByteBuffer bufferData = ByteBuffer.wrap(data);
281                 artifactData.setData(bufferData);
282
283                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
284                                 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
285
286                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
287                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false);
288                 } catch (Exception e) {
289                         e.printStackTrace();
290                 }
291         }
292
293         @Test
294         void testPopulateZipWhenComponentIsServiceAndCollectComponentCsarDefinitionIsRight() {
295                 Component component = new Service();
296                 boolean getFromCS = false;
297
298                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
299                 ArtifactDefinition artifact = new ArtifactDefinition();
300                 artifact.setArtifactName("artifactName");
301                 artifact.setEsId("esId");
302                 artifact.setArtifactUUID("artifactUUID");
303                 artifact.setArtifactType("YANG");
304                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
305                 artifact.setDescription("description");
306                 artifact.setArtifactLabel("artifactLabel");
307                 toscaArtifacts.put("assettoscatemplate", artifact);
308
309                 component.setToscaArtifacts(toscaArtifacts);
310                 component.setDeploymentArtifacts(toscaArtifacts);
311                 component.setArtifacts(toscaArtifacts);
312                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
313                 component.setVersion("1.0");
314                 component.setLastUpdaterUserId("userId");
315                 component.setUniqueId("uid");
316                 DAOArtifactData artifactData = new DAOArtifactData();
317                 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
318                 artifactData.setData(bufferData);
319
320                 List<SdcSchemaFilesData> filesData = new ArrayList<>();
321                 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
322                 filedata.setPayloadAsArray(contentData);
323                 filesData.add(filedata);
324
325                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
326                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
327                 Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
328                 dependencies.add(triple);
329                 toscaTemplate.setDependencies(dependencies);
330
331                 ToscaRepresentation tosca = ToscaRepresentation.make("value".getBytes());
332
333                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
334
335                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
336
337                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
338                                 .thenReturn(Either.left(toscaTemplate));
339
340                 Mockito.when(
341                                 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
342                                 .thenReturn(Either.left(filesData));
343
344                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
345                                 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
346
347                 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(User.class))).thenReturn(new User());
348
349
350                 Mockito.when(artifactsBusinessLogic.validateAndHandleArtifact(Mockito.any(String.class),
351                                 Mockito.any(ComponentTypeEnum.class), Mockito.any(ArtifactOperationInfo.class), Mockito.isNull(),
352                                 Mockito.any(ArtifactDefinition.class), Mockito.any(String.class), Mockito.any(String.class),
353                                 Mockito.isNull(), Mockito.isNull(), Mockito.any(User.class), Mockito.any(Component.class),
354                                 Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(Boolean.class)))
355                                 .thenReturn(Either.left(Mockito.any(ArtifactDefinition.class)));
356
357                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
358                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
359                 } catch (Exception e) {
360                         e.printStackTrace();
361                 }
362         }
363
364         @Test
365         void testPopulateZipWhenGetEntryDataIsRight() {
366                 Component component = new Service();
367                 boolean getFromCS = true;
368
369                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
370                 ArtifactDefinition artifact = new ArtifactDefinition();
371                 artifact.setArtifactName("artifactName");
372                 artifact.setEsId("esId");
373                 artifact.setArtifactUUID("artifactUUID");
374                 artifact.setArtifactType("YANG");
375                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
376                 artifact.setDescription("description");
377                 artifact.setArtifactLabel("artifactLabel");
378                 toscaArtifacts.put("assettoscatemplate", artifact);
379
380                 component.setToscaArtifacts(toscaArtifacts);
381                 component.setDeploymentArtifacts(toscaArtifacts);
382                 component.setArtifacts(toscaArtifacts);
383                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
384                 component.setVersion("1.0");
385                 component.setLastUpdaterUserId("userId");
386                 component.setUniqueId("uid");
387                 DAOArtifactData artifactData = new DAOArtifactData();
388                 byte[] data = "value".getBytes();
389                 ByteBuffer bufferData = ByteBuffer.wrap(data);
390                 artifactData.setData(bufferData);
391
392                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
393                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
394                 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
395                 dependencies.add(triple);
396                 toscaTemplate.setDependencies(dependencies);
397
398                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
399
400                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
401                                 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
402
403                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
404                                 .thenReturn(Either.left(toscaTemplate));
405
406                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
407                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
408                 } catch (Exception e) {
409                         e.printStackTrace();
410                 }
411         }
412
413         @Test
414         void testPopulateZipWhenGetEntryDataOfInnerComponentIsRight() {
415                 Component component = new Service();
416                 boolean getFromCS = false;
417
418                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
419                 ArtifactDefinition artifact = new ArtifactDefinition();
420                 artifact.setArtifactName("artifactName");
421                 artifact.setEsId("esId");
422                 artifact.setArtifactUUID("artifactUUID");
423                 artifact.setArtifactType("YANG");
424                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
425                 artifact.setDescription("description");
426                 artifact.setArtifactLabel("artifactLabel");
427                 toscaArtifacts.put("assettoscatemplate", artifact);
428
429                 component.setToscaArtifacts(toscaArtifacts);
430                 component.setDeploymentArtifacts(toscaArtifacts);
431                 component.setArtifacts(toscaArtifacts);
432                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
433                 component.setVersion("1.0");
434                 component.setLastUpdaterUserId("userId");
435                 component.setUniqueId("uid");
436                 DAOArtifactData artifactData = new DAOArtifactData();
437                 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
438                 artifactData.setData(bufferData);
439
440                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
441                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
442                 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
443                 dependencies.add(triple);
444                 toscaTemplate.setDependencies(dependencies);
445
446                 ToscaRepresentation tosca = ToscaRepresentation.make(contentData);
447
448                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
449
450                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca),
451                                 Either.left(tosca), Either.right(ToscaError.GENERAL_ERROR));
452
453                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
454                                 .thenReturn(Either.left(toscaTemplate));
455
456                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
457                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
458                 } catch (Exception e) {
459                         e.printStackTrace();
460                 }
461         }
462
463         @Test
464         void testPopulateZipWhenLatestSchemaFilesFromCassandraIsRight() {
465                 Component component = new Service();
466                 boolean getFromCS = false;
467
468                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
469                 ArtifactDefinition artifact = new ArtifactDefinition();
470                 artifact.setArtifactName("artifactName");
471                 artifact.setEsId("esId");
472                 artifact.setArtifactUUID("artifactUUID");
473                 artifact.setArtifactType("YANG");
474                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
475                 artifact.setDescription("description");
476                 artifact.setArtifactLabel("artifactLabel");
477                 toscaArtifacts.put("assettoscatemplate", artifact);
478
479                 component.setToscaArtifacts(toscaArtifacts);
480                 component.setDeploymentArtifacts(toscaArtifacts);
481                 component.setArtifacts(toscaArtifacts);
482                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
483                 component.setVersion("1.0");
484                 component.setLastUpdaterUserId("userId");
485                 component.setUniqueId("uid");
486                 DAOArtifactData artifactData = new DAOArtifactData();
487                 ByteBuffer bufferData = ByteBuffer.wrap(contentData);
488                 artifactData.setData(bufferData);
489
490                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
491                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
492                 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
493                 dependencies.add(triple);
494                 toscaTemplate.setDependencies(dependencies);
495
496                 ToscaRepresentation tosca = ToscaRepresentation.make(contentData);
497
498                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
499
500                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
501
502                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
503                                 .thenReturn(Either.left(toscaTemplate));
504
505                 Mockito.when(
506                                 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
507                                 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
508
509                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
510                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true);
511                 } catch (Exception e) {
512                         e.printStackTrace();
513                 }
514         }
515
516         @Test
517         void testAddInnerComponentsToCache() {
518                 ComponentCache componentCache = ComponentCache.overwritable(overwriteIfSameVersions());
519                 Component childComponent = new Resource();
520                 Component componentRI = new Service();
521                 List<ComponentInstance> componentInstances = new ArrayList<>();
522                 ComponentInstance instance = new ComponentInstance();
523                 instance.setComponentUid("resourceUid");
524                 componentInstances.add(instance);
525                 childComponent.setComponentInstances(componentInstances);
526
527                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
528                 ArtifactDefinition artifact = new ArtifactDefinition();
529                 artifact.setArtifactName("artifactName");
530                 artifact.setEsId("esId");
531                 artifact.setArtifactUUID("artifactUUID");
532                 artifact.setArtifactType("YANG");
533                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
534                 artifact.setDescription("description");
535                 artifact.setArtifactLabel("artifactLabel");
536                 toscaArtifacts.put("assettoscatemplate", artifact);
537
538                 componentRI.setToscaArtifacts(toscaArtifacts);
539
540                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
541                                 .thenReturn(Either.left(componentRI));
542
543                 Deencapsulation.invoke(testSubject, "addInnerComponentsToCache", componentCache, childComponent);
544
545                 io.vavr.collection.List<CacheEntry> expected = io.vavr.collection.List.of(entry("esId","artifactName",componentRI));
546                 assertEquals(expected, componentCache.all().toList());
547         }
548
549         @Test
550         void testAddInnerComponentsToCacheWhenGetToscaElementIsRight() {
551                 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
552                 Component childComponent = new Resource();
553
554                 List<ComponentInstance> componentInstances = new ArrayList<>();
555                 ComponentInstance instance = new ComponentInstance();
556                 instance.setComponentUid("abc");
557                 componentInstances.add(instance);
558                 childComponent.setComponentInstances(componentInstances);
559
560                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
561                 ArtifactDefinition artifact = new ArtifactDefinition();
562                 artifact.setArtifactName("artifactName");
563                 artifact.setEsId("esId");
564                 artifact.setArtifactUUID("artifactUUID");
565                 artifact.setArtifactType("YANG");
566                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
567                 artifact.setDescription("description");
568                 artifact.setArtifactLabel("artifactLabel");
569                 toscaArtifacts.put("assettoscatemplate", artifact);
570
571                 Component componentRI = new Service();
572
573                 componentRI.setToscaArtifacts(toscaArtifacts);
574
575                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
576                                 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
577
578
579                 assertTrue(componentCache.isEmpty());
580         }
581
582         @Test
583         void testWriteComponentInterface() throws IOException {
584                 String fileName = "name.hello";
585                 ToscaRepresentation tosca = ToscaRepresentation.make("value".getBytes());
586
587                 Mockito.when(toscaExportUtils.exportComponentInterface(Mockito.any(Component.class), Mockito.any(Boolean.class)))
588                                 .thenReturn(Either.left(tosca));
589
590
591                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out)) {
592                     List<Triple<String, String, Component>> output = Deencapsulation.invoke(testSubject, "writeComponentInterface", new Resource(), zip, fileName);
593                         assertNotNull(output);
594                 }
595         }
596
597         @Test
598         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         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         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         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         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         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         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         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         void testNonMetaArtifactInfoCtor() {
706                 createNonMetaArtifactInfoTestSubject();
707         }
708
709         @Test
710         void testNonMetaArtifactInfoGetPath() {
711                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
712
713                 testSubject.getPath();
714         }
715
716         @Test
717         void testNonMetaArtifactInfoGetArtifactName() {
718                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
719
720                 testSubject.getArtifactName();
721         }
722
723         @Test
724         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         void testNonMetaArtifactInfoGetDisplayName() {
732                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
733
734                 testSubject.getDisplayName();
735         }
736
737         @Test
738         void testNonMetaArtifactInfoGetArtifactGroupType() {
739                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
740
741                 testSubject.getArtifactGroupType();
742         }
743
744         @Test
745         void testNonMetaArtifactInfoGetArtifactLabel() {
746                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
747
748                 testSubject.getArtifactLabel();
749         }
750
751         @Test
752         void testNonMetaArtifactInfoGetIsFromCsar() {
753                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
754
755                 testSubject.isFromCsar();
756         }
757
758         @Test
759         void testNonMetaArtifactInfoGetPayloadData() {
760                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
761
762                 testSubject.getPayloadData();
763         }
764
765         @Test
766         void testNonMetaArtifactInfoGetArtifaactChecksum() {
767                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
768
769                 testSubject.getArtifactChecksum();
770         }
771
772         @Test
773         void testNonMetaArtifactInfoGetArtifactUniqueId() {
774                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
775
776                 testSubject.getArtifactUniqueId();
777         }
778
779         @Test
780         void testNonMetaArtifactInfosetArtifactUniqueId() {
781                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
782
783                 testSubject.setArtifactUniqueId("artifactUniqueId");
784         }
785
786         @Test
787         void testValidateNonMetaArtifactWithExceptionCaught() {
788                 CsarUtils.validateNonMetaArtifact("", new byte[0], new HashMap<>());
789         }
790
791         @Test
792         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         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         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         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         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                 assertEquals(1, collectedWarningMessages.size());
931                 assertEquals(2, collectedWarningMessages.values().iterator().next().size());
932         }
933
934         @Test
935         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                 assertFalse(collectedWarningMessages.isEmpty());
943         }
944
945         @Test
946         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                 final byte[] data = Files.readAllBytes(path);
950                 try (final ByteArrayOutputStream out = new ByteArrayOutputStream(); final ZipOutputStream zip = new ZipOutputStream(out)) {
951                         Deencapsulation.invoke(testSubject, "addSchemaFilesFromCassandra", zip, data, nodesFromPackage);
952                         final IOException actualException = assertThrows(IOException.class, () -> zip.putNextEntry(new ZipEntry("Definitions/nodes.yml")));
953                         assertEquals("duplicate entry: Definitions/nodes.yml", actualException.getMessage());
954                 }
955         }
956
957         @Test
958         void testFindNonRootNodesFromPackage() {
959                 final Resource resource = new Resource();
960                 resource.setDerivedList(nodesFromPackage);
961                 final Component component = resource;
962                 final List<Triple<String, String, Component>> dependencies = new ArrayList<>();
963                 final Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
964                 dependencies.add(triple);
965                 final List<String> expectedResult = Arrays.asList("tosca.nodes.Container.Application");
966                 final List<String> result = Deencapsulation.invoke(testSubject,
967                         "findNonRootNodesFromPackage", dependencies);
968                 assertTrue(CollectionUtils.isNotEmpty(result));
969                 assertEquals(expectedResult, result);
970         }
971
972     private byte[] getFileResource(final String filePath) throws IOException {
973         try (final InputStream inputStream = getFileResourceAsInputStream(filePath)) {
974             return IOUtils.toByteArray(inputStream);
975         }
976     }
977
978     private InputStream getFileResourceAsInputStream(final String filePath) {
979         return Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
980     }
981
982 }