Onboard PNF software version Ui test case
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / general / VendorLicenseModelRestUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.utils.general;
22
23 import com.aventstack.extentreports.Status;
24 import org.json.JSONObject;
25 import org.openecomp.sdc.be.model.User;
26 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
27 import org.openecomp.sdc.ci.tests.api.Urls;
28 import org.openecomp.sdc.ci.tests.config.Config;
29 import org.openecomp.sdc.ci.tests.datatypes.VendorLicenseModel;
30 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
31 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
32 import org.openecomp.sdc.ci.tests.utils.Utils;
33 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
34
35 import java.util.Arrays;
36 import java.util.Map;
37
38 import static org.testng.AssertJUnit.assertEquals;
39
40 public class VendorLicenseModelRestUtils {
41
42     public static void updateVendorLicense(VendorLicenseModel vendorLicenseModel, User user, Boolean isVlmUpdated) throws Exception {
43
44 //              create major method
45         RestResponse creationMethodVendorLicense = creationMethodVendorLicense(vendorLicenseModel, user);
46         assertEquals("did not succeed to create method for vendor license", 200, creationMethodVendorLicense.getErrorCode().intValue());
47         vendorLicenseModel
48             .setVersion(ResponseParser.getValueFromJsonResponse(creationMethodVendorLicense.getResponse(), "id"));
49
50         if(isVlmUpdated) {
51 //              TODO update vlm do nothing
52 //              commit
53             RestResponse commitVendorLicense = commitVendorLicense(vendorLicenseModel, user);
54             assertEquals("did not succeed to commit vendor license", 200, commitVendorLicense.getErrorCode().intValue());
55         }
56
57 //              submit
58         RestResponse submitVendorLicense = submitVendorLicense(vendorLicenseModel, user);
59         assertEquals("did not succeed to submit vendor license", 200, submitVendorLicense.getErrorCode().intValue());
60
61         if(ComponentBaseTest.getExtendTest() != null){
62             ComponentBaseTest.getExtendTest().log(Status.INFO, "Succeeded in updating the vendor license");
63         }
64     }
65
66      private static RestResponse getVLMComponentByVersion(String vlmId, String vlmVersion, User user) throws Exception{
67         Config config = Utils.getConfig();
68         String url = String.format(Urls.GET_VLM_COMPONENT_BY_VERSION, config.getOnboardingBeHost(),config.getOnboardingBePort(), vlmId,vlmVersion);
69         String userId = user.getUserId();
70
71         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
72
73         HttpRequest http = new HttpRequest();
74         RestResponse response = http.httpSendGet(url, headersMap);
75         return response;
76     }
77
78      public static boolean validateVlmExist(String vlmId, String vlmVersion, User user) throws Exception {
79         RestResponse restResponse = getVLMComponentByVersion(vlmId, vlmVersion, user);
80         assertEquals(String.format("VLM version not updated, reponse code message: %s", restResponse.getResponse()),restResponse.getErrorCode().intValue(),200);
81         return (restResponse.getErrorCode()==200);
82     }
83
84     public static VendorLicenseModel createVendorLicense(User user) throws Exception {
85
86         VendorLicenseModel vendorLicenseModel;
87 //              ComponentBaseTest.getExtendTest().log(Status.INFO, "Starting to create the vendor license");
88         String vendorLicenseName = "ciLicense" + OnboardingUtils.getShortUUID();
89         RestResponse vendorLicenseResponse = createVendorLicenseModels_1(vendorLicenseName, user);
90         assertEquals("did not succeed to create vendor license model", 200, vendorLicenseResponse.getErrorCode().intValue());
91         String vendorId = ResponseParser.getValueFromJsonResponse(vendorLicenseResponse.getResponse(), "itemId");
92         String versionId = ResponseParser.getValueFromJsonResponse(vendorLicenseResponse.getResponse(), "version:id");
93
94         RestResponse vendorKeyGroupsResponse = createVendorKeyGroups_2(vendorId, versionId, user);
95         assertEquals("did not succeed to create vendor key groups", 200, vendorKeyGroupsResponse.getErrorCode().intValue());
96         String keyGroupId = ResponseParser.getValueFromJsonResponse(vendorKeyGroupsResponse.getResponse(), "value");
97
98         RestResponse vendorEntitlementPool = createVendorEntitlementPool_3(vendorId, versionId, user);
99         assertEquals("did not succeed to create vendor entitlement pool", 200, vendorEntitlementPool.getErrorCode().intValue());
100         String entitlementPoolId = ResponseParser.getValueFromJsonResponse(vendorEntitlementPool.getResponse(), "value");
101
102         RestResponse vendorLicenseFeatureGroups = createVendorLicenseFeatureGroups_4(vendorId, versionId, keyGroupId, entitlementPoolId, user);
103         assertEquals("did not succeed to create vendor license feature groups", 200, vendorLicenseFeatureGroups.getErrorCode().intValue());
104         String featureGroupId = ResponseParser.getValueFromJsonResponse(vendorLicenseFeatureGroups.getResponse(), "value");
105
106         RestResponse vendorLicenseAgreement = createVendorLicenseAgreement_5(vendorId, versionId, featureGroupId, user);
107         assertEquals("did not succeed to create vendor license agreement", 200, vendorLicenseAgreement.getErrorCode().intValue());
108         String vendorLicenseAgreementId = ResponseParser.getValueFromJsonResponse(vendorLicenseAgreement.getResponse(), "value");
109
110 //              RestResponse checkinVendorLicense = OnboardingUtils.checkinVendorLicense(vendorId, user, versionId);
111 //              assertEquals("did not succeed to checkin vendor license", 200, checkinVendorLicense.getErrorCode().intValue());
112
113         vendorLicenseModel = new VendorLicenseModel(vendorId, vendorLicenseName, vendorLicenseAgreementId, featureGroupId);
114         vendorLicenseModel.setVersion(versionId); // Once object created and submitted, his initial version is 1.0
115
116         RestResponse submitVendorLicense = submitVendorLicense(vendorLicenseModel, user);
117         assertEquals("did not succeed to submit vendor license", 200, submitVendorLicense.getErrorCode().intValue());
118
119 //              ComponentBaseTest.getExtendTest().log(Status.INFO, "Succeeded in creating the vendor license");
120
121         return vendorLicenseModel;
122     }
123
124     private static RestResponse actionOnComponent(String vspid, String body, String onboardComponent, User user, String componentVersion) throws Exception {
125         Config config = Utils.getConfig();
126         String url = String.format(Urls.ACTION_ON_COMPONENT, config.getOnboardingBeHost(), config.getOnboardingBePort(), onboardComponent, vspid, componentVersion);
127         String userId = user.getUserId();
128         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
129
130         HttpRequest http = new HttpRequest();
131         RestResponse response = http.httpSendPut(url, body, headersMap);
132         return response;
133     }
134
135     private static RestResponse createMethodVendorLicense(String vendorId, String body, String onboardComponent, User user, String componentVersion) throws Exception {
136         Config config = Utils.getConfig();
137         String url = String.format(Urls.CREATE_METHOD, config.getOnboardingBeHost(), config.getOnboardingBePort(), onboardComponent, vendorId, componentVersion);
138         String userId = user.getUserId();
139         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
140
141         HttpRequest http = new HttpRequest();
142         RestResponse response = http.httpSendPost(url, body, headersMap);
143         return response;
144     }
145
146     public static RestResponse submitVendorLicense(VendorLicenseModel vendorLicenseModel, User user) throws Exception {
147         return actionOnComponent(vendorLicenseModel.getVendorId(), "{\"action\":\"Submit\"}", "vendor-license-models", user, vendorLicenseModel
148             .getVersion());
149     }
150
151     /**
152      * @param vendorLicenseModel
153      * @param user
154      * @return
155      * checkOut exist VLM method
156      * @throws Exception
157      */
158     public static RestResponse creationMethodVendorLicense(VendorLicenseModel vendorLicenseModel, User user) throws Exception {
159         String messageBody = "{\"description\":\"2.0\",\"creationMethod\":\"major\"}";
160         return createMethodVendorLicense(vendorLicenseModel.getVendorId(), messageBody, "items", user, vendorLicenseModel
161             .getVersion());
162     }
163
164     public static RestResponse commitVendorLicense(VendorLicenseModel vendorLicenseModel, User user) throws Exception {
165         String messageBody = "{\"action\":\"Commit\",\"commitRequest\":{\"message\":\"commit\"}}";
166         return actionOnComponent(vendorLicenseModel.getVendorId(), messageBody, "items", user, vendorLicenseModel.getVersion());
167     }
168
169     public static RestResponse createVendorLicenseModels_1(String name, User user) throws Exception {
170         Config config = Utils.getConfig();
171         String url = String.format(Urls.CREATE_VENDOR_LISENCE_MODELS, config.getOnboardingBeHost(), config.getOnboardingBePort());
172         String userId = user.getUserId();
173
174         JSONObject jObject = new JSONObject();
175         jObject.put("vendorName", name);
176         jObject.put("description", "new vendor license model");
177         jObject.put("iconRef", "icon");
178
179         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
180
181         HttpRequest http = new HttpRequest();
182         RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
183         return response;
184
185     }
186
187     public static RestResponse createVendorLicenseAgreement_5(String vspid, String versionId, String featureGroupId, User user)
188             throws Exception {
189         Config config = Utils.getConfig();
190         String url = String.format(Urls.CREATE_VENDOR_LISENCE_AGREEMENT, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid, versionId);
191         String userId = user.getUserId();
192
193         JSONObject licenseTermpObject = new JSONObject();
194         licenseTermpObject.put("choice", "Fixed_Term");
195         licenseTermpObject.put("other", "");
196
197         JSONObject jObjectBody = new JSONObject();
198         jObjectBody.put("name", "abc");
199         jObjectBody.put("description", "new vendor license agreement");
200         jObjectBody.put("requirementsAndConstrains", "abc");
201         jObjectBody.put("licenseTerm", licenseTermpObject);
202         jObjectBody.put("addedFeatureGroupsIds", Arrays.asList(featureGroupId).toArray());
203
204         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
205
206         HttpRequest http = new HttpRequest();
207         RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
208         return response;
209     }
210
211     public static RestResponse createVendorLicenseFeatureGroups_4(String vspid, String versionId, String licenseKeyGroupId,
212                                                                   String entitlementPoolId, User user) throws Exception {
213         Config config = Utils.getConfig();
214         String url = String.format(Urls.CREATE_VENDOR_LISENCE_FEATURE_GROUPS, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid, versionId);
215         String userId = user.getUserId();
216
217         JSONObject jObject = new JSONObject();
218         jObject.put("name", "xyz");
219         jObject.put("description", "new vendor license feature groups");
220         jObject.put("partNumber", "123abc456");
221 //      jObject.put("manufacturerReferenceNumber", "5");
222         jObject.put("addedLicenseKeyGroupsIds", Arrays.asList(licenseKeyGroupId).toArray());
223         jObject.put("addedEntitlementPoolsIds", Arrays.asList(entitlementPoolId).toArray());
224
225         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
226
227         HttpRequest http = new HttpRequest();
228         RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
229         return response;
230
231     }
232
233     public static RestResponse createVendorEntitlementPool_3(String vspid, String versionId, User user) throws Exception {
234         Config config = Utils.getConfig();
235         String url = String.format(Urls.CREATE_VENDOR_LISENCE_ENTITLEMENT_POOL, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid, versionId);
236         String userId = user.getUserId();
237
238         JSONObject jEntitlementMetricObject = new JSONObject();
239         jEntitlementMetricObject.put("choice", "CPU");
240         jEntitlementMetricObject.put("other", "");
241
242         JSONObject jAggregationFunctionObject = new JSONObject();
243         jAggregationFunctionObject.put("choice", "Peak");
244         jAggregationFunctionObject.put("other", "");
245
246         JSONObject jOperationalScope = new JSONObject();
247         jOperationalScope.put("choices", Arrays.asList("Availability_Zone").toArray());
248         jOperationalScope.put("other", "");
249
250         JSONObject jTimeObject = new JSONObject();
251         jTimeObject.put("choice", "Hour");
252         jTimeObject.put("other", "");
253
254         JSONObject jObjectBody = new JSONObject();
255         jObjectBody.put("name", "def"+ OnboardingUtils.getShortUUID());
256         jObjectBody.put("description", "new vendor license entitlement pool");
257         jObjectBody.put("thresholdValue", "23");
258         jObjectBody.put("thresholdUnits", "Absolute");
259         jObjectBody.put("entitlementMetric", jEntitlementMetricObject);
260         jObjectBody.put("increments", "abcd");
261         jObjectBody.put("aggregationFunction", jAggregationFunctionObject);
262         jObjectBody.put("operationalScope", jOperationalScope);
263         jObjectBody.put("time", jTimeObject);
264         jObjectBody.put("manufacturerReferenceNumber", "123aaa");
265
266         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
267
268         HttpRequest http = new HttpRequest();
269         RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
270         return response;
271     }
272
273     public static RestResponse createVendorKeyGroups_2(String vspid, String versionId, User user) throws Exception {
274         Config config = Utils.getConfig();
275         String url = String.format(Urls.CREATE_VENDOR_LISENCE_KEY_GROUPS, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid, versionId);
276         String userId = user.getUserId();
277
278         JSONObject jOperationalScope = new JSONObject();
279         jOperationalScope.put("choices", Arrays.asList("Tenant").toArray());
280         jOperationalScope.put("other", "");
281
282         JSONObject jObjectBody = new JSONObject();
283         jObjectBody.put("name", "keyGroup" + OnboardingUtils.getShortUUID());
284         jObjectBody.put("description", "new vendor license key group");
285         jObjectBody.put("operationalScope", jOperationalScope);
286         jObjectBody.put("type", "Universal");
287
288         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
289
290         HttpRequest http = new HttpRequest();
291         RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
292         return response;
293     }
294
295     public static RestResponse validateUpload(String vspid, User user, String vspVersion) throws Exception {
296         String body = null;
297         Config config = Utils.getConfig();
298         String url = String.format(Urls.VALIDATE_UPLOAD, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid,vspVersion);
299         String userId = user.getUserId();
300
301         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
302         HttpRequest http = new HttpRequest();
303         RestResponse response = http.httpSendPut(url, body, headersMap);
304
305         return response;
306     }
307
308 }