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