push addional code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / VendorSoftwareProductManagerTest.java
1 package org.openecomp.sdc.vendorsoftwareproduct;
2
3 import org.openecomp.sdc.common.errors.CoreException;
4 import org.openecomp.sdc.common.errors.ErrorCode;
5 import org.openecomp.sdc.common.errors.ValidationErrorBuilder;
6 import org.openecomp.sdc.common.utils.AsdcCommon;
7 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
8 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
9 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
10 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
11 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
12 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
13
14 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory;
15 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.PackageInfo;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadDataEntity;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
18 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
19 import org.openecomp.sdc.vendorsoftwareproduct.impl.VendorSoftwareProductManagerImpl;
20 import org.openecomp.sdc.vendorsoftwareproduct.tree.UploadFileTest;
21 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
22 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
23 import org.openecomp.sdc.vendorsoftwareproduct.types.VersionedVendorSoftwareProductInfo;
24 import org.openecomp.sdc.versioning.dao.types.Version;
25 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
26 import org.openecomp.core.model.dao.EnrichedServiceModelDaoFactory;
27 import org.openecomp.core.util.UniqueValueUtil;
28 import org.openecomp.core.utilities.CommonMethods;
29 import org.openecomp.core.validation.errors.Messages;
30 import org.apache.commons.collections4.MapUtils;
31 import org.apache.commons.io.IOUtils;
32 import org.testng.Assert;
33 import org.testng.annotations.BeforeTest;
34 import org.testng.annotations.Test;
35
36 import java.io.*;
37 import java.net.URL;
38 import java.util.*;
39 import java.util.stream.Collectors;
40
41 public class VendorSoftwareProductManagerTest {
42   public static final Version VERSION01 = new Version(0, 1);
43   public static final Version VERSION10 = new Version(1, 0);
44   private static final String USER1 = "vspTestUser1";
45   private static final String USER2 = "vspTestUser2";
46   private static final String USER3 = "vspTestUser3";
47   public static String id001 = null;
48   public static String id002 = null;
49   public static String id003 = null;
50   public static String id004 = null;
51   public static String id005 = null;
52   public static String id006 = null;
53   public static String id007 = null;
54   public static Version activeVersion002 = null;
55   private static VendorSoftwareProductManager vendorSoftwareProductManager =
56       new VendorSoftwareProductManagerImpl();
57   private static org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao
58       vendorSoftwareProductDao =
59       VendorSoftwareProductDaoFactory.getInstance().createInterface();
60   private static VendorLicenseFacade vendorLicenseFacade =
61       org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory.getInstance().createInterface();
62   private static String vlm1Id;
63   private static String licenseAgreementId;
64   private static String featureGroupId;
65   private static VspDetails vsp1;
66   private static VspDetails vsp2;
67   UploadFileTest ut = new UploadFileTest();
68
69   static void assertVspsEquals(VspDetails actual, VspDetails expected) {
70     Assert.assertEquals(actual.getId(), expected.getId());
71     Assert.assertEquals(actual.getVersion(), expected.getVersion());
72     Assert.assertEquals(actual.getName(), expected.getName());
73     Assert.assertEquals(actual.getDescription(), expected.getDescription());
74     Assert.assertEquals(actual.getIcon(), expected.getIcon());
75     Assert.assertEquals(actual.getCategory(), expected.getCategory());
76     Assert.assertEquals(actual.getSubCategory(), expected.getSubCategory());
77     Assert.assertEquals(actual.getVendorName(), expected.getVendorName());
78     Assert.assertEquals(actual.getVendorId(), expected.getVendorId());
79     Assert.assertEquals(actual.getLicenseAgreement(), expected.getLicenseAgreement());
80     Assert.assertEquals(actual.getFeatureGroups(), expected.getFeatureGroups());
81   }
82
83   @BeforeTest
84   private void init() {
85     UniqueValueUtil
86         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
87             "VSP1");
88     UniqueValueUtil
89         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
90             "VSP3");
91     UniqueValueUtil
92         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
93             "VSP4");
94     UniqueValueUtil
95         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
96             "VSP5");
97     UniqueValueUtil
98         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
99             "vsp1_test");
100     UniqueValueUtil
101         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
102             "vsp2_test");
103     createVlm();
104   }
105
106   private void createVlm() {
107     vlm1Id = vendorLicenseFacade.createVendorLicenseModel(VSPCommon
108             .createVendorLicenseModel("vlmName " + CommonMethods.nextUuId(), "vlm1Id desc", "icon1"),
109         USER1).getId();
110
111     String entitlementPoolId = vendorLicenseFacade
112         .createEntitlementPool(new EntitlementPoolEntity(vlm1Id, null, null), USER1).getId();
113
114     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity
115         featureGroup = new org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity(vlm1Id, null, null);
116     featureGroup.getEntitlementPoolIds().add(entitlementPoolId);
117     featureGroupId = vendorLicenseFacade.createFeatureGroup(featureGroup, USER1).getId();
118
119     LicenseAgreementEntity licenseAgreement = new LicenseAgreementEntity(vlm1Id, null, null);
120     licenseAgreement.getFeatureGroupIds().add(featureGroupId);
121     licenseAgreementId =
122         vendorLicenseFacade.createLicenseAgreement(licenseAgreement, USER1).getId();
123
124     vendorLicenseFacade.checkin(vlm1Id, USER1);
125     vendorLicenseFacade.submit(vlm1Id, USER1);
126   }
127
128   @Test
129   public void testHeatSet() {
130     Set<HeatStructureTree> set = new HashSet<>();
131     HeatStructureTree heatStructureTree1 = new HeatStructureTree();
132     HeatStructureTree heatStructureTree2 = new HeatStructureTree();
133
134     heatStructureTree1.setFileName("file");
135
136     HeatStructureTree env = new HeatStructureTree();
137     env.setFileName("env");
138     heatStructureTree1.setEnv(env);
139
140     heatStructureTree2.setFileName("file");
141     heatStructureTree2.setEnv(env);
142
143     set.add(heatStructureTree1);
144     set.add(heatStructureTree2);
145
146     Assert.assertEquals(set.size(), 1);
147   }
148
149   @Test(dependsOnMethods = {"testHeatSet"})
150   public void testCreateVSP() {
151     VspDetails expectedVsp = VSPCommon
152         .createVspDetails(null, null, "VSP1", "Test-vsp", "vendorName", vlm1Id, "icon", "category",
153             "subCategory", "123", null);
154
155     VspDetails createdVsp = vendorSoftwareProductManager.createNewVsp(expectedVsp, USER1);
156     id001 = createdVsp.getId();
157     Assert.assertNotNull(id001);
158     Assert.assertNotNull(createdVsp.getVersion());
159
160     VspDetails actualVsp =
161         vendorSoftwareProductDao.getVendorSoftwareProductInfo(new VspDetails(id001, VERSION01));
162     expectedVsp.setId(id001);
163     expectedVsp.setVersion(VERSION01);
164
165     assertVspsEquals(actualVsp, expectedVsp);
166     Assert.assertNotNull(
167         vendorSoftwareProductManager.getVspQuestionnaire(id001, null, USER1).getData());
168   }
169
170   @Test(dependsOnMethods = {"testCreateVSP"})
171   public void testCreateWithExistingName_negative() {
172     try {
173       VspDetails expectedVsp = VSPCommon
174           .createVspDetails(null, null, "Vsp1", "Test-vsp", "vendorName", vlm1Id, "icon",
175               "category", "subCategory", "123", null);
176       vendorSoftwareProductManager.createNewVsp(expectedVsp, USER1);
177       Assert.fail();
178     } catch (CoreException e) {
179       Assert.assertEquals(e.code().id(), UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
180     }
181   }
182
183   @Test(dependsOnMethods = {"testCreateWithExistingName_negative"})
184   public void testGetVSPDetails() {
185     VersionedVendorSoftwareProductInfo actualVsp =
186         vendorSoftwareProductManager.getVspDetails(id001, null, USER1);
187
188     VspDetails expectedVsp =
189         vendorSoftwareProductDao.getVendorSoftwareProductInfo(new VspDetails(id001, VERSION01));
190     assertVspsEquals(actualVsp.getVspDetails(), expectedVsp);
191     Assert.assertEquals(actualVsp.getVersionInfo().getActiveVersion(), VERSION01);
192     Assert.assertEquals(actualVsp.getVersionInfo().getStatus(), org.openecomp.sdc.versioning.dao.types.VersionStatus.Locked);
193     Assert.assertEquals(actualVsp.getVersionInfo().getLockingUser(), USER1);
194   }
195
196   @Test(dependsOnMethods = {"testGetVSPDetails"})
197   public void testUpdateVSP() {
198     VspDetails expectedVsp = VSPCommon
199         .createVspDetails(id001, VERSION01, "VSP1", null, "vendorName", vlm1Id, "icon", "category",
200             "subCategory", "456", null);
201     vendorSoftwareProductManager.updateVsp(expectedVsp, USER1);
202
203     VspDetails actualVsp =
204         vendorSoftwareProductDao.getVendorSoftwareProductInfo(new VspDetails(id001, VERSION01));
205
206     assertVspsEquals(actualVsp, expectedVsp);
207   }
208
209   @Test(dependsOnMethods = {"testUpdateVSP"})
210   public void testGetVSPDetailsAfterUpdate() {
211     VersionedVendorSoftwareProductInfo vspDetails =
212         vendorSoftwareProductManager.getVspDetails(id001, null, USER1);
213     Assert.assertEquals(vspDetails.getVspDetails().getName(), "VSP1");
214     Assert.assertEquals(vspDetails.getVspDetails().getCategory(), "category");
215     Assert.assertEquals(vspDetails.getVspDetails().getSubCategory(), "subCategory");
216     Assert.assertEquals(vspDetails.getVspDetails().getVendorId(), vlm1Id);
217     Assert.assertEquals(vspDetails.getVersionInfo().getActiveVersion(), VERSION01);
218     Assert.assertEquals(vspDetails.getVersionInfo().getStatus(), org.openecomp.sdc.versioning.dao.types.VersionStatus.Locked);
219     Assert.assertEquals(vspDetails.getVersionInfo().getLockingUser(), USER1);
220   }
221
222   @Test(dependsOnMethods = {"testGetVSPDetailsAfterUpdate"})
223   public void testGetVSPList() {
224     String licenseAgreementId = "bla bla";
225     VspDetails vspDetails = vendorSoftwareProductManager.createNewVsp(VSPCommon
226         .createVspDetails(null, null, "VSP3", "Test-vsp", "vendorName", vlm1Id, "icon", "category",
227             "subCategory", licenseAgreementId, null), USER1);
228     id002 = vspDetails.getId();
229     vspDetails = vendorSoftwareProductManager.createNewVsp(VSPCommon
230         .createVspDetails(null, null, "VSP4", "Test-vsp", "vendorName", vlm1Id, "icon", "category",
231             "subCategory", licenseAgreementId, null), USER1);
232     id003 = vspDetails.getId();
233
234     List<VersionedVendorSoftwareProductInfo> vspDetailsList =
235         vendorSoftwareProductManager.getVspList(null, USER1);
236     int foundCount = 0;
237     for (VersionedVendorSoftwareProductInfo vsp : vspDetailsList) {
238       if (vsp.getVspDetails().getId().equals(id001) || vsp.getVspDetails().getId().equals(id002) ||
239           vsp.getVspDetails().getId().equals(id003)) {
240         foundCount++;
241       }
242     }
243
244     Assert.assertEquals(foundCount, 3);
245   }
246
247   @Test(dependsOnMethods = {"testGetVSPList"})
248   // Unsupported operation for 1607 release.
249 /*    public void testDeleteVSP() {
250         vendorSoftwareProductManager.deleteVsp(id001, USER1);
251
252         VspDetails vspDetails = vendorSoftwareProductDao.getVendorSoftwareProductInfo(new VspDetails(id001, VERSION01));
253         Assert.assertNull(vspDetails);
254
255         List<VersionedVendorSoftwareProductInfo> vspDetailsList = vendorSoftwareProductManager.getVspList(null, USER1);
256         boolean found001 = false;
257         for (VersionedVendorSoftwareProductInfo vsp : vspDetailsList) {
258             if (vsp.getVspDetails().getId().equals(id001)) {
259                 found001 = true;
260             }
261         }
262
263         Assert.assertFalse(found001);
264     }
265
266
267     @Test(dependsOnMethods = {"testDeleteVSP"})*/
268   public void testCheckin() {
269     vendorSoftwareProductManager.checkin(id002, USER1);
270
271     VersionedVendorSoftwareProductInfo vsp2 =
272         vendorSoftwareProductManager.getVspDetails(id002, null, USER1);
273     Assert.assertEquals(vsp2.getVersionInfo().getActiveVersion(), VERSION01);
274     Assert.assertEquals(vsp2.getVersionInfo().getStatus(), org.openecomp.sdc.versioning.dao.types.VersionStatus.Available);
275     Assert.assertNull(vsp2.getVersionInfo().getLockingUser());
276   }
277
278   @Test(dependsOnMethods = {"testCheckin"})
279   public void testCheckout() {
280     vendorSoftwareProductManager.checkout(id002, USER2);
281
282     VersionedVendorSoftwareProductInfo vsp2 =
283         vendorSoftwareProductManager.getVspDetails(id002, null, USER2);
284     Assert.assertEquals(vsp2.getVersionInfo().getActiveVersion(), new Version(0, 2));
285     Assert.assertEquals(vsp2.getVersionInfo().getStatus(), org.openecomp.sdc.versioning.dao.types.VersionStatus.Locked);
286     Assert.assertEquals(vsp2.getVersionInfo().getLockingUser(), USER2);
287
288     vsp2 = vendorSoftwareProductManager.getVspDetails(id002, null, USER1);
289     Assert.assertEquals(vsp2.getVersionInfo().getActiveVersion(), VERSION01);
290     Assert.assertEquals(vsp2.getVersionInfo().getStatus(), org.openecomp.sdc.versioning.dao.types.VersionStatus.Locked);
291     Assert.assertEquals(vsp2.getVersionInfo().getLockingUser(), USER2);
292   }
293
294   @Test(dependsOnMethods = {"testCheckout"})
295   public void testUndoCheckout() {
296     vendorSoftwareProductManager.undoCheckout(id002, USER2);
297
298     VersionedVendorSoftwareProductInfo vsp2 =
299         vendorSoftwareProductManager.getVspDetails(id002, null, USER2);
300     Assert.assertEquals(vsp2.getVersionInfo().getActiveVersion(), VERSION01);
301     Assert.assertEquals(vsp2.getVersionInfo().getStatus(), org.openecomp.sdc.versioning.dao.types.VersionStatus.Available);
302     Assert.assertNull(vsp2.getVersionInfo().getLockingUser());
303   }
304
305   @Test(dependsOnMethods = {"testUndoCheckout"})
306   public void testListFinalVspsWhenNone() {
307     List<VersionedVendorSoftwareProductInfo> vspDetailsList =
308         vendorSoftwareProductManager.getVspList(
309             org.openecomp.sdc.versioning.dao.types.VersionStatus.Final.name(), USER1);
310     int nonFinalFoundCount = 0;
311     for (VersionedVendorSoftwareProductInfo vsp : vspDetailsList) {
312       if (vsp.getVspDetails().getId().equals(id001) || vsp.getVspDetails().getId().equals(id002) ||
313           vsp.getVspDetails().getId().equals(id003)) {
314         nonFinalFoundCount++;
315       }
316     }
317
318     Assert.assertEquals(nonFinalFoundCount, 0);
319   }
320
321   @Test(dependsOnMethods = "testListFinalVspsWhenNone")
322   public void testSubmitWithoutLicencingData() throws IOException {
323     ValidationResponse validationResponse = vendorSoftwareProductManager.submit(id002, USER2);
324     Assert.assertNotNull(validationResponse);
325     Assert.assertFalse(validationResponse.isValid());
326     List<String> errorIds = validationResponse.getVspErrors().stream().map(ErrorCode::id).distinct()
327         .collect(Collectors.toList());
328     Assert.assertTrue(errorIds.contains(ValidationErrorBuilder.FIELD_VALIDATION_ERROR_ERR_ID));
329     Assert.assertTrue(errorIds.contains(VendorSoftwareProductErrorCodes.VSP_INVALID));
330   }
331
332   @Test(dependsOnMethods = {"testSubmitWithoutLicencingData"})
333   public void testSubmitWithoutUploadData() throws IOException {
334     vendorSoftwareProductManager.checkout(id002, USER2);
335
336     VspDetails updatedVsp2 =
337         vendorSoftwareProductManager.getVspDetails(id002, null, USER2).getVspDetails();
338     updatedVsp2.setFeatureGroups(new ArrayList<>());
339     updatedVsp2.getFeatureGroups().add(featureGroupId);
340     updatedVsp2.setLicenseAgreement(licenseAgreementId);
341
342     vendorSoftwareProductManager.updateVsp(updatedVsp2, USER2);
343     activeVersion002 = vendorSoftwareProductManager.checkin(id002, USER2);
344
345     ValidationResponse validationResponse = vendorSoftwareProductManager.submit(id002, USER2);
346     Assert.assertNotNull(validationResponse);
347     Assert.assertFalse(validationResponse.isValid());
348     Assert.assertTrue(validationResponse.getVspErrors().size() > 0);
349   }
350
351   @Test(dependsOnMethods = {"testSubmitWithoutUploadData"})
352   public void testUploadFile() throws IOException {
353     activeVersion002 = vendorSoftwareProductManager.checkout(id002, USER1);
354     testLegalUpload(id002, activeVersion002,
355         getFileInputStream("/vspmanager/zips/emptyComposition.zip"), USER1);
356   }
357
358 /*    @Test(dependsOnMethods = {"testUploadFile"})
359     public void testUploadFile2() throws IOException {
360         testLegalUpload(id002, activeVersion002, ut.getZipInputStream("/legalUpload2"), USER1);
361     }*/
362
363   @Test
364   public void testDownloadFile() throws IOException {
365     VspDetails expectedVsp = VSPCommon
366         .createVspDetails(null, null, String.format("VSP-test-%s", vlm1Id), "Test-vsp",
367             "vendorName", vlm1Id, "icon", "category", "subCategory", "123", null);
368     VspDetails createdVsp = vendorSoftwareProductManager.createNewVsp(expectedVsp, USER1);
369
370     id005 = createdVsp.getId();
371     Assert.assertNotNull(id005);
372     Assert.assertNotNull(createdVsp.getVersion());
373
374     //InputStream zipInputStream = getFileInputStream("/legalUpload/zip/legalUpload.zip")
375     try (InputStream zipInputStream = ut.getZipInputStream("/legalUpload")) {
376
377       UploadFileResponse resp =
378           vendorSoftwareProductManager.uploadFile(id005, zipInputStream, USER1);
379       File latestHeatPackage = vendorSoftwareProductManager.getLatestHeatPackage(id005, USER1);
380
381       zipInputStream.reset();
382       byte[] uploaded = IOUtils.toByteArray(zipInputStream);
383
384       byte[] downloaded;
385       try (BufferedInputStream fileStream = new BufferedInputStream(
386           new FileInputStream(latestHeatPackage))) {
387         downloaded = IOUtils.toByteArray(fileStream);
388       }
389
390       Assert.assertTrue(Arrays.equals(uploaded, downloaded));
391     }
392   }
393
394   @Test(dependsOnMethods = {"testUploadFile"})
395   public void testUploadNotExistingFile() throws IOException {
396     URL url = this.getClass().getResource("notExist.zip");
397     testLegalUpload(id002, activeVersion002, url == null ? null : url.openStream(), USER1);
398   }
399
400   @Test(dependsOnMethods = {"testUploadFile"}, expectedExceptions = CoreException.class)
401   public void negativeTestCreatePackageBeforeSubmit() throws IOException {
402     vendorSoftwareProductManager.createPackage(id002, USER1);
403   }
404
405   @Test(dependsOnMethods = {"negativeTestCreatePackageBeforeSubmit"})
406   public void negativeTestGetVSPDetailsNonExistingVersion() {
407     try {
408       vendorSoftwareProductManager.getVspDetails(id002, new Version(43, 8), USER1);
409       Assert.assertTrue(false);
410     } catch (CoreException e) {
411       Assert.assertEquals(e.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
412     }
413   }
414
415   @Test(dependsOnMethods = {"negativeTestCreatePackageBeforeSubmit"})
416   public void negativeTestGetVSPDetailsCheckoutByOtherVersion() {
417     try {
418       vendorSoftwareProductManager.getVspDetails(id002, activeVersion002, USER2);
419       Assert.assertTrue(false);
420     } catch (CoreException e) {
421       Assert.assertEquals(e.code().id(), VersioningErrorCodes.REQUESTED_VERSION_INVALID);
422     }
423   }
424
425   @Test(dependsOnMethods = {"negativeTestCreatePackageBeforeSubmit"})
426   public void testGetVSPDetailsCandidateVersion() {
427     VersionedVendorSoftwareProductInfo actualVsp =
428         vendorSoftwareProductManager.getVspDetails(id002, new Version(0, 3), USER1);
429
430     VspDetails expectedVsp = vendorSoftwareProductDao
431         .getVendorSoftwareProductInfo(new VspDetails(id002, new Version(0, 3)));
432     assertVspsEquals(actualVsp.getVspDetails(), expectedVsp);
433     Assert.assertEquals(actualVsp.getVersionInfo().getActiveVersion(), new Version(0, 3));
434     Assert.assertEquals(actualVsp.getVersionInfo().getStatus(), org.openecomp.sdc.versioning.dao.types.VersionStatus.Locked);
435     Assert.assertEquals(actualVsp.getVersionInfo().getLockingUser(), USER1);
436   }
437
438   @Test(dependsOnMethods = {"negativeTestCreatePackageBeforeSubmit"})
439   public void testGetVSPDetailsOldVersion() {
440     VersionedVendorSoftwareProductInfo actualVsp =
441         vendorSoftwareProductManager.getVspDetails(id002, new Version(0, 1), USER2);
442
443     VspDetails expectedVsp = vendorSoftwareProductDao
444         .getVendorSoftwareProductInfo(new VspDetails(id002, new Version(0, 1)));
445     assertVspsEquals(actualVsp.getVspDetails(), expectedVsp);
446     Assert.assertEquals(actualVsp.getVersionInfo().getActiveVersion(), new Version(0, 2));
447     Assert.assertEquals(actualVsp.getVersionInfo().getStatus(), org.openecomp.sdc.versioning.dao.types.VersionStatus.Locked);
448     Assert.assertEquals(actualVsp.getVersionInfo().getLockingUser(), USER1);
449   }
450
451   @Test(dependsOnMethods = {"negativeTestGetVSPDetailsNonExistingVersion",
452       "negativeTestGetVSPDetailsCheckoutByOtherVersion", "testGetVSPDetailsCandidateVersion",
453       "testGetVSPDetailsOldVersion"})
454   public void testSubmit() throws IOException {
455     activeVersion002 = vendorSoftwareProductManager.checkin(id002, USER1);
456     ValidationResponse validationResponse = vendorSoftwareProductManager.submit(id002, USER1);
457     Assert.assertTrue(validationResponse.isValid());
458
459     VersionedVendorSoftwareProductInfo vsp2 =
460         vendorSoftwareProductManager.getVspDetails(id002, null, USER1);
461     Assert.assertEquals(vsp2.getVersionInfo().getActiveVersion(), VERSION10);
462     Assert.assertEquals(vsp2.getVersionInfo().getStatus(), org.openecomp.sdc.versioning.dao.types.VersionStatus.Final);
463     Assert.assertNull(vsp2.getVersionInfo().getLockingUser());
464   }
465
466   @Test(dependsOnMethods = {"testSubmit"})
467   public void testListFinalVspsWhenExist() {
468     List<VersionedVendorSoftwareProductInfo> vspDetailsList =
469         vendorSoftwareProductManager.getVspList(
470             org.openecomp.sdc.versioning.dao.types.VersionStatus.Final.name(), USER1);
471     int nonFinalFoundCount = 0;
472     boolean found002 = false;
473     for (VersionedVendorSoftwareProductInfo vsp : vspDetailsList) {
474       if (vsp.getVspDetails().getId().equals(id002)) {
475         found002 = true;
476       }
477       if (vsp.getVspDetails().getId().equals(id001) || vsp.getVspDetails().getId().equals(id003)) {
478         nonFinalFoundCount++;
479       }
480     }
481
482     Assert.assertEquals(nonFinalFoundCount, 0);
483     Assert.assertTrue(found002);
484   }
485
486   @Test(dependsOnMethods = {"testSubmit"})
487   public void testCreatePackage() throws IOException {
488     PackageInfo packageInfo = vendorSoftwareProductManager.createPackage(id002, USER1);
489     Assert.assertNotNull(packageInfo.getVspId());
490   }
491
492   @Test
493   public void testUploadFileWithoutManifest() {
494     InputStream zis = getFileInputStream("/vspmanager/zips/withoutManifest.zip");
495     VspDetails vspDetails = vendorSoftwareProductManager.createNewVsp(VSPCommon
496         .createVspDetails(null, null, "VSP5", "Test-vsp", "vendorName", vlm1Id, "icon", "category",
497             "subCategory", "456", null), USER1);
498     id004 = vspDetails.getId();
499
500     UploadFileResponse uploadFileResponse =
501         vendorSoftwareProductManager.uploadFile(id004, zis, USER1);
502
503     Assert.assertNotNull(uploadFileResponse.getErrors());
504     Assert.assertEquals(uploadFileResponse.getErrors().size(), 1);
505   }
506
507   @Test(dependsOnMethods = {"testUploadFileWithoutManifest"})
508   public void testUploadFileMissingFile() {
509     InputStream zis = getFileInputStream("/vspmanager/zips/missingYml.zip");
510
511     UploadFileResponse uploadFileResponse =
512         vendorSoftwareProductManager.uploadFile(id004, zis, USER1);
513
514     Assert.assertEquals(uploadFileResponse.getErrors().size(), 3);
515   }
516
517   @Test(dependsOnMethods = {"testUploadFileMissingFile"})
518   public void testUploadNotZipFile() throws IOException {
519     URL url = this.getClass().getResource("/notZipFile");
520     UploadFileResponse uploadFileResponse =
521         vendorSoftwareProductManager.uploadFile(id004, url.openStream(), USER1);
522
523     Assert.assertNotNull(uploadFileResponse.getErrors());
524     Assert.assertEquals(
525         uploadFileResponse.getErrors().get(AsdcCommon.UPLOAD_FILE).get(0).getMessage(),
526         Messages.INVALID_ZIP_FILE.getErrorMessage());
527   }
528
529   @Test
530   public void testEnrichModelInSubmit() {
531     UniqueValueUtil
532         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
533             "VSP_syb");
534     VspDetails vspDetails = vendorSoftwareProductManager.createNewVsp(VSPCommon
535         .createVspDetails(null, null, "VSP_syb", "Test-vsp_syb", "vendorName", vlm1Id, "icon",
536             "category", "subCategory", "456", null), USER1);
537     String id = vspDetails.getId();
538
539     //upload file
540     InputStream zis = getFileInputStream("/vspmanager/zips/fullComposition.zip");
541     UploadFileResponse uploadFileResponse = vendorSoftwareProductManager.uploadFile(id, zis, USER1);
542
543     //check in
544     vendorSoftwareProductManager.checkin(id, USER1);
545     //submit
546     try {
547       ValidationResponse result = vendorSoftwareProductManager.submit(id, USER1);
548     } catch (IOException e) {
549       Assert.fail();
550     }
551     VersionedVendorSoftwareProductInfo details =
552         vendorSoftwareProductManager.getVspDetails(id, null, USER1);
553     Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity> components = vendorSoftwareProductManager
554         .listComponents(id, details.getVersionInfo().getActiveVersion(), USER1);
555
556     ToscaServiceModel model =
557         (ToscaServiceModel) EnrichedServiceModelDaoFactory.getInstance().createInterface()
558             .getServiceModel(id, details.getVersionInfo().getActiveVersion());
559
560     Map<String, CapabilityDefinition> capabilities = new HashMap<>();
561     for (org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity component : components) {
562       model.getServiceTemplates().
563           entrySet().
564           stream().
565           filter(entryValue -> entryValue.getValue() != null &&
566               entryValue.getValue().getNode_types() != null &&
567               entryValue.getValue().
568                   getNode_types().
569                   containsKey(component.getComponentCompositionData().getName())).
570           forEach(entryValue -> entryValue.getValue().getNode_types().
571               values().
572               stream().
573               filter(type -> MapUtils.isNotEmpty(type.getCapabilities())).
574               forEach(type -> type.getCapabilities().
575                   entrySet().
576                   forEach(entry -> addCapability(entryValue.getKey(), capabilities, entry.getKey(),
577                       entry.getValue()))));
578
579     }
580
581     Assert.assertNotNull(capabilities);
582   }
583
584   @Test(dependsOnMethods = {"testEnrichModelInSubmit"})
585   public void testVSPListSortedByModificationTimeDescOreder() {
586     vsp1 = VSPCommon
587         .createVspDetails(null, null, "vsp1_test", "Test-vsp", "vendorName", vlm1Id, "icon",
588             "category", "subCategory", "123", null);
589     id006 = vendorSoftwareProductManager.createNewVsp(vsp1, USER3).getId();
590
591     vsp2 = VSPCommon
592         .createVspDetails(null, null, "vsp2_test", "Test-vsp", "vendorName", vlm1Id, "icon",
593             "category", "subCategory", "123", null);
594     id007 = vendorSoftwareProductManager.createNewVsp(vsp2, USER3).getId();
595
596     assertVSPInWantedLocationInVSPList(id007, 0, USER3);
597     assertVSPInWantedLocationInVSPList(id006, 1, USER3);
598   }
599
600   @Test(dependsOnMethods = {"testVSPListSortedByModificationTimeDescOreder"})
601   public void testUpdatedVSPShouldBeInBeginningOfList() {
602     vendorSoftwareProductManager.updateVsp(vsp1, USER3);
603     assertVSPInWantedLocationInVSPList(id006, 0, USER3);
604
605     vendorSoftwareProductManager
606         .uploadFile(id007, getFileInputStream("/vspmanager/zips/emptyComposition.zip"), USER3);
607     assertVSPInWantedLocationInVSPList(id007, 0, USER3);
608   }
609
610   @Test(dependsOnMethods = {"testUpdatedVSPShouldBeInBeginningOfList"})
611   public void testVSPInBeginningOfListAfterCheckin() {
612     vendorSoftwareProductManager.checkin(id006, USER3);
613     assertVSPInWantedLocationInVSPList(id006, 0, USER3);
614
615     vendorSoftwareProductManager.checkin(id007, USER3);
616     assertVSPInWantedLocationInVSPList(id007, 0, USER3);
617   }
618
619   @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterCheckin"})
620   public void testVSPInBeginningOfListAfterCheckout() {
621     vendorSoftwareProductManager.checkout(id006, USER3);
622     assertVSPInWantedLocationInVSPList(id006, 0, USER3);
623   }
624
625   @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterCheckout"})
626   public void testVSPInBeginningOfListAfterUndoCheckout() {
627     vendorSoftwareProductManager.checkout(id007, USER3);
628     assertVSPInWantedLocationInVSPList(id007, 0, USER3);
629
630     vendorSoftwareProductManager.undoCheckout(id006, USER3);
631     assertVSPInWantedLocationInVSPList(id006, 0, USER3);
632   }
633
634   @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterUndoCheckout"})
635   public void testVSPInBeginningOfListAfterSubmit() throws IOException {
636     vendorSoftwareProductManager.checkin(id007, USER3);
637     vendorSoftwareProductManager.submit(id007, USER3);
638
639     assertVSPInWantedLocationInVSPList(id007, 0, USER3);
640   }
641
642   private void testLegalUpload(String vspId, Version version, InputStream upload, String user) {
643     vendorSoftwareProductManager.uploadFile(vspId, upload, user);
644
645     UploadDataEntity uploadData =
646         vendorSoftwareProductDao.getUploadData(new UploadDataEntity(vspId, version));
647     Assert.assertNotNull(uploadData);
648     Assert.assertNotNull(uploadData.getContentData());
649   }
650
651   private void addCapability(String entryValueKey, Map<String, CapabilityDefinition> capabilities,
652                              String key, CapabilityDefinition value) {
653
654     capabilities.put(entryValueKey + "_" + key, value);
655   }
656
657   private InputStream getFileInputStream(String fileName) {
658     URL url = this.getClass().getResource(fileName);
659     try {
660       return url.openStream();
661     } catch (IOException e) {
662       e.printStackTrace();
663       return null;
664     }
665   }
666
667   private void assertVSPInWantedLocationInVSPList(String vspId, int location, String user) {
668     List<VersionedVendorSoftwareProductInfo> vspList =
669         vendorSoftwareProductManager.getVspList(null, user);
670     Assert.assertEquals(vspList.get(location).getVspDetails().getId(), vspId);
671   }
672 }