Raised JUnit coverage for tosca package
[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.junit.Assert.assertTrue;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.Set;
33 import java.util.zip.ZipEntry;
34 import java.util.zip.ZipOutputStream;
35
36 import org.apache.commons.io.output.ByteArrayOutputStream;
37 import org.apache.commons.lang3.tuple.ImmutablePair;
38 import org.apache.commons.lang3.tuple.ImmutableTriple;
39 import org.apache.commons.lang3.tuple.Triple;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.sdc.generator.data.ArtifactType;
47 import org.openecomp.sdc.be.components.BeConfDependentTest;
48 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
49 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationInfo;
50 import org.openecomp.sdc.be.dao.api.ActionStatus;
51 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
52 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
53 import org.openecomp.sdc.be.dao.cassandra.SdcSchemaFilesCassandraDao;
54 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
55 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
56 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
57 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
58 import org.openecomp.sdc.be.impl.ComponentsUtils;
59 import org.openecomp.sdc.be.model.ArtifactDefinition;
60 import org.openecomp.sdc.be.model.Component;
61 import org.openecomp.sdc.be.model.ComponentInstance;
62 import org.openecomp.sdc.be.model.InterfaceDefinition;
63 import org.openecomp.sdc.be.model.LifecycleStateEnum;
64 import org.openecomp.sdc.be.model.Resource;
65 import org.openecomp.sdc.be.model.Service;
66 import org.openecomp.sdc.be.model.User;
67 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
68 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
69 import org.openecomp.sdc.be.resources.data.ESArtifactData;
70 import org.openecomp.sdc.be.resources.data.SdcSchemaFilesData;
71 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
72 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
73 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
74 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
75 import org.openecomp.sdc.exception.ResponseFormat;
76
77 import fj.data.Either;
78 import mockit.Deencapsulation;
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                 MockitoAnnotations.initMocks(this);
106                 
107         }
108
109         private NonMetaArtifactInfo createNonMetaArtifactInfoTestSubject() {
110                 return new CsarUtils.NonMetaArtifactInfo("mock", "mock", ArtifactTypeEnum.AAI_SERVICE_MODEL,
111                                 ArtifactGroupTypeEnum.DEPLOYMENT, new byte[0], "mock", true);
112         }
113
114         @Test
115         public void testCreateCsar() {
116                 Component component = new Resource();
117                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
118                 ArtifactDefinition artifact = new ArtifactDefinition();
119                 artifact.setArtifactName("artifactName");
120                 artifact.setEsId("esId");
121                 toscaArtifacts.put("assettoscatemplate", artifact);
122
123                 component.setToscaArtifacts(toscaArtifacts);
124
125                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
126                                 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
127
128                 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
129                                 .thenReturn(ActionStatus.GENERAL_ERROR);
130
131                 testSubject.createCsar(component, true, true);
132         }
133
134         @Test
135         public void testCreateCsarWithGenerateCsarZipResponseIsLeft() {
136                 Component component = new Resource();
137                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
138                 ArtifactDefinition artifact = new ArtifactDefinition();
139                 artifact.setArtifactName("artifactName");
140                 artifact.setEsId("esId");
141                 artifact.setArtifactUUID("artifactUUID");
142                 artifact.setArtifactType("YANG");
143                 toscaArtifacts.put("assettoscatemplate", artifact);
144
145                 component.setToscaArtifacts(toscaArtifacts);
146                 component.setDeploymentArtifacts(toscaArtifacts);
147                 component.setArtifacts(toscaArtifacts);
148                 ESArtifactData artifactData = new ESArtifactData();
149                 byte[] data = "value".getBytes();
150                 artifactData.setDataAsArray(data);
151
152                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
153                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
154                 toscaTemplate.setDependencies(dependencies);
155
156                 List<SdcSchemaFilesData> filesData = new ArrayList<>();
157                 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
158                 filedata.setPayloadAsArray(data);
159                 filesData.add(filedata);
160
161                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
162
163                 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
164                                 .thenReturn(ActionStatus.GENERAL_ERROR);
165
166                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
167                                 .thenReturn(Either.left(toscaTemplate));
168
169                 Mockito.when(
170                                 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
171                                 .thenReturn(Either.left(filesData));
172
173                 testSubject.createCsar(component, false, true);
174         }
175
176         @Test
177         public void testGenerateCsarZipThrowsIOException() {
178                 Deencapsulation.invoke(testSubject, "generateCsarZip", byte[].class, byte[].class, new Resource(), true, false,
179                                 false);
180         }
181
182         @Test
183         public void testPopulateZipWhenGetDependenciesIsRight() {
184                 Component component = new Service();
185                 boolean getFromCS = false;
186
187                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
188                 ArtifactDefinition artifact = new ArtifactDefinition();
189                 artifact.setArtifactName("artifactName");
190                 artifact.setEsId("esId");
191                 artifact.setArtifactUUID("artifactUUID");
192                 artifact.setArtifactType("YANG");
193                 toscaArtifacts.put("assettoscatemplate", artifact);
194
195                 component.setToscaArtifacts(toscaArtifacts);
196                 component.setDeploymentArtifacts(toscaArtifacts);
197                 component.setArtifacts(toscaArtifacts);
198                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
199                 ESArtifactData artifactData = new ESArtifactData();
200                 byte[] data = "value".getBytes();
201                 artifactData.setDataAsArray(data);
202
203                 ToscaRepresentation tosca = new ToscaRepresentation();
204                 tosca.setMainYaml("value");
205
206                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
207
208                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
209
210                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
211                                 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
212
213                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
214                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false, false);
215                 } catch (Exception e) {
216                         e.printStackTrace();
217                 }
218         }
219
220         @Test
221         public void testPopulateZipWhenExportComponentIsRight() {
222                 Component component = new Resource();
223                 boolean getFromCS = false;
224
225                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
226                 ArtifactDefinition artifact = new ArtifactDefinition();
227                 artifact.setArtifactName("artifactName");
228                 artifact.setEsId("esId");
229                 artifact.setArtifactUUID("artifactUUID");
230                 artifact.setArtifactType("YANG");
231                 toscaArtifacts.put("assettoscatemplate", artifact);
232
233                 component.setToscaArtifacts(toscaArtifacts);
234                 component.setDeploymentArtifacts(toscaArtifacts);
235                 component.setArtifacts(toscaArtifacts);
236                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
237                 ESArtifactData artifactData = new ESArtifactData();
238                 byte[] data = "value".getBytes();
239                 artifactData.setDataAsArray(data);
240
241                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class)))
242                                 .thenReturn(Either.right(ToscaError.GENERAL_ERROR));
243
244                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
245                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, false, false);
246                 } catch (Exception e) {
247                         e.printStackTrace();
248                 }
249         }
250
251         @Test
252         public void testPopulateZipWhenComponentIsServiceAndCollectComponentCsarDefinitionIsRight() {
253                 Component component = new Service();
254                 boolean getFromCS = false;
255
256                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
257                 ArtifactDefinition artifact = new ArtifactDefinition();
258                 artifact.setArtifactName("artifactName");
259                 artifact.setEsId("esId");
260                 artifact.setArtifactUUID("artifactUUID");
261                 artifact.setArtifactType("YANG");
262                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
263                 artifact.setDescription("description");
264                 artifact.setArtifactLabel("artifactLabel");
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                 component.setVersion("1.0");
272                 component.setLastUpdaterUserId("userId");
273                 component.setUniqueId("uid");
274                 ESArtifactData artifactData = new ESArtifactData();
275                 byte[] data = "value".getBytes();
276                 artifactData.setDataAsArray(data);
277
278                 List<SdcSchemaFilesData> filesData = new ArrayList<>();
279                 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
280                 filedata.setPayloadAsArray(data);
281                 filesData.add(filedata);
282
283                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
284                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
285                 Triple<String, String, Component> triple = Triple.of("fileName", "cassandraId", component);
286                 dependencies.add(triple);
287                 toscaTemplate.setDependencies(dependencies);
288
289                 ToscaRepresentation tosca = new ToscaRepresentation();
290                 tosca.setMainYaml("value");
291
292                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
293
294                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
295
296                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
297                                 .thenReturn(Either.left(toscaTemplate));
298
299                 Mockito.when(
300                                 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
301                                 .thenReturn(Either.left(filesData));
302
303                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
304                                 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
305
306                 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(String.class), Mockito.any(String.class),
307                                 Mockito.any(Boolean.class))).thenReturn(Either.left(new User()));
308
309                 Mockito.when(artifactsBusinessLogic.validateAndHandleArtifact(Mockito.any(String.class),
310                                 Mockito.any(ComponentTypeEnum.class), Mockito.any(ArtifactOperationInfo.class), Mockito.isNull(),
311                                 Mockito.any(ArtifactDefinition.class), Mockito.any(String.class), Mockito.any(String.class),
312                                 Mockito.isNull(), Mockito.isNull(), Mockito.any(User.class), Mockito.any(Component.class),
313                                 Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(Boolean.class)))
314                                 .thenReturn(Either.left(Mockito.any(Either.class)));
315
316                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
317                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true, true);
318                 } catch (Exception e) {
319                         e.printStackTrace();
320                 }
321         }
322
323         @Test
324         public void testPopulateZipWhenGetEntryDataIsRight() {
325                 Component component = new Service();
326                 boolean getFromCS = true;
327
328                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
329                 ArtifactDefinition artifact = new ArtifactDefinition();
330                 artifact.setArtifactName("artifactName");
331                 artifact.setEsId("esId");
332                 artifact.setArtifactUUID("artifactUUID");
333                 artifact.setArtifactType("YANG");
334                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
335                 artifact.setDescription("description");
336                 artifact.setArtifactLabel("artifactLabel");
337                 toscaArtifacts.put("assettoscatemplate", artifact);
338
339                 component.setToscaArtifacts(toscaArtifacts);
340                 component.setDeploymentArtifacts(toscaArtifacts);
341                 component.setArtifacts(toscaArtifacts);
342                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
343                 component.setVersion("1.0");
344                 component.setLastUpdaterUserId("userId");
345                 component.setUniqueId("uid");
346                 ESArtifactData artifactData = new ESArtifactData();
347                 byte[] data = "value".getBytes();
348                 artifactData.setDataAsArray(data);
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, 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                 ESArtifactData artifactData = new ESArtifactData();
395                 byte[] data = "value".getBytes();
396                 artifactData.setDataAsArray(data);
397
398                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
399                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
400                 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
401                 dependencies.add(triple);
402                 toscaTemplate.setDependencies(dependencies);
403
404                 ToscaRepresentation tosca = new ToscaRepresentation();
405                 tosca.setMainYaml("value");
406
407                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
408
409                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca),
410                                 Either.left(tosca), Either.right(ToscaError.GENERAL_ERROR));
411
412                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
413                                 .thenReturn(Either.left(toscaTemplate));
414
415                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
416                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true, true);
417                 } catch (Exception e) {
418                         e.printStackTrace();
419                 }
420         }
421
422         @Test
423         public void testPopulateZipWhenLatestSchemaFilesFromCassandraIsRight() {
424                 Component component = new Service();
425                 boolean getFromCS = false;
426
427                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
428                 ArtifactDefinition artifact = new ArtifactDefinition();
429                 artifact.setArtifactName("artifactName");
430                 artifact.setEsId("esId");
431                 artifact.setArtifactUUID("artifactUUID");
432                 artifact.setArtifactType("YANG");
433                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
434                 artifact.setDescription("description");
435                 artifact.setArtifactLabel("artifactLabel");
436                 toscaArtifacts.put("assettoscatemplate", artifact);
437
438                 component.setToscaArtifacts(toscaArtifacts);
439                 component.setDeploymentArtifacts(toscaArtifacts);
440                 component.setArtifacts(toscaArtifacts);
441                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
442                 component.setVersion("1.0");
443                 component.setLastUpdaterUserId("userId");
444                 component.setUniqueId("uid");
445                 ESArtifactData artifactData = new ESArtifactData();
446                 byte[] data = "value".getBytes();
447                 artifactData.setDataAsArray(data);
448
449                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
450                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
451                 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
452                 dependencies.add(triple);
453                 toscaTemplate.setDependencies(dependencies);
454
455                 ToscaRepresentation tosca = new ToscaRepresentation();
456                 tosca.setMainYaml("value");
457
458                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
459
460                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
461
462                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
463                                 .thenReturn(Either.left(toscaTemplate));
464
465                 Mockito.when(
466                                 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
467                                 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
468
469                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
470                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true, true);
471                 } catch (Exception e) {
472                         e.printStackTrace();
473                 }
474         }
475
476         @Test
477         public void testPopulateZipWhenAddSchemaFilesFromCassandraIsRight() {
478                 Component component = new Service();
479                 boolean getFromCS = false;
480
481                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
482                 ArtifactDefinition artifact = new ArtifactDefinition();
483                 artifact.setArtifactName("artifactName");
484                 artifact.setEsId("esId");
485                 artifact.setArtifactUUID("artifactUUID");
486                 artifact.setArtifactType("YANG");
487                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
488                 artifact.setDescription("description");
489                 artifact.setArtifactLabel("artifactLabel");
490                 toscaArtifacts.put("assettoscatemplate", artifact);
491
492                 component.setToscaArtifacts(toscaArtifacts);
493                 component.setDeploymentArtifacts(toscaArtifacts);
494                 component.setArtifacts(toscaArtifacts);
495                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
496                 component.setVersion("1.0");
497                 component.setLastUpdaterUserId("userId");
498                 component.setUniqueId("uid");
499                 ESArtifactData artifactData = new ESArtifactData();
500                 byte[] data = "value".getBytes();
501                 artifactData.setDataAsArray(data);
502
503                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
504                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
505                 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
506                 dependencies.add(triple);
507                 toscaTemplate.setDependencies(dependencies);
508
509                 ToscaRepresentation tosca = new ToscaRepresentation();
510                 tosca.setMainYaml("value");
511
512                 List<SdcSchemaFilesData> schemaList = new ArrayList<>();
513                 SdcSchemaFilesData schemaData = new SdcSchemaFilesData();
514                 schemaData.setPayloadAsArray(null);
515                 schemaList.add(schemaData);
516
517                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
518
519                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
520
521                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
522                                 .thenReturn(Either.left(toscaTemplate));
523
524                 Mockito.when(
525                                 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
526                                 .thenReturn(Either.left(schemaList));
527
528                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
529                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true, true);
530                 } catch (Exception e) {
531                         e.printStackTrace();
532                 }
533         }
534
535         @Test
536         public void testPopulateZipWhenHandleAllAAIArtifactsInDataModelIsRight() {
537                 Component component = new Service();
538                 boolean getFromCS = false;
539
540                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
541                 ArtifactDefinition artifact = new ArtifactDefinition();
542                 artifact.setArtifactName("artifactName");
543                 artifact.setEsId("esId");
544                 artifact.setArtifactUUID("artifactUUID");
545                 artifact.setArtifactType("YANG");
546                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
547                 artifact.setDescription("description");
548                 artifact.setArtifactLabel("artifactLabel");
549                 toscaArtifacts.put("assettoscatemplate", artifact);
550
551                 component.setToscaArtifacts(toscaArtifacts);
552                 component.setDeploymentArtifacts(toscaArtifacts);
553                 component.setArtifacts(toscaArtifacts);
554                 component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
555                 component.setVersion("1.0");
556                 component.setLastUpdaterUserId("userId");
557                 component.setUniqueId("uid");
558                 ESArtifactData artifactData = new ESArtifactData();
559                 byte[] data = "value".getBytes();
560                 artifactData.setDataAsArray(data);
561
562                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
563                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
564                 Triple<String, String, Component> triple = Triple.of("fileName", "", component);
565                 dependencies.add(triple);
566                 toscaTemplate.setDependencies(dependencies);
567
568                 ToscaRepresentation tosca = new ToscaRepresentation();
569                 tosca.setMainYaml("value");
570
571                 List<SdcSchemaFilesData> schemaList = new ArrayList<>();
572                 SdcSchemaFilesData schemaData = new SdcSchemaFilesData();
573                 schemaData.setPayloadAsArray(data);
574                 schemaList.add(schemaData);
575
576                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData));
577
578                 Mockito.when(toscaExportUtils.exportComponent(Mockito.any(Component.class))).thenReturn(Either.left(tosca));
579
580                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
581                                 .thenReturn(Either.left(toscaTemplate));
582
583                 Mockito.when(
584                                 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
585                                 .thenReturn(Either.left(schemaList));
586
587                 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(String.class), Mockito.any(String.class),
588                                 Mockito.any(Boolean.class))).thenReturn(Either.right(new ResponseFormat(500)));
589
590                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
591                         Deencapsulation.invoke(testSubject, "populateZip", component, getFromCS, zip, true, true);
592                 } catch (Exception e) {
593                         e.printStackTrace();
594                 }
595         }
596
597         @Test
598         public void testAddSchemaFilesFromCassandra() {
599                 try (ByteArrayOutputStream out = new ByteArrayOutputStream();
600                                 ZipOutputStream zip = new ZipOutputStream(out);
601                                 ByteArrayOutputStream outMockStream = new ByteArrayOutputStream();
602                                 ZipOutputStream outMock = new ZipOutputStream(outMockStream);) {
603
604                         outMock.putNextEntry(new ZipEntry("mock1"));
605                         outMock.write(new byte[1]);
606                         outMock.putNextEntry(new ZipEntry("mock2"));
607                         outMock.write(new byte[3]);
608                         outMock.close();
609                         byte[] byteArray = outMockStream.toByteArray();
610                         Deencapsulation.invoke(testSubject, "addSchemaFilesFromCassandra", zip, byteArray);
611                 } catch (Exception e) {
612                         e.printStackTrace();
613                 }
614
615         }
616
617         @Test
618         public void testAddInnerComponentsToCache() {
619                 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
620                 Component childComponent = new Resource();
621                 Component componentRI = new Service();
622                 List<ComponentInstance> componentInstances = new ArrayList<>();
623                 ComponentInstance instance = new ComponentInstance();
624                 instance.setComponentUid("resourceUid");
625                 componentInstances.add(instance);
626                 childComponent.setComponentInstances(componentInstances);
627
628                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
629                 ArtifactDefinition artifact = new ArtifactDefinition();
630                 artifact.setArtifactName("artifactName");
631                 artifact.setEsId("esId");
632                 artifact.setArtifactUUID("artifactUUID");
633                 artifact.setArtifactType("YANG");
634                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
635                 artifact.setDescription("description");
636                 artifact.setArtifactLabel("artifactLabel");
637                 toscaArtifacts.put("assettoscatemplate", artifact);
638
639                 componentRI.setToscaArtifacts(toscaArtifacts);
640
641                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
642                                 .thenReturn(Either.left(componentRI));
643
644                 Deencapsulation.invoke(testSubject, "addInnerComponentsToCache", componentCache, childComponent);
645         }
646
647         @Test
648         public void testAddInnerComponentsToCacheWhenGetToscaElementIsRight() {
649                 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
650                 Component childComponent = new Resource();
651
652                 List<ComponentInstance> componentInstances = new ArrayList<>();
653                 ComponentInstance instance = new ComponentInstance();
654                 componentInstances.add(instance);
655                 childComponent.setComponentInstances(componentInstances);
656
657                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
658                 ArtifactDefinition artifact = new ArtifactDefinition();
659                 artifact.setArtifactName("artifactName");
660                 artifact.setEsId("esId");
661                 artifact.setArtifactUUID("artifactUUID");
662                 artifact.setArtifactType("YANG");
663                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
664                 artifact.setDescription("description");
665                 artifact.setArtifactLabel("artifactLabel");
666                 toscaArtifacts.put("assettoscatemplate", artifact);
667
668                 Component componentRI = new Service();
669
670                 componentRI.setToscaArtifacts(toscaArtifacts);
671
672                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class)))
673                                 .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
674
675                 Deencapsulation.invoke(testSubject, "addInnerComponentsToCache", componentCache, childComponent);
676         }
677
678         @Test
679         public void testAddComponentToCache() {
680                 Map<String, ImmutableTriple<String, String, Component>> componentCache = new HashMap<>();
681                 String id = "id";
682                 String fileName = "fileName";
683                 Component component = new Resource();
684                 component.setInvariantUUID("key");
685                 component.setVersion("1.0");
686
687                 Component cachedComponent = new Resource();
688                 cachedComponent.setVersion("0.3");
689
690                 componentCache.put("key", new ImmutableTriple<String, String, Component>(id, fileName, cachedComponent));
691
692                 Deencapsulation.invoke(testSubject, "addComponentToCache", componentCache, id, fileName, component);
693         }
694
695         @Test
696         public void testWriteComponentInterface() {
697                 String fileName = "name.hello";
698                 ToscaRepresentation tosca = new ToscaRepresentation();
699                 tosca.setMainYaml("value");
700
701                 Mockito.when(toscaExportUtils.exportComponentInterface(Mockito.any(Component.class)))
702                                 .thenReturn(Either.left(tosca));
703
704                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
705                         Deencapsulation.invoke(testSubject, "writeComponentInterface", new Resource(), zip, fileName);
706                 } catch (IOException e) {
707                         e.printStackTrace();
708                 }
709
710         }
711
712         @Test
713         public void testHandleAAIArtifacts() {
714                 Component component = new Service();
715                 component.setComponentType(ComponentTypeEnum.SERVICE);
716                 byte[] data = "value".getBytes();
717
718                 List<ImmutablePair<Component, byte[]>> generatorInputs = new ArrayList<>();
719                 generatorInputs.add(new ImmutablePair<Component, byte[]>(component, data));
720
721                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
722                 ArtifactDefinition artifact = new ArtifactDefinition();
723                 artifact.setArtifactName("artifactName");
724                 artifact.setEsId("esId");
725                 artifact.setArtifactUUID("artifactUUID");
726                 artifact.setArtifactType("YANG");
727                 artifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
728                 artifact.setDescription("description");
729                 artifact.setArtifactLabel("artifactLabel");
730                 toscaArtifacts.put("assettoscatemplate", artifact);
731                 component.setToscaArtifacts(toscaArtifacts);
732                 component.setVersion("1.3");
733
734                 Deencapsulation.invoke(testSubject, "handleAAIArtifacts", component, false, generatorInputs);
735         }
736
737         @Test
738         public void testHandleAllAAIArtifactsInDataModelWhenArtifactOperationDeleteAndCreateIsRight() {
739                 Component component = new Resource();
740                 List<ArtifactDefinition> artifactsFromAAI = new ArrayList<>();
741                 ArtifactDefinition AAIartifact = new ArtifactDefinition();
742                 AAIartifact.setArtifactLabel("artifactLabel");
743                 AAIartifact.setGenerated(true);
744                 Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
745                 artifactsFromAAI.add(AAIartifact);
746                 ArtifactDefinition artifact = new ArtifactDefinition();
747                 artifact.setArtifactLabel("label");
748                 artifact.setGenerated(true);
749                 deploymentArtifacts.put("label", artifact);
750                 component.setDeploymentArtifacts(deploymentArtifacts);
751                 component.setArtifacts(deploymentArtifacts);
752                 component.setLastUpdaterUserId("userId");
753                 component.setUniqueId("id");
754
755                 Mockito.when(artifactsBusinessLogic.validateUserExists(Mockito.any(String.class), Mockito.any(String.class),
756                                 Mockito.any(Boolean.class))).thenReturn(Either.left(new User()));
757
758                 Mockito.when(artifactsBusinessLogic.validateAndHandleArtifact(Mockito.any(String.class),
759                                 Mockito.any(ComponentTypeEnum.class), Mockito.any(ArtifactOperationInfo.class), Mockito.isNull(),
760                                 Mockito.any(ArtifactDefinition.class), Mockito.any(String.class), Mockito.any(String.class),
761                                 Mockito.isNull(), Mockito.isNull(), Mockito.any(User.class), Mockito.any(Component.class),
762                                 Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(Boolean.class)))
763                                 .thenReturn(Either.right(new ResponseFormat()));
764
765                 Deencapsulation.invoke(testSubject, "handleAllAAIArtifactsInDataModel", component, artifactsFromAAI, true,
766                                 true);
767         }
768
769         @Test
770         public void testCheckAaiForUpdateWithGetGeneratedFalse() {
771                 Component component = new Resource();
772                 ArtifactDefinition artifact = new ArtifactDefinition();
773                 artifact.setArtifactLabel("label");
774                 artifact.setGenerated(false);
775                 Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
776                 deploymentArtifacts.put("label", artifact);
777                 component.setDeploymentArtifacts(deploymentArtifacts);
778
779                 Deencapsulation.invoke(testSubject, "checkAaiForUpdate", component, artifact);
780         }
781
782         @Test
783         public void testCheckAaiForUpdateWithGetGeneratedTrue() {
784                 Component component = new Resource();
785                 ArtifactDefinition artifact = new ArtifactDefinition();
786                 artifact.setArtifactLabel("label");
787                 artifact.setGenerated(true);
788                 Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
789                 deploymentArtifacts.put("label", artifact);
790                 component.setDeploymentArtifacts(deploymentArtifacts);
791
792                 Deencapsulation.invoke(testSubject, "checkAaiForUpdate", component, artifact);
793         }
794
795         @Test
796         public void testCheckAaiForUpdateWithDeploymentArtifactIsNull() {
797                 Component component = new Resource();
798                 ArtifactDefinition artifact = new ArtifactDefinition();
799                 artifact.setArtifactLabel("label1");
800                 artifact.setGenerated(true);
801                 Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
802                 deploymentArtifacts.put("label", artifact);
803                 component.setDeploymentArtifacts(deploymentArtifacts);
804
805                 Deencapsulation.invoke(testSubject, "checkAaiForUpdate", component, artifact);
806         }
807
808         @Test
809         public void testGetEntryData() {
810                 String cassandraId = "id";
811                 Component childComponent = new Resource();
812
813                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
814                                 .thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
815
816                 Deencapsulation.invoke(testSubject, "getEntryData", cassandraId, childComponent);
817         }
818
819         @Test
820         public void testGetLatestSchemaFilesFromCassandraWhenListOfSchemasIsEmpty() {
821                 List<SdcSchemaFilesData> filesData = new ArrayList<>();
822
823                 Mockito.when(
824                                 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
825                                 .thenReturn(Either.left(filesData));
826                 Deencapsulation.invoke(testSubject, "getLatestSchemaFilesFromCassandra");
827         }
828
829         @Test
830         public void testArtifactGenerator() {
831                 Component component = new Resource();
832
833                 component.setVersion("1.0");
834
835                 Deencapsulation.invoke(testSubject, "artifactGenerator", new ArrayList<>(), ArtifactType.class, component);
836         }
837
838         @Test
839         public void testExtractVfcsArtifactsFromCsar() {
840                 String key = "Artifacts/org.openecomp.resource.some/path/to/resource";
841                 byte[] data = "value".getBytes();
842
843                 Map<String, byte[]> csar = new HashMap<>();
844                 csar.put(key, data);
845
846                 CsarUtils.extractVfcsArtifactsFromCsar(csar);
847         }
848
849         @Test
850         public void testAddExtractedVfcArtifactWhenArtifactsContainsExtractedArtifactKey() {
851                 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
852                                 "key", new ArtifactDefinition());
853                 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
854                 artifacts.put("key", new ArrayList<>());
855
856                 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
857         }
858
859         @Test
860         public void testAddExtractedVfcArtifactWhenArtifactsDoesntContainsExtractedArtifactKey() {
861                 ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact = new ImmutablePair<String, ArtifactDefinition>(
862                                 "key", new ArtifactDefinition());
863                 Map<String, List<ArtifactDefinition>> artifacts = new HashMap<>();
864                 artifacts.put("key1", new ArrayList<>());
865
866                 Deencapsulation.invoke(testSubject, "addExtractedVfcArtifact", extractedVfcArtifact, artifacts);
867         }
868
869         @Test
870         public void testExtractVfcArtifact() {
871                 String path = "path/to/informational/artificat";
872                 Map<String, byte[]> map = new HashMap<>();
873                 map.put(path, "value".getBytes());
874                 Entry<String, byte[]> entry = map.entrySet().iterator().next();
875
876                 Deencapsulation.invoke(testSubject, "extractVfcArtifact", entry, new HashMap<>());
877         }
878
879         @Test
880         public void testDetectArtifactGroupTypeWithExceptionBeingCaught() {
881                 Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", Map.class);
882         }
883
884         @Test
885         public void testDetectArtifactGroupTypeWWhenCollectedWarningMessagesContainesKey() {
886                 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
887
888                 collectedWarningMessages.put("Warning - unrecognized artifact group type {} was received.", new HashSet<>());
889                 Deencapsulation.invoke(testSubject, "detectArtifactGroupType", "type", collectedWarningMessages);
890         }
891
892         @Test
893         public void testNonMetaArtifactInfoCtor() {
894                 createNonMetaArtifactInfoTestSubject();
895         }
896
897         @Test
898         public void testNonMetaArtifactInfoGetPath() {
899                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
900
901                 testSubject.getPath();
902         }
903
904         @Test
905         public void testNonMetaArtifactInfoGetArtifactName() {
906                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
907
908                 testSubject.getArtifactName();
909         }
910
911         @Test
912         public void testNonMetaArtifactInfoGetArtifactType() {
913                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
914
915                 testSubject.getArtifactType();
916         }
917
918         @Test
919         public void testNonMetaArtifactInfoGetDisplayName() {
920                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
921
922                 testSubject.getDisplayName();
923         }
924
925         @Test
926         public void testNonMetaArtifactInfoGetArtifactGroupType() {
927                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
928
929                 testSubject.getArtifactGroupType();
930         }
931
932         @Test
933         public void testNonMetaArtifactInfoGetArtifactLabel() {
934                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
935
936                 testSubject.getArtifactLabel();
937         }
938
939         @Test
940         public void testNonMetaArtifactInfoGetIsFromCsar() {
941                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
942
943                 testSubject.isFromCsar();
944         }
945
946         @Test
947         public void testNonMetaArtifactInfoGetPayloadData() {
948                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
949
950                 testSubject.getPayloadData();
951         }
952
953         @Test
954         public void testNonMetaArtifactInfoGetArtifaactChecksum() {
955                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
956
957                 testSubject.getArtifactChecksum();
958         }
959
960         @Test
961         public void testNonMetaArtifactInfoGetArtifactUniqueId() {
962                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
963
964                 testSubject.getArtifactUniqueId();
965         }
966
967         @Test
968         public void testNonMetaArtifactInfosetArtifactUniqueId() {
969                 NonMetaArtifactInfo testSubject = createNonMetaArtifactInfoTestSubject();
970
971                 testSubject.setArtifactUniqueId("artifactUniqueId");
972         }
973
974         @Test
975         public void testValidateNonMetaArtifactWithExceptionCaught() {
976                 CsarUtils.validateNonMetaArtifact("", new byte[0], new HashMap<>());
977         }
978
979         @Test
980         public void testWriteAllFilesToCsarWhenWriteOperationsArtifactsToCsarIsRight() {
981                 Component component = new Resource();
982                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
983                 ArtifactDefinition artifact = new ArtifactDefinition();
984                 artifact.setArtifactName("artifactName");
985                 artifact.setEsId("esId");
986                 artifact.setArtifactUUID("artifactUUID");
987                 artifact.setArtifactType("YANG");
988                 toscaArtifacts.put("assettoscatemplate", artifact);
989
990                 component.setToscaArtifacts(toscaArtifacts);
991                 component.setDeploymentArtifacts(toscaArtifacts);
992                 component.setArtifacts(toscaArtifacts);
993                 Map<String, InterfaceDefinition> interfaces = new HashMap<>();
994                 InterfaceDefinition interfaceDef = new InterfaceDefinition();
995                 Map<String, OperationDataDefinition> operations = new HashMap<>();
996                 OperationDataDefinition operation = new OperationDataDefinition();
997                 ArtifactDataDefinition implementation = new ArtifactDataDefinition();
998                 implementation.setArtifactUUID("artifactUUID");
999                 implementation.setArtifactName("artifactName");
1000                 operation.setImplementation(implementation);
1001                 operations.put("key", operation);
1002                 interfaceDef.setOperations(operations);
1003                 interfaces.put("key", interfaceDef);
1004                 ((Resource) component).setInterfaces(interfaces);
1005
1006                 ESArtifactData artifactData = new ESArtifactData();
1007                 byte[] data = "value".getBytes();
1008                 artifactData.setDataAsArray(data);
1009
1010                 ToscaTemplate toscaTemplate = new ToscaTemplate("version");
1011                 List<Triple<String, String, Component>> dependencies = new ArrayList<>();
1012                 toscaTemplate.setDependencies(dependencies);
1013
1014                 List<SdcSchemaFilesData> filesData = new ArrayList<>();
1015                 SdcSchemaFilesData filedata = new SdcSchemaFilesData();
1016                 filedata.setPayloadAsArray(data);
1017                 filesData.add(filedata);
1018
1019                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData),
1020                                 Either.right(CassandraOperationStatus.GENERAL_ERROR));
1021
1022                 Mockito.when(componentsUtils.convertFromStorageResponse(Mockito.any(StorageOperationStatus.class)))
1023                                 .thenReturn(ActionStatus.GENERAL_ERROR);
1024
1025                 Mockito.when(toscaExportUtils.getDependencies(Mockito.any(Component.class)))
1026                                 .thenReturn(Either.left(toscaTemplate));
1027
1028                 Mockito.when(
1029                                 sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(Mockito.any(String.class), Mockito.any(String.class)))
1030                                 .thenReturn(Either.left(filesData));
1031
1032                 testSubject.createCsar(component, false, true);
1033         }
1034
1035         @Test
1036         public void testWriteOperationsArtifactsToCsarWhenComponentIsService() {
1037                 Component component = new Service();
1038
1039                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
1040                         Deencapsulation.invoke(testSubject, "writeOperationsArtifactsToCsar", component, zip);
1041                 } catch (Exception e) {
1042                         e.printStackTrace();
1043                 }
1044         }
1045
1046         @Test
1047         public void testWriteOperationsArtifactsToCsarWhenOperationGetImplementaionIsNull() {
1048                 Component component = new Resource();
1049                 Map<String, InterfaceDefinition> interfaces = new HashMap<>();
1050                 InterfaceDefinition interfaceDef = new InterfaceDefinition();
1051                 Map<String, OperationDataDefinition> operations = new HashMap<>();
1052                 operations.put("key", new OperationDataDefinition());
1053                 interfaceDef.setOperations(operations);
1054                 interfaces.put("key", interfaceDef);
1055
1056                 ((Resource) component).setInterfaces(interfaces);
1057
1058                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
1059                         Deencapsulation.invoke(testSubject, "writeOperationsArtifactsToCsar", component, zip);
1060                 } catch (Exception e) {
1061                         e.printStackTrace();
1062                 }
1063         }
1064
1065         @Test
1066         public void testWriteOperationsArtifactsToCsarWhenOperationGetArtifactNameIsNull() {
1067                 Component component = new Resource();
1068
1069                 Map<String, InterfaceDefinition> interfaces = new HashMap<>();
1070                 InterfaceDefinition interfaceDef = new InterfaceDefinition();
1071                 Map<String, OperationDataDefinition> operations = new HashMap<>();
1072                 OperationDataDefinition operation = new OperationDataDefinition();
1073                 ArtifactDataDefinition implementation = new ArtifactDataDefinition();
1074                 operation.setImplementation(implementation);
1075                 operations.put("key", operation);
1076                 interfaceDef.setOperations(operations);
1077                 interfaces.put("key", interfaceDef);
1078                 ((Resource) component).setInterfaces(interfaces);
1079
1080                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
1081                         Deencapsulation.invoke(testSubject, "writeOperationsArtifactsToCsar", component, zip);
1082                 } catch (Exception e) {
1083                         e.printStackTrace();
1084                 }
1085         }
1086
1087         @Test
1088         public void testWriteOperationsArtifactsToCsarWhenGettingArtifactFromCassandra() {
1089                 Component component = new Resource();
1090
1091                 Map<String, InterfaceDefinition> interfaces = new HashMap<>();
1092                 InterfaceDefinition interfaceDef = new InterfaceDefinition();
1093                 Map<String, OperationDataDefinition> operations = new HashMap<>();
1094                 OperationDataDefinition operation = new OperationDataDefinition();
1095                 ArtifactDataDefinition implementation = new ArtifactDataDefinition();
1096                 implementation.setArtifactName("artifactName");
1097                 implementation.setArtifactUUID("artifactUUID");
1098                 operation.setImplementation(implementation);
1099                 operations.put("key", operation);
1100                 interfaceDef.setOperations(operations);
1101                 interfaceDef.setToscaResourceName("toscaResourceName");
1102                 interfaces.put("key", interfaceDef);
1103                 ((Resource) component).setInterfaces(interfaces);
1104                 component.setNormalizedName("normalizedName");
1105
1106                 ESArtifactData data = new ESArtifactData();
1107                 data.setDataAsArray("data".getBytes());
1108
1109                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(data));
1110
1111                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
1112                         Deencapsulation.invoke(testSubject, "writeOperationsArtifactsToCsar", component, zip);
1113                 } catch (Exception e) {
1114                         e.printStackTrace();
1115                 }
1116         }
1117
1118         @Test
1119         public void testWriteOperationsArtifactsToCsarWhenNullPointerExceptionIsCaught() {
1120                 Component component = new Resource();
1121
1122                 Map<String, InterfaceDefinition> interfaces = new HashMap<>();
1123                 InterfaceDefinition interfaceDef = new InterfaceDefinition();
1124                 Map<String, OperationDataDefinition> operations = new HashMap<>();
1125                 OperationDataDefinition operation = new OperationDataDefinition();
1126                 ArtifactDataDefinition implementation = new ArtifactDataDefinition();
1127                 implementation.setArtifactName("artifactName");
1128                 implementation.setArtifactUUID("artifactUUID");
1129                 operation.setImplementation(implementation);
1130                 operations.put("key", operation);
1131                 interfaceDef.setOperations(operations);
1132                 interfaceDef.setToscaResourceName("toscaResourceName");
1133                 interfaces.put("key", interfaceDef);
1134                 ((Resource) component).setInterfaces(interfaces);
1135                 component.setNormalizedName("normalizedName");
1136
1137                 Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class)))
1138                                 .thenReturn(Either.left(new ESArtifactData()));
1139
1140                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
1141                         Deencapsulation.invoke(testSubject, "writeOperationsArtifactsToCsar", component, zip);
1142                 } catch (Exception e) {
1143                         e.printStackTrace();
1144                 }
1145         }
1146
1147         @Test
1148         public void testWriteArtifactDefinition() {
1149                 Component component = new Service();
1150                 List<ArtifactDefinition> artifactDefinitionList = new ArrayList<>();
1151                 String artifactPathAndFolder = "";
1152
1153                 ArtifactDefinition artifact = new ArtifactDefinition();
1154                 artifact.setArtifactType(ArtifactTypeEnum.HEAT_ENV.getType());
1155                 artifactDefinitionList.add(artifact);
1156
1157                 try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(out);) {
1158                         Deencapsulation.invoke(testSubject, "writeArtifactDefinition", component, zip, artifactDefinitionList,
1159                                         artifactPathAndFolder, false);
1160                 } catch (Exception e) {
1161                         e.printStackTrace();
1162                 }
1163         }
1164
1165         @Test
1166         public void testCollectComponentCsarDefinitionWhenComponentIsServiceAndGetToscaElementIsLeft() {
1167                 Component component = new Service();
1168                 component.setUniqueId("uniqueId");
1169                 List<ComponentInstance> resourceInstances = new ArrayList<>();
1170                 ComponentInstance instance = new ComponentInstance();
1171                 instance.setComponentUid("resourceUid");
1172                 instance.setOriginType(OriginTypeEnum.SERVICE);
1173                 resourceInstances.add(instance);
1174                 component.setComponentInstances(resourceInstances);
1175
1176                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
1177                 ArtifactDefinition artifact = new ArtifactDefinition();
1178                 artifact.setArtifactName("artifactName");
1179                 artifact.setEsId("esId");
1180                 artifact.setArtifactUUID("artifactUUID");
1181                 artifact.setArtifactType("YANG");
1182                 toscaArtifacts.put("assettoscatemplate", artifact);
1183
1184                 component.setToscaArtifacts(toscaArtifacts);
1185                 component.setDeploymentArtifacts(toscaArtifacts);
1186                 component.setArtifacts(toscaArtifacts);
1187
1188                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
1189                                 Either.right(StorageOperationStatus.BAD_REQUEST));
1190
1191                 Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
1192
1193         }
1194
1195         @Test
1196         public void testCollectComponentTypeArtifactsWhenFetchedComponentHasComponentInstances() {
1197                 Component component = new Service();
1198                 Component fetchedComponent = new Resource();
1199                 component.setUniqueId("uniqueId");
1200                 List<ComponentInstance> resourceInstances = new ArrayList<>();
1201                 ComponentInstance instance = new ComponentInstance();
1202                 instance.setComponentUid("resourceUid");
1203                 instance.setOriginType(OriginTypeEnum.SERVICE);
1204                 resourceInstances.add(instance);
1205                 component.setComponentInstances(resourceInstances);
1206                 fetchedComponent.setComponentInstances(resourceInstances);
1207
1208                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
1209                 ArtifactDefinition artifact = new ArtifactDefinition();
1210                 artifact.setArtifactName("artifactName");
1211                 artifact.setEsId("esId");
1212                 artifact.setArtifactUUID("artifactUUID");
1213                 artifact.setArtifactType("YANG");
1214                 toscaArtifacts.put("assettoscatemplate", artifact);
1215
1216                 component.setToscaArtifacts(toscaArtifacts);
1217                 component.setDeploymentArtifacts(toscaArtifacts);
1218                 component.setArtifacts(toscaArtifacts);
1219
1220                 fetchedComponent.setToscaArtifacts(toscaArtifacts);
1221                 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
1222                 fetchedComponent.setArtifacts(toscaArtifacts);
1223
1224                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
1225                                 Either.left(fetchedComponent), Either.right(StorageOperationStatus.BAD_REQUEST));
1226
1227                 Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
1228         }
1229
1230         @Test
1231         public void testCollectComponentTypeArtifactsWhenFetchedComponentDontHaveComponentInstances() {
1232                 Component component = new Service();
1233                 Component fetchedComponent = new Resource();
1234                 component.setUniqueId("uniqueId");
1235                 List<ComponentInstance> resourceInstances = new ArrayList<>();
1236                 ComponentInstance instance = new ComponentInstance();
1237                 instance.setComponentUid("resourceUid");
1238                 instance.setOriginType(OriginTypeEnum.SERVICE);
1239
1240                 Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
1241                 ArtifactDefinition artifact = new ArtifactDefinition();
1242                 artifact.setArtifactName("artifactName");
1243                 artifact.setEsId("esId");
1244                 artifact.setArtifactUUID("artifactUUID");
1245                 artifact.setArtifactType("PLAN");
1246                 toscaArtifacts.put("assettoscatemplate", artifact);
1247
1248                 instance.setDeploymentArtifacts(toscaArtifacts);
1249
1250                 resourceInstances.add(instance);
1251                 component.setComponentInstances(resourceInstances);
1252
1253                 component.setToscaArtifacts(toscaArtifacts);
1254                 component.setDeploymentArtifacts(toscaArtifacts);
1255                 component.setArtifacts(toscaArtifacts);
1256
1257                 fetchedComponent.setToscaArtifacts(toscaArtifacts);
1258                 fetchedComponent.setDeploymentArtifacts(toscaArtifacts);
1259                 fetchedComponent.setArtifacts(toscaArtifacts);
1260
1261                 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class))).thenReturn(Either.left(component),
1262                                 Either.left(fetchedComponent));
1263
1264                 Deencapsulation.invoke(testSubject, "collectComponentCsarDefinition", component);
1265         }
1266
1267         @Test
1268         public void testValidateNonMetaArtifactHappyScenario() {
1269                 String artifactPath = "Artifacts/Deployment/YANG_XML/myYang.xml";
1270                 byte[] payloadData = "some payload data".getBytes();
1271                 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
1272                 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
1273                                 payloadData, collectedWarningMessages);
1274                 assertTrue(eitherNonMetaArtifact.isLeft());
1275                 assertTrue(collectedWarningMessages.isEmpty());
1276
1277                 artifactPath = "Artifacts/Informational/OTHER/someArtifact.xml";
1278                 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
1279                 assertTrue(eitherNonMetaArtifact.isLeft());
1280                 assertTrue(collectedWarningMessages.isEmpty());
1281         }
1282
1283         @Test
1284         public void testValidateNonMetaArtifactScenarioWithWarnnings() {
1285                 String artifactPath = "Artifacts/Deployment/Buga/myYang.xml";
1286                 byte[] payloadData = "some payload data".getBytes();
1287                 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
1288                 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
1289                                 payloadData, collectedWarningMessages);
1290                 assertTrue(eitherNonMetaArtifact.isLeft());
1291
1292                 artifactPath = "Artifacts/Informational/Buga2/someArtifact.xml";
1293                 eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath, payloadData, collectedWarningMessages);
1294                 assertTrue(eitherNonMetaArtifact.isLeft());
1295
1296                 assertTrue(collectedWarningMessages.size() == 1);
1297                 assertTrue(collectedWarningMessages.values().iterator().next().size() == 2);
1298         }
1299
1300         @Test
1301         public void testValidateNonMetaArtifactUnhappyScenario() {
1302                 String artifactPath = "Artifacts/Buga/YANG_XML/myYang.xml";
1303                 byte[] payloadData = "some payload data".getBytes();
1304                 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
1305                 Either<NonMetaArtifactInfo, Boolean> eitherNonMetaArtifact = CsarUtils.validateNonMetaArtifact(artifactPath,
1306                                 payloadData, collectedWarningMessages);
1307                 assertTrue(eitherNonMetaArtifact.isRight());
1308                 assertTrue(!collectedWarningMessages.isEmpty());
1309         }
1310 }