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