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