aboutsummaryrefslogtreecommitdiffstats
path: root/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/execute/property/PropertyApisTest.java
blob: 789b118419d7926248580e005de555ac0eebd3c3 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*-
 * ============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.ci.tests.execute.property;

import static org.testng.AssertJUnit.assertTrue;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
import org.openecomp.sdc.ci.tests.api.Urls;
import org.openecomp.sdc.ci.tests.config.Config;
import org.openecomp.sdc.ci.tests.datatypes.PropertyReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.ci.tests.preRequisites.SimpleOneRsrcOneServiceTest;
import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
import org.openecomp.sdc.ci.tests.utils.rest.PropertyRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
import org.openecomp.sdc.ci.tests.utils.rest.UserRestUtils;
import org.openecomp.sdc.ci.tests.utils.validation.ErrorValidationUtils;
import org.testng.AssertJUnit;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class PropertyApisTest extends SimpleOneRsrcOneServiceTest {

	protected static final String RESOURCE_CATEGORY = "Generic/Databases";
	protected Config config = Config.instance();
	protected String contentTypeHeaderData = "application/json";
	protected String acceptHeaderDate = "application/json";;
	protected PropertyReqDetails property;
	protected String body;

	protected HttpRequest httpRequest = new HttpRequest();
	protected Map<String, String> headersMap = new HashMap<String, String>();

	@Rule
	public static TestName testName = new TestName();

	public PropertyApisTest() {
		super(testName, PropertyApisTest.class.getName());
	}

	@BeforeMethod
	public void init() throws Exception {
		property = ElementFactory.getDefaultProperty();
		body = property.propertyToJsonString();
		headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
		headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
		headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncDesignerDetails.getUserId());

	}

	@Test
	public void validateValidValueConstraintFailTest() throws Exception {
		body = "{\"SubnetProp\": {\"schema\": {\"property\": {\"type\": \"\"}},\"type\": \"org.openecomp.datatypes.heat.network.neutron.Subnet\",\"name\": \"SubnetProp\"}}";
		RestResponse createPropertyResponse = PropertyRestUtils.createProperty(getResourceId(resourceDetails), body,
				sdncDesignerDetails);
		AssertJUnit.assertEquals("Expected result code - 200, received - " + createPropertyResponse.getErrorCode(), 200,
				(int) createPropertyResponse.getErrorCode());

		ObjectMapper mapper = new ObjectMapper();
		mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		try {
			PropertyReqDetails propertyResponseObject = mapper.readValue(createPropertyResponse.getResponse(),
					PropertyReqDetails.class);

			PropertyReqDetails propertyReqDetails = new PropertyReqDetails();
			propertyReqDetails.setPropertyType("org.openecomp.datatypes.heat.network.neutron.Subnet");
			propertyReqDetails.setName(propertyResponseObject.getName());
			propertyReqDetails.setValue(mapper.writeValueAsString(Collections.singletonMap("ipv6_address_mode", "Fail")));
			propertyReqDetails.setParentUniqueId(propertyResponseObject.getParentUniqueId());
			propertyReqDetails.setUniqueId(propertyResponseObject.getUniqueId());

			body = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(Collections.singletonList(propertyReqDetails));
			body = body.replace("propertyType", "type");
			RestResponse addValueToPropertyResponse =
					PropertyRestUtils.addValueToProperty(getResourceId(resourceDetails), body, sdncDesignerDetails);

			AssertJUnit.assertEquals("Expected result code - 400, received - " + addValueToPropertyResponse.getErrorCode(),
					400, (int) addValueToPropertyResponse.getErrorCode());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void validateValidValueConstraintSuccessTest() throws Exception {
		body = "{\"SubnetProp\": {\"schema\": {\"property\": {\"type\": \"\"}},\"type\": \"org.openecomp.datatypes.heat.network.neutron.Subnet\",\"name\": \"SubnetProp\"}}";
		RestResponse createPropertyResponse = PropertyRestUtils.createProperty(getResourceId(resourceDetails), body,
				sdncDesignerDetails);
		AssertJUnit.assertEquals("Expected result code - 201, received - " + createPropertyResponse.getErrorCode(), 200,
				(int) createPropertyResponse.getErrorCode());

		ObjectMapper mapper = new ObjectMapper();
		mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		try {
			PropertyReqDetails propertyResponseObject = mapper.readValue(createPropertyResponse.getResponse(),
					PropertyReqDetails.class);

			PropertyReqDetails propertyReqDetails = new PropertyReqDetails();
			propertyReqDetails.setPropertyType("org.openecomp.datatypes.heat.network.neutron.Subnet");
			propertyReqDetails.setName(propertyResponseObject.getName());
			propertyReqDetails.setValue(mapper.writeValueAsString(Collections.singletonMap("ipv6_address_mode", "slaac")));
			propertyReqDetails.setParentUniqueId(propertyResponseObject.getParentUniqueId());
			propertyReqDetails.setUniqueId(propertyResponseObject.getUniqueId());

			body = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(Collections.singletonList(propertyReqDetails));
			body = body.replace("propertyType", "type");
			RestResponse addValueToPropertyResponse =
					PropertyRestUtils.addValueToProperty(getResourceId(resourceDetails), body, sdncDesignerDetails);

			AssertJUnit.assertEquals("Expected result code - 200, received - " + addValueToPropertyResponse.getErrorCode(),
					200, (int) addValueToPropertyResponse.getErrorCode());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testPropertyApis() throws Exception {
		String propertyId = UniqueIdBuilder.buildComponentPropertyUniqueId(getResourceId(resourceDetails), property.getName());

		PropertyRestUtils.deleteProperty(getResourceId(resourceDetails), propertyId, sdncDesignerDetails);
		RestResponse createPropertyResponse = PropertyRestUtils.createProperty(getResourceId(resourceDetails), body,
				sdncDesignerDetails);
		AssertJUnit.assertTrue("Expected result code - 201, received - " + createPropertyResponse.getErrorCode(),
				createPropertyResponse.getErrorCode() == 201);

		// Get property
		RestResponse getPropertyResponse = PropertyRestUtils.getProperty(getResourceId(resourceDetails), propertyId,
				sdncDesignerDetails);
		AssertJUnit.assertTrue("Expected result code - 200, received - " + getPropertyResponse.getErrorCode(),
				getPropertyResponse.getErrorCode() == 200);

		JSONObject jsonResp = (JSONObject) JSONValue.parse(getPropertyResponse.getResponse());

		// Update property
		property.setPropertyDescription("Updated description");
		body = property.propertyToJsonString();

		RestResponse updatePropertyResponse = PropertyRestUtils.updateProperty(getResourceId(resourceDetails),
				propertyId, body, sdncDesignerDetails);
		AssertJUnit.assertTrue("Expected result code - 200, received - " + updatePropertyResponse.getErrorCode(),
				updatePropertyResponse.getErrorCode() == 200);

		// Get property
		getPropertyResponse = PropertyRestUtils.getProperty(getResourceId(resourceDetails), propertyId,
				sdncDesignerDetails);
		AssertJUnit.assertTrue("Expected result code - 200, received - " + getPropertyResponse.getErrorCode(),
				getPropertyResponse.getErrorCode() == 200);

		jsonResp = (JSONObject) JSONValue.parse(getPropertyResponse.getResponse());

		// Delete property
		RestResponse deletePropertyResponse = PropertyRestUtils.deleteProperty(getResourceId(resourceDetails),
				propertyId, sdncDesignerDetails);
		AssertJUnit.assertTrue("Expected result code - 204, received - " + deletePropertyResponse.getErrorCode(),
				deletePropertyResponse.getErrorCode() == 204);

		// Get property - verify that the property doesn't exist.
		getPropertyResponse = PropertyRestUtils.getProperty(getResourceId(resourceDetails), propertyId,
				sdncDesignerDetails);
		List<String> variables = Arrays.asList("");
		ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.PROPERTY_NOT_FOUND.name(), variables,
				getPropertyResponse.getResponse());

	}

	// --------------------------------------------------------------------------------------

	protected String getPropertyId(ResourceReqDetails resource, PropertyReqDetails property) {
		return UniqueIdBuilder.buildComponentPropertyUniqueId(resource.getUniqueId(), property.getName());
	}

	protected String getResourceId(ResourceReqDetails resource) {
		return resource.getUniqueId();
	}

	protected User createUser(String cspUserId, String firstName, String lastName, String email, String role)
			throws Exception {
		User sdncUserDetails = new User(firstName, lastName, cspUserId, email, role, null);

		User adminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
		UserRestUtils.createUser(sdncUserDetails, adminUser);

		return sdncUserDetails;
	}

	protected ResourceReqDetails createResource(User sdncUserDetails, String resourceName) throws Exception {
		String description = "description";
		ArrayList<String> resourceTags = new ArrayList<String>();
		resourceTags.add(resourceName);
		ArrayList<String> derivedFrom = new ArrayList<String>();
		derivedFrom.add("tosca.nodes.Root");
		String vendorName = "Oracle";
		String vendorRelease = "1.0";
		String contactId = sdncUserDetails.getUserId();
		String icon = "myICON";

		ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, null,
				derivedFrom, vendorName, vendorRelease, contactId, icon);
		resourceDetails.addCategoryChain(ResourceCategoryEnum.GENERIC_DATABASE.getCategory(),
				ResourceCategoryEnum.GENERIC_DATABASE.getSubCategory());
		// TODO delete by name
		// deleteResource(UniqueIdBuilder.buildResourceUniqueId(resourceName,
		// "0.1"), sdncUserDetails.getUserId());
		RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncUserDetails);
		AssertJUnit.assertTrue(createResource.getErrorCode().intValue() == 201);
		String resourceId = ResponseParser.getUniqueIdFromResponse(createResource);
		resourceDetails.setUniqueId(resourceId);

		return resourceDetails;

	}

	@Test
	public void putReqToCreateUriNotAllowed() throws Exception {
		String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
				getResourceId(resourceDetails));
		RestResponse propertyErrorResponse = httpRequest.httpSendByMethod(url, "PUT", body, headersMap);
		List<String> variables = Arrays.asList();
		ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.NOT_ALLOWED.name(), variables,
				propertyErrorResponse.getResponse());
	}

	@Test
	public void getReqToCreateUriNotAllowed() throws Exception {
		String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
				getResourceId(resourceDetails));
		RestResponse propertyErrorResponse = httpRequest.httpSendGet(url, headersMap);
		List<String> variables = Arrays.asList();
		ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.NOT_ALLOWED.name(), variables,
				propertyErrorResponse.getResponse());
	}

	@Test
	public void deleteReqToCreateUriNotAllowed() throws Exception {
		String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
				getResourceId(resourceDetails));
		RestResponse propertyErrorResponse = httpRequest.httpSendDelete(url, headersMap);
		List<String> variables = Arrays.asList();
		ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.NOT_ALLOWED.name(), variables,
				propertyErrorResponse.getResponse());
	}

	@Test
	public void postReqToUpdateUriNotAllowed() throws Exception {
		String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
				getResourceId(resourceDetails), getPropertyId(resourceDetails, property));
		RestResponse propertyErrorResponse = httpRequest.httpSendPost(url, body, headersMap);
		List<String> variables = Arrays.asList();
		ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.NOT_ALLOWED.name(), variables,
				propertyErrorResponse.getResponse());
	}

	@Test
	public void deleteReqPropertyNotFound() throws Exception {
		String unknownPropertyId = getPropertyId(resourceDetails, property) + "111";
		String url = String.format(Urls.DELETE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
				getResourceId(resourceDetails), unknownPropertyId);
		RestResponse propertyErrorResponse = httpRequest.httpSendDelete(url, headersMap);
		List<String> variables = Arrays.asList("");
		ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.PROPERTY_NOT_FOUND.name(), variables,
				propertyErrorResponse.getResponse());
	}

	@Test
	public void updateReqPropertyNotFound() throws Exception {
		String unknownPropertyId = getPropertyId(resourceDetails, property) + "111";
		String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
				getResourceId(resourceDetails), unknownPropertyId);
		RestResponse propertyErrorResponse = httpRequest.httpSendByMethod(url, "PUT", body, headersMap);
		List<String> variables = Arrays.asList("");
		ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.PROPERTY_NOT_FOUND.name(), variables,
				propertyErrorResponse.getResponse());
	}

	@Test
	public void modifierNotTheStateOwner() throws Exception {
		// Operation Not Allowed ----");
		User sdncUserDetails2 = createUser("tu5555", "Test", "User", "tu5555@intl.sdc.com", "DESIGNER");
		headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncUserDetails2.getUserId());
		property.setPropertyDescription("new description");
		body = property.propertyToJsonString();
		String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
				getResourceId(resourceDetails), getPropertyId(resourceDetails, property));
		RestResponse propertyErrorResponse = httpRequest.httpSendByMethod(url, "PUT", body, headersMap);
		List<String> variables = Arrays.asList();
		ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.RESTRICTED_OPERATION.name(), variables,
				propertyErrorResponse.getResponse());

	}

	@Test
	public void postReqInvalidContent() throws Exception {
		body = "invalid";
		String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
				getResourceId(resourceDetails), getPropertyId(resourceDetails, property));
		RestResponse propertyErrorResponse = httpRequest.httpSendPost(url, body, headersMap);

		List<String> variables = Arrays.asList();
		ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_CONTENT.name(), variables,
				propertyErrorResponse.getResponse());
	}

	@Test
	public void putReqInvalidContent() throws Exception {

		// Create property
		RestResponse createPropertyResponse = PropertyRestUtils.createProperty(getResourceId(resourceDetails), body,
				sdncDesignerDetails);
		assertTrue("Expected result code - 201, received - " + createPropertyResponse.getErrorCode(),
				createPropertyResponse.getErrorCode() == 201);

		body = "invalid";

		String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
				getResourceId(resourceDetails), getPropertyId(resourceDetails, property));

		RestResponse propertyErrorResponse = httpRequest.httpSendByMethod(url, "PUT", body, headersMap);

		List<String> variables = Arrays.asList();
		ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_CONTENT.name(), variables,
				propertyErrorResponse.getResponse());
	}
}