Fix for radio buttons
[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.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
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;
59
60 import com.google.gson.Gson;
61
62 public class CatalogDataApiTest extends ComponentBaseTest {
63
64         protected Config config = Config.instance();
65         protected String contentTypeHeaderData = "application/json";
66         protected String acceptHeaderDate = "application/json";
67         protected boolean isInitialized = false;
68
69         @Rule
70         public static TestName name = new TestName();
71         protected User user;
72         protected RestResponse res1;
73         protected RestResponse res2;
74         protected RestResponse svc1;
75         protected ResourceReqDetails resourceDetails1;
76         protected ResourceReqDetails resourceDetails2;
77         protected ServiceReqDetails svcDetails1;
78
79         public CatalogDataApiTest() {
80                 super(name, CatalogDataApiTest.class.getName());
81         }
82
83         @BeforeMethod
84         public void setUp() throws Exception {
85                 if (isInitialized)
86                         return;
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");
92
93                 // VFCMT
94                 res1 = createResource(user, resourceDetails1);
95                 AssertJUnit.assertEquals("create resorce failed", 201, res1.getErrorCode().intValue());
96                 resourceDetails1.setUniqueId(ResponseParser.getUniqueIdFromResponse(res1));
97
98                 resourceDetails2.setVersion(ResponseParser.getVersionFromResponse(res1));
99
100                 // VFC
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));
105
106                 // SERVICE
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;
112         }
113
114         @AfterClass
115         public void tearDown() throws Exception {
116                 deleteResource(resourceDetails1.getUniqueId(), user.getUserId());
117                 deleteResource(resourceDetails2.getUniqueId(), user.getUserId());
118                 deleteService(svcDetails1.getUniqueId(), user);
119         }
120
121         // Keep 1
122         @Test
123         public void getCatalogData() throws Exception {
124
125                 RestResponse checkInResponse = LifecycleRestUtils.changeResourceState(resourceDetails1, user, "0.1",
126                                 LifeCycleStatesEnum.CHECKIN);
127                 AssertJUnit.assertEquals("check in operation failed", 200, checkInResponse.getErrorCode().intValue());
128
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");
134
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));
142
143         }
144
145         @Test
146         public void getCatalogDataNoVFCMT() throws Exception {
147
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");
154
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));
162
163         }
164
165         @Test
166         public void getCatalogDataNoVFCandVFCMT() throws Exception {
167
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");
174
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));
182
183         }
184
185         @Test
186         public void getCatalogDataNoServiceAndVFC() throws Exception {
187
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");
194
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));
202
203         }
204
205         protected void deleteResource(String resourceUniqueId, String httpCspUserId) throws Exception {
206                 RestResponse deleteResourceResponse = ResourceRestUtils.deleteResource(resourceUniqueId, httpCspUserId);
207
208         }
209
210         protected RestResponse createResource(User user, ResourceReqDetails resourceDetails) throws Exception {
211                 deleteResource(resourceDetails.getName(), user.getUserId());
212                 return ResourceRestUtils.createResource(resourceDetails, user);
213         }
214
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";
225
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;
231         }
232
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())) {
237                                 return true;
238                         }
239                 }
240                 return false;
241         }
242
243         protected RestResponse createService(User user, ServiceReqDetails svcDetails) throws Exception {
244
245                 deleteService(svcDetails1.getUniqueId(), user);
246                 Config config = Utils.getConfig();
247
248                 Map<String, String> headersMap = getHeadersMap(user);
249
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());
257                 return res;
258         }
259
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());
265                 return headersMap;
266         }
267
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";
278
279                 ServiceReqDetails svcdetails = new ServiceReqDetails(serviceName, category, serviceTags, description,
280                                 contactId, icon);
281                 return svcdetails;
282         }
283
284         public RestResponse deleteService(String serviceId, User user) throws Exception {
285                 RestResponse deleteServiceResponse = ResourceRestUtils.deleteResource(serviceId, user.getUserId());
286                 return deleteServiceResponse;
287         }
288
289         public class NewObject {
290                 private String _name;
291
292                 public String getName() {
293                         return _name;
294                 }
295
296                 public void setName(String name) {
297                         this._name = name;
298                 }
299         }
300
301 }