af4b9380fd2cc233ff599171dd3ba54f956f88da
[sdc.git] /
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.execute.devCI;
22
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.UnsupportedEncodingException;
26 import java.util.Base64.Decoder;
27
28 import javax.json.Json;
29
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Random;
36
37 import org.apache.cassandra.cli.CliParser.operator_return;
38 import org.apache.http.HttpEntity;
39 import org.apache.http.HttpResponse;
40 import org.apache.http.client.HttpResponseException;
41 import org.apache.http.client.methods.CloseableHttpResponse;
42 import org.apache.http.client.methods.HttpDelete;
43 import org.apache.http.client.methods.HttpGet;
44 import org.apache.http.client.methods.HttpPost;
45 import org.apache.http.entity.StringEntity;
46 import org.apache.http.impl.client.BasicResponseHandler;
47 import org.apache.http.util.EntityUtils;
48 import org.codehaus.jackson.map.ObjectMapper;
49 import org.json.simple.JSONObject;
50 import org.json.simple.parser.JSONParser;
51 import org.json.simple.parser.ParseException;
52 import org.junit.Rule;
53 import org.junit.rules.TestName;
54 import org.openecomp.sdc.be.dao.api.ActionStatus;
55 import org.openecomp.sdc.be.dao.graph.datatype.ActionEnum;
56 import org.openecomp.sdc.be.datatypes.enums.AssetTypeEnum;
57 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
58 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
59 import org.openecomp.sdc.be.model.ArtifactDefinition;
60 import org.openecomp.sdc.be.model.ArtifactUiDownloadData;
61 import org.openecomp.sdc.be.model.Component;
62 import org.openecomp.sdc.be.model.ComponentInstance;
63 import org.openecomp.sdc.be.model.Resource;
64 import org.openecomp.sdc.be.model.Service;
65 import org.openecomp.sdc.be.model.User;
66 import org.openecomp.sdc.be.model.category.CategoryDefinition;
67 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
68 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
69 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
70 import org.openecomp.sdc.ci.tests.api.Urls;
71 import org.openecomp.sdc.ci.tests.config.Config;
72 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
73 import org.openecomp.sdc.ci.tests.datatypes.ResourceDetailedAssetStructure;
74 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
75 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
76 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
77 import org.openecomp.sdc.ci.tests.datatypes.enums.DistributionNotificationStatusEnum;
78 import org.openecomp.sdc.ci.tests.datatypes.enums.ErrorInfo;
79 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
80 import org.openecomp.sdc.ci.tests.datatypes.enums.NormativeTypesEnum;
81 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
82 import org.openecomp.sdc.ci.tests.datatypes.enums.SearchCriteriaEnum;
83 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
84 import org.openecomp.sdc.ci.tests.datatypes.expected.ExpectedExternalAudit;
85 import org.openecomp.sdc.ci.tests.datatypes.expected.ExpectedResourceAuditJavaObject;
86 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
87 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
88 import org.openecomp.sdc.ci.tests.utils.Utils;
89 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
90 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
91 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
92 import org.openecomp.sdc.ci.tests.utils.rest.AssetRestUtils;
93 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
94 import org.openecomp.sdc.ci.tests.utils.rest.CategoryRestUtils;
95 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
96 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
97 import org.openecomp.sdc.ci.tests.utils.validation.AuditValidationUtils;
98 import org.openecomp.sdc.ci.tests.utils.validation.DistributionValidationUtils;
99 import org.openecomp.sdc.ci.tests.utils.validation.ErrorValidationUtils;
100 import org.openecomp.sdc.common.api.ApplicationErrorCodesEnum;
101 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
102 import org.openecomp.sdc.common.api.Constants;
103 import org.openecomp.sdc.common.api.UploadArtifactInfo;
104 import org.openecomp.sdc.common.datastructure.AuditingFieldsKeysEnum;
105 import org.openecomp.sdc.common.util.GeneralUtility;
106 import org.slf4j.Logger;
107 import org.slf4j.LoggerFactory;
108 import org.testng.Assert;
109 import org.testng.annotations.BeforeMethod;
110 import org.testng.annotations.DataProvider;
111 import org.testng.annotations.Test;
112
113 import org.openecomp.sdc.utils.Pair;
114 import com.google.common.base.CaseFormat;
115 import com.google.gson.Gson;
116 import com.google.gson.JsonArray;
117 import com.google.gson.JsonElement;
118 import com.google.gson.JsonObject;
119 import com.google.gson.JsonParser;
120 import com.relevantcodes.extentreports.LogStatus;
121
122 import fj.P;
123 import fj.data.Either;
124 import it.unimi.dsi.fastutil.bytes.ByteSortedSets.SynchronizedSortedSet;
125
126 import static java.util.Arrays.asList;
127
128 public class SearchFilterCategoryExternalAPI extends ComponentBaseTest {
129
130         private static Logger log = LoggerFactory.getLogger(CRUDExternalAPI.class.getName());
131
132         protected Config config = Config.instance();
133         protected String contentTypeHeaderData = "application/json";
134         protected String acceptHeaderDate = "application/json";
135
136         protected Gson gson = new Gson();
137         protected JSONParser jsonParser = new JSONParser();
138
139         @BeforeMethod
140         public void init() throws Exception{
141                 AtomicOperationUtils.createDefaultConsumer(true);
142         }
143         
144         ;
145         @Rule 
146         public static TestName name = new TestName();
147
148         public SearchFilterCategoryExternalAPI() {
149                 super(name, SearchFilterCategoryExternalAPI.class.getName());
150
151         }
152         
153         // Search for invalid resourceType
154         @Test
155         public void searchWithInvalidFilter() throws Exception {
156                 RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "invalid", ResourceTypeEnum.VFC.toString());
157                 
158                 Integer expectedResponseCode = 400;
159                 Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
160                 
161                 List<String> variables = Arrays.asList("resourceTypeinvalid", "[resourceType, subCategory, category]");
162                 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_FILTER_KEY.name(), variables, restResponse.getResponse());
163
164                 ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "invalid=" + ResourceTypeEnum.VFC.toString());
165                 ErrorInfo errorInfo = ErrorValidationUtils.parseErrorConfigYaml(ActionStatus.INVALID_FILTER_KEY.name());
166                 expectedExternalAudit.setDESC(AuditValidationUtils.buildAuditDescription(errorInfo, variables));
167                 expectedExternalAudit.setSTATUS("400");
168                 Map <AuditingFieldsKeysEnum, String> body = new HashMap<>();
169                 body.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_URL, expectedExternalAudit.getRESOURCE_URL());
170                 AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), body);
171         }
172         
173         @DataProvider(name="searchForResourceTypeNegativeTest") 
174         public static Object[][] dataProviderSearchForResourceTypeNegativeTest() {
175                 return new Object[][] {
176                         {"invalidResourceType"},
177                         {""}
178                         };
179         }
180         
181         // Search for invalid resourceType
182         @Test(dataProvider="searchForResourceTypeNegativeTest")
183         public void searchForResourceTypeNegativeTest(String resourceType) throws Exception {
184                 RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue(), resourceType);
185                 
186                 Integer expectedResponseCode = 400;
187                 Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
188                 
189                 List<String> variables = Arrays.asList();
190                 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_CONTENT.name(), variables, restResponse.getResponse());
191
192                 ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "=" + resourceType);
193                 ErrorInfo errorInfo = ErrorValidationUtils.parseErrorConfigYaml(ActionStatus.INVALID_CONTENT.name());
194                 expectedExternalAudit.setDESC(AuditValidationUtils.buildAuditDescription(errorInfo, variables));
195                 expectedExternalAudit.setSTATUS("400");
196                 Map <AuditingFieldsKeysEnum, String> body = new HashMap<>();
197                 body.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_URL, expectedExternalAudit.getRESOURCE_URL());
198                 AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), body);
199         }
200         
201         // Searching for resource filter incorrect resource type using external API
202         @Test
203         public void searchingForResouceFilterIncorrectResouceTypeUsingExternalAPI() throws Exception {
204                 Resource resource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VF, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_DATABASE, UserRoleEnum.DESIGNER, true).left().value();
205                 List<String> createdResoucesName = new ArrayList<String>();
206                 createdResoucesName.add(resource.getName());
207                 
208                 for(ResourceTypeEnum resourceTypeEnum: ResourceTypeEnum.values()) {
209                         // Create resource for each type so it will not return 404
210                         AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(resourceTypeEnum, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_DATABASE, UserRoleEnum.DESIGNER, true).left().value();
211
212                         RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue(), resourceTypeEnum.toString());
213                         
214                         Integer expectedResponseCode = 200;
215                         Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
216                         if(resourceTypeEnum == ResourceTypeEnum.VF) {
217                                 validateJsonContainResource(restResponse.getResponse(), createdResoucesName, true);
218                         } else {
219                                 validateJsonContainResource(restResponse.getResponse(), createdResoucesName, false);
220                         }
221                         
222                         
223                         ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "=" + resourceTypeEnum.toString());
224                         Map <AuditingFieldsKeysEnum, String> body = new HashMap<>();
225                         body.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_URL, expectedExternalAudit.getRESOURCE_URL());
226                         AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), body);
227                 }
228         }
229         
230         // Searching for several resource types using external API
231         @Test
232         public void searchingForSeveralResouceTypesUsingExternalAPI() throws Exception {
233                 for(ResourceTypeEnum resourceTypeEnum: ResourceTypeEnum.values()) {
234                         List<String> createdResoucesName = new ArrayList<String>();
235                         Resource resource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(resourceTypeEnum, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_DATABASE, UserRoleEnum.DESIGNER, true).left().value();
236                         createdResoucesName.add(resource.getName());
237                         
238                         RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue(), resourceTypeEnum.toString());
239                         
240                         Integer expectedResponseCode = 200;
241                         Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
242                         validateJsonContainResource(restResponse.getResponse(), createdResoucesName, true);
243                         
244                         ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "=" + resourceTypeEnum.toString());
245                         Map <AuditingFieldsKeysEnum, String> body = new HashMap<>();
246                         body.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_URL, expectedExternalAudit.getRESOURCE_URL());
247                         AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), body);
248                 }
249         }
250         
251         // Searching for several resources of type VFCMT using external API
252         @Test
253         public void searchingForSeveralResourcesOfTypeVFCMTUsingExternalAPI() throws Exception {
254                 Random random = new Random();
255                 int numberOfResouceToCreate = random.nextInt(5) + 1;    
256                 List<String> createdResoucesName = new ArrayList<String>();
257                 
258                 for(int i=0; i<numberOfResouceToCreate; i++) {
259                         
260                         Resource resource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VFCMT, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_DATABASE, UserRoleEnum.DESIGNER, true).left().value();
261                         createdResoucesName.add(resource.getName());
262                 }
263                 
264                 RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue(), ResourceTypeEnum.VFCMT.toString());
265                 
266                 Integer expectedResponseCode = 200;
267                 Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
268                 validateJsonContainResource(restResponse.getResponse(), createdResoucesName, true);
269                 
270                 ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "=" + ResourceTypeEnum.VFCMT.toString());
271                 AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), null);
272         }
273         
274         
275         
276         @DataProvider(name="normativeResourceCategory") 
277         public static Object[][] dataProviderNormativeResourceCategory() {
278                 return new Object[][] {
279                         {ResourceCategoryEnum.TEMPLATE_MONITORING_TEMPLATE},
280                         {ResourceCategoryEnum.TEMPLATE_MONITORING_TEMPLATE},
281                         };
282         }
283         
284         // Verify exist of normative resource category from data provider
285         @Test(dataProvider="normativeResourceCategory")
286         public void normativeResourceCategory(ResourceCategoryEnum resourceCategoryEnum) throws Exception {
287                 RestResponse restResponse = CategoryRestUtils.getAllCategories(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), "resources");
288                 validateJsonContainResourceCategory(restResponse.getResponse(), resourceCategoryEnum);
289                 
290                 Resource resource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VF, NormativeTypesEnum.ROOT, resourceCategoryEnum, UserRoleEnum.DESIGNER, true).left().value();
291                 List<String> createdResoucesName = new ArrayList<String>();
292                 createdResoucesName.add(resource.getName());
293                 
294                 restResponse = ResourceRestUtils.getResourceListFilterByCategory(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), resourceCategoryEnum.getCategory());
295                 
296                 Integer expectedResponseCode = 200;
297                 Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
298                 validateJsonContainResource(restResponse.getResponse(), createdResoucesName, true);
299         }
300         
301         protected void validateJsonContainResource(String json, List<String> resourceNameList, Boolean willBeFound) {
302                 int lenResourceNameList = resourceNameList.size();
303                 Gson gson = new Gson();
304                 JsonElement jsonElement = new JsonParser().parse(json);
305                 JsonArray jsonArray = jsonElement.getAsJsonArray();
306                 for(JsonElement jElement: jsonArray) {
307                         ResourceReqDetails jResource = gson.fromJson(jElement, ResourceReqDetails.class);
308                         
309                         if(resourceNameList.contains(jResource.getName())) {
310                                 resourceNameList.remove(jResource.getName());
311                         }
312                 }
313                 
314                 if(resourceNameList.size() != 0 && willBeFound) {                       
315                         Assert.assertTrue(false, "Created resource not found on search filtered by category.");
316                 } else if (lenResourceNameList != resourceNameList.size() & !willBeFound) {
317                         Assert.assertTrue(false, "Some of the resources found when expect that no resource will be found.");
318                 }
319         }
320         
321         
322         
323         protected void validateJsonContainResourceCategory(String json, ResourceCategoryEnum resourceCategoryEnum) {
324                 Gson gson = new Gson();
325                 JsonElement jelement = new JsonParser().parse(json);
326                 JsonArray jsonArray = jelement.getAsJsonArray();
327                 for(JsonElement jsonElement : jsonArray){
328                         CategoryDefinition categoryDefinition = gson.fromJson(jsonElement, CategoryDefinition.class);
329                         
330                         if(categoryDefinition.getName().equals(resourceCategoryEnum.getCategory())) { 
331                                 for(SubCategoryDefinition subcategory: categoryDefinition.getSubcategories()) {
332                                         if(subcategory.getName().equals(resourceCategoryEnum.getSubCategory())) {
333                                                 return;
334                                         }
335                                 }
336                         }
337                         
338                 }
339                 
340                 Assert.assertTrue(false, "Category and subcategory not found in categories list.");
341         }
342
343         
344 }