re base code
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / rest / ProductRestUtils.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.utils.rest;
22
23 import com.google.gson.Gson;
24 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
25 import org.openecomp.sdc.be.model.LifecycleStateEnum;
26 import org.openecomp.sdc.be.model.Product;
27 import org.openecomp.sdc.be.model.User;
28 import org.openecomp.sdc.ci.tests.api.Urls;
29 import org.openecomp.sdc.ci.tests.config.Config;
30 import org.openecomp.sdc.ci.tests.datatypes.ProductReqDetails;
31 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
32 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
33 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
34 import org.openecomp.sdc.ci.tests.utils.Utils;
35 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import java.io.IOException;
40
41 public class ProductRestUtils extends BaseRestUtils {
42         private static Gson gson = new Gson();
43         private static Logger logger = LoggerFactory.getLogger(ProductRestUtils.class.getName());
44
45         public static RestResponse createProduct(ProductReqDetails product, User user) throws Exception {
46                 Config config = Utils.getConfig();
47                 String url = String.format(Urls.CREATE_PRODUCT, config.getCatalogBeHost(), config.getCatalogBePort());
48                 String serviceBodyJson = gson.toJson(product);
49
50                 logger.debug("Send POST request to create service: {}", url);
51                 logger.debug("Service body: {}", serviceBodyJson);
52
53                 RestResponse res = sendPost(url, serviceBodyJson, user.getUserId(), acceptHeaderData);
54                 if (res.getErrorCode() == STATUS_CODE_CREATED) {
55                         product.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
56                         product.setVersion(ResponseParser.getVersionFromResponse(res));
57                         product.setUUID(ResponseParser.getUuidFromResponse(res));
58                         // Creator details never change after component is created - Ella,
59                         // 12/1/2016
60                         product.setCreatorUserId(user.getUserId());
61                         product.setCreatorFullName(user.getFullName());
62                         product.setLastUpdaterFullName(user.getFullName());
63                         product.setLastUpdaterUserId(user.getUserId());
64                         product.setLastUpdaterFullName(user.getFullName());
65                         product.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
66                         product.setVersion("0.1");
67                         String lastUpdate = ResponseParser.getValueFromJsonResponse(res.getResponse(), "lastUpdateDate");
68                         product.setLastUpdateDate(Long.parseLong(lastUpdate, 10));
69                         product.setCreationDate(Long.parseLong(lastUpdate, 10));
70                 }
71                 return res;
72         }
73
74         public static RestResponse updateProduct(ProductReqDetails product, User user) throws Exception {
75                 Config config = Utils.getConfig();
76                 String url = String.format(Urls.UPDATE_PRODUCT, config.getCatalogBeHost(), config.getCatalogBePort(),
77                                 product.getUniqueId());
78                 String serviceBodyJson = gson.toJson(product);
79
80                 logger.debug("Send POST request to create service: {}", url);
81                 logger.debug("Service body: {}", serviceBodyJson);
82
83                 RestResponse res = sendPut(url, serviceBodyJson, user.getUserId(), acceptHeaderData);
84                 if (res.getErrorCode() == STATUS_CODE_CREATED) {
85                         product.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
86                         product.setVersion(ResponseParser.getVersionFromResponse(res));
87                         product.setUUID(ResponseParser.getUuidFromResponse(res));
88                         // Creator details never change after component is created - Ella,
89                         // 12/1/2016
90                         product.setCreatorUserId(user.getUserId());
91                         product.setCreatorFullName(user.getFullName());
92                         product.setLastUpdaterFullName(user.getFullName());
93                         product.setLastUpdaterUserId(user.getUserId());
94                         product.setLastUpdaterFullName(user.getFullName());
95                         product.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
96                         String valueFromJsonResponse = ResponseParser.getValueFromJsonResponse(res.getResponse(), "version");
97                         product.setVersion(valueFromJsonResponse);
98                         String lastUpdate = ResponseParser.getValueFromJsonResponse(res.getResponse(), "lastUpdateDate");
99                         product.setLastUpdateDate(Long.parseLong(lastUpdate, 10));
100                         product.setCreationDate(Long.parseLong(lastUpdate, 10));
101                 }
102                 return res;
103         }
104
105         public static RestResponse createProduct_Invalid_Json(String userId) throws Exception {
106                 Config config = Utils.getConfig();
107                 String url = String.format(Urls.CREATE_PRODUCT, config.getCatalogBeHost(), config.getCatalogBePort());
108
109                 RestResponse res = sendPost(url, "kukumuku", userId, acceptHeaderData);
110                 return res;
111         }
112
113         public static RestResponse deleteProduct(String id, String userId) throws IOException {
114
115                 Config config = Utils.getConfig();
116                 String url = String.format(Urls.DELETE_PRODUCT, config.getCatalogBeHost(), config.getCatalogBePort(), id);
117                 return sendDelete(url, userId);
118         }
119
120         public static RestResponse getProduct(String productId) throws Exception {
121
122                 Config config = Utils.getConfig();
123                 String url = String.format(Urls.GET_PRODUCT, config.getCatalogBeHost(), config.getCatalogBePort(), productId);
124                 logger.debug("Send GET request to get product: {}", url);
125
126                 return sendGet(url, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER).getUserId());
127         }
128
129         public static RestResponse getProduct(String productId, String userId) throws Exception {
130
131                 Config config = Utils.getConfig();
132                 String url = String.format(Urls.GET_PRODUCT, config.getCatalogBeHost(), config.getCatalogBePort(), productId);
133                 logger.debug("Send GET request to get product: {}", url);
134
135                 return sendGet(url, userId);
136         }
137
138         public static RestResponse getFollowed(String userId) throws Exception {
139                 Config config = Utils.getConfig();
140                 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
141                 logger.debug("Send GET request to get user followed page: {}", url);
142                 return sendGet(url, userId);
143
144         }
145
146         public static RestResponse changeProductLifeCycle(Product product, User userModifier, LifeCycleStatesEnum lifeCycle)
147                         throws Exception {
148                 String checkinComment = "my comment";
149                 RestResponse changeLifeCycleResponse = LifecycleRestUtils.changeProductState(product, userModifier, lifeCycle,
150                                 checkinComment);
151                 if (changeLifeCycleResponse.getErrorCode() == STATUS_CODE_SUCCESS) {
152                         product.setLastUpdaterUserId(userModifier.getUserId());
153                         product.setLastUpdaterFullName(userModifier.getFullName());
154                         String latestVersion = ResponseParser.getValueFromJsonResponse(changeLifeCycleResponse.getResponse(),
155                                         "version");
156                         product.setVersion(latestVersion);
157                         String lifecycleState = ResponseParser.getValueFromJsonResponse(changeLifeCycleResponse.getResponse(),
158                                         "lifecycleState");
159                         product.setLifecycleState((LifecycleStateEnum.valueOf(lifecycleState)));
160                         String uniqueId = ResponseParser.getValueFromJsonResponse(changeLifeCycleResponse.getResponse(),
161                                         "uniqueId");
162                         product.setUniqueId(uniqueId);
163                         String lastUpdate = ResponseParser.getValueFromJsonResponse(changeLifeCycleResponse.getResponse(),
164                                         "lastUpdateDate");
165                         product.setLastUpdateDate((Long.parseLong(lastUpdate, 10)));
166                         String uuid = ResponseParser.getValueFromJsonResponse(changeLifeCycleResponse.getResponse(), "uuid");
167                         product.setUUID(uuid);
168                 }
169                 return changeLifeCycleResponse;
170         }
171
172         public static RestResponse changeServiceInstanceVersion(String componentUniqueId,
173                         String serviceInstanceToReplaceUniqueId, String serviceUniqueId, User sdncModifierDetails,
174                         ComponentTypeEnum componentType) throws IOException {
175                 Config config = Utils.getConfig();
176                 String resourceUid = ("{\"componentUid\":\"" + serviceUniqueId + "\"}");
177                 String url = String.format(Urls.CHANGE__RESOURCE_INSTANCE_VERSION, config.getCatalogBeHost(),
178                                 config.getCatalogBePort(), ComponentTypeEnum.findParamByType(componentType), componentUniqueId,
179                                 serviceInstanceToReplaceUniqueId);
180                 RestResponse changeResourceInstanceVersion = sendPost(url, resourceUid, sdncModifierDetails.getUserId(),
181                                 acceptHeaderData);
182                 return changeResourceInstanceVersion;
183
184         }
185
186         public static RestResponse getProductByNameAndVersion(String productName, String productVersion, String userId)
187                         throws Exception {
188                 Config config = Utils.getConfig();
189                 String url = String.format(Urls.GET_PRODUCT_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
190                                 config.getCatalogBePort(), productName, productVersion);
191                 logger.debug("Send GET request to get product by name and version: {}", url);
192                 return sendGet(url, userId);
193         }
194
195 }