[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / property / PropertyApisTest.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.property;
22
23 import static org.testng.AssertJUnit.assertTrue;
24
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
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;
59
60 import com.google.gson.Gson;
61
62 public class PropertyApisTest extends SimpleOneRsrcOneServiceTest {
63
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";;
68
69         // protected User sdncDesignerDetails;
70         // protected ResourceReqDetails resourceDetails;
71         protected PropertyReqDetails property;
72         protected String body;
73
74         protected HttpRequest httpRequest = new HttpRequest();
75         protected Map<String, String> headersMap = new HashMap<String, String>();
76
77         @Rule
78         public static TestName testName = new TestName();
79
80         public PropertyApisTest() {
81                 super(testName, PropertyApisTest.class.getName());
82         }
83
84         @BeforeMethod
85         public void init() throws Exception {
86                 //
87                 // //Delete resource
88                 //
89                 // resourceDetails = new ResourceReqDetails();
90                 // resourceDetails.setResourceName("testresourceDetails");
91                 //
92                 // resourceUtils.deleteResource_allVersions(resourceDetails,
93                 // sdncDesignerDetails);
94                 //
95                 // //Create resource
96                 // resourceDetails = createResource(sdncDesignerDetails,
97                 // "testresourceDetails");
98
99                 // Create property
100                 // property.setPropertyName("test");
101                 // property.setPropertyType("integer");
102                 // property.setPropertySource("A&AI");
103                 // property.setPropertyDescription("test property");
104
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());
113
114         }
115
116         @Test
117         public void testPropertyApis() throws Exception {
118                 // Create property
119                 // System.out.println ("---- Create Property (POST) ----");
120
121                 String propertyId = UniqueIdBuilder.buildComponentPropertyUniqueId(getResourceId(resourceDetails), property.getName());
122
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);
128
129                 // Get property
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);
135
136                 JSONObject jsonResp = (JSONObject) JSONValue.parse(getPropertyResponse.getResponse());
137
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()));
146
147                 // Update property
148                 // System.out.println ("---- Update Property (UPDATE) ----");
149                 property.setPropertyDescription("Updated description");
150                 // body = gson.toJson(property);
151                 body = property.propertyToJsonString();
152
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);
157
158                 // Get property
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);
164
165                 jsonResp = (JSONObject) JSONValue.parse(getPropertyResponse.getResponse());
166
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()));
175
176                 // Delete property
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);
182
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());
190
191         }
192
193         // --------------------------------------------------------------------------------------
194
195         protected String getPropertyId(ResourceReqDetails resource, PropertyReqDetails property) {
196                 // return
197                 // resource.getResourceName().toLowerCase()+".0.1."+property.getPropertyName();
198                 return UniqueIdBuilder.buildComponentPropertyUniqueId(resource.getUniqueId(), property.getName());
199         }
200
201         protected String getResourceId(ResourceReqDetails resource) {
202                 // String resourceUid =
203                 // UniqueIdBuilder.buildResourceUniqueId(resource.getResourceName(),
204                 // "0.1");
205
206                 return resource.getUniqueId();
207         }
208
209         protected User createUser(String cspUserId, String firstName, String lastName, String email, String role)
210                         throws Exception {
211                 User sdncUserDetails = new User(firstName, lastName, cspUserId, email, role, null);
212
213                 User adminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
214                 UserRestUtils.createUser(sdncUserDetails, adminUser);
215
216                 return sdncUserDetails;
217         }
218
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";
230
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);
243
244                 return resourceDetails;
245
246         }
247
248         @Test
249         public void putReqToCreateUriNotAllowed() throws Exception {
250                 // System.out.println("---- PUT request to Create uri - Not Allowed
251                 // ----");
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());
258         }
259
260         @Test
261         public void getReqToCreateUriNotAllowed() throws Exception {
262                 // System.out.println("---- GET request to Create uri - Not Allowed
263                 // ----");
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());
270         }
271
272         @Test
273         public void deleteReqToCreateUriNotAllowed() throws Exception {
274                 // System.out.println("---- DELETE request to Create uri - Not Allowed
275                 // ----");
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());
282         }
283
284         @Test
285         public void postReqToUpdateUriNotAllowed() throws Exception {
286                 // System.out.println("---- POST request to Update uri - Not Allowed
287                 // ----");
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());
294         }
295
296         @Test
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());
306         }
307
308         @Test
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());
318         }
319
320         @Test
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());
335
336         }
337
338         @Test
339         public void postReqInvalidContent() throws Exception {
340                 // System.out.println("---- POST - Invalid Content ----");
341                 body = "invalid";
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);
345
346                 // System.out.println(propertyErrorResponse.getResponse()+" "+
347                 // propertyErrorResponse.getErrorCode());
348
349                 List<String> variables = Arrays.asList();
350                 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_CONTENT.name(), variables,
351                                 propertyErrorResponse.getResponse());
352         }
353
354         @Test
355         public void putReqInvalidContent() throws Exception {
356
357                 // Create property
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);
363
364                 // System.out.println("---- PUT - Invalid Content ----");
365                 body = "invalid";
366
367                 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
368                                 getResourceId(resourceDetails), getPropertyId(resourceDetails, property));
369
370                 // System.out.println(url + "\n" + body);
371
372                 RestResponse propertyErrorResponse = httpRequest.httpSendByMethod(url, "PUT", body, headersMap);
373
374                 // System.out.println(propertyErrorResponse.getResponse()+" "+
375                 // propertyErrorResponse.getErrorCode());
376
377                 List<String> variables = Arrays.asList();
378                 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_CONTENT.name(), variables,
379                                 propertyErrorResponse.getResponse());
380         }
381
382         // --------------------------------------------------------------------------------------
383
384 }