1 package org.openecomp.sdc.uici.tests.utilities;
3 import static org.testng.AssertJUnit.assertTrue;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.StringWriter;
8 import java.nio.file.FileSystems;
9 import java.util.Arrays;
10 import java.util.HashMap;
11 import java.util.Iterator;
13 import java.util.UUID;
15 import javax.validation.constraints.AssertTrue;
17 import org.apache.commons.io.IOUtils;
18 import org.apache.http.HttpEntity;
19 import org.apache.http.HttpStatus;
20 import org.apache.http.client.methods.CloseableHttpResponse;
21 import org.apache.http.client.methods.HttpPost;
22 import org.apache.http.entity.mime.MultipartEntityBuilder;
23 import org.apache.http.entity.mime.content.FileBody;
24 import org.apache.http.impl.client.CloseableHttpClient;
25 import org.apache.http.impl.client.HttpClients;
26 import org.json.JSONObject;
27 import org.junit.Test;
28 import org.openecomp.sdc.uici.tests.datatypes.DataTestIdEnum;
30 import org.openecomp.sdc.ci.tests.config.Config;
31 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
32 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
33 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
34 import org.openecomp.sdc.ci.tests.utils.Utils;
35 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
36 import org.openecomp.sdc.common.datastructure.FunctionalInterfaces;
39 * Utility Class For Onboarding
44 public final class OnboardUtility {
46 private OnboardUtility() {
47 throw new UnsupportedOperationException();
50 private static final class Constants {
51 private static final String VENDOR_SOFTWARE_PRODUCTS = "vendor-software-products";
52 private static final String VENDOR_LICENSE_MODELS = "vendor-license-models";
54 private static final String VSP_ID = "vspId";
55 private static final String VALUE = "value";
58 CHECK_IN("Checkin"), SUBMIT("Submit"), CREATE_PACKAGE("Create_Package");
62 private Actions(String value) {
66 public String getValue() {
80 public static void createVendorSoftwareProduct(String heatFileName, String filepath, String userId,
81 VendorLicenseDetails vld) {
82 RestResponse createNewVendorSoftwareProduct = FunctionalInterfaces
83 .swallowException(() -> createNewVendorSoftwareProduct(vld, userId));
84 assertTrue(createNewVendorSoftwareProduct.getErrorCode() == HttpStatus.SC_OK);
85 String vspid = ResponseParser.getValueFromJsonResponse(createNewVendorSoftwareProduct.getResponse(),
88 RestResponse response = FunctionalInterfaces
89 .swallowException(() -> uploadHeatPackage(filepath, heatFileName, vspid, userId));
90 assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
92 response = actionOnComponent(vspid, Constants.Actions.CHECK_IN.getValue(), Constants.VENDOR_SOFTWARE_PRODUCTS,
94 assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
96 response = actionOnComponent(vspid, Constants.Actions.SUBMIT.getValue(), Constants.VENDOR_SOFTWARE_PRODUCTS,
98 assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
100 response = actionOnComponent(vspid, Constants.Actions.CREATE_PACKAGE.getValue(),
101 Constants.VENDOR_SOFTWARE_PRODUCTS, userId);
102 assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
107 * Contains Details Relevant to Vendor License
112 public static final class VendorLicenseDetails {
113 private final String vendorId;
114 private final String vendorLicenseName;
115 private final String vendorLicenseAgreementId;
116 private final String featureGroupId;
117 private final String vendorSoftwareProduct;
119 private VendorLicenseDetails(String vendorId, String vendorLicenseName, String vendorLicenseAgreementId,
120 String featureGroupId) {
122 this.vendorId = vendorId;
123 this.vendorLicenseName = vendorLicenseName;
124 this.vendorLicenseAgreementId = vendorLicenseAgreementId;
125 this.featureGroupId = featureGroupId;
126 vendorSoftwareProduct = UUID.randomUUID().toString().split("-")[0];
129 public String getVendorId() {
133 public String getVendorLicenseName() {
134 return vendorLicenseName;
137 public String getVendorLicenseAgreementId() {
138 return vendorLicenseAgreementId;
141 public String getFeatureGroupId() {
142 return featureGroupId;
145 public String getVendorSoftwareProduct() {
146 return vendorSoftwareProduct;
152 * Creates Vendor License
158 public static VendorLicenseDetails createVendorLicense(String userId) {
159 final String fieldNameValue = Constants.VALUE;
160 String vendorLicenseName = UUID.randomUUID().toString().split("-")[0];
161 RestResponse vendorLicenseResponse = FunctionalInterfaces
162 .swallowException(() -> createVendorLicenseModels(vendorLicenseName, userId));
163 assertTrue(vendorLicenseResponse.getErrorCode() == HttpStatus.SC_OK);
165 String vendorId = ResponseParser.getValueFromJsonResponse(vendorLicenseResponse.getResponse(), fieldNameValue);
167 RestResponse vendorKeyGroupsResponse = FunctionalInterfaces
168 .swallowException(() -> createVendorKeyGroups(vendorId, userId));
169 assertTrue(vendorKeyGroupsResponse.getErrorCode() == HttpStatus.SC_OK);
170 String keyGroupId = ResponseParser.getValueFromJsonResponse(vendorKeyGroupsResponse.getResponse(),
173 RestResponse vendorEntitlementPool = FunctionalInterfaces
174 .swallowException(() -> createVendorEntitlementPool(vendorId, userId));
175 assertTrue(vendorEntitlementPool.getErrorCode() == HttpStatus.SC_OK);
176 String entitlementPoolId = ResponseParser.getValueFromJsonResponse(vendorEntitlementPool.getResponse(),
179 RestResponse vendorLicenseFeatureGroups = FunctionalInterfaces.swallowException(
180 () -> createVendorLicenseFeatureGroups(vendorId, keyGroupId, entitlementPoolId, userId));
181 assertTrue(vendorLicenseFeatureGroups.getErrorCode() == HttpStatus.SC_OK);
182 String featureGroupId = ResponseParser.getValueFromJsonResponse(vendorLicenseFeatureGroups.getResponse(),
185 RestResponse vendorLicenseAgreement = FunctionalInterfaces
186 .swallowException(() -> createVendorLicenseAgreement(vendorId, featureGroupId, userId));
187 assertTrue(vendorLicenseAgreement.getErrorCode() == HttpStatus.SC_OK);
188 String vendorLicenseAgreementId = ResponseParser.getValueFromJsonResponse(vendorLicenseAgreement.getResponse(),
191 RestResponse actionOnComponent = actionOnComponent(vendorId, Constants.Actions.CHECK_IN.getValue(),
192 Constants.VENDOR_LICENSE_MODELS, userId);
193 assertTrue(actionOnComponent.getErrorCode() == HttpStatus.SC_OK);
195 actionOnComponent = actionOnComponent(vendorId, Constants.Actions.SUBMIT.getValue(),
196 Constants.VENDOR_LICENSE_MODELS, userId);
197 assertTrue(actionOnComponent.getErrorCode() == HttpStatus.SC_OK);
199 return new VendorLicenseDetails(vendorId, vendorLicenseName, vendorLicenseAgreementId, featureGroupId);
202 private static RestResponse actionOnComponent(String vspid, String action, String onboardComponent, String userId) {
203 Config config = FunctionalInterfaces.swallowException(() -> Utils.getConfig());
204 String url = String.format("http://%s:%s/onboarding-api/v1.0/%s/%s/actions", config.getCatalogBeHost(),
205 config.getCatalogBePort(), onboardComponent, vspid);
207 JSONObject jObject = new JSONObject();
208 FunctionalInterfaces.swallowException(() -> jObject.put("action", action));
210 Map<String, String> headersMap = prepareHeadersMap(userId);
212 HttpRequest http = new HttpRequest();
213 RestResponse response = FunctionalInterfaces
214 .swallowException(() -> http.httpSendPut(url, jObject.toString(), headersMap));
218 private static RestResponse createVendorLicenseModels(String name, String userId) throws Exception {
219 Config config = Utils.getConfig();
220 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-license-models", config.getCatalogBeHost(),
221 config.getCatalogBePort());
223 JSONObject jObject = new JSONObject();
224 jObject.put("vendorName", name);
225 jObject.put("description", "new vendor license model");
226 jObject.put("iconRef", "icon");
228 Map<String, String> headersMap = prepareHeadersMap(userId);
230 HttpRequest http = new HttpRequest();
231 RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
236 private static RestResponse createVendorLicenseAgreement(String vspid, String featureGroupId, String userId)
238 Config config = Utils.getConfig();
239 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-license-models/%s/license-agreements",
240 config.getCatalogBeHost(), config.getCatalogBePort(), vspid);
242 JSONObject licenseTermpObject = new JSONObject();
243 licenseTermpObject.put("choice", "Fixed_Term");
244 licenseTermpObject.put("other", "");
246 JSONObject jObjectBody = new JSONObject();
247 jObjectBody.put("name", "abc");
248 jObjectBody.put("description", "new vendor license agreement");
249 jObjectBody.put("requirementsAndConstrains", "abc");
250 jObjectBody.put("licenseTerm", licenseTermpObject);
251 jObjectBody.put("addedFeatureGroupsIds", Arrays.asList(featureGroupId).toArray());
253 Map<String, String> headersMap = prepareHeadersMap(userId);
255 HttpRequest http = new HttpRequest();
256 RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
260 private static RestResponse createVendorLicenseFeatureGroups(String vspid, String licenseKeyGroupId,
261 String entitlementPoolId, String userId) throws Exception {
262 Config config = Utils.getConfig();
263 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-license-models/%s/feature-groups",
264 config.getCatalogBeHost(), config.getCatalogBePort(), vspid);
266 JSONObject jObject = new JSONObject();
267 jObject.put("name", "xyz");
268 jObject.put("description", "new vendor license feature groups");
269 jObject.put("partNumber", "123abc456");
270 jObject.put("addedLicenseKeyGroupsIds", Arrays.asList(licenseKeyGroupId).toArray());
271 jObject.put("addedEntitlementPoolsIds", Arrays.asList(entitlementPoolId).toArray());
273 Map<String, String> headersMap = prepareHeadersMap(userId);
275 HttpRequest http = new HttpRequest();
276 RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
281 private static RestResponse createVendorEntitlementPool(String vspid, String userId) throws Exception {
282 Config config = Utils.getConfig();
283 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-license-models/%s/entitlement-pools",
284 config.getCatalogBeHost(), config.getCatalogBePort(), vspid);
286 JSONObject jEntitlementMetricObject = new JSONObject();
287 jEntitlementMetricObject.put("choice", "CPU");
288 jEntitlementMetricObject.put("other", "");
290 JSONObject jAggregationFunctionObject = new JSONObject();
291 jAggregationFunctionObject.put("choice", "Peak");
292 jAggregationFunctionObject.put("other", "");
294 JSONObject jOperationalScope = new JSONObject();
295 jOperationalScope.put("choices", Arrays.asList("Availability_Zone").toArray());
296 jOperationalScope.put("other", "");
298 JSONObject jTimeObject = new JSONObject();
299 jTimeObject.put("choice", "Hour");
300 jTimeObject.put("other", "");
302 JSONObject jObjectBody = new JSONObject();
303 jObjectBody.put("name", "def");
304 jObjectBody.put("description", "new vendor license entitlement pool");
305 jObjectBody.put("thresholdValue", "23");
306 jObjectBody.put("thresholdUnits", "Absolute");
307 jObjectBody.put("entitlementMetric", jEntitlementMetricObject);
308 jObjectBody.put("increments", "abcd");
309 jObjectBody.put("aggregationFunction", jAggregationFunctionObject);
310 jObjectBody.put("operationalScope", jOperationalScope);
311 jObjectBody.put("time", jTimeObject);
312 jObjectBody.put("manufacturerReferenceNumber", "123aaa");
314 Map<String, String> headersMap = prepareHeadersMap(userId);
316 HttpRequest http = new HttpRequest();
317 RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
321 private static RestResponse createVendorKeyGroups(String vspid, String userId) throws Exception {
322 Config config = Utils.getConfig();
323 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-license-models/%s/license-key-groups",
324 config.getCatalogBeHost(), config.getCatalogBePort(), vspid);
326 JSONObject jOperationalScope = new JSONObject();
327 jOperationalScope.put("choices", Arrays.asList("Tenant").toArray());
328 jOperationalScope.put("other", "");
330 JSONObject jObjectBody = new JSONObject();
331 jObjectBody.put("name", "keyGroup");
332 jObjectBody.put("description", "new vendor license key group");
333 jObjectBody.put("operationalScope", jOperationalScope);
334 jObjectBody.put("type", "Universal");
336 Map<String, String> headersMap = prepareHeadersMap(userId);
338 HttpRequest http = new HttpRequest();
339 RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
343 private static RestResponse createNewVendorSoftwareProduct(VendorLicenseDetails vld, String userId)
345 Config config = Utils.getConfig();
346 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-software-products",
347 config.getCatalogBeHost(), config.getCatalogBePort());
349 JSONObject jlicensingDataObj = new JSONObject();
350 jlicensingDataObj.put("licenseAgreement", vld.getVendorLicenseAgreementId());
351 jlicensingDataObj.put("featureGroups", Arrays.asList(vld.getFeatureGroupId()).toArray());
353 JSONObject jObject = new JSONObject();
354 jObject.put("name", vld.getVendorSoftwareProduct());
355 jObject.put("description", "new VSP description");
356 jObject.put("category", "resourceNewCategory.generic");
357 jObject.put("subCategory", "resourceNewCategory.generic.database");
358 jObject.put("licensingVersion", "1.0");
359 jObject.put("vendorName", vld.getVendorLicenseName());
360 jObject.put("vendorId", vld.getVendorId());
361 jObject.put("icon", "icon");
362 jObject.put("licensingData", jlicensingDataObj);
364 Map<String, String> headersMap = prepareHeadersMap(userId);
365 HttpRequest http = new HttpRequest();
367 RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
372 private static RestResponse uploadHeatPackage(String filepath, String filename, String vspid, String userId)
374 Config config = Utils.getConfig();
375 CloseableHttpResponse response = null;
377 MultipartEntityBuilder mpBuilder = MultipartEntityBuilder.create();
378 mpBuilder.addPart("resourceZip", new FileBody(getTestZipFile(filepath, filename)));
380 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-software-products/%s/upload",
381 config.getCatalogBeHost(), config.getCatalogBePort(), vspid);
383 Map<String, String> headersMap = prepareHeadersMap(userId);
384 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "multipart/form-data");
386 CloseableHttpClient client = HttpClients.createDefault();
388 HttpPost httpPost = new HttpPost(url);
389 RestResponse restResponse = new RestResponse();
391 Iterator<String> iterator = headersMap.keySet().iterator();
392 while (iterator.hasNext()) {
393 String key = iterator.next();
394 String value = headersMap.get(key);
395 httpPost.addHeader(key, value);
397 httpPost.setEntity(mpBuilder.build());
398 response = client.execute(httpPost);
399 HttpEntity entity = response.getEntity();
400 String responseBody = null;
401 if (entity != null) {
402 InputStream instream = entity.getContent();
403 StringWriter writer = new StringWriter();
404 IOUtils.copy(instream, writer);
405 responseBody = writer.toString();
413 restResponse.setErrorCode(response.getStatusLine().getStatusCode());
414 restResponse.setResponse(responseBody);
419 closeResponse(response);
420 closeHttpClient(client);
425 private static void closeResponse(CloseableHttpResponse response) {
427 if (response != null) {
430 } catch (IOException e) {
431 System.out.println(String.format("failed to close client or response: %s", e.getMessage()));
435 private static void closeHttpClient(CloseableHttpClient client) {
437 if (client != null) {
440 } catch (IOException e) {
441 System.out.println(String.format("failed to close client or response: %s", e.getMessage()));
445 private static File getTestZipFile(String filepath, String filename) throws IOException {
446 Config config = Utils.getConfig();
447 String sourceDir = config.getImportResourceTestsConfigDir();
448 java.nio.file.Path filePath = FileSystems.getDefault().getPath(filepath + File.separator + filename);
449 return filePath.toFile();
452 protected static Map<String, String> prepareHeadersMap(String userId) {
453 Map<String, String> headersMap = new HashMap<String, String>();
454 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
455 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
456 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), userId);
460 public static void createVfFromOnboarding(String userID, String zipFile, String filepath) {
461 VendorLicenseDetails vld = createVendorLicense(userID);
462 createVendorSoftwareProduct(zipFile, filepath, userID, vld);
464 GeneralUIUtils.getWebElementWaitForVisible(DataTestIdEnum.OnBoardingTable.OPEN_MODAL_BUTTON.getValue()).click();
465 GeneralUIUtils.getWebElementWaitForVisible(DataTestIdEnum.OnBoardingTable.ONBOARDING_SEARCH.getValue())
466 .sendKeys(vld.getVendorSoftwareProduct());
468 GeneralUIUtils.waitForLoader();
469 GeneralUIUtils.sleep(1000);
470 GeneralUIUtils.getWebElementWaitForClickable(vld.getVendorSoftwareProduct()).click();
471 GeneralUIUtils.waitForLoader();
472 GeneralUIUtils.getWebElementWaitForVisible(DataTestIdEnum.OnBoardingTable.IMPORT_ICON.getValue()).click();
473 GeneralUIUtils.getWebElementWaitForClickable(DataTestIdEnum.LifeCyleChangeButtons.CREATE.getValue()).click();
474 GeneralUIUtils.waitForLoader(300);