Sync Integ to Master
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / rest / PropertyRestUtils.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.*;
26 import org.openecomp.sdc.ci.tests.api.Urls;
27 import org.openecomp.sdc.ci.tests.config.Config;
28 import org.openecomp.sdc.ci.tests.datatypes.PropertyObject;
29 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
30 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
31 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
32 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
33 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
34 import org.openecomp.sdc.common.util.GeneralUtility;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.openecomp.sdc.ci.tests.datatypes.PropertyObject;
38
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43
44 import static org.testng.AssertJUnit.*;
45
46 import com.google.gson.Gson;
47
48 public class PropertyRestUtils extends BaseRestUtils {
49         private static Logger logger = LoggerFactory.getLogger(PropertyRestUtils.class.getName());
50
51         public static RestResponse createProperty(String resourceId, String body, User user) throws Exception {
52                 Config config = Config.instance();
53                 String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
54                                 resourceId);
55
56                 return sendPost(url, body, user.getUserId(), acceptHeaderData);
57         }
58
59         public static RestResponse updateProperty(String resourceId, String propertyId, String body, User user)
60                         throws Exception {
61                 Config config = Config.instance();
62
63                 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
64                                 resourceId, propertyId);
65                 return sendPut(url, body, user.getUserId(), acceptHeaderData);
66         }
67
68         public static RestResponse updateGroupProperty(Component component, String groupId, String body, User user)
69                         throws Exception {
70                 Config config = Config.instance();
71
72                 String url = String.format(Urls.RESOURCE_GROUP_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(), component.getUniqueId(), groupId);
73                 return sendPut(url, body, user.getUserId(), acceptHeaderData);
74         }
75
76         public static RestResponse getProperty(String resourceId, String propertyId, User user) throws Exception {
77                 Config config = Config.instance();
78                 String url = String.format(Urls.GET_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId,
79                                 propertyId);
80                 return sendGet(url, user.getUserId());
81         }
82
83         public static RestResponse deleteProperty(String resourceId, String propertyId, User user) throws Exception {
84                 Config config = Config.instance();
85                 String url = String.format(Urls.DELETE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
86                                 resourceId, propertyId);
87
88                 return sendDelete(url, user.getUserId());
89         }
90
91         public static ComponentInstanceProperty getPropFromListByPropNameAndType(List<ComponentInstanceProperty> propList,
92                         String propNameToUpdate, String propTypeToUpdate) {
93                 for (ComponentInstanceProperty componentInstanceProperty : propList) {
94                         if (componentInstanceProperty.getName().equals(propNameToUpdate)
95                                         && componentInstanceProperty.getType().equals(propTypeToUpdate)) {
96                                 return componentInstanceProperty;
97                         }
98                 }
99                 return null;
100         }
101
102         public static ComponentInstanceProperty getPropFromListByPropNameTypeAndPath(
103                         List<ComponentInstanceProperty> propList, String propNameToUpdate, String propTypeToUpdate,
104                         List<String> path) {
105                 for (ComponentInstanceProperty componentInstanceProperty : propList) {
106                         if (componentInstanceProperty.getPath() == null) {
107                                 return getPropFromListByPropNameAndType(propList, propNameToUpdate, propTypeToUpdate);
108                         }
109                         if (componentInstanceProperty.getName().equals(propNameToUpdate)
110                                         && componentInstanceProperty.getType().equals(propTypeToUpdate)
111                                         && path.containsAll(componentInstanceProperty.getPath())) {
112                                 return componentInstanceProperty;
113                         }
114                 }
115                 return null;
116         }
117
118         public static ComponentInstanceProperty getPropFromListByPropIdAndPath(List<ComponentInstanceProperty> propList,
119                         String propId, List<String> path) {
120
121                 for (ComponentInstanceProperty componentInstanceProperty : propList) {
122                         if (path != null) {
123                                 if (componentInstanceProperty.getUniqueId().equals(propId)
124                                                 && componentInstanceProperty.getPath().equals(path)) {
125                                         return componentInstanceProperty;
126                                 }
127                         } else {
128                                 if (componentInstanceProperty.getUniqueId().equals(propId)) {
129                                         return componentInstanceProperty;
130                                 }
131                         }
132                 }
133                 return null;
134         }
135
136         public static void comparePropertyLists(List<ComponentInstanceProperty> expectedList,
137                         List<ComponentInstanceProperty> actualList, Boolean isUpdate) {
138
139                 assertTrue(
140                                 "list size are not equals, expected size is: " + expectedList.size() + " ,actual: " + actualList.size(),
141                                 expectedList.size() == actualList.size());
142                 Boolean flag = false;
143                 for (ComponentInstanceProperty expectedcompInstProp : expectedList) {
144                         for (ComponentInstanceProperty actualcompInstProp : actualList) {
145                                 flag = comparePropertyObjects(expectedcompInstProp, actualcompInstProp, isUpdate);
146                                 if (flag) {
147                                         break;
148                                 }
149                         }
150                 }
151                 // System.out.println("expected: " + expectedList + ", actual: " +
152                 // actualList);
153                 logger.debug("expected: {}, actual: {}",expectedList,actualList);
154                 assertTrue("actual lists does not contain all uniqeIds", flag);
155         }
156
157         public static Boolean comparePropertyObjects(ComponentInstanceProperty expectedCompInstProp,
158                         ComponentInstanceProperty actualCompInstProp, Boolean isUpdate) {
159                 String uniqueId = expectedCompInstProp.getUniqueId();
160                 String type = expectedCompInstProp.getType();
161                 String defaulValue = expectedCompInstProp.getDefaultValue();
162                 if (actualCompInstProp.getUniqueId().equals(uniqueId)
163                                 && actualCompInstProp.getPath().equals(expectedCompInstProp.getPath())) {
164                         assertTrue("expected type is: " + type + " ,actual: " + actualCompInstProp.getType(),
165                                         actualCompInstProp.getType().equals(type));
166                         if (defaulValue == null) {
167                                 assertTrue(
168                                                 "expected defaulValue is: " + defaulValue + " ,actual: " + actualCompInstProp.getDefaultValue(),
169                                                 actualCompInstProp.getDefaultValue() == defaulValue);
170                         } else {
171                                 assertTrue(
172                                                 "expected defaulValue is: " + defaulValue + " ,actual: " + actualCompInstProp.getDefaultValue(),
173                                                 actualCompInstProp.getDefaultValue().equals(defaulValue));
174                         }
175                         if (isUpdate) {
176                                 assertTrue(
177                                                 "actual [Value] parameter " + actualCompInstProp.getName()
178                                                                 + "should equal to expected [Value]: " + actualCompInstProp.getValue() + " ,Value: "
179                                                                 + actualCompInstProp.getValue(),
180                                                 actualCompInstProp.getValue().equals(expectedCompInstProp.getValue()));
181                                 assertNotNull("valueId is null", actualCompInstProp.getValueUniqueUid());
182                         } else {
183                                 if (defaulValue == null) {
184                                         assertTrue(
185                                                         "actual [Value] parameter " + actualCompInstProp.getName()
186                                                                         + "should equal to expected [defaultValue]: " + actualCompInstProp.getValue()
187                                                                         + " ,defaultValue: " + actualCompInstProp.getDefaultValue(),
188                                                         actualCompInstProp.getValue() == expectedCompInstProp.getDefaultValue());
189                                 } else {
190                                         assertTrue(
191                                                         "actual [Value] parameter " + actualCompInstProp.getName()
192                                                                         + "should equal to expected [defaultValue]: " + actualCompInstProp.getValue()
193                                                                         + " ,defaultValue: " + actualCompInstProp.getDefaultValue(),
194                                                         actualCompInstProp.getValue().equals(expectedCompInstProp.getDefaultValue()));
195                                 }
196                                 assertNull("valueId is not null", actualCompInstProp.getValueUniqueUid());
197                         }
198                         return true;
199                 }
200                 return false;
201         }
202
203         public static List<ComponentInstanceProperty> addResourcePropertiesToList(Resource resource,
204                         List<ComponentInstanceProperty> listToFill) {
205                 for (PropertyDefinition prop : resource.getProperties()) {
206                         listToFill.add(new ComponentInstanceProperty(prop, null, null));
207                 }
208                 return listToFill;
209         }
210
211         public static List<ComponentInstanceProperty> addComponentInstPropertiesToList(Component component,
212                         List<ComponentInstanceProperty> listToFill, String componentId) {
213
214                 if (componentId != null) {
215                         List<ComponentInstanceProperty> list = component.getComponentInstancesProperties().get(componentId);
216                         for (ComponentInstanceProperty prop : list) {
217                                 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(prop, null, null);
218                                 componentInstanceProperty.setPath(prop.getPath());
219                                 componentInstanceProperty.setValueUniqueUid(prop.getValueUniqueUid());
220                                 componentInstanceProperty.setValue(prop.getValue());
221                                 listToFill.add(componentInstanceProperty);
222                         }
223                 } else {
224                         Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = component
225                                         .getComponentInstancesProperties();
226                         for (Map.Entry<String, List<ComponentInstanceProperty>> componentInstanceProperties : componentInstancesProperties
227                                         .entrySet()) {
228                                 for (ComponentInstanceProperty prop : componentInstanceProperties.getValue()) {
229                                         ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(prop, null,
230                                                         null);
231                                         componentInstanceProperty.setPath(prop.getPath());
232                                         componentInstanceProperty.setValueUniqueUid(prop.getValueUniqueUid());
233                                         componentInstanceProperty.setValue(prop.getValue());
234                                         listToFill.add(componentInstanceProperty);
235                                 }
236                         }
237                 }
238
239                 if (component.getComponentType().getValue().equals("Resource")) {
240                         for (PropertyDefinition prop : ((Resource) component).getProperties()) {
241                                 listToFill.add(new ComponentInstanceProperty(prop, null, null));
242                         }
243                 }
244                 return listToFill;
245         }
246
247         public static ComponentInstanceProperty getCompPropInstListByInstIdAndPropName(Component component,
248                         ComponentInstance componentInstanceDetails, String name, String type) {
249                 List<ComponentInstanceProperty> propList = component.getComponentInstancesProperties()
250                                 .get(componentInstanceDetails.getUniqueId());
251                 if (propList != null) {
252                         return getPropFromListByPropNameAndType(propList, name, type);
253                 }
254                 return null;
255         }
256
257         private static void updatePropertyListWithPathParameter(Resource resource, List<String> path,
258                         List<ComponentInstanceProperty> expectedPropertyList) {
259                 List<PropertyDefinition> propertyList = resource.getProperties();
260                 for (PropertyDefinition propertyDefinition : propertyList) {
261                         ComponentInstanceProperty propDetailsToRemove = PropertyRestUtils.getPropFromListByPropNameAndType(
262                                         expectedPropertyList, propertyDefinition.getName(), propertyDefinition.getType());
263                         ComponentInstanceProperty propDetailsToAdd = propDetailsToRemove;
264                         propDetailsToAdd.setPath(path);
265                         expectedPropertyList.remove(propDetailsToRemove);
266                         expectedPropertyList.add(propDetailsToAdd);
267                 }
268         }
269
270         private static void updatePropertyListWithPathParameterOnCompInst(Service service, List<String> path,
271                         List<ComponentInstanceProperty> expectedPropertyList) {
272                 List<ComponentInstanceProperty> servicePropertyList = new ArrayList<>();
273                 servicePropertyList = PropertyRestUtils.addComponentInstPropertiesToList(service, servicePropertyList,
274                                 path.get(0));
275
276                 for (ComponentInstanceProperty serviceCompInstProperty : servicePropertyList) {
277                         ComponentInstanceProperty propDetailsToRemove = PropertyRestUtils.getPropFromListByPropNameTypeAndPath(
278                                         expectedPropertyList, serviceCompInstProperty.getName(), serviceCompInstProperty.getType(),
279                                         serviceCompInstProperty.getPath());
280                         ComponentInstanceProperty propDetailsToAdd = propDetailsToRemove;
281                         List<String> tempPathList = new ArrayList<String>();
282                         for (String tempPath : path) {
283                                 tempPathList.add(tempPath);
284                         }
285                         // path parameter can not contain the same service unique ID twice
286                         if (propDetailsToAdd.getPath() != null
287                                         && !propDetailsToAdd.getPath().get(0).contains(service.getUniqueId())) {
288                                 if (!propDetailsToAdd.getPath().containsAll(tempPathList)) {
289                                         tempPathList.addAll(propDetailsToAdd.getPath());
290                                 }
291                         }
292                         propDetailsToAdd.setPath(tempPathList);
293                         expectedPropertyList.remove(propDetailsToRemove);
294                         expectedPropertyList.add(propDetailsToAdd);
295                 }
296         }
297
298         public static void updatePropertyListWithPathOnResource(ComponentInstance componentInstDetails, Resource resource,
299                         List<ComponentInstanceProperty> list, Component container) {
300                 List<String> path = new ArrayList<>();
301                 if (container != null) {
302                         List<ComponentInstance> componentInstances = container.getComponentInstances();
303                         for (ComponentInstance componentInstance : componentInstances) {
304                                 if (componentInstance.getNormalizedName().equals(componentInstDetails.getNormalizedName())) {
305                                         path.add(componentInstance.getUniqueId());
306                                         break;
307                                 }
308                         }
309
310                 } else {
311                         path.add(componentInstDetails.getUniqueId());
312                 }
313                 updatePropertyListWithPathParameter(resource, path, list);
314         }
315
316         public static void updatePropertyListWithPathOnComponentInstance(ComponentInstance componentInstDetails,
317                         Service service, List<ComponentInstanceProperty> list) {
318                 List<String> path = new ArrayList<>();
319                 path.add(componentInstDetails.getUniqueId());
320                 updatePropertyListWithPathParameterOnCompInst(service, path, list);
321         }
322
323         public static RestResponse declareProporties(Component componentObject, Map<String, List<ComponentInstanceInput>> componentInstancesInputs, User sdncModifierDetails)
324                         throws Exception {
325                 Config config = Config.instance();
326                 String url = String.format(Urls.DECLARE_PROPERTIES, config.getCatalogBeHost(), config.getCatalogBePort(), ComponentTypeEnum.findParamByType(componentObject.getComponentType()), componentObject.getUniqueId());
327                 String userId = sdncModifierDetails.getUserId();
328                 Map<String, String> headersMap = prepareHeadersMap(userId);
329                 Map<String, Object> jsonBuilder = new HashMap<>();
330                 jsonBuilder.put("componentInstanceInputsMap", componentInstancesInputs);
331                 Gson gson = new Gson();
332                 String userBodyJson = gson.toJson(jsonBuilder);
333                 String calculateMD5 = GeneralUtility.calculateMD5Base64EncodedByString(userBodyJson);
334                 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
335                 HttpRequest http = new HttpRequest();
336                 // System.out.println(url);
337                 // System.out.println(userBodyJson);
338                 RestResponse declareProportiesResponse = http.httpSendPost(url, userBodyJson, headersMap);
339                 if (declareProportiesResponse.getErrorCode() == STATUS_CODE_GET_SUCCESS) {
340
341                 }
342                 return declareProportiesResponse;
343         }
344
345         public static RestResponse updateInput(Component componentObject, PropertyObject componentInput, User sdncModifierDetails)
346                         throws Exception {
347                 Config config = Config.instance();
348                 String url = String.format(Urls.UPDATE_INPUT, config.getCatalogBeHost(), config.getCatalogBePort(), componentObject.getUniqueId());
349                 String userId = sdncModifierDetails.getUserId();
350                 Map<String, String> headersMap = prepareHeadersMap(userId);
351                 Gson gson = new Gson();
352                 String userBodyJson = gson.toJson(componentInput);
353                 String calculateMD5 = GeneralUtility.calculateMD5Base64EncodedByString(userBodyJson);
354                 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
355                 HttpRequest http = new HttpRequest();
356                 // System.out.println(url);
357                 // System.out.println(userBodyJson);
358                 RestResponse declareProportiesResponse = http.httpSendPost(url, userBodyJson, headersMap);
359                 if (declareProportiesResponse.getErrorCode() == STATUS_CODE_GET_SUCCESS) {
360
361                 }
362                 return declareProportiesResponse;
363         }
364 }