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;
25 import org.json.simple.JSONArray;
26 import org.json.simple.JSONObject;
27 import org.json.simple.JSONValue;
28 import org.junit.Rule;
29 import org.junit.rules.TestName;
30 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
31 import org.openecomp.sdc.be.model.User;
32 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
33 import org.openecomp.sdc.ci.tests.api.Urls;
34 import org.openecomp.sdc.ci.tests.config.Config;
35 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
36 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
37 import org.openecomp.sdc.ci.tests.datatypes.enums.*;
38 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
39 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
40 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
41 import org.openecomp.sdc.ci.tests.utils.Utils;
42 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
43 import org.openecomp.sdc.ci.tests.utils.rest.CatalogRestUtils;
44 import org.openecomp.sdc.ci.tests.utils.rest.LifecycleRestUtils;
45 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
46 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
47 import org.testng.AssertJUnit;
48 import org.testng.annotations.AfterClass;
49 import org.testng.annotations.BeforeMethod;
50 import org.testng.annotations.Test;
52 import com.google.gson.Gson;
54 public class CatalogDataApiTest extends ComponentBaseTest {
56 protected Config config = Config.instance();
57 protected String contentTypeHeaderData = "application/json";
58 protected String acceptHeaderDate = "application/json";
59 protected boolean isInitialized = false;
62 public static TestName name = new TestName();
64 protected RestResponse res1;
65 protected RestResponse res2;
66 protected RestResponse svc1;
67 protected ResourceReqDetails resourceDetails1;
68 protected ResourceReqDetails resourceDetails2;
69 protected ServiceReqDetails svcDetails1;
71 public CatalogDataApiTest() {
72 super(name, CatalogDataApiTest.class.getName());
76 public void setUp() throws Exception {
79 user = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
80 resourceDetails1 = buildResourceDetails(user, "TestResource1");
81 resourceDetails1.setResourceType(ResourceTypeEnum.VFCMT.name());
82 resourceDetails2 = buildResourceDetails(user, "TestResource2");
83 svcDetails1 = buildServiceDetails("TestService1");
86 res1 = createResource(user, resourceDetails1);
87 AssertJUnit.assertEquals("create resorce failed", 201, res1.getErrorCode().intValue());
88 resourceDetails1.setUniqueId(ResponseParser.getUniqueIdFromResponse(res1));
90 resourceDetails2.setVersion(ResponseParser.getVersionFromResponse(res1));
93 res2 = createResource(user, resourceDetails2);
94 AssertJUnit.assertEquals("create resorce failed", 201, res2.getErrorCode().intValue());
95 resourceDetails2.setUniqueId(ResponseParser.getUniqueIdFromResponse(res2));
96 resourceDetails2.setVersion(ResponseParser.getVersionFromResponse(res2));
99 svc1 = createService(user, svcDetails1);
100 AssertJUnit.assertEquals("create resorce failed", 201, svc1.getErrorCode().intValue());
101 svcDetails1.setUniqueId(ResponseParser.convertServiceResponseToJavaObject(svc1.getResponse()).getUniqueId());
102 svcDetails1.setVersion(ResponseParser.convertServiceResponseToJavaObject(svc1.getResponse()).getVersion());
103 isInitialized = true;
107 public void tearDown() throws Exception {
108 deleteResource(resourceDetails1.getUniqueId(), user.getUserId());
109 deleteResource(resourceDetails2.getUniqueId(), user.getUserId());
110 deleteService(svcDetails1.getUniqueId(), user);
115 public void getCatalogData() throws Exception {
117 RestResponse checkInResponse = LifecycleRestUtils.changeResourceState(resourceDetails1, user, "0.1",
118 LifeCycleStatesEnum.CHECKIN);
119 AssertJUnit.assertEquals("check in operation failed", 200, checkInResponse.getErrorCode().intValue());
121 RestResponse res = CatalogRestUtils.getCatalog(user.getUserId());
122 String json = res.getResponse();
123 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
124 JSONArray resources = (JSONArray) jsonResp.get("resources");
125 JSONArray services = (JSONArray) jsonResp.get("services");
127 // Verify all the expected resources received.
128 AssertJUnit.assertTrue("check resource1 is in response",
129 isComponentInArray(resourceDetails1.getUniqueId(), resources));
130 AssertJUnit.assertTrue("check resource2 is in response",
131 isComponentInArray(resourceDetails2.getUniqueId(), resources));
132 AssertJUnit.assertTrue("check service1 is in response",
133 isComponentInArray(svcDetails1.getUniqueId(), services));
138 public void getCatalogDataNoVFCMT() throws Exception {
140 List<String> excludeTyps = Arrays.asList(OriginTypeEnum.VFCMT.name());
141 RestResponse res = CatalogRestUtils.getCatalog(user.getUserId(), excludeTyps);
142 String json = res.getResponse();
143 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
144 JSONArray resources = (JSONArray) jsonResp.get("resources");
145 JSONArray services = (JSONArray) jsonResp.get("services");
147 // Verify all the expected resources received except of resource1 which is VFCMT
148 AssertJUnit.assertFalse("check resource1 is in response",
149 isComponentInArray(resourceDetails1.getUniqueId(), resources));
150 AssertJUnit.assertTrue("check resource2 is in response",
151 isComponentInArray(resourceDetails2.getUniqueId(), resources));
152 AssertJUnit.assertTrue("check service1 is in response",
153 isComponentInArray(svcDetails1.getUniqueId(), services));
158 public void getCatalogDataNoVFCandVFCMT() throws Exception {
160 List<String> excludeTyps = Arrays.asList(OriginTypeEnum.VFCMT.name(), OriginTypeEnum.VFC.name());
161 RestResponse res = CatalogRestUtils.getCatalog(user.getUserId(), excludeTyps);
162 String json = res.getResponse();
163 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
164 JSONArray resources = (JSONArray) jsonResp.get("resources");
165 JSONArray services = (JSONArray) jsonResp.get("services");
167 // Verify all the expected resources received except of VFCMT & VFC
168 AssertJUnit.assertFalse("check resource1 is in response",
169 isComponentInArray(resourceDetails1.getUniqueId(), resources));
170 AssertJUnit.assertFalse("check resource2 is in response",
171 isComponentInArray(resourceDetails2.getUniqueId(), resources));
172 AssertJUnit.assertTrue("check service1 is in response",
173 isComponentInArray(svcDetails1.getUniqueId(), services));
178 public void getCatalogDataNoServiceAndVFC() throws Exception {
180 List<String> excludeTyps = Arrays.asList(OriginTypeEnum.SERVICE.name(), OriginTypeEnum.VFC.name());
181 RestResponse res = CatalogRestUtils.getCatalog(user.getUserId(), excludeTyps);
182 String json = res.getResponse();
183 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
184 JSONArray resources = (JSONArray) jsonResp.get("resources");
185 JSONArray services = (JSONArray) jsonResp.get("services");
187 // Verify all the expected resources received except of VFC & SERVICE
188 AssertJUnit.assertTrue("check resource1 is in response",
189 isComponentInArray(resourceDetails1.getUniqueId(), resources));
190 AssertJUnit.assertFalse("check resource2 is in response",
191 isComponentInArray(resourceDetails2.getUniqueId(), resources));
192 AssertJUnit.assertFalse("check service1 is in response",
193 isComponentInArray(svcDetails1.getUniqueId(), services));
197 protected void deleteResource(String resourceUniqueId, String httpCspUserId) throws Exception {
198 RestResponse deleteResourceResponse = ResourceRestUtils.deleteResource(resourceUniqueId, httpCspUserId);
202 protected RestResponse createResource(User user, ResourceReqDetails resourceDetails) throws Exception {
203 deleteResource(resourceDetails.getName(), user.getUserId());
204 return ResourceRestUtils.createResource(resourceDetails, user);
207 protected ResourceReqDetails buildResourceDetails(User user, String resourceName) {
208 String description = "description";
209 ArrayList<String> resourceTags = new ArrayList<String>();
210 resourceTags.add(resourceName);
211 ArrayList<String> derivedFrom = new ArrayList<String>();
212 derivedFrom.add("tosca.nodes.Root");
213 String vendorName = "Oracle";
214 String vendorRelease = "1.0";
215 String contactId = user.getUserId();
216 String icon = "myICON";
218 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, null,
219 derivedFrom, vendorName, vendorRelease, contactId, icon);
220 resourceDetails.addCategoryChain(ResourceCategoryEnum.GENERIC_DATABASE.getCategory(),
221 ResourceCategoryEnum.GENERIC_DATABASE.getSubCategory());
222 return resourceDetails;
225 protected boolean isComponentInArray(String id, JSONArray component) {
226 for (int i = 0; i < component.size(); i++) {
227 JSONObject jobject = (JSONObject) component.get(i);
228 if (jobject.get("uniqueId").toString().equals(id.toLowerCase())) {
235 protected RestResponse createService(User user, ServiceReqDetails svcDetails) throws Exception {
237 deleteService(svcDetails1.getUniqueId(), user);
238 Config config = Utils.getConfig();
240 Map<String, String> headersMap = getHeadersMap(user);
242 Gson gson = new Gson();
243 String body = gson.toJson(svcDetails);
244 HttpRequest http = new HttpRequest();
245 String url = String.format(Urls.CREATE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort());
246 RestResponse res = http.httpSendPost(url, body, headersMap);
247 // System.out.println("Create service was finished with response:
248 // "+res.getErrorCode());
252 protected Map<String, String> getHeadersMap(User user) {
253 Map<String, String> headersMap = new HashMap<String, String>();
254 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
255 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
256 headersMap.put("USER_ID", user.getUserId());
260 protected ServiceReqDetails buildServiceDetails(String serviceName) {
261 String description = "description";
262 ArrayList<String> serviceTags = new ArrayList<String>();
263 serviceTags.add("tag1");
264 serviceTags.add(serviceName);
265 String category = ServiceCategoriesEnum.MOBILITY.getValue();
266 String vendorName = "Oracle";
267 String vendorRelease = "0.1";
268 String contactId = "al1976";
269 String icon = "myIcon";
271 ServiceReqDetails svcdetails = new ServiceReqDetails(serviceName, category, serviceTags, description,
276 public RestResponse deleteService(String serviceId, User user) throws Exception {
277 RestResponse deleteServiceResponse = ResourceRestUtils.deleteResource(serviceId, user.getUserId());
278 return deleteServiceResponse;
281 public class NewObject {
282 private String _name;
284 public String getName() {
288 public void setName(String name) {