2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.utils.rest;
23 import com.google.gson.Gson;
24 import com.google.gson.reflect.TypeToken;
25 import org.apache.http.entity.mime.MultipartEntityBuilder;
26 import org.openecomp.sdc.be.model.User;
27 import org.openecomp.sdc.be.model.category.CategoryDefinition;
28 import org.openecomp.sdc.be.model.category.GroupingDefinition;
29 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
30 import org.openecomp.sdc.ci.tests.api.Urls;
31 import org.openecomp.sdc.ci.tests.config.Config;
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;
36 import java.io.IOException;
37 import java.util.List;
40 public class CategoryRestUtils extends BaseRestUtils {
42 private static final int STATUS_CODE_CREATED = 201;
44 private static Gson gson = new Gson();
46 public static RestResponse createCategory(CategoryDefinition categoryDefinition, User sdncModifierDetails,
47 String categoryType) throws Exception {
48 Config config = Utils.getConfig();
49 String url = String.format(Urls.CREATE_CATEGORY, config.getCatalogBeHost(), config.getCatalogBePort(),
51 String bodyJson = gson.toJson(categoryDefinition);
52 RestResponse addCategoryResponse = BaseRestUtils.sendPost(url, bodyJson, sdncModifierDetails.getUserId(),
54 if (addCategoryResponse.getErrorCode().intValue() == STATUS_CODE_CREATED)
55 categoryDefinition.setUniqueId(
56 ResponseParser.getValueFromJsonResponse(addCategoryResponse.getResponse(), "uniqueId"));
57 return addCategoryResponse;
61 public static RestResponse getAllCategories(User sdncModifierDetails, String categoryType) throws Exception {
62 Config config = Utils.getConfig();
63 String url = String.format(Urls.GET_ALL_CATEGORIES, config.getCatalogBeHost(), config.getCatalogBePort(),
65 String userId = sdncModifierDetails.getUserId();
66 Map<String, String> headersMap = prepareHeadersMap(userId);
67 HttpRequest http = new HttpRequest();
68 // System.out.println(url);
69 RestResponse getCategotyResponse = http.httpSendGet(url, headersMap);
70 return getCategotyResponse;
73 public static RestResponse getAllCategoriesTowardsFe(User sdncModifierDetails, String categoryType)
75 Config config = Utils.getConfig();
76 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
78 String userId = sdncModifierDetails.getUserId();
79 Map<String, String> headersMap = prepareHeadersMap(userId);
80 HttpRequest http = new HttpRequest();
81 // System.out.println(url);
82 RestResponse getCategotyResponse = http.httpSendGet(url, headersMap);
83 return getCategotyResponse;
87 public static RestResponse deleteCategory(String categoryId, String psUserId, String categoryType)
89 Config config = Utils.getConfig();
90 String url = String.format(Urls.DELETE_CATEGORY, config.getCatalogBeHost(), config.getCatalogBePort(),
91 categoryType, categoryId);
92 url = url.replace("#", "%23"); // HEX
93 url = url.replace(" ", "%20"); // HEX
94 RestResponse deleteCategoryResponse = sendDelete(url, psUserId);
95 return deleteCategoryResponse;
98 public static RestResponse createSubCategory(SubCategoryDefinition subCategory, CategoryDefinition parentCategory,
99 User sdncModifierDetails, String categoryType) throws Exception {
100 // categoryType = service/resource/product
101 Config config = Utils.getConfig();
102 String url = String.format(Urls.CREATE_SUB_CATEGORY, config.getCatalogBeHost(), config.getCatalogBePort(),
103 categoryType, parentCategory.getUniqueId());
104 String bodyJson = gson.toJson(subCategory);
105 RestResponse createSubCategoryPost = BaseRestUtils.sendPost(url, bodyJson, sdncModifierDetails.getUserId(),
107 if (createSubCategoryPost.getErrorCode().intValue() == STATUS_CODE_CREATED)
108 subCategory.setUniqueId(
109 ResponseParser.getValueFromJsonResponse(createSubCategoryPost.getResponse(), "uniqueId"));
111 return createSubCategoryPost;
114 public static RestResponse deleteSubCategory(String subCategoryId, String categoryId, String psUserId,
115 String categoryType) throws Exception {
116 Config config = Utils.getConfig();
117 String url = String.format(Urls.DELETE_SUB_CATEGORY, config.getCatalogBeHost(), config.getCatalogBePort(),
118 categoryType, categoryId, subCategoryId);
119 url = url.replace("#", "%23"); // HEX
120 url = url.replace(" ", "%20"); // HEX
121 RestResponse deleteSubCategoryResponse = sendDelete(url, psUserId);
122 return deleteSubCategoryResponse;
125 public static RestResponse deleteGrouping(String groupId, String subCategoryId, String categoryId, String psUserId,
126 String categoryType) throws Exception {
128 Config config = Utils.getConfig();
129 String url = String.format(Urls.DELETE_GROUPING, config.getCatalogBeHost(), config.getCatalogBePort(),
130 categoryType, categoryId, subCategoryId, groupId);
131 url = url.replace("#", "%23"); // HEX
132 url = url.replace(" ", "%20"); // HEX
133 RestResponse deleteGroupResponse = sendDelete(url, psUserId);
134 return deleteGroupResponse;
137 public static RestResponse createServiceCategoryHttpCspAtuUidIsMissing(CategoryDefinition categoryDataDefinition,
138 User sdncModifierDetails) throws Exception {
140 Config config = Utils.getConfig();
141 String url = String.format(Urls.CREATE_CATEGORY, config.getCatalogBeHost(), config.getCatalogBePort(),
142 SERVICE_COMPONENT_TYPE);
144 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
145 headersMap.remove("USER_ID");
146 Gson gson = new Gson();
147 String userBodyJson = gson.toJson(categoryDataDefinition);
148 HttpRequest http = new HttpRequest();
149 // System.out.println(url);
150 // System.out.println(userBodyJson);
151 RestResponse createCatergoryResponse = http.httpSendPost(url, userBodyJson, headersMap);
152 return createCatergoryResponse;
155 public static RestResponse createSubCategoryHttpCspAtuUidIsMissing(SubCategoryDefinition subCategory,
156 CategoryDefinition parentCategory, User sdncModifierDetails, String categoryType) throws Exception {
157 // categoryType = service/resource/product
158 Config config = Utils.getConfig();
159 String url = String.format(Urls.CREATE_SUB_CATEGORY, config.getCatalogBeHost(), config.getCatalogBePort(),
160 categoryType, parentCategory.getUniqueId());
161 String userId = sdncModifierDetails.getUserId();
162 Map<String, String> headersMap = prepareHeadersMap(userId);
163 headersMap.remove("USER_ID");
164 Gson gson = new Gson();
165 String subCatJson = gson.toJson(subCategory);
166 HttpRequest http = new HttpRequest();
167 // System.out.println(url);
168 // System.out.println(subCatJson);
169 RestResponse addCategoryResponse = http.httpSendPost(url, subCatJson, headersMap);
170 return addCategoryResponse;
173 public static RestResponse deleteCatergoryHttpCspAtuUidIsMissing(CategoryDefinition categoryDataDefinition,
174 User sdncModifierDetails) throws Exception {
176 Config config = Utils.getConfig();
177 String url = String.format(Urls.DELETE_CONSUMER, config.getCatalogBeHost(), config.getCatalogBePort(),
178 categoryDataDefinition.getName());
180 String userId = sdncModifierDetails.getUserId();
181 Map<String, String> headersMap = prepareHeadersMap(userId);
182 headersMap.remove("USER_ID");
183 Gson gson = new Gson();
184 String userBodyJson = gson.toJson(categoryDataDefinition);
185 HttpRequest http = new HttpRequest();
186 // System.out.println(url);
187 // System.out.println(userBodyJson);
188 RestResponse deleteCategotyResponse = http.httpSendDelete(url, headersMap);
189 return deleteCategotyResponse;
192 public static RestResponse createGrouping(GroupingDefinition grouping, SubCategoryDefinition subCategory,
193 CategoryDefinition parentCategory, User sdncModifierDetails, String categoryType) throws Exception {
194 Config config = Utils.getConfig();
195 String url = String.format(Urls.CREATE_GROUPING, config.getCatalogBeHost(), config.getCatalogBePort(),
196 categoryType, parentCategory.getUniqueId(), subCategory.getUniqueId());
197 String bodyJson = gson.toJson(grouping);
198 RestResponse addGroupingResponse = BaseRestUtils.sendPost(url, bodyJson, sdncModifierDetails.getUserId(),
200 return addGroupingResponse;
203 public static RestResponse importCategories(MultipartEntityBuilder mpBuilder, String userId) throws IOException {
204 Config config = Utils.getConfig();
205 String url = String.format(Urls.IMPORT_CATEGORIES, config.getCatalogBeHost(), config.getCatalogBePort());
207 RestResponse importResponse = BaseRestUtils.sendPost(url, mpBuilder.build(), userId, acceptHeaderData);
208 return importResponse;
211 public static int getMatchingCategoriesNum(RestResponse getAllCategoryRest, CategoryDefinition categoryDefinition) {
212 String response = getAllCategoryRest.getResponse();
213 Gson gson = new Gson();
214 List<CategoryDefinition> categoryDefinitions = gson.fromJson(response,
215 new TypeToken<List<CategoryDefinition>>() {
217 int categoriesNum = 0;
218 String catName = categoryDefinition.getName();
219 for (CategoryDefinition elem : categoryDefinitions) {
220 if (elem.getName().equals(catName)) {
221 categoryDefinition.setUniqueId(elem.getUniqueId());
226 return categoriesNum;
229 public static int getMatchingSubCategoriesNum(RestResponse getAllCategoryRest, String parentCategoryId,
230 SubCategoryDefinition expectedSubCategoryDefinition) {
232 String response = getAllCategoryRest.getResponse();
233 Gson gson = new Gson();
234 List<CategoryDefinition> categoryDefinitions = gson.fromJson(response,
235 new TypeToken<List<CategoryDefinition>>() {
238 String subCatName = expectedSubCategoryDefinition.getName();
239 for (CategoryDefinition elem : categoryDefinitions) {
240 if (elem.getUniqueId().equals(parentCategoryId)) {
241 List<SubCategoryDefinition> subCategories = elem.getSubcategories();
242 if (subCategories != null) {
243 for (SubCategoryDefinition subCategoryDataDefinition : subCategories) {
244 if (subCatName.equals(subCategoryDataDefinition.getName())) {
245 expectedSubCategoryDefinition.setUniqueId(subCategoryDataDefinition.getUniqueId());
256 public static int getMatchingGroupingNum(RestResponse getAllCategoryRest, String parentCategoryId,
257 String subCategoryId, GroupingDefinition expectedGroupingDefinition) {
259 String response = getAllCategoryRest.getResponse();
260 Gson gson = new Gson();
261 List<CategoryDefinition> categoryDefinitions = gson.fromJson(response,
262 new TypeToken<List<CategoryDefinition>>() {
265 String groupingName = expectedGroupingDefinition.getName();
266 for (CategoryDefinition elem : categoryDefinitions) {
267 if (elem.getUniqueId().equals(parentCategoryId)) {
268 List<SubCategoryDefinition> subCategories = elem.getSubcategories();
269 if (subCategories != null) {
270 for (SubCategoryDefinition subCategoryDataDefinition : subCategories) {
272 // (subCategoryId.equals(subCategoryDataDefinition.getUniqueId()))
274 if (subCategoryId.equals(subCategoryDataDefinition.getUniqueId())
275 && subCategoryDataDefinition.getGroupings() != null) {
276 List<GroupingDefinition> grouping = subCategoryDataDefinition.getGroupings();
277 for (GroupingDefinition groupingDataDefinition : grouping) {
278 if (groupingName.equals(groupingDataDefinition.getName())) {
279 expectedGroupingDefinition.setUniqueId(groupingDataDefinition.getUniqueId());
293 public enum CategoryAuditJsonKeysEnum {
294 ACTION("ACTION"), MODIFIER("MODIFIER"), CATEGORY_NAME("CATEGORY_NAME"), SUB_CATEGORY_NAME("SUB_CATEGORY_NAME"), GROUPING_NAME("GROUPING_NAME"), RESOURCE_TYPE("RESOURCE_TYPE"), ECOMP_USER("ECOMP_USER"), STATUS("STATUS"), DESCRIPTION("DESCRIPTION"), DETAILS("DETAILS");
296 private String auditJsonKeyName;
298 private CategoryAuditJsonKeysEnum(String auditJsonKeyName) {
299 this.auditJsonKeyName = auditJsonKeyName;
302 public String getAuditJsonKeyName() {
303 return auditJsonKeyName.toLowerCase();