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 com.google.gson.Gson;
24 import org.json.simple.JSONArray;
25 import org.json.simple.JSONObject;
26 import org.json.simple.JSONValue;
27 import org.junit.Rule;
28 import org.junit.rules.TestName;
29 import org.openecomp.sdc.be.model.User;
30 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
31 import org.openecomp.sdc.ci.tests.api.Urls;
32 import org.openecomp.sdc.ci.tests.config.Config;
33 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
34 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
35 import org.openecomp.sdc.ci.tests.datatypes.enums.*;
36 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
37 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
38 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
39 import org.openecomp.sdc.ci.tests.utils.Utils;
40 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
41 import org.openecomp.sdc.ci.tests.utils.rest.CatalogRestUtils;
42 import org.openecomp.sdc.ci.tests.utils.rest.LifecycleRestUtils;
43 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
44 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
45 import org.testng.AssertJUnit;
46 import org.testng.annotations.AfterMethod;
47 import org.testng.annotations.BeforeMethod;
48 import org.testng.annotations.Test;
50 import java.util.ArrayList;
51 import java.util.HashMap;
54 public class CatalogDataApiTest extends ComponentBaseTest {
56 protected Config config = Config.instance();
57 protected String contentTypeHeaderData = "application/json";
58 protected String acceptHeaderDate = "application/json";
61 public static TestName name = new TestName();
63 protected RestResponse res1;
64 protected RestResponse res2;
65 protected RestResponse svc1;
66 protected ResourceReqDetails resourceDetails1;
67 protected ResourceReqDetails resourceDetails2;
68 protected ServiceReqDetails svcDetails1;
70 public CatalogDataApiTest() {
71 super(name, CatalogDataApiTest.class.getName());
75 public void setUp() throws Exception {
76 user = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
77 resourceDetails1 = buildResourceDetails(user, "TestResource1");
78 resourceDetails2 = buildResourceDetails(user, "TestResource2");
79 svcDetails1 = buildServiceDetails("TestService1");
81 res1 = createResource(user, resourceDetails1);
82 AssertJUnit.assertEquals("create resorce failed", 201, res1.getErrorCode().intValue());
83 resourceDetails1.setUniqueId(ResponseParser.getUniqueIdFromResponse(res1));
84 resourceDetails2.setVersion(ResponseParser.getVersionFromResponse(res1));
86 res2 = createResource(user, resourceDetails2);
87 AssertJUnit.assertEquals("create resorce failed", 201, res2.getErrorCode().intValue());
88 resourceDetails2.setUniqueId(ResponseParser.getUniqueIdFromResponse(res2));
89 resourceDetails2.setVersion(ResponseParser.getVersionFromResponse(res2));
91 svc1 = createService(user, svcDetails1);
92 AssertJUnit.assertEquals("create resorce failed", 201, svc1.getErrorCode().intValue());
93 svcDetails1.setUniqueId(ResponseParser.convertServiceResponseToJavaObject(svc1.getResponse()).getUniqueId());
94 svcDetails1.setVersion(ResponseParser.convertServiceResponseToJavaObject(svc1.getResponse()).getVersion());
98 public void tearDown() throws Exception {
99 deleteResource(resourceDetails1.getUniqueId(), user.getUserId());
100 deleteResource(resourceDetails2.getUniqueId(), user.getUserId());
101 deleteService(svcDetails1.getUniqueId(), user);
106 public void getCatalogData() throws Exception {
108 RestResponse checkInResponse = LifecycleRestUtils.changeResourceState(resourceDetails1, user, "0.1",
109 LifeCycleStatesEnum.CHECKIN);
110 AssertJUnit.assertEquals("check in operation failed", 200, checkInResponse.getErrorCode().intValue());
112 RestResponse res = CatalogRestUtils.getCatalog(user.getUserId());
113 String json = res.getResponse();
114 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
115 JSONArray resources = (JSONArray) jsonResp.get("resources");
116 JSONArray services = (JSONArray) jsonResp.get("services");
118 // Verify all the expected resources received.
119 AssertJUnit.assertTrue("check resource1 is in response",
120 isComponentInArray(resourceDetails1.getUniqueId(), resources));
121 AssertJUnit.assertTrue("check resource2 is in response",
122 isComponentInArray(resourceDetails2.getUniqueId(), resources));
123 AssertJUnit.assertTrue("check service1 is in response",
124 isComponentInArray(svcDetails1.getUniqueId(), services));
128 protected void deleteResource(String resourceUniqueId, String httpCspUserId) throws Exception {
129 RestResponse deleteResourceResponse = ResourceRestUtils.deleteResource(resourceUniqueId, httpCspUserId);
133 protected RestResponse createResource(User user, ResourceReqDetails resourceDetails) throws Exception {
134 deleteResource(resourceDetails.getName(), user.getUserId());
135 return ResourceRestUtils.createResource(resourceDetails, user);
138 protected ResourceReqDetails buildResourceDetails(User user, String resourceName) {
139 String description = "description";
140 ArrayList<String> resourceTags = new ArrayList<String>();
141 resourceTags.add(resourceName);
142 ArrayList<String> derivedFrom = new ArrayList<String>();
143 derivedFrom.add("tosca.nodes.Root");
144 String vendorName = "Oracle";
145 String vendorRelease = "1.0";
146 String contactId = user.getUserId();
147 String icon = "myICON";
149 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, null,
150 derivedFrom, vendorName, vendorRelease, contactId, icon);
151 resourceDetails.addCategoryChain(ResourceCategoryEnum.GENERIC_DATABASE.getCategory(),
152 ResourceCategoryEnum.GENERIC_DATABASE.getSubCategory());
153 return resourceDetails;
156 protected boolean isComponentInArray(String id, JSONArray component) {
157 for (int i = 0; i < component.size(); i++) {
158 JSONObject jobject = (JSONObject) component.get(i);
159 if (jobject.get("uniqueId").toString().equals(id.toLowerCase())) {
166 protected RestResponse createService(User user, ServiceReqDetails svcDetails) throws Exception {
168 Config config = Utils.getConfig();
170 Map<String, String> headersMap = getHeadersMap(user);
172 Gson gson = new Gson();
173 String body = gson.toJson(svcDetails);
174 HttpRequest http = new HttpRequest();
175 String url = String.format(Urls.CREATE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort());
176 RestResponse res = http.httpSendPost(url, body, headersMap);
177 // System.out.println("Create service was finished with response:
178 // "+res.getErrorCode());
182 protected Map<String, String> getHeadersMap(User user) {
183 Map<String, String> headersMap = new HashMap<String, String>();
184 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
185 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
186 headersMap.put("USER_ID", user.getUserId());
190 protected ServiceReqDetails buildServiceDetails(String serviceName) {
191 String description = "description";
192 ArrayList<String> serviceTags = new ArrayList<String>();
193 serviceTags.add("tag1");
194 serviceTags.add(serviceName);
195 String category = ServiceCategoriesEnum.MOBILITY.getValue();
196 String vendorName = "Oracle";
197 String vendorRelease = "0.1";
198 String contactId = "al1976";
199 String icon = "myIcon";
200 String instantiationType = ServiceInstantiationType.A_LA_CARTE.getValue();
202 ServiceReqDetails svcdetails = new ServiceReqDetails(serviceName, category, serviceTags, description,
203 contactId, icon, instantiationType);
207 public RestResponse deleteService(String serviceId, User user) throws Exception {
208 HttpRequest httpRequest = new HttpRequest();
209 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(),
212 Map<String, String> headersMap = getHeadersMap(user);
213 RestResponse res = httpRequest.httpSendDelete(url, headersMap);
214 // System.out.println("Delete service was finished with response:
215 // "+res.getErrorCode());
219 public class NewObject {
220 private String _name;
222 public String getName() {
226 public void setName(String name) {