Fix for radio buttons
[sdc.git] / asdc-tests / src / main / java / org / openecomp / sdc / ci / tests / execute / devCI / SearchFilterCategoryExternalAPI.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.execute.devCI;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Random;
29
30 import org.json.simple.parser.JSONParser;
31 import org.junit.Rule;
32 import org.junit.rules.TestName;
33 import org.openecomp.sdc.be.dao.api.ActionStatus;
34 import org.openecomp.sdc.be.datatypes.enums.AssetTypeEnum;
35 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
36 import org.openecomp.sdc.be.model.Resource;
37 import org.openecomp.sdc.be.model.category.CategoryDefinition;
38 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
39 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
40 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
41 import org.openecomp.sdc.ci.tests.config.Config;
42 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
43 import org.openecomp.sdc.ci.tests.datatypes.enums.ErrorInfo;
44 import org.openecomp.sdc.ci.tests.datatypes.enums.NormativeTypesEnum;
45 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
46 import org.openecomp.sdc.ci.tests.datatypes.enums.SearchCriteriaEnum;
47 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
48 import org.openecomp.sdc.ci.tests.datatypes.expected.ExpectedExternalAudit;
49 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
50 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
51 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
52 import org.openecomp.sdc.ci.tests.utils.rest.CategoryRestUtils;
53 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
54 import org.openecomp.sdc.ci.tests.utils.validation.AuditValidationUtils;
55 import org.openecomp.sdc.ci.tests.utils.validation.ErrorValidationUtils;
56 import org.openecomp.sdc.common.datastructure.AuditingFieldsKeysEnum;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59 import org.testng.Assert;
60 import org.testng.annotations.BeforeMethod;
61 import org.testng.annotations.DataProvider;
62 import org.testng.annotations.Test;
63
64 import com.google.gson.Gson;
65 import com.google.gson.JsonArray;
66 import com.google.gson.JsonElement;
67 import com.google.gson.JsonParser;
68
69 public class SearchFilterCategoryExternalAPI extends ComponentBaseTest {
70
71         private static Logger log = LoggerFactory.getLogger(CRUDExternalAPI.class.getName());
72
73         protected Config config = Config.instance();
74         protected String contentTypeHeaderData = "application/json";
75         protected String acceptHeaderDate = "application/json";
76
77         protected Gson gson = new Gson();
78         protected JSONParser jsonParser = new JSONParser();
79
80         @BeforeMethod
81         public void init() throws Exception{
82                 AtomicOperationUtils.createDefaultConsumer(true);
83         }
84         
85         ;
86         @Rule 
87         public static TestName name = new TestName();
88
89         public SearchFilterCategoryExternalAPI() {
90                 super(name, SearchFilterCategoryExternalAPI.class.getName());
91
92         }
93         
94         // Search for invalid resourceType
95         @Test
96         public void searchWithInvalidFilter() throws Exception {
97                 RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "invalid", ResourceTypeEnum.VFC.toString());
98                 
99                 Integer expectedResponseCode = 400;
100                 Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
101                 
102                 List<String> variables = Arrays.asList("resourceTypeinvalid", "[resourceType, subCategory, category]");
103                 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_FILTER_KEY.name(), variables, restResponse.getResponse());
104
105                 ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "invalid=" + ResourceTypeEnum.VFC.toString());
106                 ErrorInfo errorInfo = ErrorValidationUtils.parseErrorConfigYaml(ActionStatus.INVALID_FILTER_KEY.name());
107                 expectedExternalAudit.setDESC(AuditValidationUtils.buildAuditDescription(errorInfo, variables));
108                 expectedExternalAudit.setSTATUS("400");
109                 Map <AuditingFieldsKeysEnum, String> body = new HashMap<>();
110                 body.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_URL, expectedExternalAudit.getRESOURCE_URL());
111                 AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), body);
112         }
113         
114         @DataProvider(name="searchForResourceTypeNegativeTest") 
115         public static Object[][] dataProviderSearchForResourceTypeNegativeTest() {
116                 return new Object[][] {
117                         {"invalidResourceType"},
118                         {""}
119                         };
120         }
121         
122         // Search for invalid resourceType
123         @Test(dataProvider="searchForResourceTypeNegativeTest")
124         public void searchForResourceTypeNegativeTest(String resourceType) throws Exception {
125                 RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue(), resourceType);
126                 
127                 Integer expectedResponseCode = 400;
128                 Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
129                 
130                 List<String> variables = Arrays.asList();
131                 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_CONTENT.name(), variables, restResponse.getResponse());
132
133                 ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "=" + resourceType);
134                 ErrorInfo errorInfo = ErrorValidationUtils.parseErrorConfigYaml(ActionStatus.INVALID_CONTENT.name());
135                 expectedExternalAudit.setDESC(AuditValidationUtils.buildAuditDescription(errorInfo, variables));
136                 expectedExternalAudit.setSTATUS("400");
137                 Map <AuditingFieldsKeysEnum, String> body = new HashMap<>();
138                 body.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_URL, expectedExternalAudit.getRESOURCE_URL());
139                 AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), body);
140         }
141         
142         // Searching for resource filter incorrect resource type using external API
143         @Test
144         public void searchingForResouceFilterIncorrectResouceTypeUsingExternalAPI() throws Exception {
145                 Resource resource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VF, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_DATABASE, UserRoleEnum.DESIGNER, true).left().value();
146                 List<String> createdResoucesName = new ArrayList<String>();
147                 createdResoucesName.add(resource.getName());
148                 
149                 for(ResourceTypeEnum resourceTypeEnum: ResourceTypeEnum.values()) {
150                         // Create resource for each type so it will not return 404
151                         AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(resourceTypeEnum, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_DATABASE, UserRoleEnum.DESIGNER, true).left().value();
152
153                         RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue(), resourceTypeEnum.toString());
154                         
155                         Integer expectedResponseCode = 200;
156                         Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
157                         if(resourceTypeEnum == ResourceTypeEnum.VF) {
158                                 validateJsonContainResource(restResponse.getResponse(), createdResoucesName, true);
159                         } else {
160                                 validateJsonContainResource(restResponse.getResponse(), createdResoucesName, false);
161                         }
162                         
163                         
164                         ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "=" + resourceTypeEnum.toString());
165                         Map <AuditingFieldsKeysEnum, String> body = new HashMap<>();
166                         body.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_URL, expectedExternalAudit.getRESOURCE_URL());
167                         AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), body);
168                 }
169         }
170         
171         // Searching for several resource types using external API
172         @Test
173         public void searchingForSeveralResouceTypesUsingExternalAPI() throws Exception {
174                 for(ResourceTypeEnum resourceTypeEnum: ResourceTypeEnum.values()) {
175                         List<String> createdResoucesName = new ArrayList<String>();
176                         Resource resource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(resourceTypeEnum, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_DATABASE, UserRoleEnum.DESIGNER, true).left().value();
177                         createdResoucesName.add(resource.getName());
178                         
179                         RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue(), resourceTypeEnum.toString());
180                         
181                         Integer expectedResponseCode = 200;
182                         Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
183                         validateJsonContainResource(restResponse.getResponse(), createdResoucesName, true);
184                         
185                         ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "=" + resourceTypeEnum.toString());
186                         Map <AuditingFieldsKeysEnum, String> body = new HashMap<>();
187                         body.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_URL, expectedExternalAudit.getRESOURCE_URL());
188                         AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), body);
189                 }
190         }
191         
192         // Searching for several resources of type VFCMT using external API
193         @Test
194         public void searchingForSeveralResourcesOfTypeVFCMTUsingExternalAPI() throws Exception {
195                 Random random = new Random();
196                 int numberOfResouceToCreate = random.nextInt(5) + 1;    
197                 List<String> createdResoucesName = new ArrayList<String>();
198                 
199                 for(int i=0; i<numberOfResouceToCreate; i++) {
200                         
201                         Resource resource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VFCMT, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_DATABASE, UserRoleEnum.DESIGNER, true).left().value();
202                         createdResoucesName.add(resource.getName());
203                 }
204                 
205                 RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue(), ResourceTypeEnum.VFCMT.toString());
206                 
207                 Integer expectedResponseCode = 200;
208                 Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
209                 validateJsonContainResource(restResponse.getResponse(), createdResoucesName, true);
210                 
211                 ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "=" + ResourceTypeEnum.VFCMT.toString());
212                 AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), null);
213         }
214         
215         
216         
217         @DataProvider(name="normativeResourceCategory") 
218         public static Object[][] dataProviderNormativeResourceCategory() {
219                 return new Object[][] {
220                         {ResourceCategoryEnum.TEMPLATE_MONITORING_TEMPLATE},
221                         {ResourceCategoryEnum.TEMPLATE_MONITORING_TEMPLATE},
222                         };
223         }
224         
225         // Verify exist of normative resource category from data provider
226         @Test(dataProvider="normativeResourceCategory")
227         public void normativeResourceCategory(ResourceCategoryEnum resourceCategoryEnum) throws Exception {
228                 RestResponse restResponse = CategoryRestUtils.getAllCategories(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), "resources");
229                 validateJsonContainResourceCategory(restResponse.getResponse(), resourceCategoryEnum);
230                 
231                 Resource resource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VF, NormativeTypesEnum.ROOT, resourceCategoryEnum, UserRoleEnum.DESIGNER, true).left().value();
232                 List<String> createdResoucesName = new ArrayList<String>();
233                 createdResoucesName.add(resource.getName());
234                 
235                 restResponse = ResourceRestUtils.getResourceListFilterByCategory(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), resourceCategoryEnum.getCategory());
236                 
237                 Integer expectedResponseCode = 200;
238                 Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
239                 validateJsonContainResource(restResponse.getResponse(), createdResoucesName, true);
240         }
241         
242         protected void validateJsonContainResource(String json, List<String> resourceNameList, Boolean willBeFound) {
243                 int lenResourceNameList = resourceNameList.size();
244                 Gson gson = new Gson();
245                 JsonElement jsonElement = new JsonParser().parse(json);
246                 JsonArray jsonArray = jsonElement.getAsJsonArray();
247                 for(JsonElement jElement: jsonArray) {
248                         ResourceReqDetails jResource = gson.fromJson(jElement, ResourceReqDetails.class);
249                         
250                         if(resourceNameList.contains(jResource.getName())) {
251                                 resourceNameList.remove(jResource.getName());
252                         }
253                 }
254                 
255                 if(resourceNameList.size() != 0 && willBeFound) {                       
256                         Assert.assertTrue(false, "Created resource not found on search filtered by category.");
257                 } else if (lenResourceNameList != resourceNameList.size() & !willBeFound) {
258                         Assert.assertTrue(false, "Some of the resources found when expect that no resource will be found.");
259                 }
260         }
261         
262         
263         
264         protected void validateJsonContainResourceCategory(String json, ResourceCategoryEnum resourceCategoryEnum) {
265                 Gson gson = new Gson();
266                 JsonElement jelement = new JsonParser().parse(json);
267                 JsonArray jsonArray = jelement.getAsJsonArray();
268                 for(JsonElement jsonElement : jsonArray){
269                         CategoryDefinition categoryDefinition = gson.fromJson(jsonElement, CategoryDefinition.class);
270                         
271                         if(categoryDefinition.getName().equals(resourceCategoryEnum.getCategory())) { 
272                                 for(SubCategoryDefinition subcategory: categoryDefinition.getSubcategories()) {
273                                         if(subcategory.getName().equals(resourceCategoryEnum.getSubCategory())) {
274                                                 return;
275                                         }
276                                 }
277                         }
278                         
279                 }
280                 
281                 Assert.assertTrue(false, "Category and subcategory not found in categories list.");
282         }
283
284         
285 }