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