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