Fix for radio buttons
[sdc.git] / asdc-tests / 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.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;
57
58 public class PropertyApisTest extends SimpleOneRsrcOneServiceTest {
59
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";;
64
65         // protected User sdncDesignerDetails;
66         // protected ResourceReqDetails resourceDetails;
67         protected PropertyReqDetails property;
68         protected String body;
69
70         protected HttpRequest httpRequest = new HttpRequest();
71         protected Map<String, String> headersMap = new HashMap<String, String>();
72
73         @Rule
74         public static TestName testName = new TestName();
75
76         public PropertyApisTest() {
77                 super(testName, PropertyApisTest.class.getName());
78         }
79
80         @BeforeMethod
81         public void init() throws Exception {
82                 // //Delete resource
83                 //
84                 // resourceDetails = new ResourceReqDetails();
85                 // resourceDetails.setResourceName("testresourceDetails");
86                 //
87                 // resourceUtils.deleteResource_allVersions(resourceDetails,
88                 // sdncDesignerDetails);
89                 //
90                 // //Create resource
91                 // resourceDetails = createResource(sdncDesignerDetails,
92                 // "testresourceDetails");
93
94                 // Create property
95                 // property.setPropertyName("test");
96                 // property.setPropertyType("integer");
97                 // property.setPropertySource("A&AI");
98                 // property.setPropertyDescription("test property");
99
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());
108
109         }
110
111         @Test
112         public void testPropertyApis() throws Exception {
113                 // Create property
114                 // System.out.println ("---- Create Property (POST) ----");
115
116                 String propertyId = UniqueIdBuilder.buildComponentPropertyUniqueId(getResourceId(resourceDetails), property.getName());
117
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);
123
124                 // Get property
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);
130
131                 JSONObject jsonResp = (JSONObject) JSONValue.parse(getPropertyResponse.getResponse());
132
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()));
141
142                 // Update property
143                 // System.out.println ("---- Update Property (UPDATE) ----");
144                 property.setPropertyDescription("Updated description");
145                 // body = gson.toJson(property);
146                 body = property.propertyToJsonString();
147
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);
152
153                 // Get property
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);
159
160                 jsonResp = (JSONObject) JSONValue.parse(getPropertyResponse.getResponse());
161
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()));
170
171                 // Delete property
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);
177
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());
185
186         }
187
188         // --------------------------------------------------------------------------------------
189
190         protected String getPropertyId(ResourceReqDetails resource, PropertyReqDetails property) {
191                 // return
192                 // resource.getResourceName().toLowerCase()+".0.1."+property.getPropertyName();
193                 return UniqueIdBuilder.buildComponentPropertyUniqueId(resource.getUniqueId(), property.getName());
194         }
195
196         protected String getResourceId(ResourceReqDetails resource) {
197                 // String resourceUid =
198                 // UniqueIdBuilder.buildResourceUniqueId(resource.getResourceName(),
199                 // "0.1");
200
201                 return resource.getUniqueId();
202         }
203
204         protected User createUser(String cspUserId, String firstName, String lastName, String email, String role)
205                         throws Exception {
206                 User sdncUserDetails = new User(firstName, lastName, cspUserId, email, role, null);
207
208                 User adminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
209                 UserRestUtils.createUser(sdncUserDetails, adminUser);
210
211                 return sdncUserDetails;
212         }
213
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";
225
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);
238
239                 return resourceDetails;
240
241         }
242
243         @Test
244         public void putReqToCreateUriNotAllowed() throws Exception {
245                 // System.out.println("---- PUT request to Create uri - Not Allowed
246                 // ----");
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());
253         }
254
255         @Test
256         public void getReqToCreateUriNotAllowed() throws Exception {
257                 // System.out.println("---- GET request to Create uri - Not Allowed
258                 // ----");
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());
265         }
266
267         @Test
268         public void deleteReqToCreateUriNotAllowed() throws Exception {
269                 // System.out.println("---- DELETE request to Create uri - Not Allowed
270                 // ----");
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());
277         }
278
279         @Test
280         public void postReqToUpdateUriNotAllowed() throws Exception {
281                 // System.out.println("---- POST request to Update uri - Not Allowed
282                 // ----");
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());
289         }
290
291         @Test
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());
301         }
302
303         @Test
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());
313         }
314
315         @Test
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());
330
331         }
332
333         @Test
334         public void postReqInvalidContent() throws Exception {
335                 // System.out.println("---- POST - Invalid Content ----");
336                 body = "invalid";
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);
340
341                 // System.out.println(propertyErrorResponse.getResponse()+" "+
342                 // propertyErrorResponse.getErrorCode());
343
344                 List<String> variables = Arrays.asList();
345                 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_CONTENT.name(), variables,
346                                 propertyErrorResponse.getResponse());
347         }
348
349         @Test
350         public void putReqInvalidContent() throws Exception {
351
352                 // Create property
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);
358
359                 // System.out.println("---- PUT - Invalid Content ----");
360                 body = "invalid";
361
362                 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
363                                 getResourceId(resourceDetails), getPropertyId(resourceDetails, property));
364
365                 // System.out.println(url + "\n" + body);
366
367                 RestResponse propertyErrorResponse = httpRequest.httpSendByMethod(url, "PUT", body, headersMap);
368
369                 // System.out.println(propertyErrorResponse.getResponse()+" "+
370                 // propertyErrorResponse.getErrorCode());
371
372                 List<String> variables = Arrays.asList();
373                 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.INVALID_CONTENT.name(), variables,
374                                 propertyErrorResponse.getResponse());
375         }
376
377         // --------------------------------------------------------------------------------------
378
379 }