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.apache.log4j.lf5.util.ResourceUtils;
32 import org.json.simple.JSONObject;
33 import org.json.simple.JSONValue;
34 import org.junit.Rule;
35 import org.junit.rules.TestName;
36 import org.openecomp.sdc.be.dao.api.ActionStatus;
37 import org.openecomp.sdc.be.model.User;
38 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
39 import org.openecomp.sdc.ci.tests.api.Urls;
40 import org.openecomp.sdc.ci.tests.config.Config;
41 import org.openecomp.sdc.ci.tests.datatypes.PropertyReqDetails;
42 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
43 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
44 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
45 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
46 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
47 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
48 import org.openecomp.sdc.ci.tests.preRequisites.SimpleOneRsrcOneServiceTest;
49 import org.openecomp.sdc.ci.tests.utils.Utils;
50 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
51 import org.openecomp.sdc.ci.tests.utils.rest.PropertyRestUtils;
52 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
53 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
54 import org.openecomp.sdc.ci.tests.utils.rest.UserRestUtils;
55 import org.openecomp.sdc.ci.tests.utils.validation.ErrorValidationUtils;
56 import org.testng.AssertJUnit;
57 import org.testng.annotations.BeforeMethod;
58 import org.testng.annotations.Test;
60 import com.google.gson.Gson;
62 public class PropertyApisTest extends SimpleOneRsrcOneServiceTest {
64 protected static final String RESOURCE_CATEGORY = "Generic/Databases";
65 protected Config config = Config.instance();
66 protected String contentTypeHeaderData = "application/json";
67 protected String acceptHeaderDate = "application/json";;
69 // protected User sdncDesignerDetails;
70 // protected ResourceReqDetails resourceDetails;
71 protected PropertyReqDetails property;
72 protected String body;
74 protected HttpRequest httpRequest = new HttpRequest();
75 protected Map<String, String> headersMap = new HashMap<String, String>();
78 public static TestName testName = new TestName();
80 public PropertyApisTest() {
81 super(testName, PropertyApisTest.class.getName());
85 public void init() throws Exception {
89 // resourceDetails = new ResourceReqDetails();
90 // resourceDetails.setResourceName("testresourceDetails");
92 // resourceUtils.deleteResource_allVersions(resourceDetails,
93 // sdncDesignerDetails);
96 // resourceDetails = createResource(sdncDesignerDetails,
97 // "testresourceDetails");
100 // property.setPropertyName("test");
101 // property.setPropertyType("integer");
102 // property.setPropertySource("A&AI");
103 // property.setPropertyDescription("test property");
105 // body = gson.toJson(property);
106 property = ElementFactory.getDefaultProperty();
107 body = property.propertyToJsonString();
108 // System.out.println(body);
109 // HTTP (for negative tests)
110 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
111 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
112 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncDesignerDetails.getUserId());
117 public void testPropertyApis() throws Exception {
119 // System.out.println ("---- Create Property (POST) ----");
121 String propertyId = UniqueIdBuilder.buildComponentPropertyUniqueId(getResourceId(resourceDetails), property.getName());
123 PropertyRestUtils.deleteProperty(getResourceId(resourceDetails), propertyId, sdncDesignerDetails);
124 RestResponse createPropertyResponse = PropertyRestUtils.createProperty(getResourceId(resourceDetails), body,
125 sdncDesignerDetails);
126 AssertJUnit.assertTrue("Expected result code - 201, received - " + createPropertyResponse.getErrorCode(),
127 createPropertyResponse.getErrorCode() == 201);
130 // System.out.println ("---- Get Property (GET) ----");
131 RestResponse getPropertyResponse = PropertyRestUtils.getProperty(getResourceId(resourceDetails), propertyId,
132 sdncDesignerDetails);
133 AssertJUnit.assertTrue("Expected result code - 200, received - " + getPropertyResponse.getErrorCode(),
134 getPropertyResponse.getErrorCode() == 200);
136 JSONObject jsonResp = (JSONObject) JSONValue.parse(getPropertyResponse.getResponse());
138 // assertTrue("Wrong 'type' in the
139 // response",jsonResp.get("type").equals(property.getPropertyType()));
140 // assertTrue("Wrong 'source' in the
141 // response",jsonResp.get("name").equals(property.getPropertyName()));
142 // assertTrue("Wrong 'name' in the
143 // response",jsonResp.get("source").equals(property.getPropertySource()));
144 // assertTrue("Wrong 'description' in the
145 // response",jsonResp.get("description").equals(property.getPropertyDescription()));
148 // System.out.println ("---- Update Property (UPDATE) ----");
149 property.setPropertyDescription("Updated description");
150 // body = gson.toJson(property);
151 body = property.propertyToJsonString();
153 RestResponse updatePropertyResponse = PropertyRestUtils.updateProperty(getResourceId(resourceDetails),
154 propertyId, body, sdncDesignerDetails);
155 AssertJUnit.assertTrue("Expected result code - 200, received - " + updatePropertyResponse.getErrorCode(),
156 updatePropertyResponse.getErrorCode() == 200);
159 // System.out.println ("---- Get Property (GET) ----");
160 getPropertyResponse = PropertyRestUtils.getProperty(getResourceId(resourceDetails), propertyId,
161 sdncDesignerDetails);
162 AssertJUnit.assertTrue("Expected result code - 200, received - " + getPropertyResponse.getErrorCode(),
163 getPropertyResponse.getErrorCode() == 200);
165 jsonResp = (JSONObject) JSONValue.parse(getPropertyResponse.getResponse());
167 // assertTrue("Wrong 'type' in the
168 // response",jsonResp.get("type").equals(property.getPropertyType()));
169 // assertTrue("Wrong 'source' in the
170 // response",jsonResp.get("name").equals(property.getPropertyName()));
171 // assertTrue("Wrong 'name' in the
172 // response",jsonResp.get("source").equals(property.getPropertySource()));
173 // assertTrue("Wrong 'description' in the
174 // response",jsonResp.get("description").equals(property.getPropertyDescription()));
177 // System.out.println ("---- Delete Property (DELETE) ----");
178 RestResponse deletePropertyResponse = PropertyRestUtils.deleteProperty(getResourceId(resourceDetails),
179 propertyId, sdncDesignerDetails);
180 AssertJUnit.assertTrue("Expected result code - 204, received - " + deletePropertyResponse.getErrorCode(),
181 deletePropertyResponse.getErrorCode() == 204);
183 // Get property - verify that the property doesn't exist.
184 // System.out.println("---- GET - Property Not Found ----");
185 getPropertyResponse = PropertyRestUtils.getProperty(getResourceId(resourceDetails), propertyId,
186 sdncDesignerDetails);
187 List<String> variables = Arrays.asList("");
188 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.PROPERTY_NOT_FOUND.name(), variables,
189 getPropertyResponse.getResponse());
193 // --------------------------------------------------------------------------------------
195 protected String getPropertyId(ResourceReqDetails resource, PropertyReqDetails property) {
197 // resource.getResourceName().toLowerCase()+".0.1."+property.getPropertyName();
198 return UniqueIdBuilder.buildComponentPropertyUniqueId(resource.getUniqueId(), property.getName());
201 protected String getResourceId(ResourceReqDetails resource) {
202 // String resourceUid =
203 // UniqueIdBuilder.buildResourceUniqueId(resource.getResourceName(),
206 return resource.getUniqueId();
209 protected User createUser(String cspUserId, String firstName, String lastName, String email, String role)
211 User sdncUserDetails = new User(firstName, lastName, cspUserId, email, role, null);
213 User adminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
214 UserRestUtils.createUser(sdncUserDetails, adminUser);
216 return sdncUserDetails;
219 protected ResourceReqDetails createResource(User sdncUserDetails, String resourceName) throws Exception {
220 String description = "description";
221 ArrayList<String> resourceTags = new ArrayList<String>();
222 resourceTags.add(resourceName);
223 // String category = ResourceCategoryEnum.DATABASE.getValue();
224 ArrayList<String> derivedFrom = new ArrayList<String>();
225 derivedFrom.add("tosca.nodes.Root");
226 String vendorName = "Oracle";
227 String vendorRelease = "1.0";
228 String contactId = sdncUserDetails.getUserId();
229 String icon = "myICON";
231 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, null,
232 derivedFrom, vendorName, vendorRelease, contactId, icon);
233 resourceDetails.addCategoryChain(ResourceCategoryEnum.GENERIC_DATABASE.getCategory(),
234 ResourceCategoryEnum.GENERIC_DATABASE.getSubCategory());
235 // deleteResource(resourceName.toLowerCase()+".0.1",sdncUserDetails.getUserId());
236 // TODO delete by name
237 // deleteResource(UniqueIdBuilder.buildResourceUniqueId(resourceName,
238 // "0.1"), sdncUserDetails.getUserId());
239 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncUserDetails);
240 AssertJUnit.assertTrue(createResource.getErrorCode().intValue() == 201);
241 String resourceId = ResponseParser.getUniqueIdFromResponse(createResource);
242 resourceDetails.setUniqueId(resourceId);
244 return resourceDetails;
249 public void putReqToCreateUriNotAllowed() throws Exception {
250 // System.out.println("---- PUT request to Create uri - Not Allowed
252 String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
253 getResourceId(resourceDetails));
254 RestResponse propertyErrorResponse = httpRequest.httpSendByMethod(url, "PUT", body, headersMap);
255 List<String> variables = Arrays.asList();
256 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.NOT_ALLOWED.name(), variables,
257 propertyErrorResponse.getResponse());
261 public void getReqToCreateUriNotAllowed() throws Exception {
262 // System.out.println("---- GET request to Create uri - Not Allowed
264 String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
265 getResourceId(resourceDetails));
266 RestResponse propertyErrorResponse = httpRequest.httpSendGet(url, headersMap);
267 List<String> variables = Arrays.asList();
268 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.NOT_ALLOWED.name(), variables,
269 propertyErrorResponse.getResponse());
273 public void deleteReqToCreateUriNotAllowed() throws Exception {
274 // System.out.println("---- DELETE request to Create uri - Not Allowed
276 String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
277 getResourceId(resourceDetails));
278 RestResponse propertyErrorResponse = httpRequest.httpSendDelete(url, headersMap);
279 List<String> variables = Arrays.asList();
280 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.NOT_ALLOWED.name(), variables,
281 propertyErrorResponse.getResponse());
285 public void postReqToUpdateUriNotAllowed() throws Exception {
286 // System.out.println("---- POST request to Update uri - Not Allowed
288 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
289 getResourceId(resourceDetails), getPropertyId(resourceDetails, property));
290 RestResponse propertyErrorResponse = httpRequest.httpSendPost(url, body, headersMap);
291 List<String> variables = Arrays.asList();
292 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.NOT_ALLOWED.name(), variables,
293 propertyErrorResponse.getResponse());
297 public void deleteReqPropertyNotFound() throws Exception {
298 // System.out.println("---- DELETE - Property Not Found ----");
299 String unknownPropertyId = getPropertyId(resourceDetails, property) + "111";
300 String url = String.format(Urls.DELETE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
301 getResourceId(resourceDetails), unknownPropertyId);
302 RestResponse propertyErrorResponse = httpRequest.httpSendDelete(url, headersMap);
303 List<String> variables = Arrays.asList("");
304 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.PROPERTY_NOT_FOUND.name(), variables,
305 propertyErrorResponse.getResponse());
309 public void updateReqPropertyNotFound() throws Exception {
310 // System.out.println("---- PUT - Property Not Found ----");
311 String unknownPropertyId = getPropertyId(resourceDetails, property) + "111";
312 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
313 getResourceId(resourceDetails), unknownPropertyId);
314 RestResponse propertyErrorResponse = httpRequest.httpSendByMethod(url, "PUT", body, headersMap);
315 List<String> variables = Arrays.asList("");
316 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.PROPERTY_NOT_FOUND.name(), variables,
317 propertyErrorResponse.getResponse());
321 public void modifierNotTheStateOwner() throws Exception {
322 // System.out.println("---- The modifier is not the state owner -
323 // Operation Not Allowed ----");
324 User sdncUserDetails2 = createUser("tu5555", "Test", "User", "tu5555@intl.sdc.com", "DESIGNER");
325 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncUserDetails2.getUserId());
326 property.setPropertyDescription("new description");
327 // body = gson.toJson(property);
328 body = property.propertyToJsonString();
329 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
330 getResourceId(resourceDetails), getPropertyId(resourceDetails, property));
331 RestResponse propertyErrorResponse = httpRequest.httpSendByMethod(url, "PUT", body, headersMap);
332 List<String> variables = Arrays.asList();
333 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.RESTRICTED_OPERATION.name(), variables,
334 propertyErrorResponse.getResponse());
339 public void postReqInvalidContent() throws Exception {
340 // System.out.println("---- POST - Invalid Content ----");
342 String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
343 getResourceId(resourceDetails), getPropertyId(resourceDetails, property));
344 RestResponse propertyErrorResponse = httpRequest.httpSendPost(url, body, headersMap);
346 // System.out.println(propertyErrorResponse.getResponse()+" "+
347 // propertyErrorResponse.getErrorCode());
349 List<String> variables = Arrays.asList();
350 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_CONTENT.name(), variables,
351 propertyErrorResponse.getResponse());
355 public void putReqInvalidContent() throws Exception {
358 // System.out.println ("---- Create Property (POST) ----");
359 RestResponse createPropertyResponse = PropertyRestUtils.createProperty(getResourceId(resourceDetails), body,
360 sdncDesignerDetails);
361 assertTrue("Expected result code - 201, received - " + createPropertyResponse.getErrorCode(),
362 createPropertyResponse.getErrorCode() == 201);
364 // System.out.println("---- PUT - Invalid Content ----");
367 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
368 getResourceId(resourceDetails), getPropertyId(resourceDetails, property));
370 // System.out.println(url + "\n" + body);
372 RestResponse propertyErrorResponse = httpRequest.httpSendByMethod(url, "PUT", body, headersMap);
374 // System.out.println(propertyErrorResponse.getResponse()+" "+
375 // propertyErrorResponse.getErrorCode());
377 List<String> variables = Arrays.asList();
378 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_CONTENT.name(), variables,
379 propertyErrorResponse.getResponse());
382 // --------------------------------------------------------------------------------------