[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / resource / ResourceApiTest.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.resource;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import org.apache.http.client.methods.CloseableHttpResponse;
32 import org.apache.http.client.methods.HttpGet;
33 import org.apache.http.impl.client.CloseableHttpClient;
34 import org.apache.http.impl.client.HttpClients;
35 import org.apache.http.util.EntityUtils;
36 import org.apache.log4j.lf5.util.ResourceUtils;
37 import org.json.simple.JSONArray;
38 import org.json.simple.JSONObject;
39 import org.json.simple.JSONValue;
40 import org.junit.Rule;
41 import org.junit.rules.TestName;
42 import org.openecomp.sdc.be.model.Resource;
43 import org.openecomp.sdc.be.model.User;
44 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
45 import org.openecomp.sdc.ci.tests.api.Urls;
46 import org.openecomp.sdc.ci.tests.config.Config;
47 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
48 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
49 import org.openecomp.sdc.ci.tests.datatypes.enums.ServiceCategoriesEnum;
50 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
51 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
52 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
53 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
54 import org.openecomp.sdc.ci.tests.utils.Utils;
55 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
56 import org.openecomp.sdc.ci.tests.utils.rest.CatalogRestUtils;
57 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
58 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
59 import org.openecomp.sdc.ci.tests.utils.validation.ResourceValidationUtils;
60 import org.testng.annotations.Test;
61
62 import com.google.gson.Gson;
63
64 public class ResourceApiTest extends ComponentBaseTest {
65
66         protected final String contentTypeHeaderData = "application/json";
67         protected final String acceptHeaderDate = "application/json";
68
69         @Rule
70         public static TestName name = new TestName();
71
72         public ResourceApiTest() {
73                 super(name, ResourceApiTest.class.getName());
74         }
75
76         // Keep
77         @Test
78         public void updateResourceMetadataSuccess() throws Exception {
79                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
80                 sdncModifierDetails.setUserId("jh0003");
81                 RestResponse restResponse = createResourceForUpdate(sdncModifierDetails);
82                 Resource resourceRespJavaObject = ResponseParser
83                                 .convertResourceResponseToJavaObject(restResponse.getResponse());
84
85                 Config config = Utils.getConfig();
86                 Map<String, String> headersMap = new HashMap<String, String>();
87                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
88                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
89                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
90
91                 // set resource details
92                 ResourceReqDetails resourceDetails = new ResourceReqDetails();
93                 resourceDetails.setDescription("updatedDescription");
94                 ArrayList<String> resourceTags = new ArrayList<String>();
95                 // Duplicate tags are allowed and should be de-duplicated by the server
96                 // side
97                 resourceTags.add(resourceRespJavaObject.getName());
98                 resourceTags.add("tag1");
99                 resourceTags.add("tag1");
100                 resourceTags.add("tag2");
101                 resourceTags.add("tag2");
102                 resourceDetails.setTags(resourceTags);
103                 resourceDetails.addCategoryChain(ResourceCategoryEnum.NETWORK_L2_3_ROUTERS.getCategory(),
104                                 ResourceCategoryEnum.NETWORK_L2_3_ROUTERS.getSubCategory());
105                 resourceDetails.setVendorName("OracleUp");
106                 resourceDetails.setVendorRelease("1.5Up");
107                 resourceDetails.setContactId("pe1116");
108
109                 resourceDetails.setIcon(resourceRespJavaObject.getIcon());
110                 resourceDetails.setName(resourceRespJavaObject.getName());
111                 resourceDetails.setDerivedFrom(resourceRespJavaObject.getDerivedFrom());
112
113                 // ResourceReqDetails resourceDetails = new
114                 // ResourceReqDetails(resourceName, description, resourceTags, category,
115                 // derivedFrom, vendorName, vendorRelease, contactId, null);
116
117                 Gson gson = new Gson();
118                 String userBodyJson = gson.toJson(resourceDetails);
119                 HttpRequest http = new HttpRequest();
120                 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
121                                 resourceRespJavaObject.getUniqueId());
122                 RestResponse updateResourceResponse = http.httpSendByMethod(url, "PUT", userBodyJson, headersMap);
123
124                 // resourceDetails.setResourceName(resourceRespJavaObject.getResourceName());
125                 ResourceValidationUtils.validateResourceReqVsResp(resourceDetails,
126                                 ResponseParser.convertResourceResponseToJavaObject(updateResourceResponse.getResponse()));
127
128                 // Delete resource
129                 deleteResource(resourceRespJavaObject.getUniqueId(), sdncModifierDetails.getUserId());
130
131         }
132
133         protected void deleteResource(String resourceUniqueId, String httpCspUserId) throws Exception {
134                 RestResponse res = ResourceRestUtils.deleteResource(resourceUniqueId, httpCspUserId);
135
136                 // System.out.println("Delete resource was finished with response: " +
137                 // res.getErrorCode());
138         }
139
140         protected RestResponse createResourceForUpdate(User sdncModifierDetails) throws Exception {
141
142                 ResourceReqDetails resourceDetails = getResourceObj();
143
144                 // create resource
145                 return ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
146
147         }
148
149         public ResourceReqDetails getResourceObj() {
150                 // set resource details
151                 String resourceName = "ResourceForUpdate" + (int) (Math.random() * 100);
152                 String description = "description";
153                 ArrayList<String> resourceTags = new ArrayList<String>();
154                 resourceTags.add(resourceName);
155                 // String category = ResourceCategoriesEnum.MOBILITY.getValue();
156                 ArrayList<String> derivedFrom = new ArrayList<String>();
157                 derivedFrom.add("tosca.nodes.Root");
158                 String vendorName = "Oracle";
159                 String vendorRelease = "1.5";
160                 String contactId = "pe1116";
161                 String icon = "myICON";
162
163                 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, null,
164                                 derivedFrom, vendorName, vendorRelease, contactId, icon);
165                 resourceDetails.addCategoryChain(ResourceCategoryEnum.GENERIC_INFRASTRUCTURE.getCategory(),
166                                 ResourceCategoryEnum.GENERIC_INFRASTRUCTURE.getSubCategory());
167                 return resourceDetails;
168         }
169
170         // -------------------------------------------------------------------
171
172         protected ResourceReqDetails defineResourse_Benny(int n) {
173                 String resourceName = "cisco" + String.valueOf(n);
174                 String description = "description";
175                 ArrayList<String> resourceTags = new ArrayList<String>();
176                 resourceTags.add("tag1");
177                 String category = ServiceCategoriesEnum.MOBILITY.getValue();
178                 ArrayList<String> derivedFrom = new ArrayList<String>();
179                 derivedFrom.add("tosca.nodes.Root");
180                 String vendorName = "Oracle";
181                 String vendorRelease = "1.5";
182                 String contactId = "jh0003";
183                 String icon = "borderElement";
184
185                 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, category,
186                                 derivedFrom, vendorName, vendorRelease, contactId, icon);
187
188                 return resourceDetails;
189         }
190
191         @Test
192         public void getAllAbstractResources() throws Exception {
193                 RestResponse abstractResources = CatalogRestUtils.getAbstractResources();
194
195                 int status = abstractResources.getErrorCode();
196                 assertTrue(status == 200);
197                 String json = abstractResources.getResponse();
198                 JSONArray array = (JSONArray) JSONValue.parse(json);
199                 for (Object o : array) {
200                         JSONObject value = (JSONObject) o;
201                         Boolean element = (Boolean) value.get("abstract");
202                         assertTrue(element);
203                 }
204
205         }
206
207         @Test
208         public void getAllNotAbstractResources() throws Exception {
209                 CloseableHttpClient httpclient = HttpClients.createDefault();
210                 try {
211                         String url = String.format(Urls.GET_ALL_NOT_ABSTRACT_RESOURCES, config.getCatalogBeHost(),
212                                         config.getCatalogBePort());
213                         HttpGet httpget = new HttpGet(url);
214
215                         httpget.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
216
217                         httpget.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
218
219                         httpget.addHeader(HttpHeaderEnum.USER_ID.getValue(),
220                                         ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER).getUserId());
221
222                         // System.out.println("Executing request " +
223                         // httpget.getRequestLine());
224                         CloseableHttpResponse response = httpclient.execute(httpget);
225                         int status = response.getStatusLine().getStatusCode();
226                         assertTrue(status == 200);
227                         try {
228                                 String json = EntityUtils.toString(response.getEntity());
229                                 JSONArray array = (JSONArray) JSONValue.parse(json);
230                                 for (Object o : array) {
231                                         JSONObject value = (JSONObject) o;
232                                         Boolean element = (Boolean) value.get("abstract");
233                                         assertTrue(!element);
234                                 }
235
236                         } finally {
237                                 response.close();
238                         }
239                 } finally {
240                         httpclient.close();
241                 }
242         }
243
244         @Test
245         public void updateResourceMetadata_methodNotAllowed() throws Exception {
246                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
247                 Config config = Utils.getConfig();
248                 Map<String, String> headersMap = new HashMap<String, String>();
249                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
250                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
251                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
252
253                 // set resource details
254                 String resourceName = "ResForUpdate";
255                 String description = "updatedDescription";
256                 ArrayList<String> resourceTags = new ArrayList<String>();
257                 resourceTags.add("tag1");
258                 resourceTags.add("tag2");
259                 ArrayList<String> derivedFrom = new ArrayList<String>();
260                 derivedFrom.add("tosca.nodes.root");
261                 String category = ServiceCategoriesEnum.VOIP.getValue();
262                 String vendorName = "OracleUp";
263                 String vendorRelease = "1.5Up";
264                 String contactId = "pe1117";
265                 String icon = "myICON.jpgUp";
266
267                 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, category,
268                                 derivedFrom, vendorName, vendorRelease, contactId, icon);
269
270                 Gson gson = new Gson();
271                 String userBodyJson = gson.toJson(resourceDetails);
272                 HttpRequest http = new HttpRequest();
273                 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
274                                 "NotExistsId");
275
276                 RestResponse updateResourceResponse = http.httpSendByMethod(url, "POST", userBodyJson, headersMap);
277
278                 assertNotNull("Check error code exists in response after wrong update resource",
279                                 updateResourceResponse.getErrorCode());
280                 assertEquals("Check error code after update resource", 405, updateResourceResponse.getErrorCode().intValue());
281         }
282
283         @Test
284         public void validateResourceNameTest() throws Exception {
285                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
286                 sdncModifierDetails.setUserId("jh0003");
287
288                 ResourceReqDetails resourceDetails = getResourceObj();
289
290                 // create resource
291                 RestResponse restResponse = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
292                 Resource resourceRespJavaObject = ResponseParser
293                                 .convertResourceResponseToJavaObject(restResponse.getResponse());
294                 CloseableHttpClient httpclient = HttpClients.createDefault();
295                 try {
296
297                         // check invalid
298                         String url = String.format(Urls.VALIDATE_RESOURCE_NAME, config.getCatalogBeHost(),
299                                         config.getCatalogBePort(), resourceDetails.getName());
300
301                         HttpGet httpget = new HttpGet(url);
302
303                         httpget.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
304
305                         httpget.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
306
307                         httpget.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
308
309                         // System.out.println("Executing request " +
310                         // httpget.getRequestLine());
311                         CloseableHttpResponse response = httpclient.execute(httpget);
312                         int status = response.getStatusLine().getStatusCode();
313                         assertTrue(status == 200);
314                         try {
315                                 String json = EntityUtils.toString(response.getEntity());
316                                 JSONObject object = (JSONObject) JSONValue.parse(json);
317                                 Boolean element = (Boolean) object.get("isValid");
318                                 assertTrue(!element);
319
320                         } finally {
321                                 response.close();
322                         }
323                         // check valid
324                         url = String.format(Urls.VALIDATE_RESOURCE_NAME, config.getCatalogBeHost(), config.getCatalogBePort(),
325                                         resourceDetails.getName() + "temp");
326
327                         httpget = new HttpGet(url);
328
329                         httpget.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
330
331                         httpget.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
332
333                         httpget.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
334
335                         // System.out.println("Executing request " +
336                         // httpget.getRequestLine());
337                         response = httpclient.execute(httpget);
338                         status = response.getStatusLine().getStatusCode();
339                         assertTrue(status == 200);
340                         try {
341                                 String json = EntityUtils.toString(response.getEntity());
342                                 JSONObject object = (JSONObject) JSONValue.parse(json);
343                                 Boolean element = (Boolean) object.get("isValid");
344                                 assertTrue(element);
345
346                         } finally {
347                                 response.close();
348                         }
349                 } finally {
350                         httpclient.close();
351                 }
352
353                 // Delete resource
354                 ResourceRestUtils.deleteResource(resourceDetails, sdncModifierDetails, "0.1");
355
356         }
357
358         // -------------------------------------------------------------------
359         // //Benny Tal
360         // @Test
361         // public void createResource_Benny() throws Exception {
362         // for (int i = 0; i < 100; i++) {
363         // ResourceReqDetails resourceDetails = defineResourse_Benny(i);
364         //
365         // ResourceRestUtils.createResource(resourceDetails,
366         // UserUtils.getDesignerDetails());
367         // // resourceUtils.deleteResource(resourceDetails,
368         // UserUtils.getDesignerDetails(), "0.1");
369         // }
370         // }
371
372 }