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