re base code
[sdc.git] / test-apis-ci / 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 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;
49
50 import java.util.ArrayList;
51 import java.util.HashMap;
52 import java.util.Map;
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
60         @Rule
61         public static TestName name = new TestName();
62         protected User user;
63         protected RestResponse res1;
64         protected RestResponse res2;
65         protected RestResponse svc1;
66         protected ResourceReqDetails resourceDetails1;
67         protected ResourceReqDetails resourceDetails2;
68         protected ServiceReqDetails svcDetails1;
69
70         public CatalogDataApiTest() {
71                 super(name, CatalogDataApiTest.class.getName());
72         }
73
74         @BeforeMethod
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");
80
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));
85
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));
90
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());
95         }
96
97         @AfterMethod
98         public void tearDown() throws Exception {
99                 deleteResource(resourceDetails1.getUniqueId(), user.getUserId());
100                 deleteResource(resourceDetails2.getUniqueId(), user.getUserId());
101                 deleteService(svcDetails1.getUniqueId(), user);
102         }
103
104         // Keep 1
105         @Test
106         public void getCatalogData() throws Exception {
107
108                 RestResponse checkInResponse = LifecycleRestUtils.changeResourceState(resourceDetails1, user, "0.1",
109                                 LifeCycleStatesEnum.CHECKIN);
110                 AssertJUnit.assertEquals("check in operation failed", 200, checkInResponse.getErrorCode().intValue());
111
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");
117
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));
125
126         }
127
128         protected void deleteResource(String resourceUniqueId, String httpCspUserId) throws Exception {
129                 RestResponse deleteResourceResponse = ResourceRestUtils.deleteResource(resourceUniqueId, httpCspUserId);
130
131         }
132
133         protected RestResponse createResource(User user, ResourceReqDetails resourceDetails) throws Exception {
134                 deleteResource(resourceDetails.getName(), user.getUserId());
135                 return ResourceRestUtils.createResource(resourceDetails, user);
136         }
137
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";
148
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;
154         }
155
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())) {
160                                 return true;
161                         }
162                 }
163                 return false;
164         }
165
166         protected RestResponse createService(User user, ServiceReqDetails svcDetails) throws Exception {
167
168                 Config config = Utils.getConfig();
169
170                 Map<String, String> headersMap = getHeadersMap(user);
171
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());
179                 return res;
180         }
181
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());
187                 return headersMap;
188         }
189
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();
201
202                 ServiceReqDetails svcdetails = new ServiceReqDetails(serviceName, category, serviceTags, description,
203                                 contactId, icon, instantiationType);
204                 return svcdetails;
205         }
206
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(),
210                                 serviceId);
211
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());
216                 return res;
217         }
218
219         public class NewObject {
220                 private String _name;
221
222                 public String getName() {
223                         return _name;
224                 }
225
226                 public void setName(String name) {
227                         this._name = name;
228                 }
229         }
230
231 }