[SDC-29] rebase continue work to align source
[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 java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.log4j.lf5.util.ResourceUtils;
28 import org.json.simple.JSONArray;
29 import org.json.simple.JSONObject;
30 import org.json.simple.JSONValue;
31 import org.junit.Rule;
32 import org.junit.rules.TestName;
33 import org.openecomp.sdc.be.model.User;
34 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
35 import org.openecomp.sdc.ci.tests.api.Urls;
36 import org.openecomp.sdc.ci.tests.config.Config;
37 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
38 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
39 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
40 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
41 import org.openecomp.sdc.ci.tests.datatypes.enums.ServiceCategoriesEnum;
42 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
43 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
44 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
45 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
46 import org.openecomp.sdc.ci.tests.utils.Utils;
47 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
48 import org.openecomp.sdc.ci.tests.utils.rest.CatalogRestUtils;
49 import org.openecomp.sdc.ci.tests.utils.rest.LifecycleRestUtils;
50 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
51 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
52 import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
53 import org.testng.AssertJUnit;
54 import org.testng.annotations.AfterMethod;
55 import org.testng.annotations.BeforeMethod;
56 import org.testng.annotations.Test;
57
58 import com.google.gson.Gson;
59
60 public class CatalogDataApiTest extends ComponentBaseTest {
61
62         protected Config config = Config.instance();
63         protected String contentTypeHeaderData = "application/json";
64         protected String acceptHeaderDate = "application/json";
65
66         @Rule
67         public static TestName name = new TestName();
68         protected User user;
69         protected RestResponse res1;
70         protected RestResponse res2;
71         protected RestResponse svc1;
72         protected ResourceReqDetails resourceDetails1;
73         protected ResourceReqDetails resourceDetails2;
74         protected ServiceReqDetails svcDetails1;
75
76         public CatalogDataApiTest() {
77                 super(name, CatalogDataApiTest.class.getName());
78         }
79
80         @BeforeMethod
81         public void setUp() throws Exception {
82                 user = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
83                 resourceDetails1 = buildResourceDetails(user, "TestResource1");
84                 resourceDetails2 = buildResourceDetails(user, "TestResource2");
85                 svcDetails1 = buildServiceDetails("TestService1");
86
87                 res1 = createResource(user, resourceDetails1);
88                 AssertJUnit.assertEquals("create resorce failed", 201, res1.getErrorCode().intValue());
89                 resourceDetails1.setUniqueId(ResponseParser.getUniqueIdFromResponse(res1));
90                 resourceDetails2.setVersion(ResponseParser.getVersionFromResponse(res1));
91
92                 res2 = createResource(user, resourceDetails2);
93                 AssertJUnit.assertEquals("create resorce failed", 201, res2.getErrorCode().intValue());
94                 resourceDetails2.setUniqueId(ResponseParser.getUniqueIdFromResponse(res2));
95                 resourceDetails2.setVersion(ResponseParser.getVersionFromResponse(res2));
96
97                 svc1 = createService(user, svcDetails1);
98                 AssertJUnit.assertEquals("create resorce failed", 201, svc1.getErrorCode().intValue());
99                 svcDetails1.setUniqueId(ResponseParser.convertServiceResponseToJavaObject(svc1.getResponse()).getUniqueId());
100                 svcDetails1.setVersion(ResponseParser.convertServiceResponseToJavaObject(svc1.getResponse()).getVersion());
101         }
102
103         @AfterMethod
104         public void tearDown() throws Exception {
105                 deleteResource(resourceDetails1.getUniqueId(), user.getUserId());
106                 deleteResource(resourceDetails2.getUniqueId(), user.getUserId());
107                 deleteService(svcDetails1.getUniqueId(), user);
108         }
109
110         // Keep 1
111         @Test
112         public void getCatalogData() throws Exception {
113
114                 RestResponse checkInResponse = LifecycleRestUtils.changeResourceState(resourceDetails1, user, "0.1",
115                                 LifeCycleStatesEnum.CHECKIN);
116                 AssertJUnit.assertEquals("check in operation failed", 200, checkInResponse.getErrorCode().intValue());
117
118                 RestResponse res = CatalogRestUtils.getCatalog(user.getUserId());
119                 String json = res.getResponse();
120                 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
121                 JSONArray resources = (JSONArray) jsonResp.get("resources");
122                 JSONArray services = (JSONArray) jsonResp.get("services");
123
124                 // Verify all the expected resources received.
125                 AssertJUnit.assertTrue("check resource1 is in response",
126                                 isComponentInArray(resourceDetails1.getUniqueId(), resources));
127                 AssertJUnit.assertTrue("check resource2 is in response",
128                                 isComponentInArray(resourceDetails2.getUniqueId(), resources));
129                 AssertJUnit.assertTrue("check service1 is in response",
130                                 isComponentInArray(svcDetails1.getUniqueId(), services));
131
132         }
133
134         protected void deleteResource(String resourceUniqueId, String httpCspUserId) throws Exception {
135                 RestResponse deleteResourceResponse = ResourceRestUtils.deleteResource(resourceUniqueId, httpCspUserId);
136
137         }
138
139         protected RestResponse createResource(User user, ResourceReqDetails resourceDetails) throws Exception {
140                 deleteResource(resourceDetails.getName(), user.getUserId());
141                 return ResourceRestUtils.createResource(resourceDetails, user);
142         }
143
144         protected ResourceReqDetails buildResourceDetails(User user, String resourceName) {
145                 String description = "description";
146                 ArrayList<String> resourceTags = new ArrayList<String>();
147                 resourceTags.add(resourceName);
148                 ArrayList<String> derivedFrom = new ArrayList<String>();
149                 derivedFrom.add("tosca.nodes.Root");
150                 String vendorName = "Oracle";
151                 String vendorRelease = "1.0";
152                 String contactId = user.getUserId();
153                 String icon = "myICON";
154
155                 ResourceReqDetails resourceDetails = new ResourceReqDetails(resourceName, description, resourceTags, null,
156                                 derivedFrom, vendorName, vendorRelease, contactId, icon);
157                 resourceDetails.addCategoryChain(ResourceCategoryEnum.GENERIC_DATABASE.getCategory(),
158                                 ResourceCategoryEnum.GENERIC_DATABASE.getSubCategory());
159                 return resourceDetails;
160         }
161
162         protected boolean isComponentInArray(String id, JSONArray component) {
163                 for (int i = 0; i < component.size(); i++) {
164                         JSONObject jobject = (JSONObject) component.get(i);
165                         if (jobject.get("uniqueId").toString().equals(id.toLowerCase())) {
166                                 return true;
167                         }
168                 }
169                 return false;
170         }
171
172         protected RestResponse createService(User user, ServiceReqDetails svcDetails) throws Exception {
173
174                 Config config = Utils.getConfig();
175
176                 Map<String, String> headersMap = getHeadersMap(user);
177
178                 Gson gson = new Gson();
179                 String body = gson.toJson(svcDetails);
180                 HttpRequest http = new HttpRequest();
181                 String url = String.format(Urls.CREATE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort());
182                 RestResponse res = http.httpSendPost(url, body, headersMap);
183                 // System.out.println("Create service was finished with response:
184                 // "+res.getErrorCode());
185                 return res;
186         }
187
188         protected Map<String, String> getHeadersMap(User user) {
189                 Map<String, String> headersMap = new HashMap<String, String>();
190                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
191                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
192                 headersMap.put("USER_ID", user.getUserId());
193                 return headersMap;
194         }
195
196         protected ServiceReqDetails buildServiceDetails(String serviceName) {
197                 String description = "description";
198                 ArrayList<String> serviceTags = new ArrayList<String>();
199                 serviceTags.add("tag1");
200                 serviceTags.add(serviceName);
201                 String category = ServiceCategoriesEnum.MOBILITY.getValue();
202                 String vendorName = "Oracle";
203                 String vendorRelease = "0.1";
204                 String contactId = "al1976";
205                 String icon = "myIcon";
206
207                 ServiceReqDetails svcdetails = new ServiceReqDetails(serviceName, category, serviceTags, description,
208                                 contactId, icon);
209                 return svcdetails;
210         }
211
212         public RestResponse deleteService(String serviceId, User user) throws Exception {
213                 HttpRequest httpRequest = new HttpRequest();
214                 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(),
215                                 serviceId);
216
217                 Map<String, String> headersMap = getHeadersMap(user);
218                 RestResponse res = httpRequest.httpSendDelete(url, headersMap);
219                 // System.out.println("Delete service was finished with response:
220                 // "+res.getErrorCode());
221                 return res;
222         }
223
224         public class NewObject {
225                 private String _name;
226
227                 public String getName() {
228                         return _name;
229                 }
230
231                 public void setName(String name) {
232                         this._name = name;
233                 }
234         }
235
236 }