Fix for radio buttons
[sdc.git] / asdc-tests / 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(), resourceId);
50
51                 return sendPost(url, body, user.getUserId(), acceptHeaderData);
52         }
53
54         public static RestResponse updateProperty(String resourceId, String propertyId, String body, User user) throws Exception {
55                 Config config = Config.instance();
56
57                 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId, propertyId);
58                 return sendPut(url, body, user.getUserId(), acceptHeaderData);
59         }
60
61         public static RestResponse getProperty(String resourceId, String propertyId, User user) throws Exception {
62                 Config config = Config.instance();
63                 String url = String.format(Urls.GET_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId, propertyId);
64                 return sendGet(url, user.getUserId());
65         }
66
67         public static RestResponse deleteProperty(String resourceId, String propertyId, User user) throws Exception {
68                 Config config = Config.instance();
69                 String url = String.format(Urls.DELETE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId, propertyId);
70
71                 return sendDelete(url, user.getUserId());
72         }
73
74         public static ComponentInstanceProperty getPropFromListByPropNameAndType(List<ComponentInstanceProperty> propList, String propNameToUpdate, String propTypeToUpdate) {
75                 for (ComponentInstanceProperty componentInstanceProperty : propList) {
76                         if (componentInstanceProperty.getName().equals(propNameToUpdate) && componentInstanceProperty.getType().equals(propTypeToUpdate)) {
77                                 return componentInstanceProperty;
78                         }
79                 }
80                 return null;
81         }
82
83         public static ComponentInstanceProperty getPropFromListByPropNameTypeAndPath(List<ComponentInstanceProperty> propList, String propNameToUpdate, String propTypeToUpdate, List<String> path) {
84                 for (ComponentInstanceProperty componentInstanceProperty : propList) {
85                         if (componentInstanceProperty.getPath() == null) {
86                                 return getPropFromListByPropNameAndType(propList, propNameToUpdate, propTypeToUpdate);
87                         }
88                         if (componentInstanceProperty.getName().equals(propNameToUpdate) && componentInstanceProperty.getType().equals(propTypeToUpdate) && path.containsAll(componentInstanceProperty.getPath())) {
89                                 return componentInstanceProperty;
90                         }
91                 }
92                 return null;
93         }
94
95         public static ComponentInstanceProperty getPropFromListByPropIdAndPath(List<ComponentInstanceProperty> propList, String propId, List<String> path) {
96
97                 for (ComponentInstanceProperty componentInstanceProperty : propList) {
98                         if (path != null) {
99                                 if (componentInstanceProperty.getUniqueId().equals(propId) && componentInstanceProperty.getPath().equals(path)) {
100                                         return componentInstanceProperty;
101                                 }
102                         } else {
103                                 if (componentInstanceProperty.getUniqueId().equals(propId)) {
104                                         return componentInstanceProperty;
105                                 }
106                         }
107                 }
108                 return null;
109         }
110
111         public static void comparePropertyLists(List<ComponentInstanceProperty> expectedList, List<ComponentInstanceProperty> actualList, Boolean isUpdate) {
112
113                 assertTrue("list size are not equals, expected size is: " + expectedList.size() + " ,actual: " + actualList.size(), expectedList.size() == actualList.size());
114                 Boolean flag = false;
115                 for (ComponentInstanceProperty expectedcompInstProp : expectedList) {
116                         for (ComponentInstanceProperty actualcompInstProp : actualList) {
117                                 flag = comparePropertyObjects(expectedcompInstProp, actualcompInstProp, isUpdate);
118                                 if (flag) {
119                                         break;
120                                 }
121                         }
122                 }
123                 // System.out.println("expected: " + expectedList + ", actual: " +
124                 // actualList);
125                 logger.debug("expected: {}, actual: {}",expectedList,actualList);
126                 assertTrue("actual lists does not contain all uniqeIds", flag);
127         }
128
129         public static Boolean comparePropertyObjects(ComponentInstanceProperty expectedCompInstProp, ComponentInstanceProperty actualCompInstProp, Boolean isUpdate) {
130                 String uniqueId = expectedCompInstProp.getUniqueId();
131                 String type = expectedCompInstProp.getType();
132                 String defaulValue = expectedCompInstProp.getDefaultValue();
133                 if (actualCompInstProp.getUniqueId().equals(uniqueId) && actualCompInstProp.getPath().equals(expectedCompInstProp.getPath())) {
134                         assertTrue("expected type is: " + type + " ,actual: " + actualCompInstProp.getType(), actualCompInstProp.getType().equals(type));
135                         if (defaulValue == null) {
136                                 assertTrue("expected defaulValue is: " + defaulValue + " ,actual: " + actualCompInstProp.getDefaultValue(), actualCompInstProp.getDefaultValue() == defaulValue);
137                         } else {
138                                 assertTrue("expected defaulValue is: " + defaulValue + " ,actual: " + actualCompInstProp.getDefaultValue(), actualCompInstProp.getDefaultValue().equals(defaulValue));
139                         }
140                         if (isUpdate) {
141                                 assertTrue("actual [Value] parameter " + actualCompInstProp.getName() + "should equal to expected [Value]: " + actualCompInstProp.getValue() + " ,Value: " + actualCompInstProp.getValue(),
142                                                 actualCompInstProp.getValue().equals(expectedCompInstProp.getValue()));
143                                 assertNotNull("valueId is null", actualCompInstProp.getValueUniqueUid());
144                         } else {
145                                 if (defaulValue == null) {
146                                         assertTrue("actual [Value] parameter " + actualCompInstProp.getName() + "should equal to expected [defaultValue]: " + actualCompInstProp.getValue() + " ,defaultValue: " + actualCompInstProp.getDefaultValue(),
147                                                         actualCompInstProp.getValue() == expectedCompInstProp.getDefaultValue());
148                                 } else {
149                                         assertTrue("actual [Value] parameter " + actualCompInstProp.getName() + "should equal to expected [defaultValue]: " + actualCompInstProp.getValue() + " ,defaultValue: " + actualCompInstProp.getDefaultValue(),
150                                                         actualCompInstProp.getValue().equals(expectedCompInstProp.getDefaultValue()));
151                                 }
152                                 assertNull("valueId is not null", actualCompInstProp.getValueUniqueUid());
153                         }
154                         return true;
155                 }
156                 return false;
157         }
158
159         public static List<ComponentInstanceProperty> addResourcePropertiesToList(Resource resource, List<ComponentInstanceProperty> listToFill) {
160                 for (PropertyDefinition prop : resource.getProperties()) {
161                         listToFill.add(new ComponentInstanceProperty(prop, null, null));
162                 }
163                 return listToFill;
164         }
165
166         public static List<ComponentInstanceProperty> addComponentInstPropertiesToList(Component component, List<ComponentInstanceProperty> listToFill, String componentId) {
167
168                 if (componentId != null) {
169                         List<ComponentInstanceProperty> list = component.getComponentInstancesProperties().get(componentId);
170                         for (ComponentInstanceProperty prop : list) {
171                                 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(prop, null, null);
172                                 componentInstanceProperty.setPath(prop.getPath());
173                                 componentInstanceProperty.setValueUniqueUid(prop.getValueUniqueUid());
174                                 componentInstanceProperty.setValue(prop.getValue());
175                                 listToFill.add(componentInstanceProperty);
176                         }
177                 } else {
178                         Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = component.getComponentInstancesProperties();
179                         for (Map.Entry<String, List<ComponentInstanceProperty>> componentInstanceProperties : componentInstancesProperties.entrySet()) {
180                                 for (ComponentInstanceProperty prop : componentInstanceProperties.getValue()) {
181                                         ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(prop, null, null);
182                                         componentInstanceProperty.setPath(prop.getPath());
183                                         componentInstanceProperty.setValueUniqueUid(prop.getValueUniqueUid());
184                                         componentInstanceProperty.setValue(prop.getValue());
185                                         listToFill.add(componentInstanceProperty);
186                                 }
187                         }
188                 }
189
190                 if (component.getComponentType().getValue().equals("Resource")) {
191                         for (PropertyDefinition prop : ((Resource) component).getProperties()) {
192                                 listToFill.add(new ComponentInstanceProperty(prop, null, null));
193                         }
194                 }
195                 return listToFill;
196         }
197
198         public static ComponentInstanceProperty getCompPropInstListByInstIdAndPropName(Component component, ComponentInstance componentInstanceDetails, String name, String type) {
199                 List<ComponentInstanceProperty> propList = component.getComponentInstancesProperties().get(componentInstanceDetails.getUniqueId());
200                 if (propList != null) {
201                         return getPropFromListByPropNameAndType(propList, name, type);
202                 }
203                 return null;
204         }
205
206         private static void updatePropertyListWithPathParameter(Resource resource, List<String> path, List<ComponentInstanceProperty> expectedPropertyList) {
207                 List<PropertyDefinition> propertyList = resource.getProperties();
208                 for (PropertyDefinition propertyDefinition : propertyList) {
209                         ComponentInstanceProperty propDetailsToRemove = PropertyRestUtils.getPropFromListByPropNameAndType(expectedPropertyList, propertyDefinition.getName(), propertyDefinition.getType());
210                         ComponentInstanceProperty propDetailsToAdd = propDetailsToRemove;
211                         propDetailsToAdd.setPath(path);
212                         expectedPropertyList.remove(propDetailsToRemove);
213                         expectedPropertyList.add(propDetailsToAdd);
214                 }
215         }
216
217         private static void updatePropertyListWithPathParameterOnCompInst(Service service, List<String> path, List<ComponentInstanceProperty> expectedPropertyList) {
218                 List<ComponentInstanceProperty> servicePropertyList = new ArrayList<>();
219                 servicePropertyList = PropertyRestUtils.addComponentInstPropertiesToList(service, servicePropertyList, path.get(0));
220
221                 for (ComponentInstanceProperty serviceCompInstProperty : servicePropertyList) {
222                         ComponentInstanceProperty propDetailsToRemove = PropertyRestUtils.getPropFromListByPropNameTypeAndPath(expectedPropertyList, serviceCompInstProperty.getName(), serviceCompInstProperty.getType(), serviceCompInstProperty.getPath());
223                         ComponentInstanceProperty propDetailsToAdd = propDetailsToRemove;
224                         List<String> tempPathList = new ArrayList<String>();
225                         for (String tempPath : path) {
226                                 tempPathList.add(tempPath);
227                         }
228                         // path parameter can not contain the same service unique ID twice
229                         if (propDetailsToAdd.getPath() != null && !propDetailsToAdd.getPath().get(0).contains(service.getUniqueId())) {
230                                 if (!propDetailsToAdd.getPath().containsAll(tempPathList)) {
231                                         tempPathList.addAll(propDetailsToAdd.getPath());
232                                 }
233                         }
234                         propDetailsToAdd.setPath(tempPathList);
235                         expectedPropertyList.remove(propDetailsToRemove);
236                         expectedPropertyList.add(propDetailsToAdd);
237                 }
238         }
239
240         public static void updatePropertyListWithPathOnResource(ComponentInstance componentInstDetails, Resource resource, List<ComponentInstanceProperty> list, Component container) {
241                 List<String> path = new ArrayList<>();
242                 if (container != null) {
243                         List<ComponentInstance> componentInstances = container.getComponentInstances();
244                         for (ComponentInstance componentInstance : componentInstances) {
245                                 if (componentInstance.getNormalizedName().equals(componentInstDetails.getNormalizedName())) {
246                                         path.add(componentInstance.getUniqueId());
247                                         break;
248                                 }
249                         }
250
251                 } else {
252                         path.add(componentInstDetails.getUniqueId());
253                 }
254                 updatePropertyListWithPathParameter(resource, path, list);
255         }
256
257         public static void updatePropertyListWithPathOnComponentInstance(ComponentInstance componentInstDetails, Service service, List<ComponentInstanceProperty> list) {
258                 List<String> path = new ArrayList<>();
259                 path.add(componentInstDetails.getUniqueId());
260                 updatePropertyListWithPathParameterOnCompInst(service, path, list);
261         }
262 }