9149fcfb3f01ecd796a69812b7046bc8d80c81f6
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
18
19 import org.mockito.ArgumentCaptor;
20 import org.mockito.Captor;
21 import org.mockito.InjectMocks;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.mockito.Spy;
25 import org.openecomp.core.enrichment.factory.EnrichmentManagerFactory;
26 import org.openecomp.core.factory.impl.AbstractFactoryBase;
27 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
28 import org.openecomp.core.model.dao.ServiceModelDao;
29 import org.openecomp.core.model.types.ServiceElement;
30 import org.openecomp.core.utilities.file.FileContentHandler;
31 import org.openecomp.core.utilities.file.FileUtils;
32 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
33 import org.openecomp.sdc.common.errors.CoreException;
34 import org.openecomp.sdc.common.errors.ErrorCategory;
35 import org.openecomp.sdc.common.errors.ErrorCode;
36 import org.openecomp.sdc.common.errors.Messages;
37 import org.openecomp.sdc.healing.api.HealingManager;
38 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
39 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
40 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
41 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
42 import org.openecomp.sdc.vendorsoftwareproduct.ManualVspToscaManager;
43 import org.openecomp.sdc.vendorsoftwareproduct.MonitoringUploadsManager;
44 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
45 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
46 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
47 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
48 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
49 import org.openecomp.sdc.vendorsoftwareproduct.dao.PackageInfoDao;
50 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
51 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
52 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
53 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateEntity;
54 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.PackageInfo;
55 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
56 import org.openecomp.sdc.vendorsoftwareproduct.impl.mock.EnrichmentManagerFactoryImpl;
57 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactGenerator;
58 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
59 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
60 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
61 import org.openecomp.sdc.versioning.ActionVersioningManager;
62 import org.openecomp.sdc.versioning.dao.types.Version;
63 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
64 import org.openecomp.sdc.versioning.types.VersionInfo;
65 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
66 import org.testng.Assert;
67 import org.testng.annotations.AfterMethod;
68 import org.testng.annotations.BeforeMethod;
69 import org.testng.annotations.Test;
70
71 import java.io.File;
72 import java.io.FileInputStream;
73 import java.io.IOException;
74 import java.io.InputStream;
75 import java.net.URL;
76 import java.nio.ByteBuffer;
77 import java.util.ArrayList;
78 import java.util.Arrays;
79 import java.util.Collections;
80 import java.util.HashMap;
81 import java.util.List;
82 import java.util.Optional;
83 import java.util.zip.ZipEntry;
84 import java.util.zip.ZipInputStream;
85
86 import static org.mockito.Matchers.any;
87 import static org.mockito.Matchers.anyObject;
88 import static org.mockito.Mockito.doNothing;
89 import static org.mockito.Mockito.doReturn;
90 import static org.mockito.Mockito.doThrow;
91 import static org.mockito.Mockito.never;
92 import static org.mockito.Mockito.verify;
93
94
95 public class VendorSoftwareProductManagerImplTest {
96
97   private static final String VSP_ID = "vspId";
98   private static final Version VERSION01 = new Version("0, 1");
99   private static final Version VERSION10 = new Version("1, 0");
100   private static final String USER1 = "vspTestUser1";
101   private static final String USER2 = "vspTestUser2";
102
103   @Mock
104   private ActionVersioningManager versioningManagerMock;
105   @Mock
106   private OrchestrationTemplateDao orchestrationTemplateDataDaoMock;
107   @Mock
108   private VendorLicenseFacade vendorLicenseFacadeMock;
109   @Mock
110   private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDaoMock;
111   @Mock
112   private EnrichedServiceModelDao<ToscaServiceModel, ServiceElement> enrichedServiceModelDaoMock;
113   @Mock
114   private HealingManager healingManagerMock;
115   @Mock
116   private VendorLicenseArtifactsService licenseArtifactsServiceMock;
117   @Mock
118   private CompositionEntityDataManager compositionEntityDataManagerMock;
119   @Mock
120   private InformationArtifactGenerator informationArtifactGeneratorMock;
121   @Mock
122   private PackageInfoDao packageInfoDao;
123   @Mock
124   private VendorSoftwareProductInfoDao vspInfoDaoMock;
125   @Mock
126   private ManualVspToscaManager manualVspToscaManager;
127   @Mock
128   private DeploymentFlavorDao deploymentFlavorDaoMock;
129
130
131   @Spy
132   @InjectMocks
133   private VendorSoftwareProductManagerImpl vendorSoftwareProductManager;
134
135   @Mock
136   private OrchestrationTemplateCandidateManager orchestrationTemplateCandidateManagerMock;
137   @Mock
138   private ComponentDependencyModelDao componentDependencyModelDao;
139   private OrchestrationTemplateCandidateManager candidateManager;
140   private MonitoringUploadsManager monitoringUploadsManager;
141
142   @Captor
143   private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
144
145   @BeforeMethod
146   public void setUp() {
147     MockitoAnnotations.initMocks(this);
148   }
149
150   @AfterMethod
151   public void tearDown() {
152     vendorSoftwareProductManager = null;
153   }
154
155
156   @Test
157   public void testCreate() {
158     //doReturn(VERSION01).when(versioningManagerMock).create(anyObject(), anyObject(), anyObject());
159     doReturn("{}")
160         .when(vendorSoftwareProductManager).getVspQuestionnaireSchema(anyObject());
161
162     VspDetails vspToCreate =
163         createVspDetails(null, null, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon",
164             "category", "subCategory", "123", null);
165
166     VspDetails vsp = vendorSoftwareProductManager.createVsp(vspToCreate);
167
168     Assert.assertNotNull(vsp);
169     vspToCreate.setId(vsp.getId());
170     vspToCreate.setVersion(VERSION01);
171     assertVspsEquals(vsp, vspToCreate);
172   }
173
174   @Test(expectedExceptions = CoreException.class)
175   public void testUpdateWithExistingName_negative() {
176     VersionInfo versionInfo = new VersionInfo();
177     versionInfo.setActiveVersion(VERSION01);
178     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
179         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
180         VersionableEntityAction.Write);
181
182     VspDetails existingVsp =
183         createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-existingVsp", "vendorName", "vlm1Id",
184             "icon", "category", "subCategory", "123", null);
185     VspDetails updatedVsp =
186         createVspDetails(VSP_ID, VERSION01, "Vsp1_updated", "Test-existingVsp", "vendorName",
187             "vlm1Id", "icon", "category", "subCategory", "123", null);
188     doReturn(existingVsp).when(vspInfoDaoMock)
189         .get(any(VspDetails.class));
190     doThrow(new CoreException(
191         new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).build()))
192         .when(vendorSoftwareProductManager)
193         .updateUniqueName(existingVsp.getName(), updatedVsp.getName());
194
195     vendorSoftwareProductManager.updateVsp(updatedVsp);
196   }
197
198   @Test
199   public void testUpdate() {
200     VersionInfo versionInfo = new VersionInfo();
201     versionInfo.setActiveVersion(VERSION01);
202     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
203         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
204         VersionableEntityAction.Write);
205     VspDetails existingVsp =
206         createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
207             "category",
208             "subCategory", "456", null);
209     VspDetails updatedVsp =
210         createVspDetails(VSP_ID, VERSION01, "VSP1_updated", null, "vendorName", "vlm1Id", "icon",
211             "category_updated",
212             "subCategory", "456", null);
213     doReturn(existingVsp).when(vspInfoDaoMock)
214         .get(any(VspDetails.class));
215     doNothing().when(vendorSoftwareProductManager)
216         .updateUniqueName(existingVsp.getName(), updatedVsp.getName());
217
218     vendorSoftwareProductManager.updateVsp(updatedVsp);
219
220     verify(vspInfoDaoMock).update(updatedVsp);
221   }
222
223   @Test
224   public void testUpdateRemoveFG() {
225     VersionInfo versionInfo = new VersionInfo();
226     versionInfo.setActiveVersion(VERSION01);
227     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
228         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
229         VersionableEntityAction.Write);
230     List<String> fgs = new ArrayList<>();
231     fgs.add("fg1");
232     fgs.add("fg2");
233     VspDetails existingVsp =
234         createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
235             "category",
236             "subCategory", "456", fgs);
237
238     List<String> updFgs = new ArrayList<>();
239     //updFgs.add("fg2");
240     VspDetails updatedVsp =
241         createVspDetails(VSP_ID, VERSION01, "VSP1_updated", null, "vendorName", "vlm1Id", "icon",
242             "category_updated",
243             "subCategory", "456", updFgs);
244     doReturn(existingVsp).when(vspInfoDaoMock)
245         .get(any(VspDetails.class));
246     doNothing().when(vendorSoftwareProductManager)
247         .updateUniqueName(existingVsp.getName(), updatedVsp.getName());
248
249     DeploymentFlavorEntity dfEntity = new DeploymentFlavorEntity(VSP_ID, VERSION01, "DF_ID");
250     DeploymentFlavor flavor = new DeploymentFlavor();
251     flavor.setFeatureGroupId("fg1");
252     dfEntity.setDeploymentFlavorCompositionData(flavor);
253
254     List<DeploymentFlavorEntity> dfList = new ArrayList<>();
255     dfList.add(dfEntity);
256
257     doReturn(dfList).when(deploymentFlavorDaoMock).list(anyObject());
258
259     vendorSoftwareProductManager.updateVsp(updatedVsp);
260
261     verify(deploymentFlavorDaoMock).update(dfEntity);
262
263     Assert.assertNull(dfEntity.getDeploymentFlavorCompositionData().getFeatureGroupId());
264
265   }
266
267   @Test(expectedExceptions = CoreException.class)
268   public void testGetNonExistingVersion_negative() {
269     Version notExistversion = new Version("43, 8");
270     doReturn(null).when(vspInfoDaoMock).get(any(VspDetails.class));
271     vendorSoftwareProductManager.getVsp(VSP_ID, notExistversion);
272   }
273
274   @Test
275   public void testGetCheckedOutVersion() {
276     VersionInfo versionInfo = new VersionInfo();
277     versionInfo.setActiveVersion(VERSION01);
278     versionInfo.setStatus(VersionStatus.Locked);
279     versionInfo.setLockingUser(USER1);
280     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
281         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
282         VersionableEntityAction.Read);
283
284     VspDetails existingVsp =
285         createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
286             "category",
287             "subCategory", "456", null);
288     doReturn(existingVsp).when(vspInfoDaoMock).get(any(VspDetails.class));
289
290     VspDetails actualVsp =
291         vendorSoftwareProductManager.getVsp(VSP_ID, VERSION01);
292
293     assertVspsEquals(actualVsp, existingVsp);
294   }
295
296   @Test
297   public void testGetOldVersion() {
298     VersionInfo versionInfo = new VersionInfo();
299     versionInfo.setActiveVersion(new Version("0, 2"));
300     versionInfo.setViewableVersions(Arrays.asList(VERSION01, new Version("0, 2")));
301     versionInfo.setStatus(VersionStatus.Locked);
302     versionInfo.setLockingUser(USER2);
303     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
304         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
305         VersionableEntityAction.Read);
306
307     VspDetails existingVsp =
308         createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
309             "category",
310             "subCategory", "456", null);
311     doReturn(existingVsp)
312         .when(vspInfoDaoMock).get(any(VspDetails.class));
313
314     VspDetails actualVsp =
315         vendorSoftwareProductManager.getVsp(VSP_ID, VERSION01);
316
317     VspDetails expectedVsp =
318         vspInfoDaoMock
319             .get(new VspDetails(VSP_ID, VERSION01));
320     assertVspsEquals(actualVsp, expectedVsp);
321   }
322
323 /*
324   @Test
325   public void testSubmitWithMissingData() throws IOException {
326     VersionInfo versionInfo = new VersionInfo();
327     versionInfo.setActiveVersion(VERSION01);
328
329     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
330         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
331         VSP_ID, USER1, VersionableEntityAction.Read);
332
333     VspDetails vsp = new VspDetails(VSP_ID, VERSION01);
334     vsp.setOnboardingMethod("Manual");
335     doReturn(vsp).when(vspInfoDaoMock).get(anyObject());
336
337     VspQuestionnaireEntity vspQuestionnaire = new VspQuestionnaireEntity(VSP_ID, VERSION01);
338     vspQuestionnaire.setQuestionnaireData("{}");
339     doReturn(vspQuestionnaire).when(vspInfoDaoMock).getQuestionnaire(VSP_ID, VERSION01);
340
341     ComponentEntity comp1 = new ComponentEntity(VSP_ID, VERSION01, "comp1");
342     comp1.setQuestionnaireData("{}");
343     doReturn(Collections.singleton(comp1)).when(vendorSoftwareProductDaoMock)
344         .listComponentsCompositionAndQuestionnaire(VSP_ID, VERSION01);
345
346     NicEntity nic1 = new NicEntity(VSP_ID, VERSION01, "comp1", "nic1");
347     nic1.setQuestionnaireData("{}");
348     doReturn(Collections.singleton(nic1))
349         .when(vendorSoftwareProductDaoMock).listNicsByVsp(VSP_ID, VERSION01);
350
351     ValidationResponse validationResponse = vendorSoftwareProductManager.submit(VSP_ID, USER1);
352     Assert.assertNotNull(validationResponse);
353     Assert.assertFalse(validationResponse.isValid());
354     List<String> errorIds = validationResponse.getVspErrors().stream().map(ErrorCode::id).distinct()
355         .collect(Collectors.toList());
356     Assert.assertTrue(errorIds.contains(ValidationErrorBuilder.FIELD_VALIDATION_ERROR_ERR_ID));
357     Assert.assertTrue(errorIds.contains(VendorSoftwareProductErrorCodes.VSP_INVALID));
358
359     verify(versioningManagerMock, never())
360         .submit(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
361             USER1, null);
362     verify(activityLogManagerMock, never()).addActionLog(any(ActivityLogEntity.class), eq(USER1));
363   }
364
365   */
366
367   // TODO: 3/15/2017 fix and enable
368   //@Test
369   public void testSubmitWithInvalidLicensingData() throws IOException {
370     VersionInfo versionInfo = new VersionInfo();
371     versionInfo.setActiveVersion(VERSION01);
372     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
373         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
374         VSP_ID, USER1, VersionableEntityAction.Read);
375
376     VspDetails vsp =
377         createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon",
378             "category", "subCategory", "licenseAgreementId",
379             Collections.singletonList("featureGroupId"));
380     doReturn(vsp).when(vspInfoDaoMock).get(anyObject());
381     OrchestrationTemplateEntity uploadData = new OrchestrationTemplateEntity(VSP_ID, VERSION01);
382     uploadData.setContentData(
383         ByteBuffer.wrap(FileUtils.toByteArray(getFileInputStream("/emptyComposition"))));
384     doReturn(uploadData).when(orchestrationTemplateDataDaoMock)
385         .get(anyObject(), anyObject());
386     doReturn(new ToscaServiceModel(new FileContentHandler(), new HashMap<>(),
387         "MainServiceTemplate.yaml"))
388         .when(serviceModelDaoMock).getServiceModel(VSP_ID, VERSION01);
389
390     ValidationResponse validationResponse =
391         vendorSoftwareProductManager.validate(VSP_ID, VERSION01);
392     Assert.assertNotNull(validationResponse);
393     Assert.assertFalse(validationResponse.isValid());
394     Assert.assertNull(validationResponse.getVspErrors());
395     Assert.assertEquals(validationResponse.getLicensingDataErrors().size(), 1);
396
397     verify(versioningManagerMock, never())
398         .submit(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
399             USER1, null);
400   }
401
402   // TODO: 3/15/2017 fix and enable
403   //@Test
404   public void testSubmit() throws IOException {
405     mockVersioning(VersionableEntityAction.Read);
406
407     EnrichmentManagerFactory.getInstance();
408     AbstractFactoryBase
409         .registerFactory(EnrichmentManagerFactory.class, EnrichmentManagerFactoryImpl.class);
410
411     VspDetails vsp =
412         createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon",
413             "category", "subCategory", "123", Collections.singletonList("fg1"));
414     doReturn(vsp).when(vspInfoDaoMock).get(anyObject());
415     OrchestrationTemplateEntity uploadData = new OrchestrationTemplateEntity(VSP_ID, VERSION01);
416     uploadData.setContentData(
417         ByteBuffer.wrap(FileUtils.toByteArray(getFileInputStream("/emptyComposition"))));
418     doReturn(uploadData).when(orchestrationTemplateDataDaoMock)
419         .get(anyObject(), anyObject());
420     doReturn(new ToscaServiceModel(new FileContentHandler(), new HashMap<>(),
421         "MainServiceTemplate.yaml"))
422         .when(serviceModelDaoMock).getServiceModel(VSP_ID, VERSION01);
423
424     ValidationResponse validationResponse =
425         vendorSoftwareProductManager.validate(VSP_ID, VERSION01);
426     Assert.assertTrue(validationResponse.isValid());
427
428 /*    Assert.assertEquals(vsp2.getVersionInfo().getVersion(), VERSION10);
429     Assert.assertEquals(vsp2.getVersionInfo().getStatus(), VersionStatus.Certified);
430     Assert.assertNull(vsp2.getVersionInfo().getLockingUser());*/
431
432     verify(versioningManagerMock)
433         .submit(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
434             USER1, null);
435   }
436
437   @Test
438   public void testCreatePackage() {
439     /*VspDetails vspDetailsMock = new VspDetails("vspId", new Version(1, 0));
440     doReturn(vspDetailsMock).when(vspInfoDaoMock).get(anyObject());*/
441     VersionInfo versionInfo = new VersionInfo();
442     versionInfo.setActiveVersion(VERSION10);
443     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
444         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
445         VersionableEntityAction.Read);
446
447     doReturn(new ToscaServiceModel(new FileContentHandler(), new HashMap<>(), "")).when
448         (enrichedServiceModelDaoMock).getServiceModel(VSP_ID, VERSION10);
449     doNothing().when(vendorSoftwareProductManager).populateVersionsForVlm(anyObject(), anyObject());
450     VspDetails vsp = new VspDetails(VSP_ID, VERSION10);
451     vsp.setVendorId("vendorId");
452     vsp.setVlmVersion(VERSION10);
453     vsp.setFeatureGroups(Arrays.asList("fg1", "fg2"));
454     doReturn(vsp).when(vspInfoDaoMock).get(any(VspDetails.class));
455
456     doReturn(new FileContentHandler()).when(licenseArtifactsServiceMock)
457         .createLicenseArtifacts(VSP_ID, vsp.getVendorId(), VERSION10, vsp.getFeatureGroups()
458         );
459
460     PackageInfo packageInfo = vendorSoftwareProductManager.createPackage(VSP_ID, VERSION10);
461     Assert.assertNotNull(packageInfo.getVspId());
462   }
463
464   // TODO: 3/15/2017 fix and enable
465   //@Test(dependsOnMethods = {"testListFinals"})
466   public void testUploadFileMissingFile() throws IOException {
467     try (InputStream zis = getFileInputStream("/vspmanager/zips/missingYml.zip")) {
468
469       UploadFileResponse uploadFileResponse =
470           candidateManager.upload(VSP_ID, VERSION01, zis, "zip", "file");
471
472       Assert.assertEquals(uploadFileResponse.getErrors().size(), 0);
473     }
474   }
475
476   // TODO: 3/15/2017 fix and enable
477   //@Test(dependsOnMethods = {"testUploadFileMissingFile"})
478   public void testUploadNotZipFile() {
479     URL url = this.getClass().getResource("/notZipFile");
480
481     try {
482       candidateManager.upload(VSP_ID, VERSION01, url.openStream(), "zip", "file");
483       candidateManager.process(VSP_ID, VERSION01);
484     } catch (Exception ce) {
485       Assert.assertEquals(ce.getMessage(), Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage());
486     }
487   }
488
489   private List<String> getWantedFileNamesFromCsar(String pathInCsar)
490       throws IOException {
491     File translatedFile = vendorSoftwareProductManager.getTranslatedFile(VSP_ID, VERSION10);
492
493     return getFileNamesFromFolderInCsar(translatedFile,
494         pathInCsar);
495   }
496
497   private List<String> getFileNamesFromFolderInCsar(File csar, String folderName)
498       throws IOException {
499     List<String> fileNames = new ArrayList<>();
500
501     try (ZipInputStream zip = new ZipInputStream(new FileInputStream(csar))) {
502       ZipEntry ze;
503
504       while ((ze = zip.getNextEntry()) != null) {
505         String name = ze.getName();
506         if (name.contains(folderName)) {
507           fileNames.add(name);
508         }
509       }
510     }
511
512     return fileNames;
513   }
514   /*
515   //Disabled for sonar null pointer issue for componentEntities
516   private Pair<String, String> uploadMib(String vspId, String user, String filePath,
517                                          String fileName) {
518     List<ComponentEntity> componentEntities = null;
519     //(List<ComponentEntity>) vendorSoftwareProductManager.listComponents(vspId, null, user);
520     monitoringUploadsManager.upload(getFileInputStream(filePath),
521         fileName, vspId,
522 <<<<<<< HEAD
523         VERSION01, componentEntities.get(0).getId(), ArtifactType.SNMP_POLL);
524     //TODO: add validate of logActivity() func call
525 =======
526         VERSION01, componentEntities.get(0).getId(), MonitoringUploadType.SNMP_POLL, user);
527     //TODO: add validate of addActionLog() func call
528 >>>>>>> feature/Amdocs-ASDC-1710
529
530     return new ImmutablePair<>(componentEntities.get(0).getId(),
531         componentEntities.get(0).getComponentCompositionData()
532             .getDisplayName());
533   }*/
534
535   // TODO: 3/15/2017 fix and enable
536 /*
537
538   public void testUpdatedVSPShouldBeInBeginningOfList() {
539     vendorSoftwareProductManager.updateVsp(new VspDetails(), USER3);
540     assertVSPInWantedLocationInVSPList(id006, 0, USER3);
541
542     InputStream zis = getFileInputStream("/vspmanager/zips/fullComposition.zip");
543     candidateManager.upload(id007, VERSION01, zis, USER3);
544     candidateManager.process(id007, VERSION01, USER3);
545     assertVSPInWantedLocationInVSPList(id007, 0, USER3);
546   }
547
548   @Test(dependsOnMethods = {"testUpdatedVSPShouldBeInBeginningOfList"})
549   public void testVSPInBeginningOfListAfterCheckin() {
550     vendorSoftwareProductManager.checkin(id006, USER3);
551     assertVSPInWantedLocationInVSPList(id006, 0, USER3);
552
553     vendorSoftwareProductManager.checkin(id007, USER3);
554     assertVSPInWantedLocationInVSPList(id007, 0, USER3);
555   }
556
557   @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterCheckin"})
558   public void testVSPInBeginningOfListAfterCheckout() {
559     vendorSoftwareProductManager.checkout(id006, USER3);
560     assertVSPInWantedLocationInVSPList(id006, 0, USER3);
561   }
562
563   @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterCheckout"})
564   public void testVSPInBeginningOfListAfterUndoCheckout() {
565     vendorSoftwareProductManager.checkout(id007, USER3);
566     assertVSPInWantedLocationInVSPList(id007, 0, USER3);
567
568     vendorSoftwareProductManager.undoCheckout(id006, USER3);
569     assertVSPInWantedLocationInVSPList(id006, 0, USER3);
570   }
571
572   @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterUndoCheckout"})
573   public void testVSPInBeginningOfListAfterSubmit() throws IOException {
574     vendorSoftwareProductManager.checkin(id007, USER3);
575     vendorSoftwareProductManager.submit(id007, USER3);
576
577     assertVSPInWantedLocationInVSPList(id007, 0, USER3);
578   }
579 */
580   @Test
581   public void testValidateWithCandidateDataNotProcessed() throws IOException {
582     VspDetails vsp =
583         createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon",
584             "category", "subCategory", "licenseAgreementId",
585             Collections.singletonList("featureGroupId"));
586     vsp.setOnboardingMethod("NetworkPackage");
587     doReturn(vsp).when(vspInfoDaoMock).get(anyObject());
588
589     OrchestrationTemplateCandidateData orchestrationTemplateCandidateData = new
590         OrchestrationTemplateCandidateData();
591     orchestrationTemplateCandidateData.setFileSuffix("zip");
592     orchestrationTemplateCandidateData.setFilesDataStructure("testdata");
593     orchestrationTemplateCandidateData.setValidationData("");
594     doReturn(Optional.of(orchestrationTemplateCandidateData))
595         .when(orchestrationTemplateCandidateManagerMock)
596         .getInfo(VSP_ID, VERSION01);
597     ValidationResponse validationResponse =
598         vendorSoftwareProductManager.validate(VSP_ID, VERSION01);
599     Assert.assertNotNull(validationResponse);
600     Assert.assertFalse(validationResponse.isValid());
601     Assert.assertNotNull(validationResponse.getVspErrors());
602     Assert.assertEquals(validationResponse.getVspErrors().size(), 1);
603
604   }
605
606   @Test
607   public void testValidateWithCandidateProcessedIsInvalid() throws IOException {
608     VspDetails vsp = createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-VSP", "vendorName",
609         "vl1Id", "icond", "category", "subcategory", "licenseAgreementId", Collections
610             .singletonList("featureGroupId"));
611     vsp.setOnboardingMethod("NetworkPackage");
612     doReturn(vsp).when(vspInfoDaoMock).get(anyObject());
613
614     OrchestrationTemplateCandidateData orchestrationTemplateCandidateData = new
615         OrchestrationTemplateCandidateData();
616     orchestrationTemplateCandidateData.setFileSuffix("zip");
617     orchestrationTemplateCandidateData.setValidationData("Invalid processed data");
618     doReturn(Optional.of(orchestrationTemplateCandidateData))
619         .when(orchestrationTemplateCandidateManagerMock)
620         .getInfo(VSP_ID, VERSION01);
621     ValidationResponse validationResponse =
622         vendorSoftwareProductManager.validate(VSP_ID, VERSION01);
623     Assert.assertNotNull(validationResponse);
624     Assert.assertFalse(validationResponse.isValid());
625     Assert.assertNotNull(validationResponse.getVspErrors());
626     Assert.assertEquals(validationResponse.getVspErrors().size(), 1);
627   }
628
629   private void testLegalUpload(String vspId, Version version, InputStream upload, String user) {
630     candidateManager.upload(vspId, VERSION01, upload, "zip", "file");
631     candidateManager.process(vspId, VERSION01);
632
633     OrchestrationTemplateEntity uploadData =
634         orchestrationTemplateDataDaoMock.get(vspId, version);
635     Assert.assertNotNull(uploadData);
636   }
637
638
639   private InputStream getFileInputStream(String fileName) {
640     URL url = this.getClass().getResource(fileName);
641     try {
642       return url.openStream();
643     } catch (IOException exception) {
644       exception.printStackTrace();
645       return null;
646     }
647   }
648
649   private static VspDetails createVspDetails(String id, Version version, String name, String desc,
650                                      String vendorName, String vlm, String icon,
651                                      String category, String subCategory,
652                                      String licenseAgreement, List<String> featureGroups) {
653     VspDetails vspDetails = new VspDetails(id, version);
654     vspDetails.setName(name);
655     vspDetails.setDescription(desc);
656     vspDetails.setIcon(icon);
657     vspDetails.setCategory(category);
658     vspDetails.setSubCategory(subCategory);
659     vspDetails.setVendorName(vendorName);
660     vspDetails.setVendorId(vlm);
661     vspDetails.setVlmVersion(new Version("1, 0"));
662     vspDetails.setLicenseAgreement(licenseAgreement);
663     vspDetails.setFeatureGroups(featureGroups);
664     vspDetails.setOnboardingMethod("HEAT");
665     return vspDetails;
666   }
667
668   private static void assertVspsEquals(VspDetails actual, VspDetails expected) {
669     Assert.assertEquals(actual.getId(), expected.getId());
670     Assert.assertEquals(actual.getVersion(), expected.getVersion());
671     Assert.assertEquals(actual.getName(), expected.getName());
672     Assert.assertEquals(actual.getDescription(), expected.getDescription());
673     Assert.assertEquals(actual.getIcon(), expected.getIcon());
674     Assert.assertEquals(actual.getCategory(), expected.getCategory());
675     Assert.assertEquals(actual.getSubCategory(), expected.getSubCategory());
676     Assert.assertEquals(actual.getVendorName(), expected.getVendorName());
677     Assert.assertEquals(actual.getVendorId(), expected.getVendorId());
678     Assert.assertEquals(actual.getLicenseAgreement(), expected.getLicenseAgreement());
679     Assert.assertEquals(actual.getFeatureGroups(), expected.getFeatureGroups());
680   }
681
682
683   // todo ********************** move to common **************************************
684
685   private void mockVersioning(VersionableEntityAction action) {
686     VersionInfo versionInfo = new VersionInfo();
687     versionInfo.setActiveVersion(VERSION01);
688     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
689         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
690         action);
691   }
692
693 }