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 com.google.gson.Gson;
24 import org.apache.http.client.methods.CloseableHttpResponse;
25 import org.apache.http.client.methods.HttpGet;
26 import org.apache.http.impl.client.CloseableHttpClient;
27 import org.apache.http.impl.client.HttpClients;
28 import org.apache.http.util.EntityUtils;
29 import org.json.simple.JSONArray;
30 import org.json.simple.JSONObject;
31 import org.json.simple.JSONValue;
32 import org.junit.Rule;
33 import org.junit.rules.TestName;
34 import org.openecomp.sdc.be.model.Resource;
35 import org.openecomp.sdc.be.model.User;
36 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
37 import org.openecomp.sdc.ci.tests.api.Urls;
38 import org.openecomp.sdc.ci.tests.config.Config;
39 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
40 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
41 import org.openecomp.sdc.ci.tests.datatypes.enums.ServiceCategoriesEnum;
42 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
43 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
44 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
45 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
46 import org.openecomp.sdc.ci.tests.utils.Utils;
47 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
48 import org.openecomp.sdc.ci.tests.utils.rest.CatalogRestUtils;
49 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
50 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
51 import org.openecomp.sdc.ci.tests.utils.validation.ResourceValidationUtils;
52 import org.testng.annotations.Test;
54 import java.util.ArrayList;
55 import java.util.HashMap;
58 import static org.testng.AssertJUnit.*;
60 public class ResourceApiTest extends ComponentBaseTest {
62 protected final String contentTypeHeaderData = "application/json";
63 protected final String acceptHeaderDate = "application/json";
66 public static TestName name = new TestName();
68 public ResourceApiTest() {
69 super(name, ResourceApiTest.class.getName());
74 public void updateResourceMetadataSuccess() throws Exception {
75 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
76 sdncModifierDetails.setUserId("jh0003");
77 RestResponse restResponse = createResourceForUpdate(sdncModifierDetails);
78 Resource resourceRespJavaObject = ResponseParser
79 .convertResourceResponseToJavaObject(restResponse.getResponse());
81 Config config = Utils.getConfig();
82 Map<String, String> headersMap = new HashMap<String, String>();
83 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
84 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
85 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
87 // set resource details
88 ResourceReqDetails resourceDetails = new ResourceReqDetails();
89 resourceDetails.setDescription("updatedDescription");
90 ArrayList<String> resourceTags = new ArrayList<String>();
91 // Duplicate tags are allowed and should be de-duplicated by the server
93 resourceTags.add(resourceRespJavaObject.getName());
94 resourceTags.add("tag1");
95 resourceTags.add("tag1");
96 resourceTags.add("tag2");
97 resourceTags.add("tag2");
98 resourceDetails.setTags(resourceTags);
99 resourceDetails.addCategoryChain(ResourceCategoryEnum.NETWORK_L2_3_ROUTERS.getCategory(),
100 ResourceCategoryEnum.NETWORK_L2_3_ROUTERS.getSubCategory());
101 resourceDetails.setVendorName("OracleUp");
102 resourceDetails.setVendorRelease("1.5Up");
103 resourceDetails.setContactId("pe1116");
105 resourceDetails.setIcon(resourceRespJavaObject.getIcon());
106 resourceDetails.setName(resourceRespJavaObject.getName());
107 resourceDetails.setDerivedFrom(resourceRespJavaObject.getDerivedFrom());
109 // ResourceReqDetails resourceDetails = new
110 // ResourceReqDetails(resourceName, description, resourceTags, category,
111 // derivedFrom, vendorName, vendorRelease, contactId, null);
113 Gson gson = new Gson();
114 String userBodyJson = gson.toJson(resourceDetails);
115 HttpRequest http = new HttpRequest();
116 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
117 resourceRespJavaObject.getUniqueId());
118 RestResponse updateResourceResponse = http.httpSendByMethod(url, "PUT", userBodyJson, headersMap);
120 // resourceDetails.setResourceName(resourceRespJavaObject.getResourceName());
121 ResourceValidationUtils.validateResourceReqVsResp(resourceDetails,
122 ResponseParser.convertResourceResponseToJavaObject(updateResourceResponse.getResponse()));
125 deleteResource(resourceRespJavaObject.getUniqueId(), sdncModifierDetails.getUserId());
129 protected void deleteResource(String resourceUniqueId, String httpCspUserId) throws Exception {
130 RestResponse res = ResourceRestUtils.deleteResource(resourceUniqueId, httpCspUserId);
132 // System.out.println("Delete resource was finished with response: " +
133 // res.getErrorCode());
136 protected RestResponse createResourceForUpdate(User sdncModifierDetails) throws Exception {
138 ResourceReqDetails resourceDetails = getResourceObj();
141 return ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
145 public ResourceReqDetails getResourceObj() {
146 // set resource details
147 String resourceName = "ResourceForUpdate" + (int) (Math.random() * 100);
148 String description = "description";
149 ArrayList<String> resourceTags = new ArrayList<String>();
150 resourceTags.add(resourceName);
151 // String category = ResourceCategoriesEnum.MOBILITY.getValue();
152 ArrayList<String> derivedFrom = new ArrayList<String>();
153 derivedFrom.add("tosca.nodes.Root");
154 String vendorName = "Oracle";
155 String vendorRelease = "1.5";
156 String contactId = "pe1116";
157 String icon = "myICON";
159 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, null,
160 derivedFrom, vendorName, vendorRelease, contactId, icon);
161 resourceDetails.addCategoryChain(ResourceCategoryEnum.GENERIC_INFRASTRUCTURE.getCategory(),
162 ResourceCategoryEnum.GENERIC_INFRASTRUCTURE.getSubCategory());
163 return resourceDetails;
166 // -------------------------------------------------------------------
168 protected ResourceReqDetails defineResourse_Benny(int n) {
169 String resourceName = "cisco" + String.valueOf(n);
170 String description = "description";
171 ArrayList<String> resourceTags = new ArrayList<String>();
172 resourceTags.add("tag1");
173 String category = ServiceCategoriesEnum.MOBILITY.getValue();
174 ArrayList<String> derivedFrom = new ArrayList<String>();
175 derivedFrom.add("tosca.nodes.Root");
176 String vendorName = "Oracle";
177 String vendorRelease = "1.5";
178 String contactId = "jh0003";
179 String icon = "borderElement";
181 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, category,
182 derivedFrom, vendorName, vendorRelease, contactId, icon);
184 return resourceDetails;
188 public void getAllAbstractResources() throws Exception {
189 RestResponse abstractResources = CatalogRestUtils.getAbstractResources();
191 int status = abstractResources.getErrorCode();
192 assertTrue(status == 200);
193 String json = abstractResources.getResponse();
194 JSONArray array = (JSONArray) JSONValue.parse(json);
195 for (Object o : array) {
196 JSONObject value = (JSONObject) o;
197 Boolean element = (Boolean) value.get("abstract");
204 public void getAllNotAbstractResources() throws Exception {
205 CloseableHttpClient httpclient = HttpClients.createDefault();
207 String url = String.format(Urls.GET_ALL_NOT_ABSTRACT_RESOURCES, config.getCatalogBeHost(),
208 config.getCatalogBePort());
209 HttpGet httpget = new HttpGet(url);
211 httpget.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
213 httpget.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
215 httpget.addHeader(HttpHeaderEnum.USER_ID.getValue(),
216 ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER).getUserId());
218 // System.out.println("Executing request " +
219 // httpget.getRequestLine());
220 CloseableHttpResponse response = httpclient.execute(httpget);
221 int status = response.getStatusLine().getStatusCode();
222 assertTrue(status == 200);
224 String json = EntityUtils.toString(response.getEntity());
225 JSONArray array = (JSONArray) JSONValue.parse(json);
226 for (Object o : array) {
227 JSONObject value = (JSONObject) o;
228 Boolean element = (Boolean) value.get("abstract");
229 assertTrue(!element);
241 public void updateResourceMetadata_methodNotAllowed() throws Exception {
242 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
243 Config config = Utils.getConfig();
244 Map<String, String> headersMap = new HashMap<String, String>();
245 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
246 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
247 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
249 // set resource details
250 String resourceName = "ResForUpdate";
251 String description = "updatedDescription";
252 ArrayList<String> resourceTags = new ArrayList<String>();
253 resourceTags.add("tag1");
254 resourceTags.add("tag2");
255 ArrayList<String> derivedFrom = new ArrayList<String>();
256 derivedFrom.add("tosca.nodes.root");
257 String category = ServiceCategoriesEnum.VOIP.getValue();
258 String vendorName = "OracleUp";
259 String vendorRelease = "1.5Up";
260 String contactId = "pe1117";
261 String icon = "myICON.jpgUp";
263 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, category,
264 derivedFrom, vendorName, vendorRelease, contactId, icon);
266 Gson gson = new Gson();
267 String userBodyJson = gson.toJson(resourceDetails);
268 HttpRequest http = new HttpRequest();
269 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
272 RestResponse updateResourceResponse = http.httpSendByMethod(url, "POST", userBodyJson, headersMap);
274 assertNotNull("Check error code exists in response after wrong update resource",
275 updateResourceResponse.getErrorCode());
276 assertEquals("Check error code after update resource", 405, updateResourceResponse.getErrorCode().intValue());
280 public void validateResourceNameTest() throws Exception {
281 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
282 sdncModifierDetails.setUserId("jh0003");
284 ResourceReqDetails resourceDetails = getResourceObj();
287 RestResponse restResponse = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
288 Resource resourceRespJavaObject = ResponseParser
289 .convertResourceResponseToJavaObject(restResponse.getResponse());
290 CloseableHttpClient httpclient = HttpClients.createDefault();
294 String url = String.format(Urls.VALIDATE_RESOURCE_NAME, config.getCatalogBeHost(),
295 config.getCatalogBePort(), resourceDetails.getName());
297 HttpGet httpget = new HttpGet(url);
299 httpget.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
301 httpget.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
303 httpget.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
305 // System.out.println("Executing request " +
306 // httpget.getRequestLine());
307 CloseableHttpResponse response = httpclient.execute(httpget);
308 int status = response.getStatusLine().getStatusCode();
309 assertTrue(status == 200);
311 String json = EntityUtils.toString(response.getEntity());
312 JSONObject object = (JSONObject) JSONValue.parse(json);
313 Boolean element = (Boolean) object.get("isValid");
314 assertTrue(!element);
320 url = String.format(Urls.VALIDATE_RESOURCE_NAME, config.getCatalogBeHost(), config.getCatalogBePort(),
321 resourceDetails.getName() + "temp");
323 httpget = new HttpGet(url);
325 httpget.addHeader(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
327 httpget.addHeader(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
329 httpget.addHeader(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
331 // System.out.println("Executing request " +
332 // httpget.getRequestLine());
333 response = httpclient.execute(httpget);
334 status = response.getStatusLine().getStatusCode();
335 assertTrue(status == 200);
337 String json = EntityUtils.toString(response.getEntity());
338 JSONObject object = (JSONObject) JSONValue.parse(json);
339 Boolean element = (Boolean) object.get("isValid");
350 ResourceRestUtils.deleteResource(resourceDetails, sdncModifierDetails, "0.1");
354 // -------------------------------------------------------------------
357 // public void createResource_Benny() throws Exception {
358 // for (int i = 0; i < 100; i++) {
359 // ResourceReqDetails resourceDetails = defineResourse_Benny(i);
361 // ResourceRestUtils.createResource(resourceDetails,
362 // UserUtils.getDesignerDetails());
363 // // resourceUtils.deleteResource(resourceDetails,
364 // UserUtils.getDesignerDetails(), "0.1");