2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.execute.property;
23 import static org.testng.AssertJUnit.assertTrue;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.List;
31 import org.json.simple.JSONObject;
32 import org.json.simple.JSONValue;
33 import org.junit.Rule;
34 import org.junit.rules.TestName;
35 import org.openecomp.sdc.be.dao.api.ActionStatus;
36 import org.openecomp.sdc.be.model.User;
37 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
38 import org.openecomp.sdc.ci.tests.api.Urls;
39 import org.openecomp.sdc.ci.tests.config.Config;
40 import org.openecomp.sdc.ci.tests.datatypes.PropertyReqDetails;
41 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
42 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
43 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
44 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
45 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
46 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
47 import org.openecomp.sdc.ci.tests.preRequisites.SimpleOneRsrcOneServiceTest;
48 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
49 import org.openecomp.sdc.ci.tests.utils.rest.PropertyRestUtils;
50 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
51 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
52 import org.openecomp.sdc.ci.tests.utils.rest.UserRestUtils;
53 import org.openecomp.sdc.ci.tests.utils.validation.ErrorValidationUtils;
54 import org.testng.AssertJUnit;
55 import org.testng.annotations.BeforeMethod;
56 import org.testng.annotations.Test;
58 public class PropertyApisTest extends SimpleOneRsrcOneServiceTest {
60 protected static final String RESOURCE_CATEGORY = "Generic/Databases";
61 protected Config config = Config.instance();
62 protected String contentTypeHeaderData = "application/json";
63 protected String acceptHeaderDate = "application/json";;
65 // protected User sdncDesignerDetails;
66 // protected ResourceReqDetails resourceDetails;
67 protected PropertyReqDetails property;
68 protected String body;
70 protected HttpRequest httpRequest = new HttpRequest();
71 protected Map<String, String> headersMap = new HashMap<String, String>();
74 public static TestName testName = new TestName();
76 public PropertyApisTest() {
77 super(testName, PropertyApisTest.class.getName());
81 public void init() throws Exception {
84 // resourceDetails = new ResourceReqDetails();
85 // resourceDetails.setResourceName("testresourceDetails");
87 // resourceUtils.deleteResource_allVersions(resourceDetails,
88 // sdncDesignerDetails);
91 // resourceDetails = createResource(sdncDesignerDetails,
92 // "testresourceDetails");
95 // property.setPropertyName("test");
96 // property.setPropertyType("integer");
97 // property.setPropertySource("A&AI");
98 // property.setPropertyDescription("test property");
100 // body = gson.toJson(property);
101 property = ElementFactory.getDefaultProperty();
102 body = property.propertyToJsonString();
103 // System.out.println(body);
104 // HTTP (for negative tests)
105 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
106 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
107 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncDesignerDetails.getUserId());
112 public void testPropertyApis() throws Exception {
114 // System.out.println ("---- Create Property (POST) ----");
116 String propertyId = UniqueIdBuilder.buildComponentPropertyUniqueId(getResourceId(resourceDetails), property.getName());
118 PropertyRestUtils.deleteProperty(getResourceId(resourceDetails), propertyId, sdncDesignerDetails);
119 RestResponse createPropertyResponse = PropertyRestUtils.createProperty(getResourceId(resourceDetails), body,
120 sdncDesignerDetails);
121 AssertJUnit.assertTrue("Expected result code - 201, received - " + createPropertyResponse.getErrorCode(),
122 createPropertyResponse.getErrorCode() == 201);
125 // System.out.println ("---- Get Property (GET) ----");
126 RestResponse getPropertyResponse = PropertyRestUtils.getProperty(getResourceId(resourceDetails), propertyId,
127 sdncDesignerDetails);
128 AssertJUnit.assertTrue("Expected result code - 200, received - " + getPropertyResponse.getErrorCode(),
129 getPropertyResponse.getErrorCode() == 200);
131 JSONObject jsonResp = (JSONObject) JSONValue.parse(getPropertyResponse.getResponse());
133 // assertTrue("Wrong 'type' in the
134 // response",jsonResp.get("type").equals(property.getPropertyType()));
135 // assertTrue("Wrong 'source' in the
136 // response",jsonResp.get("name").equals(property.getPropertyName()));
137 // assertTrue("Wrong 'name' in the
138 // response",jsonResp.get("source").equals(property.getPropertySource()));
139 // assertTrue("Wrong 'description' in the
140 // response",jsonResp.get("description").equals(property.getPropertyDescription()));
143 // System.out.println ("---- Update Property (UPDATE) ----");
144 property.setPropertyDescription("Updated description");
145 // body = gson.toJson(property);
146 body = property.propertyToJsonString();
148 RestResponse updatePropertyResponse = PropertyRestUtils.updateProperty(getResourceId(resourceDetails),
149 propertyId, body, sdncDesignerDetails);
150 AssertJUnit.assertTrue("Expected result code - 200, received - " + updatePropertyResponse.getErrorCode(),
151 updatePropertyResponse.getErrorCode() == 200);
154 // System.out.println ("---- Get Property (GET) ----");
155 getPropertyResponse = PropertyRestUtils.getProperty(getResourceId(resourceDetails), propertyId,
156 sdncDesignerDetails);
157 AssertJUnit.assertTrue("Expected result code - 200, received - " + getPropertyResponse.getErrorCode(),
158 getPropertyResponse.getErrorCode() == 200);
160 jsonResp = (JSONObject) JSONValue.parse(getPropertyResponse.getResponse());
162 // assertTrue("Wrong 'type' in the
163 // response",jsonResp.get("type").equals(property.getPropertyType()));
164 // assertTrue("Wrong 'source' in the
165 // response",jsonResp.get("name").equals(property.getPropertyName()));
166 // assertTrue("Wrong 'name' in the
167 // response",jsonResp.get("source").equals(property.getPropertySource()));
168 // assertTrue("Wrong 'description' in the
169 // response",jsonResp.get("description").equals(property.getPropertyDescription()));
172 // System.out.println ("---- Delete Property (DELETE) ----");
173 RestResponse deletePropertyResponse = PropertyRestUtils.deleteProperty(getResourceId(resourceDetails),
174 propertyId, sdncDesignerDetails);
175 AssertJUnit.assertTrue("Expected result code - 204, received - " + deletePropertyResponse.getErrorCode(),
176 deletePropertyResponse.getErrorCode() == 204);
178 // Get property - verify that the property doesn't exist.
179 // System.out.println("---- GET - Property Not Found ----");
180 getPropertyResponse = PropertyRestUtils.getProperty(getResourceId(resourceDetails), propertyId,
181 sdncDesignerDetails);
182 List<String> variables = Arrays.asList("");
183 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.PROPERTY_NOT_FOUND.name(), variables,
184 getPropertyResponse.getResponse());
188 // --------------------------------------------------------------------------------------
190 protected String getPropertyId(ResourceReqDetails resource, PropertyReqDetails property) {
192 // resource.getResourceName().toLowerCase()+".0.1."+property.getPropertyName();
193 return UniqueIdBuilder.buildComponentPropertyUniqueId(resource.getUniqueId(), property.getName());
196 protected String getResourceId(ResourceReqDetails resource) {
197 // String resourceUid =
198 // UniqueIdBuilder.buildResourceUniqueId(resource.getResourceName(),
201 return resource.getUniqueId();
204 protected User createUser(String cspUserId, String firstName, String lastName, String email, String role)
206 User sdncUserDetails = new User(firstName, lastName, cspUserId, email, role, null);
208 User adminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
209 UserRestUtils.createUser(sdncUserDetails, adminUser);
211 return sdncUserDetails;
214 protected ResourceReqDetails createResource(User sdncUserDetails, String resourceName) throws Exception {
215 String description = "description";
216 ArrayList<String> resourceTags = new ArrayList<String>();
217 resourceTags.add(resourceName);
218 // String category = ResourceCategoryEnum.DATABASE.getValue();
219 ArrayList<String> derivedFrom = new ArrayList<String>();
220 derivedFrom.add("tosca.nodes.Root");
221 String vendorName = "Oracle";
222 String vendorRelease = "1.0";
223 String contactId = sdncUserDetails.getUserId();
224 String icon = "myICON";
226 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, null,
227 derivedFrom, vendorName, vendorRelease, contactId, icon);
228 resourceDetails.addCategoryChain(ResourceCategoryEnum.GENERIC_DATABASE.getCategory(),
229 ResourceCategoryEnum.GENERIC_DATABASE.getSubCategory());
230 // deleteResource(resourceName.toLowerCase()+".0.1",sdncUserDetails.getUserId());
231 // TODO delete by name
232 // deleteResource(UniqueIdBuilder.buildResourceUniqueId(resourceName,
233 // "0.1"), sdncUserDetails.getUserId());
234 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncUserDetails);
235 AssertJUnit.assertTrue(createResource.getErrorCode().intValue() == 201);
236 String resourceId = ResponseParser.getUniqueIdFromResponse(createResource);
237 resourceDetails.setUniqueId(resourceId);
239 return resourceDetails;
244 public void putReqToCreateUriNotAllowed() throws Exception {
245 // System.out.println("---- PUT request to Create uri - Not Allowed
247 String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
248 getResourceId(resourceDetails));
249 RestResponse propertyErrorResponse = httpRequest.httpSendByMethod(url, "PUT", body, headersMap);
250 List<String> variables = Arrays.asList();
251 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.NOT_ALLOWED.name(), variables,
252 propertyErrorResponse.getResponse());
256 public void getReqToCreateUriNotAllowed() throws Exception {
257 // System.out.println("---- GET request to Create uri - Not Allowed
259 String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
260 getResourceId(resourceDetails));
261 RestResponse propertyErrorResponse = httpRequest.httpSendGet(url, headersMap);
262 List<String> variables = Arrays.asList();
263 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.NOT_ALLOWED.name(), variables,
264 propertyErrorResponse.getResponse());
268 public void deleteReqToCreateUriNotAllowed() throws Exception {
269 // System.out.println("---- DELETE request to Create uri - Not Allowed
271 String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
272 getResourceId(resourceDetails));
273 RestResponse propertyErrorResponse = httpRequest.httpSendDelete(url, headersMap);
274 List<String> variables = Arrays.asList();
275 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.NOT_ALLOWED.name(), variables,
276 propertyErrorResponse.getResponse());
280 public void postReqToUpdateUriNotAllowed() throws Exception {
281 // System.out.println("---- POST request to Update uri - Not Allowed
283 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
284 getResourceId(resourceDetails), getPropertyId(resourceDetails, property));
285 RestResponse propertyErrorResponse = httpRequest.httpSendPost(url, body, headersMap);
286 List<String> variables = Arrays.asList();
287 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.NOT_ALLOWED.name(), variables,
288 propertyErrorResponse.getResponse());
292 public void deleteReqPropertyNotFound() throws Exception {
293 // System.out.println("---- DELETE - Property Not Found ----");
294 String unknownPropertyId = getPropertyId(resourceDetails, property) + "111";
295 String url = String.format(Urls.DELETE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
296 getResourceId(resourceDetails), unknownPropertyId);
297 RestResponse propertyErrorResponse = httpRequest.httpSendDelete(url, headersMap);
298 List<String> variables = Arrays.asList("");
299 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.PROPERTY_NOT_FOUND.name(), variables,
300 propertyErrorResponse.getResponse());
304 public void updateReqPropertyNotFound() throws Exception {
305 // System.out.println("---- PUT - Property Not Found ----");
306 String unknownPropertyId = getPropertyId(resourceDetails, property) + "111";
307 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
308 getResourceId(resourceDetails), unknownPropertyId);
309 RestResponse propertyErrorResponse = httpRequest.httpSendByMethod(url, "PUT", body, headersMap);
310 List<String> variables = Arrays.asList("");
311 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.PROPERTY_NOT_FOUND.name(), variables,
312 propertyErrorResponse.getResponse());
316 public void modifierNotTheStateOwner() throws Exception {
317 // System.out.println("---- The modifier is not the state owner -
318 // Operation Not Allowed ----");
319 User sdncUserDetails2 = createUser("tu5555", "Test", "User", "tu5555@intl.sdc.com", "DESIGNER");
320 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncUserDetails2.getUserId());
321 property.setPropertyDescription("new description");
322 // body = gson.toJson(property);
323 body = property.propertyToJsonString();
324 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
325 getResourceId(resourceDetails), getPropertyId(resourceDetails, property));
326 RestResponse propertyErrorResponse = httpRequest.httpSendByMethod(url, "PUT", body, headersMap);
327 List<String> variables = Arrays.asList();
328 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.RESTRICTED_OPERATION.name(), variables,
329 propertyErrorResponse.getResponse());
334 public void postReqInvalidContent() throws Exception {
335 // System.out.println("---- POST - Invalid Content ----");
337 String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
338 getResourceId(resourceDetails), getPropertyId(resourceDetails, property));
339 RestResponse propertyErrorResponse = httpRequest.httpSendPost(url, body, headersMap);
341 // System.out.println(propertyErrorResponse.getResponse()+" "+
342 // propertyErrorResponse.getErrorCode());
344 List<String> variables = Arrays.asList();
345 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_CONTENT.name(), variables,
346 propertyErrorResponse.getResponse());
350 public void putReqInvalidContent() throws Exception {
353 // System.out.println ("---- Create Property (POST) ----");
354 RestResponse createPropertyResponse = PropertyRestUtils.createProperty(getResourceId(resourceDetails), body,
355 sdncDesignerDetails);
356 assertTrue("Expected result code - 201, received - " + createPropertyResponse.getErrorCode(),
357 createPropertyResponse.getErrorCode() == 201);
359 // System.out.println("---- PUT - Invalid Content ----");
362 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
363 getResourceId(resourceDetails), getPropertyId(resourceDetails, property));
365 // System.out.println(url + "\n" + body);
367 RestResponse propertyErrorResponse = httpRequest.httpSendByMethod(url, "PUT", body, headersMap);
369 // System.out.println(propertyErrorResponse.getResponse()+" "+
370 // propertyErrorResponse.getErrorCode());
372 List<String> variables = Arrays.asList();
373 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_CONTENT.name(), variables,
374 propertyErrorResponse.getResponse());
377 // --------------------------------------------------------------------------------------