Added oparent to sdc main
[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.AmdocsLicenseMembers;
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(AmdocsLicenseMembers amdocsLicenseMembers, User user, Boolean isVlmUpdated) throws Exception {
43
44 //              create major method
45         RestResponse creationMethodVendorLicense = creationMethodVendorLicense(amdocsLicenseMembers, user);
46         assertEquals("did not succeed to create method for vendor license", 200, creationMethodVendorLicense.getErrorCode().intValue());
47         amdocsLicenseMembers.setVersion(ResponseParser.getValueFromJsonResponse(creationMethodVendorLicense.getResponse(), "id"));
48
49         if(isVlmUpdated) {
50 //              TODO update vlm do nothing
51 //              commit
52             RestResponse commitVendorLicense = commitVendorLicense(amdocsLicenseMembers, user);
53             assertEquals("did not succeed to commit vendor license", 200, commitVendorLicense.getErrorCode().intValue());
54         }
55
56 //              submit
57         RestResponse submitVendorLicense = submitVendorLicense(amdocsLicenseMembers, user);
58         assertEquals("did not succeed to submit vendor license", 200, submitVendorLicense.getErrorCode().intValue());
59
60         if(ComponentBaseTest.getExtendTest() != null){
61             ComponentBaseTest.getExtendTest().log(Status.INFO, "Succeeded in updating the vendor license");
62         }
63     }
64
65      private static RestResponse getVLMComponentByVersion(String vlmId, String vlmVersion, User user) throws Exception{
66         Config config = Utils.getConfig();
67         String url = String.format(Urls.GET_VLM_COMPONENT_BY_VERSION, config.getOnboardingBeHost(),config.getOnboardingBePort(), vlmId,vlmVersion);
68         String userId = user.getUserId();
69
70         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
71
72         HttpRequest http = new HttpRequest();
73         RestResponse response = http.httpSendGet(url, headersMap);
74         return response;
75     }
76
77      public static boolean validateVlmExist(String vlmId, String vlmVersion, User user) throws Exception {
78         RestResponse restResponse = getVLMComponentByVersion(vlmId, vlmVersion, user);
79         assertEquals(String.format("VLM version not updated, reponse code message: %s", restResponse.getResponse()),restResponse.getErrorCode().intValue(),200);
80         return (restResponse.getErrorCode()==200);
81     }
82
83     public static AmdocsLicenseMembers createVendorLicense(User user) throws Exception {
84
85         AmdocsLicenseMembers amdocsLicenseMembers;
86 //              ComponentBaseTest.getExtendTest().log(Status.INFO, "Starting to create the vendor license");
87         String vendorLicenseName = "ciLicense" + OnboardingUtils.getShortUUID();
88         RestResponse vendorLicenseResponse = createVendorLicenseModels_1(vendorLicenseName, user);
89         assertEquals("did not succeed to create vendor license model", 200, vendorLicenseResponse.getErrorCode().intValue());
90         String vendorId = ResponseParser.getValueFromJsonResponse(vendorLicenseResponse.getResponse(), "itemId");
91         String versionId = ResponseParser.getValueFromJsonResponse(vendorLicenseResponse.getResponse(), "version:id");
92
93         RestResponse vendorKeyGroupsResponse = createVendorKeyGroups_2(vendorId, versionId, user);
94         assertEquals("did not succeed to create vendor key groups", 200, vendorKeyGroupsResponse.getErrorCode().intValue());
95         String keyGroupId = ResponseParser.getValueFromJsonResponse(vendorKeyGroupsResponse.getResponse(), "value");
96
97         RestResponse vendorEntitlementPool = createVendorEntitlementPool_3(vendorId, versionId, user);
98         assertEquals("did not succeed to create vendor entitlement pool", 200, vendorEntitlementPool.getErrorCode().intValue());
99         String entitlementPoolId = ResponseParser.getValueFromJsonResponse(vendorEntitlementPool.getResponse(), "value");
100
101         RestResponse vendorLicenseFeatureGroups = createVendorLicenseFeatureGroups_4(vendorId, versionId, keyGroupId, entitlementPoolId, user);
102         assertEquals("did not succeed to create vendor license feature groups", 200, vendorLicenseFeatureGroups.getErrorCode().intValue());
103         String featureGroupId = ResponseParser.getValueFromJsonResponse(vendorLicenseFeatureGroups.getResponse(), "value");
104
105         RestResponse vendorLicenseAgreement = createVendorLicenseAgreement_5(vendorId, versionId, featureGroupId, user);
106         assertEquals("did not succeed to create vendor license agreement", 200, vendorLicenseAgreement.getErrorCode().intValue());
107         String vendorLicenseAgreementId = ResponseParser.getValueFromJsonResponse(vendorLicenseAgreement.getResponse(), "value");
108
109 //              RestResponse checkinVendorLicense = OnboardingUtils.checkinVendorLicense(vendorId, user, versionId);
110 //              assertEquals("did not succeed to checkin vendor license", 200, checkinVendorLicense.getErrorCode().intValue());
111
112         amdocsLicenseMembers = new AmdocsLicenseMembers(vendorId, vendorLicenseName, vendorLicenseAgreementId, featureGroupId);
113         amdocsLicenseMembers.setVersion(versionId); // Once object created and submitted, his initial version is 1.0
114
115         RestResponse submitVendorLicense = submitVendorLicense(amdocsLicenseMembers, user);
116         assertEquals("did not succeed to submit vendor license", 200, submitVendorLicense.getErrorCode().intValue());
117
118 //              ComponentBaseTest.getExtendTest().log(Status.INFO, "Succeeded in creating the vendor license");
119
120         return amdocsLicenseMembers;
121     }
122
123     private static RestResponse actionOnComponent(String vspid, String body, String onboardComponent, User user, String componentVersion) throws Exception {
124         Config config = Utils.getConfig();
125         String url = String.format(Urls.ACTION_ON_COMPONENT, config.getOnboardingBeHost(), config.getOnboardingBePort(), onboardComponent, vspid, componentVersion);
126         String userId = user.getUserId();
127         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
128
129         HttpRequest http = new HttpRequest();
130         RestResponse response = http.httpSendPut(url, body, headersMap);
131         return response;
132     }
133
134     private static RestResponse createMethodVendorLicense(String vendorId, String body, String onboardComponent, User user, String componentVersion) throws Exception {
135         Config config = Utils.getConfig();
136         String url = String.format(Urls.CREATE_METHOD, config.getOnboardingBeHost(), config.getOnboardingBePort(), onboardComponent, vendorId, componentVersion);
137         String userId = user.getUserId();
138         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
139
140         HttpRequest http = new HttpRequest();
141         RestResponse response = http.httpSendPost(url, body, headersMap);
142         return response;
143     }
144
145     public static RestResponse submitVendorLicense(AmdocsLicenseMembers amdocsLicenseMembers, User user) throws Exception {
146         return actionOnComponent(amdocsLicenseMembers.getVendorId(), "{\"action\":\"Submit\"}", "vendor-license-models", user, amdocsLicenseMembers.getVersion());
147     }
148
149     /**
150      * @param amdocsLicenseMembers
151      * @param user
152      * @return
153      * checkOut exist VLM method
154      * @throws Exception
155      */
156     public static RestResponse creationMethodVendorLicense(AmdocsLicenseMembers amdocsLicenseMembers, User user) throws Exception {
157         String messageBody = "{\"description\":\"2.0\",\"creationMethod\":\"major\"}";
158         return createMethodVendorLicense(amdocsLicenseMembers.getVendorId(), messageBody, "items", user, amdocsLicenseMembers.getVersion());
159     }
160
161     public static RestResponse commitVendorLicense(AmdocsLicenseMembers amdocsLicenseMembers, User user) throws Exception {
162         String messageBody = "{\"action\":\"Commit\",\"commitRequest\":{\"message\":\"commit\"}}";
163         return actionOnComponent(amdocsLicenseMembers.getVendorId(), messageBody, "items", user, amdocsLicenseMembers.getVersion());
164     }
165
166     public static RestResponse createVendorLicenseModels_1(String name, User user) throws Exception {
167         Config config = Utils.getConfig();
168         String url = String.format(Urls.CREATE_VENDOR_LISENCE_MODELS, config.getOnboardingBeHost(), config.getOnboardingBePort());
169         String userId = user.getUserId();
170
171         JSONObject jObject = new JSONObject();
172         jObject.put("vendorName", name);
173         jObject.put("description", "new vendor license model");
174         jObject.put("iconRef", "icon");
175
176         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
177
178         HttpRequest http = new HttpRequest();
179         RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
180         return response;
181
182     }
183
184     public static RestResponse createVendorLicenseAgreement_5(String vspid, String versionId, String featureGroupId, User user)
185             throws Exception {
186         Config config = Utils.getConfig();
187         String url = String.format(Urls.CREATE_VENDOR_LISENCE_AGREEMENT, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid, versionId);
188         String userId = user.getUserId();
189
190         JSONObject licenseTermpObject = new JSONObject();
191         licenseTermpObject.put("choice", "Fixed_Term");
192         licenseTermpObject.put("other", "");
193
194         JSONObject jObjectBody = new JSONObject();
195         jObjectBody.put("name", "abc");
196         jObjectBody.put("description", "new vendor license agreement");
197         jObjectBody.put("requirementsAndConstrains", "abc");
198         jObjectBody.put("licenseTerm", licenseTermpObject);
199         jObjectBody.put("addedFeatureGroupsIds", Arrays.asList(featureGroupId).toArray());
200
201         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
202
203         HttpRequest http = new HttpRequest();
204         RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
205         return response;
206     }
207
208     public static RestResponse createVendorLicenseFeatureGroups_4(String vspid, String versionId, String licenseKeyGroupId,
209                                                                   String entitlementPoolId, User user) throws Exception {
210         Config config = Utils.getConfig();
211         String url = String.format(Urls.CREATE_VENDOR_LISENCE_FEATURE_GROUPS, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid, versionId);
212         String userId = user.getUserId();
213
214         JSONObject jObject = new JSONObject();
215         jObject.put("name", "xyz");
216         jObject.put("description", "new vendor license feature groups");
217         jObject.put("partNumber", "123abc456");
218 //      jObject.put("manufacturerReferenceNumber", "5");
219         jObject.put("addedLicenseKeyGroupsIds", Arrays.asList(licenseKeyGroupId).toArray());
220         jObject.put("addedEntitlementPoolsIds", Arrays.asList(entitlementPoolId).toArray());
221
222         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
223
224         HttpRequest http = new HttpRequest();
225         RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
226         return response;
227
228     }
229
230     public static RestResponse createVendorEntitlementPool_3(String vspid, String versionId, User user) throws Exception {
231         Config config = Utils.getConfig();
232         String url = String.format(Urls.CREATE_VENDOR_LISENCE_ENTITLEMENT_POOL, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid, versionId);
233         String userId = user.getUserId();
234
235         JSONObject jEntitlementMetricObject = new JSONObject();
236         jEntitlementMetricObject.put("choice", "CPU");
237         jEntitlementMetricObject.put("other", "");
238
239         JSONObject jAggregationFunctionObject = new JSONObject();
240         jAggregationFunctionObject.put("choice", "Peak");
241         jAggregationFunctionObject.put("other", "");
242
243         JSONObject jOperationalScope = new JSONObject();
244         jOperationalScope.put("choices", Arrays.asList("Availability_Zone").toArray());
245         jOperationalScope.put("other", "");
246
247         JSONObject jTimeObject = new JSONObject();
248         jTimeObject.put("choice", "Hour");
249         jTimeObject.put("other", "");
250
251         JSONObject jObjectBody = new JSONObject();
252         jObjectBody.put("name", "def"+ OnboardingUtils.getShortUUID());
253         jObjectBody.put("description", "new vendor license entitlement pool");
254         jObjectBody.put("thresholdValue", "23");
255         jObjectBody.put("thresholdUnits", "Absolute");
256         jObjectBody.put("entitlementMetric", jEntitlementMetricObject);
257         jObjectBody.put("increments", "abcd");
258         jObjectBody.put("aggregationFunction", jAggregationFunctionObject);
259         jObjectBody.put("operationalScope", jOperationalScope);
260         jObjectBody.put("time", jTimeObject);
261         jObjectBody.put("manufacturerReferenceNumber", "123aaa");
262
263         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
264
265         HttpRequest http = new HttpRequest();
266         RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
267         return response;
268     }
269
270     public static RestResponse createVendorKeyGroups_2(String vspid, String versionId, User user) throws Exception {
271         Config config = Utils.getConfig();
272         String url = String.format(Urls.CREATE_VENDOR_LISENCE_KEY_GROUPS, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid, versionId);
273         String userId = user.getUserId();
274
275         JSONObject jOperationalScope = new JSONObject();
276         jOperationalScope.put("choices", Arrays.asList("Tenant").toArray());
277         jOperationalScope.put("other", "");
278
279         JSONObject jObjectBody = new JSONObject();
280         jObjectBody.put("name", "keyGroup" + OnboardingUtils.getShortUUID());
281         jObjectBody.put("description", "new vendor license key group");
282         jObjectBody.put("operationalScope", jOperationalScope);
283         jObjectBody.put("type", "Universal");
284
285         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
286
287         HttpRequest http = new HttpRequest();
288         RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
289         return response;
290     }
291
292     public static RestResponse validateUpload(String vspid, User user, String vspVersion) throws Exception {
293         String body = null;
294         Config config = Utils.getConfig();
295         String url = String.format(Urls.VALIDATE_UPLOAD, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid,vspVersion);
296         String userId = user.getUserId();
297
298         Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
299         HttpRequest http = new HttpRequest();
300         RestResponse response = http.httpSendPut(url, body, headersMap);
301
302         return response;
303     }
304
305 }