aboutsummaryrefslogtreecommitdiffstats
path: root/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/general/VendorLicenseModelRestUtils.java
blob: 72f8fc1a0199c3039ba12e72298611fcb861c833 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.ci.tests.utils.general;

import com.aventstack.extentreports.Status;
import org.json.JSONObject;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
import org.openecomp.sdc.ci.tests.api.Urls;
import org.openecomp.sdc.ci.tests.config.Config;
import org.openecomp.sdc.ci.tests.datatypes.VendorLicenseModel;
import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.ci.tests.utils.Utils;
import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;

import java.util.Arrays;
import java.util.Map;

import static org.testng.AssertJUnit.assertEquals;

public class VendorLicenseModelRestUtils {

    public static void updateVendorLicense(VendorLicenseModel vendorLicenseModel, User user, Boolean isVlmUpdated) throws Exception {

//		create major method
        RestResponse creationMethodVendorLicense = creationMethodVendorLicense(vendorLicenseModel, user);
        assertEquals("did not succeed to create method for vendor license", 200, creationMethodVendorLicense.getErrorCode().intValue());
        vendorLicenseModel
            .setVersion(ResponseParser.getValueFromJsonResponse(creationMethodVendorLicense.getResponse(), "id"));

        if(isVlmUpdated) {
//		TODO update vlm do nothing
//		commit
            RestResponse commitVendorLicense = commitVendorLicense(vendorLicenseModel, user);
            assertEquals("did not succeed to commit vendor license", 200, commitVendorLicense.getErrorCode().intValue());
        }

//		submit
        RestResponse submitVendorLicense = submitVendorLicense(vendorLicenseModel, user);
        assertEquals("did not succeed to submit vendor license", 200, submitVendorLicense.getErrorCode().intValue());

        if(ComponentBaseTest.getExtendTest() != null){
            ComponentBaseTest.getExtendTest().log(Status.INFO, "Succeeded in updating the vendor license");
        }
    }

     private static RestResponse getVLMComponentByVersion(String vlmId, String vlmVersion, User user) throws Exception{
        Config config = Utils.getConfig();
        String url = String.format(Urls.GET_VLM_COMPONENT_BY_VERSION, config.getOnboardingBeHost(),config.getOnboardingBePort(), vlmId,vlmVersion);
        String userId = user.getUserId();

        Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);

        HttpRequest http = new HttpRequest();
        RestResponse response = http.httpSendGet(url, headersMap);
        return response;
    }

     public static boolean validateVlmExist(String vlmId, String vlmVersion, User user) throws Exception {
        RestResponse restResponse = getVLMComponentByVersion(vlmId, vlmVersion, user);
        assertEquals(String.format("VLM version not updated, reponse code message: %s", restResponse.getResponse()),restResponse.getErrorCode().intValue(),200);
        return (restResponse.getErrorCode()==200);
    }

    public static VendorLicenseModel createVendorLicense(User user) throws Exception {

        VendorLicenseModel vendorLicenseModel;
//		ComponentBaseTest.getExtendTest().log(Status.INFO, "Starting to create the vendor license");
        String vendorLicenseName = "ciLicense" + OnboardingUtils.getShortUUID();
        RestResponse vendorLicenseResponse = createVendorLicenseModels_1(vendorLicenseName, user);
        assertEquals("did not succeed to create vendor license model", 200, vendorLicenseResponse.getErrorCode().intValue());
        String vendorId = ResponseParser.getValueFromJsonResponse(vendorLicenseResponse.getResponse(), "itemId");
        String versionId = ResponseParser.getValueFromJsonResponse(vendorLicenseResponse.getResponse(), "version:id");

        RestResponse vendorKeyGroupsResponse = createVendorKeyGroups_2(vendorId, versionId, user);
        assertEquals("did not succeed to create vendor key groups", 200, vendorKeyGroupsResponse.getErrorCode().intValue());
        String keyGroupId = ResponseParser.getValueFromJsonResponse(vendorKeyGroupsResponse.getResponse(), "value");

        RestResponse vendorEntitlementPool = createVendorEntitlementPool_3(vendorId, versionId, user);
        assertEquals("did not succeed to create vendor entitlement pool", 200, vendorEntitlementPool.getErrorCode().intValue());
        String entitlementPoolId = ResponseParser.getValueFromJsonResponse(vendorEntitlementPool.getResponse(), "value");

        RestResponse vendorLicenseFeatureGroups = createVendorLicenseFeatureGroups_4(vendorId, versionId, keyGroupId, entitlementPoolId, user);
        assertEquals("did not succeed to create vendor license feature groups", 200, vendorLicenseFeatureGroups.getErrorCode().intValue());
        String featureGroupId = ResponseParser.getValueFromJsonResponse(vendorLicenseFeatureGroups.getResponse(), "value");

        RestResponse vendorLicenseAgreement = createVendorLicenseAgreement_5(vendorId, versionId, featureGroupId, user);
        assertEquals("did not succeed to create vendor license agreement", 200, vendorLicenseAgreement.getErrorCode().intValue());
        String vendorLicenseAgreementId = ResponseParser.getValueFromJsonResponse(vendorLicenseAgreement.getResponse(), "value");

//		RestResponse checkinVendorLicense = OnboardingUtils.checkinVendorLicense(vendorId, user, versionId);
//		assertEquals("did not succeed to checkin vendor license", 200, checkinVendorLicense.getErrorCode().intValue());

        vendorLicenseModel = new VendorLicenseModel(vendorId, vendorLicenseName, vendorLicenseAgreementId, featureGroupId);
        vendorLicenseModel.setVersion(versionId); // Once object created and submitted, his initial version is 1.0

        RestResponse submitVendorLicense = submitVendorLicense(vendorLicenseModel, user);
        assertEquals("did not succeed to submit vendor license", 200, submitVendorLicense.getErrorCode().intValue());

//		ComponentBaseTest.getExtendTest().log(Status.INFO, "Succeeded in creating the vendor license");

        return vendorLicenseModel;
    }

    private static RestResponse actionOnComponent(String vspid, String body, String onboardComponent, User user, String componentVersion) throws Exception {
        Config config = Utils.getConfig();
        String url = String.format(Urls.ACTION_ON_COMPONENT, config.getOnboardingBeHost(), config.getOnboardingBePort(), onboardComponent, vspid, componentVersion);
        String userId = user.getUserId();
        Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);

        HttpRequest http = new HttpRequest();
        RestResponse response = http.httpSendPut(url, body, headersMap);
        return response;
    }

    private static RestResponse createMethodVendorLicense(String vendorId, String body, String onboardComponent, User user, String componentVersion) throws Exception {
        Config config = Utils.getConfig();
        String url = String.format(Urls.CREATE_METHOD, config.getOnboardingBeHost(), config.getOnboardingBePort(), onboardComponent, vendorId, componentVersion);
        String userId = user.getUserId();
        Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);

        HttpRequest http = new HttpRequest();
        RestResponse response = http.httpSendPost(url, body, headersMap);
        return response;
    }

    public static RestResponse submitVendorLicense(VendorLicenseModel vendorLicenseModel, User user) throws Exception {
        return actionOnComponent(vendorLicenseModel.getVendorId(), "{\"action\":\"Submit\"}", "vendor-license-models", user, vendorLicenseModel
            .getVersion());
    }

    /**
     * @param vendorLicenseModel
     * @param user
     * @return
     * checkOut exist VLM method
     * @throws Exception
     */
    public static RestResponse creationMethodVendorLicense(VendorLicenseModel vendorLicenseModel, User user) throws Exception {
        String messageBody = "{\"description\":\"2.0\",\"creationMethod\":\"major\"}";
        return createMethodVendorLicense(vendorLicenseModel.getVendorId(), messageBody, "items", user, vendorLicenseModel
            .getVersion());
    }

    public static RestResponse commitVendorLicense(VendorLicenseModel vendorLicenseModel, User user) throws Exception {
        String messageBody = "{\"action\":\"Commit\",\"commitRequest\":{\"message\":\"commit\"}}";
        return actionOnComponent(vendorLicenseModel.getVendorId(), messageBody, "items", user, vendorLicenseModel.getVersion());
    }

    public static RestResponse createVendorLicenseModels_1(String name, User user) throws Exception {
        Config config = Utils.getConfig();
        String url = String.format(Urls.CREATE_VENDOR_LISENCE_MODELS, config.getOnboardingBeHost(), config.getOnboardingBePort());
        String userId = user.getUserId();

        JSONObject jObject = new JSONObject();
        jObject.put("vendorName", name);
        jObject.put("description", "new vendor license model");
        jObject.put("iconRef", "icon");

        Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);

        HttpRequest http = new HttpRequest();
        RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
        return response;

    }

    public static RestResponse createVendorLicenseAgreement_5(String vspid, String versionId, String featureGroupId, User user)
            throws Exception {
        Config config = Utils.getConfig();
        String url = String.format(Urls.CREATE_VENDOR_LISENCE_AGREEMENT, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid, versionId);
        String userId = user.getUserId();

        JSONObject licenseTermpObject = new JSONObject();
        licenseTermpObject.put("choice", "Fixed_Term");
        licenseTermpObject.put("other", "");

        JSONObject jObjectBody = new JSONObject();
        jObjectBody.put("name", "abc");
        jObjectBody.put("description", "new vendor license agreement");
        jObjectBody.put("requirementsAndConstrains", "abc");
        jObjectBody.put("licenseTerm", licenseTermpObject);
        jObjectBody.put("addedFeatureGroupsIds", Arrays.asList(featureGroupId).toArray());

        Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);

        HttpRequest http = new HttpRequest();
        RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
        return response;
    }

    public static RestResponse createVendorLicenseFeatureGroups_4(String vspid, String versionId, String licenseKeyGroupId,
                                                                  String entitlementPoolId, User user) throws Exception {
        Config config = Utils.getConfig();
        String url = String.format(Urls.CREATE_VENDOR_LISENCE_FEATURE_GROUPS, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid, versionId);
        String userId = user.getUserId();

        JSONObject jObject = new JSONObject();
        jObject.put("name", "xyz");
        jObject.put("description", "new vendor license feature groups");
        jObject.put("partNumber", "123abc456");
//      jObject.put("manufacturerReferenceNumber", "5");
        jObject.put("addedLicenseKeyGroupsIds", Arrays.asList(licenseKeyGroupId).toArray());
        jObject.put("addedEntitlementPoolsIds", Arrays.asList(entitlementPoolId).toArray());

        Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);

        HttpRequest http = new HttpRequest();
        RestResponse response = http.httpSendPost(url, jObject.toString(), headersMap);
        return response;

    }

    public static RestResponse createVendorEntitlementPool_3(String vspid, String versionId, User user) throws Exception {
        Config config = Utils.getConfig();
        String url = String.format(Urls.CREATE_VENDOR_LISENCE_ENTITLEMENT_POOL, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid, versionId);
        String userId = user.getUserId();

        JSONObject jEntitlementMetricObject = new JSONObject();
        jEntitlementMetricObject.put("choice", "CPU");
        jEntitlementMetricObject.put("other", "");

        JSONObject jAggregationFunctionObject = new JSONObject();
        jAggregationFunctionObject.put("choice", "Peak");
        jAggregationFunctionObject.put("other", "");

        JSONObject jOperationalScope = new JSONObject();
        jOperationalScope.put("choices", Arrays.asList("Availability_Zone").toArray());
        jOperationalScope.put("other", "");

        JSONObject jTimeObject = new JSONObject();
        jTimeObject.put("choice", "Hour");
        jTimeObject.put("other", "");

        JSONObject jObjectBody = new JSONObject();
        jObjectBody.put("name", "def"+ OnboardingUtils.getShortUUID());
        jObjectBody.put("description", "new vendor license entitlement pool");
        jObjectBody.put("type", "Universal");
        jObjectBody.put("thresholdValue", "23");
        jObjectBody.put("thresholdUnits", "Absolute");
        jObjectBody.put("entitlementMetric", jEntitlementMetricObject);
        jObjectBody.put("increments", "abcd");
        jObjectBody.put("aggregationFunction", jAggregationFunctionObject);
        jObjectBody.put("operationalScope", jOperationalScope);
        jObjectBody.put("time", jTimeObject);
        jObjectBody.put("manufacturerReferenceNumber", "123aaa");

        Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);

        HttpRequest http = new HttpRequest();
        RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
        return response;
    }

    public static RestResponse createVendorKeyGroups_2(String vspid, String versionId, User user) throws Exception {
        Config config = Utils.getConfig();
        String url = String.format(Urls.CREATE_VENDOR_LISENCE_KEY_GROUPS, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid, versionId);
        String userId = user.getUserId();

        JSONObject jOperationalScope = new JSONObject();
        jOperationalScope.put("choices", Arrays.asList("Tenant").toArray());
        jOperationalScope.put("other", "");

        JSONObject jObjectBody = new JSONObject();
        jObjectBody.put("name", "keyGroup" + OnboardingUtils.getShortUUID());
        jObjectBody.put("description", "new vendor license key group");
        jObjectBody.put("operationalScope", jOperationalScope);
        jObjectBody.put("type", "Universal");

        Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);

        HttpRequest http = new HttpRequest();
        RestResponse response = http.httpSendPost(url, jObjectBody.toString(), headersMap);
        return response;
    }

    public static RestResponse validateUpload(String vspid, User user, String vspVersion) throws Exception {
        String body = null;
        Config config = Utils.getConfig();
        String url = String.format(Urls.VALIDATE_UPLOAD, config.getOnboardingBeHost(), config.getOnboardingBePort(), vspid,vspVersion);
        String userId = user.getUserId();

        Map<String, String> headersMap = OnboardingUtils.prepareHeadersMap(userId);
        HttpRequest http = new HttpRequest();
        RestResponse response = http.httpSendPut(url, body, headersMap);

        return response;
    }

}