aboutsummaryrefslogtreecommitdiffstats
path: root/test-apis-ci/src/main/java/org/openecomp/sdc/externalApis/SearchFilterCategoryExternalAPI.java
blob: e446dc356e1d798386a3d376214c717139b64899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.externalApis;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.json.simple.parser.JSONParser;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.datatypes.enums.AssetTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.category.CategoryDefinition;
import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
import org.openecomp.sdc.ci.tests.config.Config;
import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.enums.NormativeTypesEnum;
import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
import org.openecomp.sdc.ci.tests.datatypes.enums.SearchCriteriaEnum;
import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
import org.openecomp.sdc.ci.tests.utils.rest.CategoryRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
import org.openecomp.sdc.ci.tests.utils.validation.ErrorValidationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class SearchFilterCategoryExternalAPI extends ComponentBaseTest {

	private static Logger log = LoggerFactory.getLogger(CRUDExternalAPI.class.getName());

	protected Config config = Config.instance();
	protected String contentTypeHeaderData = "application/json";
	protected String acceptHeaderDate = "application/json";

	protected Gson gson = new Gson();
	protected JSONParser jsonParser = new JSONParser();

//	@BeforeMethod
//	public void init() throws Exception{
//		AtomicOperationUtils.createDefaultConsumer(true);
//	}
	
	;
	@Rule 
	public static TestName name = new TestName();

	public SearchFilterCategoryExternalAPI() {
		super(name, SearchFilterCategoryExternalAPI.class.getName());

	}
	
	// Search for invalid resourceType
	@Test
	public void searchWithInvalidFilter() throws Exception {
		RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "invalid", ResourceTypeEnum.VFC.toString());
		
		Integer expectedResponseCode = 400;
		Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
		
		List<String> variables = Arrays.asList("resourceTypeinvalid", "[resourceType, subCategory, category]");
		ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_FILTER_KEY.name(), variables, restResponse.getResponse());

		/*validateFailureAudit(variables);*/
	}

	/*private void validateFailureAudit(List<String> variables) throws Exception {
		ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "invalid=" + ResourceTypeEnum.VFC.toString());
		ErrorInfo errorInfo = ErrorValidationUtils.parseErrorConfigYaml(ActionStatus.INVALID_FILTER_KEY.name());
		expectedExternalAudit.setDESC(AuditValidationUtils.buildAuditDescription(errorInfo, variables));
		expectedExternalAudit.setSTATUS("400");
		Map<AuditingFieldsKey, String> body = new HashMap<>();
		body.put(AuditingFieldsKey.AUDIT_RESOURCE_URL, expectedExternalAudit.getRESOURCE_URL());
		AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), body);
	}*/

	@DataProvider(name="searchForResourceTypeNegativeTest", parallel=true) 
	public static Object[][] dataProviderSearchForResourceTypeNegativeTest() {
		return new Object[][] {
			{"invalidResourceType"},
			{""}
			};
	}
	
	// Search for invalid resourceType
	@Test(dataProvider="searchForResourceTypeNegativeTest")
	public void searchForResourceTypeNegativeTest(String resourceType) throws Exception {
		RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue(), resourceType);
		
		Integer expectedResponseCode = 400;
		Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
		
		List<String> variables = Arrays.asList();
		ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_CONTENT.name(), variables, restResponse.getResponse());

		/*ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "=" + resourceType);
		ErrorInfo errorInfo = ErrorValidationUtils.parseErrorConfigYaml(ActionStatus.INVALID_CONTENT.name());
		expectedExternalAudit.setDESC(AuditValidationUtils.buildAuditDescription(errorInfo, variables));
		expectedExternalAudit.setSTATUS("400");
		Map <AuditingFieldsKey, String> body = new HashMap<>();
		body.put(AuditingFieldsKey.AUDIT_RESOURCE_URL, expectedExternalAudit.getRESOURCE_URL());
		AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), body);*/
	}
	
	// Searching for resource filter incorrect resource type using external API
	@Test
	public void searchingForResouceFilterIncorrectResouceTypeUsingExternalAPI() throws Exception {
		Resource resource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VF, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_DATABASE, UserRoleEnum.DESIGNER, true).left().value();
		List<String> createdResoucesName = new ArrayList<String>();
		createdResoucesName.add(resource.getName());
		
		for(ResourceTypeEnum resourceTypeEnum: ResourceTypeEnum.values()) {
			// Create resource for each type so it will not return 404
			AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(resourceTypeEnum, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_DATABASE, UserRoleEnum.DESIGNER, true).left().value();

			RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue(), resourceTypeEnum.toString());
			
			Integer expectedResponseCode = 200;
			Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
			if(resourceTypeEnum == ResourceTypeEnum.VF) {
				validateJsonContainResource(restResponse.getResponse(), createdResoucesName, true);
			} else {
				validateJsonContainResource(restResponse.getResponse(), createdResoucesName, false);
			}
			
			
			/*ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "=" + resourceTypeEnum.toString());
			Map <AuditingFieldsKey, String> body = new HashMap<>();
			body.put(AuditingFieldsKey.AUDIT_RESOURCE_URL, expectedExternalAudit.getRESOURCE_URL());
			AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), body);*/
		}
	}
	
	// Searching for several resource types using external API
	@Test
	public void searchingForSeveralResouceTypesUsingExternalAPI() throws Exception {
		performClean();
		for(ResourceTypeEnum resourceTypeEnum: ResourceTypeEnum.values()) {
			List<String> createdResoucesName = new ArrayList<String>();
			Resource resource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(resourceTypeEnum, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_DATABASE, UserRoleEnum.DESIGNER, true).left().value();
			createdResoucesName.add(resource.getName());
			
			RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue(), resourceTypeEnum.toString());
			
			Integer expectedResponseCode = 200;
			Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
			validateJsonContainResource(restResponse.getResponse(), createdResoucesName, true);
			
			/*ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "=" + resourceTypeEnum.toString());
			Map <AuditingFieldsKey, String> body = new HashMap<>();
			body.put(AuditingFieldsKey.AUDIT_RESOURCE_URL, expectedExternalAudit.getRESOURCE_URL());
			AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), body);*/
		}
	}
	
	// Searching for several resources of type VFCMT using external API
	@Test
	public void searchingForSeveralResourcesOfTypeVFCMTUsingExternalAPI() throws Exception {
		performClean();
		Random random = new Random();
 		int numberOfResouceToCreate = random.nextInt(5) + 1;	
		List<String> createdResoucesName = new ArrayList<String>();
		
		for(int i=0; i<numberOfResouceToCreate; i++) {
			
			Resource resource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VFCMT, NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_DATABASE, UserRoleEnum.DESIGNER, true).left().value();
			createdResoucesName.add(resource.getName());
		}
		
		RestResponse restResponse = ResourceRestUtils.getResourceListFilterByCriteria(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), SearchCriteriaEnum.RESOURCE_TYPE.getValue(), ResourceTypeEnum.VFCMT.toString());
		
		Integer expectedResponseCode = 200;
		Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
		validateJsonContainResource(restResponse.getResponse(), createdResoucesName, true);
		
		/*ExpectedExternalAudit expectedExternalAudit = ElementFactory.getDefaultExternalAuditObject(AssetTypeEnum.RESOURCES, AuditingActionEnum.GET_FILTERED_ASSET_LIST, "?" + SearchCriteriaEnum.RESOURCE_TYPE.getValue() + "=" + ResourceTypeEnum.VFCMT.toString());
		AuditValidationUtils.validateAuditExternalSearchAPI(expectedExternalAudit, AuditingActionEnum.GET_FILTERED_ASSET_LIST.getName(), null);*/
	}
	
	
	
	@DataProvider(name="normativeResourceCategory", parallel=true) 
	public static Object[][] dataProviderNormativeResourceCategory() {
		return new Object[][] {
			{ResourceCategoryEnum.ALLOTTED_RESOURCE},
			{ResourceCategoryEnum.TEMPLATE_MONITORING_TEMPLATE},
			};
	}
	
	// Verify exist of normative resource category from data provider
	@Test(dataProvider="normativeResourceCategory")
	public void normativeResourceCategory(ResourceCategoryEnum resourceCategoryEnum) throws Exception {
		RestResponse restResponse = CategoryRestUtils.getAllCategories(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), "resources");
		validateJsonContainResourceCategory(restResponse.getResponse(), resourceCategoryEnum);
		
		Resource resource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VF, NormativeTypesEnum.ROOT, resourceCategoryEnum, UserRoleEnum.DESIGNER, true).left().value();
		List<String> createdResoucesName = new ArrayList<String>();
		createdResoucesName.add(resource.getName());
		
		restResponse = ResourceRestUtils.getResourceListFilterByCategory(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), resourceCategoryEnum.getCategory());
		
		Integer expectedResponseCode = 200;
		Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
		validateJsonContainResource(restResponse.getResponse(), createdResoucesName, true);
	}
	
	// Verify search filter by sub-category
	@Test
	public void validateFilterBySubcategory() throws Exception {
		ResourceCategoryEnum resourceCategoryEnum = getRandomCategoryFromResourceCategoryEnum();
		
		Resource resource = AtomicOperationUtils.createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum.VF, NormativeTypesEnum.ROOT, resourceCategoryEnum, UserRoleEnum.DESIGNER, true).left().value();
		List<String> createdResoucesName = new ArrayList<String>();
		createdResoucesName.add(resource.getName());
		
		RestResponse restResponse = ResourceRestUtils.getResourceListFilterBySubCategory(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), AssetTypeEnum.RESOURCES.getValue(), resourceCategoryEnum.getSubCategory());
		
		Integer expectedResponseCode = 200;
		Assert.assertEquals(restResponse.getErrorCode(), expectedResponseCode);
		validateJsonContainResource(restResponse.getResponse(), createdResoucesName, true);
	}
	
	
	protected ResourceCategoryEnum getRandomCategoryFromResourceCategoryEnum() throws Exception {
		Random random = new Random();
		int randint = random.nextInt(ResourceCategoryEnum.values().length);
		int i = 0;
		for(ResourceCategoryEnum resourceTypeEnum: ResourceCategoryEnum.values()) {
			if(randint == i) {
				return resourceTypeEnum;
			}
			i++;
		}
		return null;
	}
	
	protected CategoryDefinition getRandomCategory() throws Exception {
		Random random = new Random();
		
		RestResponse restResponse = CategoryRestUtils.getAllCategories(ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), "resources");
		Gson gson = new Gson();
		JsonElement jelement = new JsonParser().parse(restResponse.getResponse());
		JsonArray jsonArray = jelement.getAsJsonArray();
		
		CategoryDefinition categoryDefinition = gson.fromJson(jsonArray.get(random.nextInt(jsonArray.size())), CategoryDefinition.class);
		
		return categoryDefinition;
	}
	
	protected void validateJsonContainResource(String json, List<String> resourceNameList, Boolean willBeFound) {
		int lenResourceNameList = resourceNameList.size();
		Gson gson = new Gson();
		JsonElement jsonElement = new JsonParser().parse(json);
		JsonArray jsonArray = jsonElement.getAsJsonArray();
		for(JsonElement jElement: jsonArray) {
			ResourceReqDetails jResource = gson.fromJson(jElement, ResourceReqDetails.class);
			
			if(resourceNameList.contains(jResource.getName())) {
				resourceNameList.remove(jResource.getName());
			}
		}
		
		if(resourceNameList.size() != 0 && willBeFound) {			
			Assert.assertTrue(false, "Created resource not found on search filtered by category.");
		} else if (lenResourceNameList != resourceNameList.size() & !willBeFound) {
			Assert.assertTrue(false, "Some of the resources found when expect that no resource will be found.");
		}
	}
	
	
	
	protected void validateJsonContainResourceCategory(String json, ResourceCategoryEnum resourceCategoryEnum) {
		Gson gson = new Gson();
		JsonElement jelement = new JsonParser().parse(json);
		JsonArray jsonArray = jelement.getAsJsonArray();
		for(JsonElement jsonElement : jsonArray){
			CategoryDefinition categoryDefinition = gson.fromJson(jsonElement, CategoryDefinition.class);
			
			if(categoryDefinition.getName().equals(resourceCategoryEnum.getCategory())) { 
				for(SubCategoryDefinition subcategory: categoryDefinition.getSubcategories()) {
					if(subcategory.getName().equals(resourceCategoryEnum.getSubCategory())) {
						return;
					}
				}
			}
			
		}
		
		Assert.assertTrue(false, "Category and subcategory not found in categories list.");
	}

	
}