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.category;
23 import java.util.ArrayList;
24 import java.util.HashMap;
27 import org.apache.log4j.lf5.util.ResourceUtils;
28 import org.json.simple.JSONArray;
29 import org.json.simple.JSONObject;
30 import org.json.simple.JSONValue;
31 import org.junit.Rule;
32 import org.junit.rules.TestName;
33 import org.openecomp.sdc.be.model.User;
34 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
35 import org.openecomp.sdc.ci.tests.api.Urls;
36 import org.openecomp.sdc.ci.tests.config.Config;
37 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
38 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
39 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
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.LifecycleRestUtils;
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.ServiceRestUtils;
53 import org.testng.AssertJUnit;
54 import org.testng.annotations.AfterMethod;
55 import org.testng.annotations.BeforeMethod;
56 import org.testng.annotations.Test;
58 import com.google.gson.Gson;
60 public class CatalogDataApiTest extends ComponentBaseTest {
62 protected Config config = Config.instance();
63 protected String contentTypeHeaderData = "application/json";
64 protected String acceptHeaderDate = "application/json";
67 public static TestName name = new TestName();
69 protected RestResponse res1;
70 protected RestResponse res2;
71 protected RestResponse svc1;
72 protected ResourceReqDetails resourceDetails1;
73 protected ResourceReqDetails resourceDetails2;
74 protected ServiceReqDetails svcDetails1;
76 public CatalogDataApiTest() {
77 super(name, CatalogDataApiTest.class.getName());
81 public void setUp() throws Exception {
82 user = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
83 resourceDetails1 = buildResourceDetails(user, "TestResource1");
84 resourceDetails2 = buildResourceDetails(user, "TestResource2");
85 svcDetails1 = buildServiceDetails("TestService1");
87 res1 = createResource(user, resourceDetails1);
88 AssertJUnit.assertEquals("create resorce failed", 201, res1.getErrorCode().intValue());
89 resourceDetails1.setUniqueId(ResponseParser.getUniqueIdFromResponse(res1));
90 resourceDetails2.setVersion(ResponseParser.getVersionFromResponse(res1));
92 res2 = createResource(user, resourceDetails2);
93 AssertJUnit.assertEquals("create resorce failed", 201, res2.getErrorCode().intValue());
94 resourceDetails2.setUniqueId(ResponseParser.getUniqueIdFromResponse(res2));
95 resourceDetails2.setVersion(ResponseParser.getVersionFromResponse(res2));
97 svc1 = createService(user, svcDetails1);
98 AssertJUnit.assertEquals("create resorce failed", 201, svc1.getErrorCode().intValue());
99 svcDetails1.setUniqueId(ResponseParser.convertServiceResponseToJavaObject(svc1.getResponse()).getUniqueId());
100 svcDetails1.setVersion(ResponseParser.convertServiceResponseToJavaObject(svc1.getResponse()).getVersion());
104 public void tearDown() throws Exception {
105 deleteResource(resourceDetails1.getUniqueId(), user.getUserId());
106 deleteResource(resourceDetails2.getUniqueId(), user.getUserId());
107 deleteService(svcDetails1.getUniqueId(), user);
112 public void getCatalogData() throws Exception {
114 RestResponse checkInResponse = LifecycleRestUtils.changeResourceState(resourceDetails1, user, "0.1",
115 LifeCycleStatesEnum.CHECKIN);
116 AssertJUnit.assertEquals("check in operation failed", 200, checkInResponse.getErrorCode().intValue());
118 RestResponse res = CatalogRestUtils.getCatalog(user.getUserId());
119 String json = res.getResponse();
120 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
121 JSONArray resources = (JSONArray) jsonResp.get("resources");
122 JSONArray services = (JSONArray) jsonResp.get("services");
124 // Verify all the expected resources received.
125 AssertJUnit.assertTrue("check resource1 is in response",
126 isComponentInArray(resourceDetails1.getUniqueId(), resources));
127 AssertJUnit.assertTrue("check resource2 is in response",
128 isComponentInArray(resourceDetails2.getUniqueId(), resources));
129 AssertJUnit.assertTrue("check service1 is in response",
130 isComponentInArray(svcDetails1.getUniqueId(), services));
134 protected void deleteResource(String resourceUniqueId, String httpCspUserId) throws Exception {
135 RestResponse deleteResourceResponse = ResourceRestUtils.deleteResource(resourceUniqueId, httpCspUserId);
139 protected RestResponse createResource(User user, ResourceReqDetails resourceDetails) throws Exception {
140 deleteResource(resourceDetails.getName(), user.getUserId());
141 return ResourceRestUtils.createResource(resourceDetails, user);
144 protected ResourceReqDetails buildResourceDetails(User user, String resourceName) {
145 String description = "description";
146 ArrayList<String> resourceTags = new ArrayList<String>();
147 resourceTags.add(resourceName);
148 ArrayList<String> derivedFrom = new ArrayList<String>();
149 derivedFrom.add("tosca.nodes.Root");
150 String vendorName = "Oracle";
151 String vendorRelease = "1.0";
152 String contactId = user.getUserId();
153 String icon = "myICON";
155 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, null,
156 derivedFrom, vendorName, vendorRelease, contactId, icon);
157 resourceDetails.addCategoryChain(ResourceCategoryEnum.GENERIC_DATABASE.getCategory(),
158 ResourceCategoryEnum.GENERIC_DATABASE.getSubCategory());
159 return resourceDetails;
162 protected boolean isComponentInArray(String id, JSONArray component) {
163 for (int i = 0; i < component.size(); i++) {
164 JSONObject jobject = (JSONObject) component.get(i);
165 if (jobject.get("uniqueId").toString().equals(id.toLowerCase())) {
172 protected RestResponse createService(User user, ServiceReqDetails svcDetails) throws Exception {
174 Config config = Utils.getConfig();
176 Map<String, String> headersMap = getHeadersMap(user);
178 Gson gson = new Gson();
179 String body = gson.toJson(svcDetails);
180 HttpRequest http = new HttpRequest();
181 String url = String.format(Urls.CREATE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort());
182 RestResponse res = http.httpSendPost(url, body, headersMap);
183 // System.out.println("Create service was finished with response:
184 // "+res.getErrorCode());
188 protected Map<String, String> getHeadersMap(User user) {
189 Map<String, String> headersMap = new HashMap<String, String>();
190 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
191 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
192 headersMap.put("USER_ID", user.getUserId());
196 protected ServiceReqDetails buildServiceDetails(String serviceName) {
197 String description = "description";
198 ArrayList<String> serviceTags = new ArrayList<String>();
199 serviceTags.add("tag1");
200 serviceTags.add(serviceName);
201 String category = ServiceCategoriesEnum.MOBILITY.getValue();
202 String vendorName = "Oracle";
203 String vendorRelease = "0.1";
204 String contactId = "al1976";
205 String icon = "myIcon";
207 ServiceReqDetails svcdetails = new ServiceReqDetails(serviceName, category, serviceTags, description,
212 public RestResponse deleteService(String serviceId, User user) throws Exception {
213 HttpRequest httpRequest = new HttpRequest();
214 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(),
217 Map<String, String> headersMap = getHeadersMap(user);
218 RestResponse res = httpRequest.httpSendDelete(url, headersMap);
219 // System.out.println("Delete service was finished with response:
220 // "+res.getErrorCode());
224 public class NewObject {
225 private String _name;
227 public String getName() {
231 public void setName(String name) {