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