[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / rest / CategoryRestUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.rest;
22
23 import java.io.IOException;
24 import java.util.List;
25 import java.util.Map;
26
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;
37
38 import com.google.gson.Gson;
39 import com.google.gson.reflect.TypeToken;
40
41 public class CategoryRestUtils extends BaseRestUtils {
42
43         private static final int STATUS_CODE_CREATED = 201;
44
45         private static Gson gson = new Gson();
46
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(),
51                                 categoryType);
52                 String bodyJson = gson.toJson(categoryDefinition);
53                 RestResponse addCategoryResponse = BaseRestUtils.sendPost(url, bodyJson, sdncModifierDetails.getUserId(),
54                                 acceptHeaderData);
55                 if (addCategoryResponse.getErrorCode().intValue() == STATUS_CODE_CREATED)
56                         categoryDefinition.setUniqueId(
57                                         ResponseParser.getValueFromJsonResponse(addCategoryResponse.getResponse(), "uniqueId"));
58                 return addCategoryResponse;
59         }
60
61         // GET categories
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(),
65                                 categoryType);
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;
72         }
73
74         public static RestResponse getAllCategoriesTowardsFe(User sdncModifierDetails, String categoryType)
75                         throws Exception {
76                 Config config = Utils.getConfig();
77                 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
78                                 categoryType);
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;
85         }
86
87         // Delete Category
88         public static RestResponse deleteCategory(String categoryId, String psUserId, String categoryType)
89                         throws Exception {
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;
97         }
98
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(),
107                                 acceptHeaderData);
108                 if (createSubCategoryPost.getErrorCode().intValue() == STATUS_CODE_CREATED)
109                         subCategory.setUniqueId(
110                                         ResponseParser.getValueFromJsonResponse(createSubCategoryPost.getResponse(), "uniqueId"));
111
112                 return createSubCategoryPost;
113         }
114
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;
124         }
125
126         public static RestResponse deleteGrouping(String groupId, String subCategoryId, String categoryId, String psUserId,
127                         String categoryType) throws Exception {
128
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;
136         }
137
138         public static RestResponse createServiceCategoryHttpCspAtuUidIsMissing(CategoryDefinition categoryDataDefinition,
139                         User sdncModifierDetails) throws Exception {
140
141                 Config config = Utils.getConfig();
142                 String url = String.format(Urls.CREATE_CATEGORY, config.getCatalogBeHost(), config.getCatalogBePort(),
143                                 SERVICE_COMPONENT_TYPE);
144
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;
154         }
155
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;
172         }
173
174         public static RestResponse deleteCatergoryHttpCspAtuUidIsMissing(CategoryDefinition categoryDataDefinition,
175                         User sdncModifierDetails) throws Exception {
176
177                 Config config = Utils.getConfig();
178                 String url = String.format(Urls.DELETE_CONSUMER, config.getCatalogBeHost(), config.getCatalogBePort(),
179                                 categoryDataDefinition.getName());
180
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;
191         }
192
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(),
200                                 acceptHeaderData);
201                 return addGroupingResponse;
202         }
203
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());
207
208                 RestResponse importResponse = BaseRestUtils.sendPost(url, mpBuilder.build(), userId, acceptHeaderData);
209                 return importResponse;
210         }
211
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>>() {
217                                 }.getType());
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());
223                                 categoriesNum++;
224                         }
225                 }
226
227                 return categoriesNum;
228         }
229
230         public static int getMatchingSubCategoriesNum(RestResponse getAllCategoryRest, String parentCategoryId,
231                         SubCategoryDefinition expectedSubCategoryDefinition) {
232
233                 String response = getAllCategoryRest.getResponse();
234                 Gson gson = new Gson();
235                 List<CategoryDefinition> categoryDefinitions = gson.fromJson(response,
236                                 new TypeToken<List<CategoryDefinition>>() {
237                                 }.getType());
238                 int subCatNum = 0;
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());
247                                                         subCatNum++;
248                                                 }
249                                         }
250                                 }
251
252                         }
253                 }
254                 return subCatNum;
255         }
256
257         public static int getMatchingGroupingNum(RestResponse getAllCategoryRest, String parentCategoryId,
258                         String subCategoryId, GroupingDefinition expectedGroupingDefinition) {
259
260                 String response = getAllCategoryRest.getResponse();
261                 Gson gson = new Gson();
262                 List<CategoryDefinition> categoryDefinitions = gson.fromJson(response,
263                                 new TypeToken<List<CategoryDefinition>>() {
264                                 }.getType());
265                 int groupingNum = 0;
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) {
272                                                 // if
273                                                 // (subCategoryId.equals(subCategoryDataDefinition.getUniqueId()))
274                                                 // {
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());
281                                                                         groupingNum++;
282                                                                 }
283                                                         }
284
285                                                 }
286                                         }
287                                 }
288
289                         }
290                 }
291                 return groupingNum;
292         }
293
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");
296                 
297                 private String auditJsonKeyName;
298
299                 private CategoryAuditJsonKeysEnum(String auditJsonKeyName) {
300                         this.auditJsonKeyName = auditJsonKeyName;
301                 }
302
303                 public String getAuditJsonKeyName() {
304                         return auditJsonKeyName.toLowerCase();
305                 }
306         }
307         
308
309 }