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.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
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.datatypes.enums.ResourceTypeEnum;
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.ServiceReqDetails;
41 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
42 import org.openecomp.sdc.ci.tests.datatypes.enums.OriginTypeEnum;
43 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
44 import org.openecomp.sdc.ci.tests.datatypes.enums.ServiceCategoriesEnum;
45 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
46 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
47 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
48 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
49 import org.openecomp.sdc.ci.tests.utils.Utils;
50 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
51 import org.openecomp.sdc.ci.tests.utils.rest.CatalogRestUtils;
52 import org.openecomp.sdc.ci.tests.utils.rest.LifecycleRestUtils;
53 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
54 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
55 import org.testng.AssertJUnit;
56 import org.testng.annotations.AfterClass;
57 import org.testng.annotations.BeforeMethod;
58 import org.testng.annotations.Test;
60 import com.google.gson.Gson;
62 public class CatalogDataApiTest extends ComponentBaseTest {
64 protected Config config = Config.instance();
65 protected String contentTypeHeaderData = "application/json";
66 protected String acceptHeaderDate = "application/json";
67 protected boolean isInitialized = false;
70 public static TestName name = new TestName();
72 protected RestResponse res1;
73 protected RestResponse res2;
74 protected RestResponse svc1;
75 protected ResourceReqDetails resourceDetails1;
76 protected ResourceReqDetails resourceDetails2;
77 protected ServiceReqDetails svcDetails1;
79 public CatalogDataApiTest() {
80 super(name, CatalogDataApiTest.class.getName());
84 public void setUp() throws Exception {
87 user = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
88 resourceDetails1 = buildResourceDetails(user, "TestResource1");
89 resourceDetails1.setResourceType(ResourceTypeEnum.VFCMT.name());
90 resourceDetails2 = buildResourceDetails(user, "TestResource2");
91 svcDetails1 = buildServiceDetails("TestService1");
94 res1 = createResource(user, resourceDetails1);
95 AssertJUnit.assertEquals("create resorce failed", 201, res1.getErrorCode().intValue());
96 resourceDetails1.setUniqueId(ResponseParser.getUniqueIdFromResponse(res1));
98 resourceDetails2.setVersion(ResponseParser.getVersionFromResponse(res1));
101 res2 = createResource(user, resourceDetails2);
102 AssertJUnit.assertEquals("create resorce failed", 201, res2.getErrorCode().intValue());
103 resourceDetails2.setUniqueId(ResponseParser.getUniqueIdFromResponse(res2));
104 resourceDetails2.setVersion(ResponseParser.getVersionFromResponse(res2));
107 svc1 = createService(user, svcDetails1);
108 AssertJUnit.assertEquals("create resorce failed", 201, svc1.getErrorCode().intValue());
109 svcDetails1.setUniqueId(ResponseParser.convertServiceResponseToJavaObject(svc1.getResponse()).getUniqueId());
110 svcDetails1.setVersion(ResponseParser.convertServiceResponseToJavaObject(svc1.getResponse()).getVersion());
111 isInitialized = true;
115 public void tearDown() throws Exception {
116 deleteResource(resourceDetails1.getUniqueId(), user.getUserId());
117 deleteResource(resourceDetails2.getUniqueId(), user.getUserId());
118 deleteService(svcDetails1.getUniqueId(), user);
123 public void getCatalogData() throws Exception {
125 RestResponse checkInResponse = LifecycleRestUtils.changeResourceState(resourceDetails1, user, "0.1",
126 LifeCycleStatesEnum.CHECKIN);
127 AssertJUnit.assertEquals("check in operation failed", 200, checkInResponse.getErrorCode().intValue());
129 RestResponse res = CatalogRestUtils.getCatalog(user.getUserId());
130 String json = res.getResponse();
131 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
132 JSONArray resources = (JSONArray) jsonResp.get("resources");
133 JSONArray services = (JSONArray) jsonResp.get("services");
135 // Verify all the expected resources received.
136 AssertJUnit.assertTrue("check resource1 is in response",
137 isComponentInArray(resourceDetails1.getUniqueId(), resources));
138 AssertJUnit.assertTrue("check resource2 is in response",
139 isComponentInArray(resourceDetails2.getUniqueId(), resources));
140 AssertJUnit.assertTrue("check service1 is in response",
141 isComponentInArray(svcDetails1.getUniqueId(), services));
146 public void getCatalogDataNoVFCMT() throws Exception {
148 List<String> excludeTyps = Arrays.asList(OriginTypeEnum.VFCMT.name());
149 RestResponse res = CatalogRestUtils.getCatalog(user.getUserId(), excludeTyps);
150 String json = res.getResponse();
151 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
152 JSONArray resources = (JSONArray) jsonResp.get("resources");
153 JSONArray services = (JSONArray) jsonResp.get("services");
155 // Verify all the expected resources received except of resource1 which is VFCMT
156 AssertJUnit.assertFalse("check resource1 is in response",
157 isComponentInArray(resourceDetails1.getUniqueId(), resources));
158 AssertJUnit.assertTrue("check resource2 is in response",
159 isComponentInArray(resourceDetails2.getUniqueId(), resources));
160 AssertJUnit.assertTrue("check service1 is in response",
161 isComponentInArray(svcDetails1.getUniqueId(), services));
166 public void getCatalogDataNoVFCandVFCMT() throws Exception {
168 List<String> excludeTyps = Arrays.asList(OriginTypeEnum.VFCMT.name(), OriginTypeEnum.VFC.name());
169 RestResponse res = CatalogRestUtils.getCatalog(user.getUserId(), excludeTyps);
170 String json = res.getResponse();
171 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
172 JSONArray resources = (JSONArray) jsonResp.get("resources");
173 JSONArray services = (JSONArray) jsonResp.get("services");
175 // Verify all the expected resources received except of VFCMT & VFC
176 AssertJUnit.assertFalse("check resource1 is in response",
177 isComponentInArray(resourceDetails1.getUniqueId(), resources));
178 AssertJUnit.assertFalse("check resource2 is in response",
179 isComponentInArray(resourceDetails2.getUniqueId(), resources));
180 AssertJUnit.assertTrue("check service1 is in response",
181 isComponentInArray(svcDetails1.getUniqueId(), services));
186 public void getCatalogDataNoServiceAndVFC() throws Exception {
188 List<String> excludeTyps = Arrays.asList(OriginTypeEnum.SERVICE.name(), OriginTypeEnum.VFC.name());
189 RestResponse res = CatalogRestUtils.getCatalog(user.getUserId(), excludeTyps);
190 String json = res.getResponse();
191 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
192 JSONArray resources = (JSONArray) jsonResp.get("resources");
193 JSONArray services = (JSONArray) jsonResp.get("services");
195 // Verify all the expected resources received except of VFC & SERVICE
196 AssertJUnit.assertTrue("check resource1 is in response",
197 isComponentInArray(resourceDetails1.getUniqueId(), resources));
198 AssertJUnit.assertFalse("check resource2 is in response",
199 isComponentInArray(resourceDetails2.getUniqueId(), resources));
200 AssertJUnit.assertFalse("check service1 is in response",
201 isComponentInArray(svcDetails1.getUniqueId(), services));
205 protected void deleteResource(String resourceUniqueId, String httpCspUserId) throws Exception {
206 RestResponse deleteResourceResponse = ResourceRestUtils.deleteResource(resourceUniqueId, httpCspUserId);
210 protected RestResponse createResource(User user, ResourceReqDetails resourceDetails) throws Exception {
211 deleteResource(resourceDetails.getName(), user.getUserId());
212 return ResourceRestUtils.createResource(resourceDetails, user);
215 protected ResourceReqDetails buildResourceDetails(User user, String resourceName) {
216 String description = "description";
217 ArrayList<String> resourceTags = new ArrayList<String>();
218 resourceTags.add(resourceName);
219 ArrayList<String> derivedFrom = new ArrayList<String>();
220 derivedFrom.add("tosca.nodes.Root");
221 String vendorName = "Oracle";
222 String vendorRelease = "1.0";
223 String contactId = user.getUserId();
224 String icon = "myICON";
226 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, null,
227 derivedFrom, vendorName, vendorRelease, contactId, icon);
228 resourceDetails.addCategoryChain(ResourceCategoryEnum.GENERIC_DATABASE.getCategory(),
229 ResourceCategoryEnum.GENERIC_DATABASE.getSubCategory());
230 return resourceDetails;
233 protected boolean isComponentInArray(String id, JSONArray component) {
234 for (int i = 0; i < component.size(); i++) {
235 JSONObject jobject = (JSONObject) component.get(i);
236 if (jobject.get("uniqueId").toString().equals(id.toLowerCase())) {
243 protected RestResponse createService(User user, ServiceReqDetails svcDetails) throws Exception {
245 deleteService(svcDetails1.getUniqueId(), user);
246 Config config = Utils.getConfig();
248 Map<String, String> headersMap = getHeadersMap(user);
250 Gson gson = new Gson();
251 String body = gson.toJson(svcDetails);
252 HttpRequest http = new HttpRequest();
253 String url = String.format(Urls.CREATE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort());
254 RestResponse res = http.httpSendPost(url, body, headersMap);
255 // System.out.println("Create service was finished with response:
256 // "+res.getErrorCode());
260 protected Map<String, String> getHeadersMap(User user) {
261 Map<String, String> headersMap = new HashMap<String, String>();
262 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
263 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
264 headersMap.put("USER_ID", user.getUserId());
268 protected ServiceReqDetails buildServiceDetails(String serviceName) {
269 String description = "description";
270 ArrayList<String> serviceTags = new ArrayList<String>();
271 serviceTags.add("tag1");
272 serviceTags.add(serviceName);
273 String category = ServiceCategoriesEnum.MOBILITY.getValue();
274 String vendorName = "Oracle";
275 String vendorRelease = "0.1";
276 String contactId = "al1976";
277 String icon = "myIcon";
279 ServiceReqDetails svcdetails = new ServiceReqDetails(serviceName, category, serviceTags, description,
284 public RestResponse deleteService(String serviceId, User user) throws Exception {
285 RestResponse deleteServiceResponse = ResourceRestUtils.deleteResource(serviceId, user.getUserId());
286 return deleteServiceResponse;
289 public class NewObject {
290 private String _name;
292 public String getName() {
296 public void setName(String name) {