push addional code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / upload / HeatCleanup / HeatCleanupOnNewUploadTest.java
1 package org.openecomp.sdc.vendorsoftwareproduct.upload.HeatCleanup;
2
3 import org.openecomp.sdc.datatypes.error.ErrorLevel;
4 import org.openecomp.sdc.vendorsoftwareproduct.VSPCommon;
5 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
6 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
7 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
8 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory;
9 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
10 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadDataEntity;
11 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
12 import org.openecomp.sdc.vendorsoftwareproduct.impl.VendorSoftwareProductManagerImpl;
13 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
14 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileStatus;
15 import org.openecomp.sdc.versioning.dao.types.Version;
16 import org.openecomp.core.model.dao.ServiceModelDao;
17 import org.openecomp.core.model.dao.ServiceModelDaoFactory;
18 import org.openecomp.core.util.UniqueValueUtil;
19 import org.openecomp.core.validation.types.MessageContainerUtil;
20 import org.apache.commons.collections4.CollectionUtils;
21 import org.apache.commons.collections4.MapUtils;
22 import org.testng.Assert;
23 import org.testng.annotations.BeforeClass;
24 import org.testng.annotations.Test;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.URL;
29 import java.util.List;
30
31 public class HeatCleanupOnNewUploadTest {
32   private static final String USER1 = "vspTestUser1";
33
34   private static final VendorSoftwareProductManager vendorSoftwareProductManager =
35       new VendorSoftwareProductManagerImpl();
36   private static final VendorSoftwareProductDao vendorSoftwareProductDao =
37       VendorSoftwareProductDaoFactory.getInstance().createInterface();
38   private static final ServiceModelDao serviceModelDao =
39       ServiceModelDaoFactory.getInstance().createInterface();
40
41   private static String vspId = null;
42   private static Version vspActiveVersion = null;
43
44   private static void validateUploadContentExistence(boolean exist) {
45     UploadDataEntity uploadDataEntity =
46         vendorSoftwareProductDao.getUploadData(new UploadDataEntity(vspId, vspActiveVersion));
47     Assert.assertTrue((uploadDataEntity.getContentData() != null) == exist);
48     Assert.assertTrue((uploadDataEntity.getValidationData() != null) == exist);
49     Assert.assertTrue((uploadDataEntity.getPackageName() != null) == exist);
50     Assert.assertTrue((uploadDataEntity.getPackageVersion() != null) == exist);
51     Assert.assertTrue((serviceModelDao.getServiceModel(vspId, vspActiveVersion) != null) == exist);
52   }
53
54   private static void validateCompositionDataExistence(boolean exist) {
55     Assert.assertTrue(CollectionUtils
56         .isNotEmpty(vendorSoftwareProductDao.listNetworks(vspId, vspActiveVersion)) == exist);
57     Assert.assertTrue(CollectionUtils
58         .isNotEmpty(vendorSoftwareProductDao.listComponents(vspId, vspActiveVersion)) == exist);
59     Assert.assertTrue(CollectionUtils
60         .isNotEmpty(vendorSoftwareProductDao.listNicsByVsp(vspId, vspActiveVersion)) == exist);
61   }
62
63   private static InputStream getFileInputStream(String fileName) {
64     URL url = HeatCleanupOnNewUploadTest.class.getResource(fileName);
65     try {
66       return url.openStream();
67     } catch (IOException e) {
68       e.printStackTrace();
69       return null;
70     }
71   }
72
73   @BeforeClass
74   private void init() {
75     UniqueValueUtil
76         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
77             "VSPTestEmpty");
78
79     VspDetails vspDetails = vendorSoftwareProductManager.createNewVsp(VSPCommon
80         .createVspDetails(null, null, "VSPTestEmpty", "Test-vsp-empty", "vendorName", "vlm1Id",
81             "icon", "category", "subCategory", "123", null), USER1);
82     vspId = vspDetails.getId();
83     vspActiveVersion = vspDetails.getVersion();
84   }
85
86   @Test
87   public void testUploadWithComposition() {
88     InputStream zis = getFileInputStream("/vspmanager/zips/fullComposition.zip");
89
90     UploadFileResponse uploadFileResponse =
91         vendorSoftwareProductManager.uploadFile(vspId, zis, USER1);
92
93     Assert.assertEquals(uploadFileResponse.getStatus(), UploadFileStatus.Success);
94     Assert.assertTrue(MapUtils.isEmpty(
95         MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, uploadFileResponse.getErrors())));
96
97     validateUploadContentExistence(true);
98     validateCompositionDataExistence(true);
99   }
100
101   @Test(dependsOnMethods = {"testUploadWithComposition"})
102   public void testProccesesMIBsDeletionAfterNewUpload() {
103     InputStream zis1 = getFileInputStream("/vspmanager/zips/fullComposition.zip");
104     InputStream zis2 = getFileInputStream("/vspmanager/zips/fullComposition.zip");
105     InputStream mib = getFileInputStream("/vspmanager/zips/vDNS.zip");
106
107     vendorSoftwareProductManager.uploadFile(vspId, zis1, USER1);
108     List<ComponentEntity> components =
109         (List<ComponentEntity>) vendorSoftwareProductDao.listComponents(vspId, vspActiveVersion);
110     String componentId = components.get(0).getId();
111
112     vendorSoftwareProductManager
113         .uploadComponentMib(mib, "vDNS.zip", vspId, componentId, true, USER1);
114     vendorSoftwareProductManager
115         .createProcess(new org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity(vspId, vspActiveVersion, componentId, null), USER1);
116
117     vendorSoftwareProductManager.uploadFile(vspId, zis2, USER1);
118     Assert.assertTrue(
119         vendorSoftwareProductManager.listMibFilenames(vspId, componentId, USER1).getSnmpTrap() ==
120             null);
121     Assert.assertTrue(CollectionUtils
122         .isEmpty(vendorSoftwareProductDao.listProcesses(vspId, vspActiveVersion, componentId)));
123   }
124
125   @Test(dependsOnMethods = {"testProccesesMIBsDeletionAfterNewUpload"})
126   public void testInvalidStructureUploadAfterFullComposition() {
127     InputStream zis = getFileInputStream("/vspmanager/zips/withoutManifest.zip");
128
129     UploadFileResponse uploadFileResponse =
130         vendorSoftwareProductManager.uploadFile(vspId, zis, USER1);
131     Assert.assertEquals(uploadFileResponse.getStatus(), UploadFileStatus.Failure);
132     Assert.assertTrue(MapUtils.isNotEmpty(
133         MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, uploadFileResponse.getErrors())));
134
135     validateUploadContentExistence(true);
136     validateCompositionDataExistence(true);
137   }
138
139   @Test(dependsOnMethods = {"testInvalidStructureUploadAfterFullComposition"})
140   public void testInvalidUploadAfterFullComposition() {
141     InputStream zis = getFileInputStream("/vspmanager/zips/missingYml.zip");
142
143     UploadFileResponse uploadFileResponse =
144         vendorSoftwareProductManager.uploadFile(vspId, zis, USER1);
145     Assert.assertEquals(uploadFileResponse.getStatus(), UploadFileStatus.Success);
146     Assert.assertTrue(MapUtils.isNotEmpty(
147         MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, uploadFileResponse.getErrors())));
148
149     validateUploadContentExistence(true);
150     validateCompositionDataExistence(false);
151   }
152
153   @Test(dependsOnMethods = {"testInvalidUploadAfterFullComposition"})
154   public void testEmptyCompositionUploadAfterFullComposition() throws IOException {
155     testUploadWithComposition();
156
157     InputStream zis = getFileInputStream("/vspmanager/zips/emptyComposition.zip");
158     UploadFileResponse uploadFileResponse =
159         vendorSoftwareProductManager.uploadFile(vspId, zis, USER1);
160     Assert.assertEquals(uploadFileResponse.getStatus(), UploadFileStatus.Success);
161     Assert.assertTrue(MapUtils.isEmpty(
162         MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, uploadFileResponse.getErrors())));
163
164     validateUploadContentExistence(true);
165     validateCompositionDataExistence(false);
166   }
167 }