push addional code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / VSPFullTest.java
1 package org.openecomp.sdc.vendorsoftwareproduct;
2
3 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
4 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
5 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
6 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
7 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory;
8 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
9 import org.openecomp.sdc.vendorsoftwareproduct.impl.VendorSoftwareProductManagerImpl;
10 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
11 import org.openecomp.sdc.vendorsoftwareproduct.types.VersionedVendorSoftwareProductInfo;
12 import org.openecomp.sdc.versioning.dao.types.Version;
13 import org.openecomp.core.model.dao.EnrichedServiceModelDaoFactory;
14 import org.openecomp.core.util.UniqueValueUtil;
15 import org.openecomp.core.utilities.CommonMethods;
16 import org.apache.commons.collections4.MapUtils;
17 import org.apache.commons.io.IOUtils;
18 import org.testng.Assert;
19 import org.testng.annotations.Test;
20
21 import java.io.*;
22 import java.net.URL;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28
29 public class VSPFullTest {
30
31
32   public static final Version VERSION01 = new Version(0, 1);
33   private static final org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao
34       vendorSoftwareProductDao =
35       VendorSoftwareProductDaoFactory.getInstance().createInterface();
36   private static final String USER1 = "vspTestUser1";
37   private static VendorSoftwareProductManager vendorSoftwareProductManager =
38       new VendorSoftwareProductManagerImpl();
39   private static VendorLicenseFacade vendorLicenseFacade =
40       org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory.getInstance().createInterface();
41
42   @Test
43   public void testEnrichModelInSubmit() {
44     UniqueValueUtil
45         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
46             "VSP_FullTest");
47
48     String vlm1Id = vendorLicenseFacade.createVendorLicenseModel(VSPCommon
49             .createVendorLicenseModel("vlmName " + CommonMethods.nextUuId(), "vlm1Id desc", "icon1"),
50         USER1).getId();
51     String entitlementPoolId = vendorLicenseFacade
52         .createEntitlementPool(new EntitlementPoolEntity(vlm1Id, null, null), USER1).getId();
53
54     org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity
55         featureGroup = new org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity(vlm1Id, null, null);
56     featureGroup.getEntitlementPoolIds().add(entitlementPoolId);
57     String featureGroupId = vendorLicenseFacade.createFeatureGroup(featureGroup, USER1).getId();
58
59     org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity
60         licenseAgreement = new org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity(vlm1Id, null, null);
61     licenseAgreement.getFeatureGroupIds().add(featureGroupId);
62     String licenseAgreementId =
63         vendorLicenseFacade.createLicenseAgreement(licenseAgreement, USER1).getId();
64
65     vendorLicenseFacade.checkin(vlm1Id, USER1);
66     vendorLicenseFacade.submit(vlm1Id, USER1);
67
68     String vspId = createVsp(vlm1Id, licenseAgreementId, licenseAgreement.getFeatureGroupIds());
69
70     Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity> components = uploadFullCompositionFile(vspId);
71
72
73     //check in
74     vendorSoftwareProductManager.checkin(vspId, USER1);
75     //submit
76     try {
77       ValidationResponse result = vendorSoftwareProductManager.submit(vspId, USER1);
78       //Assert.assertTrue(result.isValid());
79       //PackageInfo createPackageResult = vendorSoftwareProductManager.createPackage(vspId, USER1);
80
81     } catch (IOException e) {
82       Assert.fail();
83     }
84     VersionedVendorSoftwareProductInfo details =
85         vendorSoftwareProductManager.getVspDetails(vspId, null, USER1);
86
87
88     //File csar = vendorSoftwareProductManager.getTranslatedFile(vspId,details.getVersionInfo().getActiveVersion(),USER1);
89     // writeFile(csar);
90
91
92     ToscaServiceModel model =
93         (ToscaServiceModel) EnrichedServiceModelDaoFactory.getInstance().createInterface()
94             .getServiceModel(vspId, details.getVersionInfo().getActiveVersion());
95
96     Map<String, CapabilityDefinition> capabilities = new HashMap<>();
97     for (org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity component : components) {
98       model.getServiceTemplates().
99           entrySet().
100           stream().
101           filter(entryValue -> entryValue.getValue() != null &&
102               entryValue.getValue().getNode_types() != null &&
103               entryValue.getValue().
104                   getNode_types().
105                   containsKey(component.getComponentCompositionData().getName())).
106           forEach(entryValue -> entryValue.getValue().getNode_types().
107               values().
108               stream().
109               filter(type -> MapUtils.isNotEmpty(type.getCapabilities())).
110               forEach(type -> type.getCapabilities().
111                   entrySet().
112                   forEach(entry -> addCapability(entryValue.getKey(), capabilities, entry.getKey(),
113                       entry.getValue()))));
114
115     }
116
117     Assert.assertNotNull(capabilities);
118   }
119
120   private Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity> uploadFullCompositionFile(String vspId) {
121     vendorSoftwareProductManager
122         .uploadFile(vspId, getFileInputStream("/vspmanager/zips/fullComposition.zip"), USER1);
123
124     Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity> components =
125         vendorSoftwareProductManager.listComponents(vspId, null, USER1);
126     Assert.assertFalse(components.isEmpty());
127
128     for (org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity component : components) {
129       Assert.assertNotNull(vendorSoftwareProductManager
130           .getComponentQuestionnaire(vspId, null, component.getId(), USER1).getData());
131
132       Collection<org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity> nics =
133           vendorSoftwareProductManager.listNics(vspId, null, component.getId(), USER1);
134       Assert.assertFalse(nics.isEmpty());
135       for (org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity nic : nics) {
136         Assert.assertNotNull(vendorSoftwareProductManager
137             .getNicQuestionnaire(vspId, null, component.getId(), nic.getId(), USER1).getData());
138       }
139     }
140
141     return components;
142   }
143
144   private String createVsp(String vlm1Id, String licenseAgreementId, Set<String> featureGroupIds) {
145     VspDetails expectedVsp = VSPCommon
146         .createVspDetails(null, null, "VSP_FullTest", "Test-vsp_fullTest", "vendorName", vlm1Id,
147             "icon", "category", "subCategory", licenseAgreementId,
148             featureGroupIds.stream().collect(Collectors.toList()));
149     String vspId = vendorSoftwareProductManager.createNewVsp(expectedVsp, USER1).getId();
150
151     VspDetails actualVsp =
152         vendorSoftwareProductDao.getVendorSoftwareProductInfo(new VspDetails(vspId, VERSION01));
153     expectedVsp.setId(vspId);
154     expectedVsp.setVersion(VERSION01);
155
156     VendorSoftwareProductManagerTest.assertVspsEquals(actualVsp, expectedVsp);
157     Assert.assertNotNull(
158         vendorSoftwareProductManager.getVspQuestionnaire(vspId, null, USER1).getData());
159     return vspId;
160   }
161
162   private void writeFile(File csar) {
163     try {
164       FileInputStream in = new FileInputStream(csar);
165       File output = new File("CSAR_vDNS.zip");
166
167       FileOutputStream out = new FileOutputStream(output);
168
169       IOUtils.copy(in, out);
170       in.close();
171       out.close();
172     } catch (IOException e) {
173       throw new RuntimeException(e);
174     }
175   }
176
177   private void addCapability(String entryValueKey, Map<String, CapabilityDefinition> capabilities,
178                              String key, CapabilityDefinition value) {
179
180     capabilities.put(entryValueKey + "_" + key, value);
181   }
182
183   private InputStream getFileInputStream(String fileName) {
184     URL url = this.getClass().getResource(fileName);
185     try {
186       return url.openStream();
187     } catch (IOException e) {
188       e.printStackTrace();
189       return null;
190     }
191   }
192
193
194 }