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 java.io.IOException;
24 import java.util.List;
27 import org.apache.http.entity.mime.MultipartEntityBuilder;
28 import org.openecomp.sdc.be.model.User;
29 import org.openecomp.sdc.be.model.category.CategoryDefinition;
30 import org.openecomp.sdc.be.model.category.GroupingDefinition;
31 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
32 import org.openecomp.sdc.ci.tests.api.Urls;
33 import org.openecomp.sdc.ci.tests.config.Config;
34 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
35 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
36 import org.openecomp.sdc.ci.tests.utils.Utils;
38 import com.google.gson.Gson;
39 import com.google.gson.reflect.TypeToken;
41 public class CategoryRestUtils extends BaseRestUtils {
43 private static final int STATUS_CODE_CREATED = 201;
45 private static Gson gson = new Gson();
47 public static RestResponse createCategory(CategoryDefinition categoryDefinition, User sdncModifierDetails,
48 String categoryType) throws Exception {
49 Config config = Utils.getConfig();
50 String url = String.format(Urls.CREATE_CATEGORY, config.getCatalogBeHost(), config.getCatalogBePort(),
52 String bodyJson = gson.toJson(categoryDefinition);
53 RestResponse addCategoryResponse = BaseRestUtils.sendPost(url, bodyJson, sdncModifierDetails.getUserId(),
55 if (addCategoryResponse.getErrorCode().intValue() == STATUS_CODE_CREATED)
56 categoryDefinition.setUniqueId(
57 ResponseParser.getValueFromJsonResponse(addCategoryResponse.getResponse(), "uniqueId"));
58 return addCategoryResponse;
62 public static RestResponse getAllCategories(User sdncModifierDetails, String categoryType) throws Exception {
63 Config config = Utils.getConfig();
64 String url = String.format(Urls.GET_ALL_CATEGORIES, config.getCatalogBeHost(), config.getCatalogBePort(),
66 String userId = sdncModifierDetails.getUserId();
67 Map<String, String> headersMap = prepareHeadersMap(userId);
68 HttpRequest http = new HttpRequest();
69 // System.out.println(url);
70 RestResponse getCategotyResponse = http.httpSendGet(url, headersMap);
71 return getCategotyResponse;
74 public static RestResponse getAllCategoriesTowardsFe(User sdncModifierDetails, String categoryType)
76 Config config = Utils.getConfig();
77 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
79 String userId = sdncModifierDetails.getUserId();
80 Map<String, String> headersMap = prepareHeadersMap(userId);
81 HttpRequest http = new HttpRequest();
82 // System.out.println(url);
83 RestResponse getCategotyResponse = http.httpSendGet(url, headersMap);
84 return getCategotyResponse;
88 public static RestResponse deleteCategory(String categoryId, String psUserId, String categoryType)
90 Config config = Utils.getConfig();
91 String url = String.format(Urls.DELETE_CATEGORY, config.getCatalogBeHost(), config.getCatalogBePort(),
92 categoryType, categoryId);
93 url = url.replace("#", "%23"); // HEX
94 url = url.replace(" ", "%20"); // HEX
95 RestResponse deleteCategoryResponse = sendDelete(url, psUserId);
96 return deleteCategoryResponse;
99 public static RestResponse createSubCategory(SubCategoryDefinition subCategory, CategoryDefinition parentCategory,
100 User sdncModifierDetails, String categoryType) throws Exception {
101 // categoryType = service/resource/product
102 Config config = Utils.getConfig();
103 String url = String.format(Urls.CREATE_SUB_CATEGORY, config.getCatalogBeHost(), config.getCatalogBePort(),
104 categoryType, parentCategory.getUniqueId());
105 String bodyJson = gson.toJson(subCategory);
106 RestResponse createSubCategoryPost = BaseRestUtils.sendPost(url, bodyJson, sdncModifierDetails.getUserId(),
108 if (createSubCategoryPost.getErrorCode().intValue() == STATUS_CODE_CREATED)
109 subCategory.setUniqueId(
110 ResponseParser.getValueFromJsonResponse(createSubCategoryPost.getResponse(), "uniqueId"));
112 return createSubCategoryPost;
115 public static RestResponse deleteSubCategory(String subCategoryId, String categoryId, String psUserId,
116 String categoryType) throws Exception {
117 Config config = Utils.getConfig();
118 String url = String.format(Urls.DELETE_SUB_CATEGORY, config.getCatalogBeHost(), config.getCatalogBePort(),
119 categoryType, categoryId, subCategoryId);
120 url = url.replace("#", "%23"); // HEX
121 url = url.replace(" ", "%20"); // HEX
122 RestResponse deleteSubCategoryResponse = sendDelete(url, psUserId);
123 return deleteSubCategoryResponse;
126 public static RestResponse deleteGrouping(String groupId, String subCategoryId, String categoryId, String psUserId,
127 String categoryType) throws Exception {
129 Config config = Utils.getConfig();
130 String url = String.format(Urls.DELETE_GROUPING, config.getCatalogBeHost(), config.getCatalogBePort(),
131 categoryType, categoryId, subCategoryId, groupId);
132 url = url.replace("#", "%23"); // HEX
133 url = url.replace(" ", "%20"); // HEX
134 RestResponse deleteGroupResponse = sendDelete(url, psUserId);
135 return deleteGroupResponse;
138 public static RestResponse createServiceCategoryHttpCspAtuUidIsMissing(CategoryDefinition categoryDataDefinition,
139 User sdncModifierDetails) throws Exception {
141 Config config = Utils.getConfig();
142 String url = String.format(Urls.CREATE_CATEGORY, config.getCatalogBeHost(), config.getCatalogBePort(),
143 SERVICE_COMPONENT_TYPE);
145 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails.getUserId());
146 headersMap.remove("USER_ID");
147 Gson gson = new Gson();
148 String userBodyJson = gson.toJson(categoryDataDefinition);
149 HttpRequest http = new HttpRequest();
150 // System.out.println(url);
151 // System.out.println(userBodyJson);
152 RestResponse createCatergoryResponse = http.httpSendPost(url, userBodyJson, headersMap);
153 return createCatergoryResponse;
156 public static RestResponse createSubCategoryHttpCspAtuUidIsMissing(SubCategoryDefinition subCategory,
157 CategoryDefinition parentCategory, User sdncModifierDetails, String categoryType) throws Exception {
158 // categoryType = service/resource/product
159 Config config = Utils.getConfig();
160 String url = String.format(Urls.CREATE_SUB_CATEGORY, config.getCatalogBeHost(), config.getCatalogBePort(),
161 categoryType, parentCategory.getUniqueId());
162 String userId = sdncModifierDetails.getUserId();
163 Map<String, String> headersMap = prepareHeadersMap(userId);
164 headersMap.remove("USER_ID");
165 Gson gson = new Gson();
166 String subCatJson = gson.toJson(subCategory);
167 HttpRequest http = new HttpRequest();
168 // System.out.println(url);
169 // System.out.println(subCatJson);
170 RestResponse addCategoryResponse = http.httpSendPost(url, subCatJson, headersMap);
171 return addCategoryResponse;
174 public static RestResponse deleteCatergoryHttpCspAtuUidIsMissing(CategoryDefinition categoryDataDefinition,
175 User sdncModifierDetails) throws Exception {
177 Config config = Utils.getConfig();
178 String url = String.format(Urls.DELETE_CONSUMER, config.getCatalogBeHost(), config.getCatalogBePort(),
179 categoryDataDefinition.getName());
181 String userId = sdncModifierDetails.getUserId();
182 Map<String, String> headersMap = prepareHeadersMap(userId);
183 headersMap.remove("USER_ID");
184 Gson gson = new Gson();
185 String userBodyJson = gson.toJson(categoryDataDefinition);
186 HttpRequest http = new HttpRequest();
187 // System.out.println(url);
188 // System.out.println(userBodyJson);
189 RestResponse deleteCategotyResponse = http.httpSendDelete(url, headersMap);
190 return deleteCategotyResponse;
193 public static RestResponse createGrouping(GroupingDefinition grouping, SubCategoryDefinition subCategory,
194 CategoryDefinition parentCategory, User sdncModifierDetails, String categoryType) throws Exception {
195 Config config = Utils.getConfig();
196 String url = String.format(Urls.CREATE_GROUPING, config.getCatalogBeHost(), config.getCatalogBePort(),
197 categoryType, parentCategory.getUniqueId(), subCategory.getUniqueId());
198 String bodyJson = gson.toJson(grouping);
199 RestResponse addGroupingResponse = BaseRestUtils.sendPost(url, bodyJson, sdncModifierDetails.getUserId(),
201 return addGroupingResponse;
204 public static RestResponse importCategories(MultipartEntityBuilder mpBuilder, String userId) throws IOException {
205 Config config = Utils.getConfig();
206 String url = String.format(Urls.IMPORT_CATEGORIES, config.getCatalogBeHost(), config.getCatalogBePort());
208 RestResponse importResponse = BaseRestUtils.sendPost(url, mpBuilder.build(), userId, acceptHeaderData);
209 return importResponse;
212 public static int getMatchingCategoriesNum(RestResponse getAllCategoryRest, CategoryDefinition categoryDefinition) {
213 String response = getAllCategoryRest.getResponse();
214 Gson gson = new Gson();
215 List<CategoryDefinition> categoryDefinitions = gson.fromJson(response,
216 new TypeToken<List<CategoryDefinition>>() {
218 int categoriesNum = 0;
219 String catName = categoryDefinition.getName();
220 for (CategoryDefinition elem : categoryDefinitions) {
221 if (elem.getName().equals(catName)) {
222 categoryDefinition.setUniqueId(elem.getUniqueId());
227 return categoriesNum;
230 public static int getMatchingSubCategoriesNum(RestResponse getAllCategoryRest, String parentCategoryId,
231 SubCategoryDefinition expectedSubCategoryDefinition) {
233 String response = getAllCategoryRest.getResponse();
234 Gson gson = new Gson();
235 List<CategoryDefinition> categoryDefinitions = gson.fromJson(response,
236 new TypeToken<List<CategoryDefinition>>() {
239 String subCatName = expectedSubCategoryDefinition.getName();
240 for (CategoryDefinition elem : categoryDefinitions) {
241 if (elem.getUniqueId().equals(parentCategoryId)) {
242 List<SubCategoryDefinition> subCategories = elem.getSubcategories();
243 if (subCategories != null) {
244 for (SubCategoryDefinition subCategoryDataDefinition : subCategories) {
245 if (subCatName.equals(subCategoryDataDefinition.getName())) {
246 expectedSubCategoryDefinition.setUniqueId(subCategoryDataDefinition.getUniqueId());
257 public static int getMatchingGroupingNum(RestResponse getAllCategoryRest, String parentCategoryId,
258 String subCategoryId, GroupingDefinition expectedGroupingDefinition) {
260 String response = getAllCategoryRest.getResponse();
261 Gson gson = new Gson();
262 List<CategoryDefinition> categoryDefinitions = gson.fromJson(response,
263 new TypeToken<List<CategoryDefinition>>() {
266 String groupingName = expectedGroupingDefinition.getName();
267 for (CategoryDefinition elem : categoryDefinitions) {
268 if (elem.getUniqueId().equals(parentCategoryId)) {
269 List<SubCategoryDefinition> subCategories = elem.getSubcategories();
270 if (subCategories != null) {
271 for (SubCategoryDefinition subCategoryDataDefinition : subCategories) {
273 // (subCategoryId.equals(subCategoryDataDefinition.getUniqueId()))
275 if (subCategoryId.equals(subCategoryDataDefinition.getUniqueId())
276 && subCategoryDataDefinition.getGroupings() != null) {
277 List<GroupingDefinition> grouping = subCategoryDataDefinition.getGroupings();
278 for (GroupingDefinition groupingDataDefinition : grouping) {
279 if (groupingName.equals(groupingDataDefinition.getName())) {
280 expectedGroupingDefinition.setUniqueId(groupingDataDefinition.getUniqueId());
294 public enum CategoryAuditJsonKeysEnum {
295 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");
297 private String auditJsonKeyName;
299 private CategoryAuditJsonKeysEnum(String auditJsonKeyName) {
300 this.auditJsonKeyName = auditJsonKeyName;
303 public String getAuditJsonKeyName() {
304 return auditJsonKeyName.toLowerCase();