push addional code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / ComponentsUploadTest.java
1 package org.openecomp.sdc.vendorsoftwareproduct;
2
3 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
4 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
5 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
6 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory;
7 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
8 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
9 import org.openecomp.sdc.vendorsoftwareproduct.impl.VendorSoftwareProductManagerImpl;
10 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.MibUploadStatus;
11 import org.openecomp.sdc.versioning.dao.types.Version;
12 import org.openecomp.core.util.UniqueValueUtil;
13 import org.openecomp.core.utilities.CommonMethods;
14
15 import org.testng.Assert;
16 import org.testng.annotations.BeforeTest;
17 import org.testng.annotations.Test;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.URL;
22
23 public class ComponentsUploadTest {
24
25   private static final String USER1 = "vspTestUser1";
26
27   private static VendorSoftwareProductManager vendorSoftwareProductManager =
28       new VendorSoftwareProductManagerImpl();
29   private static VendorSoftwareProductDao vendorSoftwareProductDao =
30       VendorSoftwareProductDaoFactory.getInstance().createInterface();
31   private static VendorLicenseFacade vendorLicenseFacade =
32       VendorLicenseFacadeFactory.getInstance().createInterface();
33
34   private static String vspId = null;
35   private static Version activeVersion = null;
36   private static String trapFileName = "MMSC.zip";
37   private static String pollFileName = "MNS OAM FW.zip";
38   private static String notZipFileName = "notZipFile";
39   private static String zipWithFoldersFileName = "zipFileWithFolder.zip";
40   private static String emptyZipFileName = "emptyZip.zip";
41   private String vlm1Id;
42   private String componentId;
43
44   @BeforeTest
45   private void init() {
46     UniqueValueUtil
47         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
48             "VSPTestMib");
49     vlm1Id = vendorLicenseFacade.createVendorLicenseModel(VSPCommon
50             .createVendorLicenseModel("vlmName " + CommonMethods.nextUuId(), "vlm1Id desc", "icon1"),
51         USER1).getId();
52     VspDetails vspDetails = vendorSoftwareProductManager.createNewVsp(VSPCommon
53         .createVspDetails(null, null, "VSPTestMib", "Test-vsp-mib", "vendorName", vlm1Id, "icon",
54             "category", "subCategory", "123", null), USER1);
55
56     vspId = vspDetails.getId();
57     activeVersion = vspDetails.getVersion();
58     componentId = createComponent(new ComponentEntity(vspId, activeVersion, null)).getId();
59   }
60
61
62   @Test
63   public void testUploadAndFilenamesList() {
64     InputStream zis1 = getFileInputStream("/validation/zips/various/MMSC.zip");
65     InputStream zis2 = getFileInputStream("/validation/zips/various/MNS OAM FW.zip");
66
67     vendorSoftwareProductManager
68         .uploadComponentMib(zis1, "MMSC.zip", vspId, componentId, true, USER1);
69     vendorSoftwareProductManager
70         .uploadComponentMib(zis2, "MNS OAM FW.zip", vspId, componentId, false, USER1);
71
72     MibUploadStatus mibUploadStatus =
73         vendorSoftwareProductManager.listMibFilenames(vspId, componentId, USER1);
74     Assert.assertEquals(mibUploadStatus.getSnmpTrap(), trapFileName);
75     Assert.assertEquals(mibUploadStatus.getSnmpPoll(), pollFileName);
76   }
77
78   @Test(dependsOnMethods = "testUploadAndFilenamesList")
79   public void testMibsExistentAfterCheckout() throws IOException {
80     activeVersion = vendorSoftwareProductManager.checkin(vspId, USER1);
81 //        UniqueValueUtil.deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.COMPONENT_ARTIFACT_NAME, "MMSC.zip");
82 //        UniqueValueUtil.deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.COMPONENT_ARTIFACT_NAME, "MNS OAM FW.zip");
83     activeVersion = vendorSoftwareProductManager.checkout(vspId, USER1);
84
85     MibUploadStatus mibUploadStatus =
86         vendorSoftwareProductManager.listMibFilenames(vspId, componentId, USER1);
87     Assert.assertNotNull(mibUploadStatus.getSnmpTrap());
88     Assert.assertNotNull(mibUploadStatus.getSnmpPoll());
89   }
90
91   @Test(dependsOnMethods = "testMibsExistentAfterCheckout")
92   public void testDeleteFile() {
93     vendorSoftwareProductManager.deleteComponentMib(vspId, componentId, true, USER1);
94     vendorSoftwareProductManager.deleteComponentMib(vspId, componentId, false, USER1);
95
96     MibUploadStatus mibUploadStatus =
97         vendorSoftwareProductManager.listMibFilenames(vspId, componentId, USER1);
98     Assert.assertNull(mibUploadStatus.getSnmpTrap());
99     Assert.assertNull(mibUploadStatus.getSnmpPoll());
100   }
101
102   @Test(dependsOnMethods = "testDeleteFile")
103   public void testUploadInvalidZip() {
104     URL url = this.getClass().getResource("/notZipFile");
105
106     try {
107       vendorSoftwareProductManager
108           .uploadComponentMib(url.openStream(), notZipFileName, vspId, componentId, true, USER1);
109       Assert.fail();
110     } catch (Exception e) {
111 //            Assert.assertEquals(e.getMessage(), "MIB uploaded for vendor software product with Id " + vspId + " and version " + activeVersion + " is invalid: Invalid zip file");
112       Assert.assertEquals(e.getMessage(), "Invalid zip file");
113     }
114   }
115
116   @Test(dependsOnMethods = "testUploadInvalidZip")
117   public void testUploadZipWithFolders() {
118     InputStream zis = getFileInputStream("/vspmanager/zips/zipFileWithFolder.zip");
119
120     try {
121       vendorSoftwareProductManager
122           .uploadComponentMib(zis, zipWithFoldersFileName, vspId, componentId, true, USER1);
123       Assert.fail();
124     } catch (Exception e) {
125       Assert.assertEquals(e.getMessage(), "Zip file should not contain folders");
126     }
127   }
128
129   @Test(dependsOnMethods = "testUploadZipWithFolders")
130   public void testUploadEmptyZip() {
131     InputStream zis = getFileInputStream("/vspmanager/zips/emptyZip.zip");
132
133     try {
134       vendorSoftwareProductManager
135           .uploadComponentMib(zis, emptyZipFileName, vspId, componentId, true, USER1);
136       Assert.fail();
137     } catch (Exception e) {
138       Assert.assertEquals(e.getMessage(), "Invalid zip file");
139     }
140   }
141
142
143   private InputStream getFileInputStream(String fileName) {
144     URL url = this.getClass().getResource(fileName);
145     try {
146       return url.openStream();
147     } catch (IOException e) {
148       e.printStackTrace();
149       return null;
150     }
151   }
152
153
154   private ComponentEntity createComponent(ComponentEntity component) {
155     component.setId(CommonMethods.nextUuId());
156     vendorSoftwareProductDao.createComponent(component);
157     return component;
158   }
159 }