[SDC-29] rebase continue work to align source
[sdc.git] / ui-ci-dev / src / main / java / org / openecomp / sdc / uici / tests / utilities / OnboardUtility.java
1 package org.openecomp.sdc.uici.tests.utilities;
2
3 import static org.testng.AssertJUnit.assertTrue;
4 import java.io.File;
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;
12 import java.util.Map;
13 import java.util.UUID;
14
15 import javax.validation.constraints.AssertTrue;
16
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;
29
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;
37
38 /**
39  * Utility Class For Onboarding
40  * 
41  * @author mshitrit
42  *
43  */
44 public final class OnboardUtility {
45
46         private OnboardUtility() {
47                 throw new UnsupportedOperationException();
48         }
49
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";
53
54                 private static final String VSP_ID = "vspId";
55                 private static final String VALUE = "value";
56
57                 enum Actions {
58                         CHECK_IN("Checkin"), SUBMIT("Submit"), CREATE_PACKAGE("Create_Package");
59
60                         private String value;
61
62                         private Actions(String value) {
63                                 this.value = value;
64                         }
65
66                         public String getValue() {
67                                 return value;
68                         }
69                 };
70         }
71
72         /**
73          * @param heatFileName
74          * @param filepath
75          * @param userId
76          * @param vld
77          * @return
78          * @throws Exception
79          */
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(),
86                                 Constants.VSP_ID);
87
88                 RestResponse response = FunctionalInterfaces
89                                 .swallowException(() -> uploadHeatPackage(filepath, heatFileName, vspid, userId));
90                 assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
91
92                 response = actionOnComponent(vspid, Constants.Actions.CHECK_IN.getValue(), Constants.VENDOR_SOFTWARE_PRODUCTS,
93                                 userId);
94                 assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
95
96                 response = actionOnComponent(vspid, Constants.Actions.SUBMIT.getValue(), Constants.VENDOR_SOFTWARE_PRODUCTS,
97                                 userId);
98                 assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
99
100                 response = actionOnComponent(vspid, Constants.Actions.CREATE_PACKAGE.getValue(),
101                                 Constants.VENDOR_SOFTWARE_PRODUCTS, userId);
102                 assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
103
104         }
105
106         /**
107          * Contains Details Relevant to Vendor License
108          * 
109          * @author mshitrit
110          *
111          */
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;
118
119                 private VendorLicenseDetails(String vendorId, String vendorLicenseName, String vendorLicenseAgreementId,
120                                 String featureGroupId) {
121                         super();
122                         this.vendorId = vendorId;
123                         this.vendorLicenseName = vendorLicenseName;
124                         this.vendorLicenseAgreementId = vendorLicenseAgreementId;
125                         this.featureGroupId = featureGroupId;
126                         vendorSoftwareProduct = UUID.randomUUID().toString().split("-")[0];
127                 }
128
129                 public String getVendorId() {
130                         return vendorId;
131                 }
132
133                 public String getVendorLicenseName() {
134                         return vendorLicenseName;
135                 }
136
137                 public String getVendorLicenseAgreementId() {
138                         return vendorLicenseAgreementId;
139                 }
140
141                 public String getFeatureGroupId() {
142                         return featureGroupId;
143                 }
144
145                 public String getVendorSoftwareProduct() {
146                         return vendorSoftwareProduct;
147                 }
148
149         }
150
151         /**
152          * Creates Vendor License
153          * 
154          * @param userId
155          * @return
156          * @throws Exception
157          */
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);
164
165                 String vendorId = ResponseParser.getValueFromJsonResponse(vendorLicenseResponse.getResponse(), fieldNameValue);
166
167                 RestResponse vendorKeyGroupsResponse = FunctionalInterfaces
168                                 .swallowException(() -> createVendorKeyGroups(vendorId, userId));
169                 assertTrue(vendorKeyGroupsResponse.getErrorCode() == HttpStatus.SC_OK);
170                 String keyGroupId = ResponseParser.getValueFromJsonResponse(vendorKeyGroupsResponse.getResponse(),
171                                 fieldNameValue);
172
173                 RestResponse vendorEntitlementPool = FunctionalInterfaces
174                                 .swallowException(() -> createVendorEntitlementPool(vendorId, userId));
175                 assertTrue(vendorEntitlementPool.getErrorCode() == HttpStatus.SC_OK);
176                 String entitlementPoolId = ResponseParser.getValueFromJsonResponse(vendorEntitlementPool.getResponse(),
177                                 fieldNameValue);
178
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(),
183                                 fieldNameValue);
184
185                 RestResponse vendorLicenseAgreement = FunctionalInterfaces
186                                 .swallowException(() -> createVendorLicenseAgreement(vendorId, featureGroupId, userId));
187                 assertTrue(vendorLicenseAgreement.getErrorCode() == HttpStatus.SC_OK);
188                 String vendorLicenseAgreementId = ResponseParser.getValueFromJsonResponse(vendorLicenseAgreement.getResponse(),
189                                 fieldNameValue);
190
191                 RestResponse actionOnComponent = actionOnComponent(vendorId, Constants.Actions.CHECK_IN.getValue(),
192                                 Constants.VENDOR_LICENSE_MODELS, userId);
193                 assertTrue(actionOnComponent.getErrorCode() == HttpStatus.SC_OK);
194
195                 actionOnComponent = actionOnComponent(vendorId, Constants.Actions.SUBMIT.getValue(),
196                                 Constants.VENDOR_LICENSE_MODELS, userId);
197                 assertTrue(actionOnComponent.getErrorCode() == HttpStatus.SC_OK);
198
199                 return new VendorLicenseDetails(vendorId, vendorLicenseName, vendorLicenseAgreementId, featureGroupId);
200         }
201
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);
206
207                 JSONObject jObject = new JSONObject();
208                 FunctionalInterfaces.swallowException(() -> jObject.put("action", action));
209
210                 Map<String, String> headersMap = prepareHeadersMap(userId);
211
212                 HttpRequest http = new HttpRequest();
213                 RestResponse response = FunctionalInterfaces
214                                 .swallowException(() -> http.httpSendPut(url, jObject.toString(), headersMap));
215                 return response;
216         }
217
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());
222
223                 JSONObject jObject = new JSONObject();
224                 jObject.put("vendorName", name);
225                 jObject.put("description", "new vendor license model");
226                 jObject.put("iconRef", "icon");
227
228                 Map<String, String> headersMap = prepareHeadersMap(userId);
229
230                 HttpRequest http = new HttpRequest();
231                 RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
232                 return response;
233
234         }
235
236         private static RestResponse createVendorLicenseAgreement(String vspid, String featureGroupId, String userId)
237                         throws Exception {
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);
241
242                 JSONObject licenseTermpObject = new JSONObject();
243                 licenseTermpObject.put("choice", "Fixed_Term");
244                 licenseTermpObject.put("other", "");
245
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());
252
253                 Map<String, String> headersMap = prepareHeadersMap(userId);
254
255                 HttpRequest http = new HttpRequest();
256                 RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
257                 return response;
258         }
259
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);
265
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());
272
273                 Map<String, String> headersMap = prepareHeadersMap(userId);
274
275                 HttpRequest http = new HttpRequest();
276                 RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
277                 return response;
278
279         }
280
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);
285
286                 JSONObject jEntitlementMetricObject = new JSONObject();
287                 jEntitlementMetricObject.put("choice", "CPU");
288                 jEntitlementMetricObject.put("other", "");
289
290                 JSONObject jAggregationFunctionObject = new JSONObject();
291                 jAggregationFunctionObject.put("choice", "Peak");
292                 jAggregationFunctionObject.put("other", "");
293
294                 JSONObject jOperationalScope = new JSONObject();
295                 jOperationalScope.put("choices", Arrays.asList("Availability_Zone").toArray());
296                 jOperationalScope.put("other", "");
297
298                 JSONObject jTimeObject = new JSONObject();
299                 jTimeObject.put("choice", "Hour");
300                 jTimeObject.put("other", "");
301
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");
313
314                 Map<String, String> headersMap = prepareHeadersMap(userId);
315
316                 HttpRequest http = new HttpRequest();
317                 RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
318                 return response;
319         }
320
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);
325
326                 JSONObject jOperationalScope = new JSONObject();
327                 jOperationalScope.put("choices", Arrays.asList("Tenant").toArray());
328                 jOperationalScope.put("other", "");
329
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");
335
336                 Map<String, String> headersMap = prepareHeadersMap(userId);
337
338                 HttpRequest http = new HttpRequest();
339                 RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
340                 return response;
341         }
342
343         private static RestResponse createNewVendorSoftwareProduct(VendorLicenseDetails vld, String userId)
344                         throws Exception {
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());
348
349                 JSONObject jlicensingDataObj = new JSONObject();
350                 jlicensingDataObj.put("licenseAgreement", vld.getVendorLicenseAgreementId());
351                 jlicensingDataObj.put("featureGroups", Arrays.asList(vld.getFeatureGroupId()).toArray());
352
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);
363
364                 Map<String, String> headersMap = prepareHeadersMap(userId);
365                 HttpRequest http = new HttpRequest();
366
367                 RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
368
369                 return response;
370         }
371
372         private static RestResponse uploadHeatPackage(String filepath, String filename, String vspid, String userId)
373                         throws Exception {
374                 Config config = Utils.getConfig();
375                 CloseableHttpResponse response = null;
376
377                 MultipartEntityBuilder mpBuilder = MultipartEntityBuilder.create();
378                 mpBuilder.addPart("resourceZip", new FileBody(getTestZipFile(filepath, filename)));
379
380                 String url = String.format("http://%s:%s/onboarding-api/v1.0/vendor-software-products/%s/upload",
381                                 config.getCatalogBeHost(), config.getCatalogBePort(), vspid);
382
383                 Map<String, String> headersMap = prepareHeadersMap(userId);
384                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "multipart/form-data");
385
386                 CloseableHttpClient client = HttpClients.createDefault();
387                 try {
388                         HttpPost httpPost = new HttpPost(url);
389                         RestResponse restResponse = new RestResponse();
390
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);
396                         }
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();
406                                 try {
407
408                                 } finally {
409                                         instream.close();
410                                 }
411                         }
412
413                         restResponse.setErrorCode(response.getStatusLine().getStatusCode());
414                         restResponse.setResponse(responseBody);
415
416                         return restResponse;
417
418                 } finally {
419                         closeResponse(response);
420                         closeHttpClient(client);
421
422                 }
423         }
424
425         private static void closeResponse(CloseableHttpResponse response) {
426                 try {
427                         if (response != null) {
428                                 response.close();
429                         }
430                 } catch (IOException e) {
431                         System.out.println(String.format("failed to close client or response: %s", e.getMessage()));
432                 }
433         }
434
435         private static void closeHttpClient(CloseableHttpClient client) {
436                 try {
437                         if (client != null) {
438                                 client.close();
439                         }
440                 } catch (IOException e) {
441                         System.out.println(String.format("failed to close client or response: %s", e.getMessage()));
442                 }
443         }
444
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();
450         }
451
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);
457                 return headersMap;
458         }
459
460         public static void createVfFromOnboarding(String userID, String zipFile, String filepath) {
461                 VendorLicenseDetails vld = createVendorLicense(userID);
462                 createVendorSoftwareProduct(zipFile, filepath, userID, vld);
463
464                 GeneralUIUtils.getWebElementWaitForVisible(DataTestIdEnum.OnBoardingTable.OPEN_MODAL_BUTTON.getValue()).click();
465                 GeneralUIUtils.getWebElementWaitForVisible(DataTestIdEnum.OnBoardingTable.ONBOARDING_SEARCH.getValue())
466                                 .sendKeys(vld.getVendorSoftwareProduct());
467
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);
475         }
476
477 }