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.resource;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
27 import java.util.ArrayList;
28 import java.util.HashMap;
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.json.simple.JSONArray;
37 import org.json.simple.JSONObject;
38 import org.json.simple.JSONValue;
39 import org.junit.Rule;
40 import org.junit.rules.TestName;
41 import org.openecomp.sdc.be.model.Resource;
42 import org.openecomp.sdc.be.model.User;
43 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
44 import org.openecomp.sdc.ci.tests.api.Urls;
45 import org.openecomp.sdc.ci.tests.config.Config;
46 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
47 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
48 import org.openecomp.sdc.ci.tests.datatypes.enums.ServiceCategoriesEnum;
49 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
50 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
51 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
52 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
53 import org.openecomp.sdc.ci.tests.utils.Utils;
54 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
55 import org.openecomp.sdc.ci.tests.utils.rest.CatalogRestUtils;
56 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
57 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
58 import org.openecomp.sdc.ci.tests.utils.validation.ResourceValidationUtils;
59 import org.testng.annotations.Test;
61 import com.google.gson.Gson;
63 public class ResourceApiTest extends ComponentBaseTest {
65 protected final String contentTypeHeaderData = "application/json";
66 protected final String acceptHeaderDate = "application/json";
69 public static TestName name = new TestName();
71 public ResourceApiTest() {
72 super(name, ResourceApiTest.class.getName());
77 public void updateResourceMetadataSuccess() throws Exception {
78 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
79 sdncModifierDetails.setUserId("jh0003");
80 RestResponse restResponse = createResourceForUpdate(sdncModifierDetails);
81 Resource resourceRespJavaObject = ResponseParser
82 .convertResourceResponseToJavaObject(restResponse.getResponse());
84 Config config = Utils.getConfig();
85 Map<String, String> headersMap = new HashMap<String, String>();
86 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
87 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
88 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
90 // set resource details
91 ResourceReqDetails resourceDetails = new ResourceReqDetails();
92 resourceDetails.setDescription("updatedDescription");
93 ArrayList<String> resourceTags = new ArrayList<String>();
94 // Duplicate tags are allowed and should be de-duplicated by the server
96 resourceTags.add(resourceRespJavaObject.getName());
97 resourceTags.add("tag1");
98 resourceTags.add("tag1");
99 resourceTags.add("tag2");
100 resourceTags.add("tag2");
101 resourceDetails.setTags(resourceTags);
102 resourceDetails.addCategoryChain(ResourceCategoryEnum.NETWORK_L2_3_ROUTERS.getCategory(),
103 ResourceCategoryEnum.NETWORK_L2_3_ROUTERS.getSubCategory());
104 resourceDetails.setVendorName("OracleUp");
105 resourceDetails.setVendorRelease("1.5Up");
106 resourceDetails.setContactId("pe1116");
108 resourceDetails.setIcon(resourceRespJavaObject.getIcon());
109 resourceDetails.setName(resourceRespJavaObject.getName());
110 resourceDetails.setDerivedFrom(resourceRespJavaObject.getDerivedFrom());
112 // ResourceReqDetails resourceDetails = new
113 // ResourceReqDetails(resourceName, description, resourceTags, category,
114 // derivedFrom, vendorName, vendorRelease, contactId, null);
116 Gson gson = new Gson();
117 String userBodyJson = gson.toJson(resourceDetails);
118 HttpRequest http = new HttpRequest();
119 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
120 resourceRespJavaObject.getUniqueId());
121 RestResponse updateResourceResponse = http.httpSendByMethod(url, "PUT", userBodyJson, headersMap);
123 // resourceDetails.setResourceName(resourceRespJavaObject.getResourceName());
124 ResourceValidationUtils.validateResourceReqVsResp(resourceDetails,
125 ResponseParser.convertResourceResponseToJavaObject(updateResourceResponse.getResponse()));
128 deleteResource(resourceRespJavaObject.getUniqueId(), sdncModifierDetails.getUserId());
132 protected void deleteResource(String resourceUniqueId, String httpCspUserId) throws Exception {
133 RestResponse res = ResourceRestUtils.deleteResource(resourceUniqueId, httpCspUserId);
135 // System.out.println("Delete resource was finished with response: " +
136 // res.getErrorCode());
139 protected RestResponse createResourceForUpdate(User sdncModifierDetails) throws Exception {
141 ResourceReqDetails resourceDetails = getResourceObj();
144 return ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
148 public ResourceReqDetails getResourceObj() {
149 // set resource details
150 String resourceName = "ResourceForUpdate" + (int) (Math.random() * 100);
151 String description = "description";
152 ArrayList<String> resourceTags = new ArrayList<String>();
153 resourceTags.add(resourceName);
154 // String category = ResourceCategoriesEnum.MOBILITY.getValue();
155 ArrayList<String> derivedFrom = new ArrayList<String>();
156 derivedFrom.add("tosca.nodes.Root");
157 String vendorName = "Oracle";
158 String vendorRelease = "1.5";
159 String contactId = "pe1116";
160 String icon = "myICON";
162 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, null,
163 derivedFrom, vendorName, vendorRelease, contactId, icon);
164 resourceDetails.addCategoryChain(ResourceCategoryEnum.GENERIC_INFRASTRUCTURE.getCategory(),
165 ResourceCategoryEnum.GENERIC_INFRASTRUCTURE.getSubCategory());
166 return resourceDetails;
169 // -------------------------------------------------------------------
171 protected ResourceReqDetails defineResourse_Benny(int n) {
172 String resourceName = "cisco" + String.valueOf(n);
173 String description = "description";
174 ArrayList<String> resourceTags = new ArrayList<String>();
175 resourceTags.add("tag1");
176 String category = ServiceCategoriesEnum.MOBILITY.getValue();
177 ArrayList<String> derivedFrom = new ArrayList<String>();
178 derivedFrom.add("tosca.nodes.Root");
179 String vendorName = "Oracle";
180 String vendorRelease = "1.5";
181 String contactId = "jh0003";
182 String icon = "borderElement";
184 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, category,
185 derivedFrom, vendorName, vendorRelease, contactId, icon);
187 return resourceDetails;
191 public void getAllAbstractResources() throws Exception {
192 RestResponse abstractResources = CatalogRestUtils.getAbstractResources();
194 int status = abstractResources.getErrorCode();
195 assertTrue(status == 200);
196 String json = abstractResources.getResponse();
197 JSONArray array = (JSONArray) JSONValue.parse(json);
198 for (Object o : array) {
199 JSONObject value = (JSONObject) o;
200 Boolean element = (Boolean) value.get("abstract");
207 public void getAllNotAbstractResources() throws Exception {
208 CloseableHttpClient httpclient = HttpClients.createDefault();
210 String url = String.format(Urls.GET_ALL_NOT_ABSTRACT_RESOURCES, config.getCatalogBeHost(),
211 config.getCatalogBePort());
212 HttpGet httpget = new HttpGet(url);
214 httpget.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
216 httpget.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
218 httpget.addHeader(HttpHeaderEnum.USER_ID.getValue(),
219 ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER).getUserId());
221 // System.out.println("Executing request " +
222 // httpget.getRequestLine());
223 CloseableHttpResponse response = httpclient.execute(httpget);
224 int status = response.getStatusLine().getStatusCode();
225 assertTrue(status == 200);
227 String json = EntityUtils.toString(response.getEntity());
228 JSONArray array = (JSONArray) JSONValue.parse(json);
229 for (Object o : array) {
230 JSONObject value = (JSONObject) o;
231 Boolean element = (Boolean) value.get("abstract");
232 assertTrue(!element);
244 public void updateResourceMetadata_methodNotAllowed() throws Exception {
245 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
246 Config config = Utils.getConfig();
247 Map<String, String> headersMap = new HashMap<String, String>();
248 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
249 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
250 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
252 // set resource details
253 String resourceName = "ResForUpdate";
254 String description = "updatedDescription";
255 ArrayList<String> resourceTags = new ArrayList<String>();
256 resourceTags.add("tag1");
257 resourceTags.add("tag2");
258 ArrayList<String> derivedFrom = new ArrayList<String>();
259 derivedFrom.add("tosca.nodes.root");
260 String category = ServiceCategoriesEnum.VOIP.getValue();
261 String vendorName = "OracleUp";
262 String vendorRelease = "1.5Up";
263 String contactId = "pe1117";
264 String icon = "myICON.jpgUp";
266 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, category,
267 derivedFrom, vendorName, vendorRelease, contactId, icon);
269 Gson gson = new Gson();
270 String userBodyJson = gson.toJson(resourceDetails);
271 HttpRequest http = new HttpRequest();
272 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
275 RestResponse updateResourceResponse = http.httpSendByMethod(url, "POST", userBodyJson, headersMap);
277 assertNotNull("Check error code exists in response after wrong update resource",
278 updateResourceResponse.getErrorCode());
279 assertEquals("Check error code after update resource", 405, updateResourceResponse.getErrorCode().intValue());
283 public void validateResourceNameTest() throws Exception {
284 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
285 sdncModifierDetails.setUserId("jh0003");
287 ResourceReqDetails resourceDetails = getResourceObj();
290 RestResponse restResponse = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
291 Resource resourceRespJavaObject = ResponseParser
292 .convertResourceResponseToJavaObject(restResponse.getResponse());
293 CloseableHttpClient httpclient = HttpClients.createDefault();
297 String url = String.format(Urls.VALIDATE_RESOURCE_NAME, config.getCatalogBeHost(),
298 config.getCatalogBePort(), resourceDetails.getName());
300 HttpGet httpget = new HttpGet(url);
302 httpget.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
304 httpget.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
306 httpget.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
308 // System.out.println("Executing request " +
309 // httpget.getRequestLine());
310 CloseableHttpResponse response = httpclient.execute(httpget);
311 int status = response.getStatusLine().getStatusCode();
312 assertTrue(status == 200);
314 String json = EntityUtils.toString(response.getEntity());
315 JSONObject object = (JSONObject) JSONValue.parse(json);
316 Boolean element = (Boolean) object.get("isValid");
317 assertTrue(!element);
323 url = String.format(Urls.VALIDATE_RESOURCE_NAME, config.getCatalogBeHost(), config.getCatalogBePort(),
324 resourceDetails.getName() + "temp");
326 httpget = new HttpGet(url);
328 httpget.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
330 httpget.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
332 httpget.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
334 // System.out.println("Executing request " +
335 // httpget.getRequestLine());
336 response = httpclient.execute(httpget);
337 status = response.getStatusLine().getStatusCode();
338 assertTrue(status == 200);
340 String json = EntityUtils.toString(response.getEntity());
341 JSONObject object = (JSONObject) JSONValue.parse(json);
342 Boolean element = (Boolean) object.get("isValid");
353 ResourceRestUtils.deleteResource(resourceDetails, sdncModifierDetails, "0.1");
357 // -------------------------------------------------------------------
360 // public void createResource_Benny() throws Exception {
361 // for (int i = 0; i < 100; i++) {
362 // ResourceReqDetails resourceDetails = defineResourse_Benny(i);
364 // ResourceRestUtils.createResource(resourceDetails,
365 // UserUtils.getDesignerDetails());
366 // // resourceUtils.deleteResource(resourceDetails,
367 // UserUtils.getDesignerDetails(), "0.1");