[SDC] rebase 1710 code
[sdc.git] / asdc-tests / src / main / java / org / openecomp / sdc / ci / tests / execute / category / CatalogDataApiTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.execute.category;
22
23 import java.util.*;
24
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;
51
52 import com.google.gson.Gson;
53
54 public class CatalogDataApiTest extends ComponentBaseTest {
55
56         protected Config config = Config.instance();
57         protected String contentTypeHeaderData = "application/json";
58         protected String acceptHeaderDate = "application/json";
59         protected boolean isInitialized = false;
60
61         @Rule
62         public static TestName name = new TestName();
63         protected User user;
64         protected RestResponse res1;
65         protected RestResponse res2;
66         protected RestResponse svc1;
67         protected ResourceReqDetails resourceDetails1;
68         protected ResourceReqDetails resourceDetails2;
69         protected ServiceReqDetails svcDetails1;
70
71         public CatalogDataApiTest() {
72                 super(name, CatalogDataApiTest.class.getName());
73         }
74
75         @BeforeMethod
76         public void setUp() throws Exception {
77                 if (isInitialized)
78                         return;
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");
84
85                 // VFCMT
86                 res1 = createResource(user, resourceDetails1);
87                 AssertJUnit.assertEquals("create resorce failed", 201, res1.getErrorCode().intValue());
88                 resourceDetails1.setUniqueId(ResponseParser.getUniqueIdFromResponse(res1));
89
90                 resourceDetails2.setVersion(ResponseParser.getVersionFromResponse(res1));
91
92                 // VFC
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));
97
98                 // SERVICE
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;
104         }
105
106         @AfterClass
107         public void tearDown() throws Exception {
108                 deleteResource(resourceDetails1.getUniqueId(), user.getUserId());
109                 deleteResource(resourceDetails2.getUniqueId(), user.getUserId());
110                 deleteService(svcDetails1.getUniqueId(), user);
111         }
112
113         // Keep 1
114         @Test
115         public void getCatalogData() throws Exception {
116
117                 RestResponse checkInResponse = LifecycleRestUtils.changeResourceState(resourceDetails1, user, "0.1",
118                                 LifeCycleStatesEnum.CHECKIN);
119                 AssertJUnit.assertEquals("check in operation failed", 200, checkInResponse.getErrorCode().intValue());
120
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");
126
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));
134
135         }
136
137         @Test
138         public void getCatalogDataNoVFCMT() throws Exception {
139
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");
146
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));
154
155         }
156
157         @Test
158         public void getCatalogDataNoVFCandVFCMT() throws Exception {
159
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");
166
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));
174
175         }
176
177         @Test
178         public void getCatalogDataNoServiceAndVFC() throws Exception {
179
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");
186
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));
194
195         }
196
197         protected void deleteResource(String resourceUniqueId, String httpCspUserId) throws Exception {
198                 RestResponse deleteResourceResponse = ResourceRestUtils.deleteResource(resourceUniqueId, httpCspUserId);
199
200         }
201
202         protected RestResponse createResource(User user, ResourceReqDetails resourceDetails) throws Exception {
203                 deleteResource(resourceDetails.getName(), user.getUserId());
204                 return ResourceRestUtils.createResource(resourceDetails, user);
205         }
206
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";
217
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;
223         }
224
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())) {
229                                 return true;
230                         }
231                 }
232                 return false;
233         }
234
235         protected RestResponse createService(User user, ServiceReqDetails svcDetails) throws Exception {
236
237                 deleteService(svcDetails1.getUniqueId(), user);
238                 Config config = Utils.getConfig();
239
240                 Map<String, String> headersMap = getHeadersMap(user);
241
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());
249                 return res;
250         }
251
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());
257                 return headersMap;
258         }
259
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";
270
271                 ServiceReqDetails svcdetails = new ServiceReqDetails(serviceName, category, serviceTags, description,
272                                 contactId, icon);
273                 return svcdetails;
274         }
275
276         public RestResponse deleteService(String serviceId, User user) throws Exception {
277                 RestResponse deleteServiceResponse = ResourceRestUtils.deleteResource(serviceId, user.getUserId());
278                 return deleteServiceResponse;
279         }
280
281         public class NewObject {
282                 private String _name;
283
284                 public String getName() {
285                         return _name;
286                 }
287
288                 public void setName(String name) {
289                         this._name = name;
290                 }
291         }
292
293 }