re base code
[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 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;
35
36 import java.io.IOException;
37 import java.util.List;
38 import java.util.Map;
39
40 public class CategoryRestUtils extends BaseRestUtils {
41
42         private static final int STATUS_CODE_CREATED = 201;
43
44         private static Gson gson = new Gson();
45
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(),
50                                 categoryType);
51                 String bodyJson = gson.toJson(categoryDefinition);
52                 RestResponse addCategoryResponse = BaseRestUtils.sendPost(url, bodyJson, sdncModifierDetails.getUserId(),
53                                 acceptHeaderData);
54                 if (addCategoryResponse.getErrorCode().intValue() == STATUS_CODE_CREATED)
55                         categoryDefinition.setUniqueId(
56                                         ResponseParser.getValueFromJsonResponse(addCategoryResponse.getResponse(), "uniqueId"));
57                 return addCategoryResponse;
58         }
59
60         // GET categories
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(),
64                                 categoryType);
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;
71         }
72
73         public static RestResponse getAllCategoriesTowardsFe(User sdncModifierDetails, String categoryType)
74                         throws Exception {
75                 Config config = Utils.getConfig();
76                 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
77                                 categoryType);
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;
84         }
85
86         // Delete Category
87         public static RestResponse deleteCategory(String categoryId, String psUserId, String categoryType)
88                         throws Exception {
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;
96         }
97
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(),
106                                 acceptHeaderData);
107                 if (createSubCategoryPost.getErrorCode().intValue() == STATUS_CODE_CREATED)
108                         subCategory.setUniqueId(
109                                         ResponseParser.getValueFromJsonResponse(createSubCategoryPost.getResponse(), "uniqueId"));
110
111                 return createSubCategoryPost;
112         }
113
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;
123         }
124
125         public static RestResponse deleteGrouping(String groupId, String subCategoryId, String categoryId, String psUserId,
126                         String categoryType) throws Exception {
127
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;
135         }
136
137         public static RestResponse createServiceCategoryHttpCspAtuUidIsMissing(CategoryDefinition categoryDataDefinition,
138                         User sdncModifierDetails) throws Exception {
139
140                 Config config = Utils.getConfig();
141                 String url = String.format(Urls.CREATE_CATEGORY, config.getCatalogBeHost(), config.getCatalogBePort(),
142                                 SERVICE_COMPONENT_TYPE);
143
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;
153         }
154
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;
171         }
172
173         public static RestResponse deleteCatergoryHttpCspAtuUidIsMissing(CategoryDefinition categoryDataDefinition,
174                         User sdncModifierDetails) throws Exception {
175
176                 Config config = Utils.getConfig();
177                 String url = String.format(Urls.DELETE_CONSUMER, config.getCatalogBeHost(), config.getCatalogBePort(),
178                                 categoryDataDefinition.getName());
179
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;
190         }
191
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(),
199                                 acceptHeaderData);
200                 return addGroupingResponse;
201         }
202
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());
206
207                 RestResponse importResponse = BaseRestUtils.sendPost(url, mpBuilder.build(), userId, acceptHeaderData);
208                 return importResponse;
209         }
210
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>>() {
216                                 }.getType());
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());
222                                 categoriesNum++;
223                         }
224                 }
225
226                 return categoriesNum;
227         }
228
229         public static int getMatchingSubCategoriesNum(RestResponse getAllCategoryRest, String parentCategoryId,
230                         SubCategoryDefinition expectedSubCategoryDefinition) {
231
232                 String response = getAllCategoryRest.getResponse();
233                 Gson gson = new Gson();
234                 List<CategoryDefinition> categoryDefinitions = gson.fromJson(response,
235                                 new TypeToken<List<CategoryDefinition>>() {
236                                 }.getType());
237                 int subCatNum = 0;
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());
246                                                         subCatNum++;
247                                                 }
248                                         }
249                                 }
250
251                         }
252                 }
253                 return subCatNum;
254         }
255
256         public static int getMatchingGroupingNum(RestResponse getAllCategoryRest, String parentCategoryId,
257                         String subCategoryId, GroupingDefinition expectedGroupingDefinition) {
258
259                 String response = getAllCategoryRest.getResponse();
260                 Gson gson = new Gson();
261                 List<CategoryDefinition> categoryDefinitions = gson.fromJson(response,
262                                 new TypeToken<List<CategoryDefinition>>() {
263                                 }.getType());
264                 int groupingNum = 0;
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) {
271                                                 // if
272                                                 // (subCategoryId.equals(subCategoryDataDefinition.getUniqueId()))
273                                                 // {
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());
280                                                                         groupingNum++;
281                                                                 }
282                                                         }
283
284                                                 }
285                                         }
286                                 }
287
288                         }
289                 }
290                 return groupingNum;
291         }
292
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");
295                 
296                 private String auditJsonKeyName;
297
298                 private CategoryAuditJsonKeysEnum(String auditJsonKeyName) {
299                         this.auditJsonKeyName = auditJsonKeyName;
300                 }
301
302                 public String getAuditJsonKeyName() {
303                         return auditJsonKeyName.toLowerCase();
304                 }
305         }
306         
307
308 }