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