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