2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.utils.rest;
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;
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.List;
44 import static org.testng.AssertJUnit.*;
46 import com.google.gson.Gson;
48 public class PropertyRestUtils extends BaseRestUtils {
49 private static Logger logger = LoggerFactory.getLogger(PropertyRestUtils.class.getName());
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(),
56 return sendPost(url, body, user.getUserId(), acceptHeaderData);
59 public static RestResponse updateProperty(String resourceId, String propertyId, String body, User user)
61 Config config = Config.instance();
63 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
64 resourceId, propertyId);
65 return sendPut(url, body, user.getUserId(), acceptHeaderData);
68 public static RestResponse updateGroupProperty(Component component, String groupId, String body, User user)
70 Config config = Config.instance();
72 String url = String.format(Urls.RESOURCE_GROUP_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(), component.getUniqueId(), groupId);
73 return sendPut(url, body, user.getUserId(), acceptHeaderData);
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,
80 return sendGet(url, user.getUserId());
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);
88 return sendDelete(url, user.getUserId());
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;
102 public static ComponentInstanceProperty getPropFromListByPropNameTypeAndPath(
103 List<ComponentInstanceProperty> propList, String propNameToUpdate, String propTypeToUpdate,
105 for (ComponentInstanceProperty componentInstanceProperty : propList) {
106 if (componentInstanceProperty.getPath() == null) {
107 return getPropFromListByPropNameAndType(propList, propNameToUpdate, propTypeToUpdate);
109 if (componentInstanceProperty.getName().equals(propNameToUpdate)
110 && componentInstanceProperty.getType().equals(propTypeToUpdate)
111 && path.containsAll(componentInstanceProperty.getPath())) {
112 return componentInstanceProperty;
118 public static ComponentInstanceProperty getPropFromListByPropIdAndPath(List<ComponentInstanceProperty> propList,
119 String propId, List<String> path) {
121 for (ComponentInstanceProperty componentInstanceProperty : propList) {
123 if (componentInstanceProperty.getUniqueId().equals(propId)
124 && componentInstanceProperty.getPath().equals(path)) {
125 return componentInstanceProperty;
128 if (componentInstanceProperty.getUniqueId().equals(propId)) {
129 return componentInstanceProperty;
136 public static void comparePropertyLists(List<ComponentInstanceProperty> expectedList,
137 List<ComponentInstanceProperty> actualList, Boolean isUpdate) {
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);
151 // System.out.println("expected: " + expectedList + ", actual: " +
153 logger.debug("expected: {}, actual: {}",expectedList,actualList);
154 assertTrue("actual lists does not contain all uniqeIds", flag);
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) {
168 "expected defaulValue is: " + defaulValue + " ,actual: " + actualCompInstProp.getDefaultValue(),
169 actualCompInstProp.getDefaultValue() == defaulValue);
172 "expected defaulValue is: " + defaulValue + " ,actual: " + actualCompInstProp.getDefaultValue(),
173 actualCompInstProp.getDefaultValue().equals(defaulValue));
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());
183 if (defaulValue == null) {
185 "actual [Value] parameter " + actualCompInstProp.getName()
186 + "should equal to expected [defaultValue]: " + actualCompInstProp.getValue()
187 + " ,defaultValue: " + actualCompInstProp.getDefaultValue(),
188 actualCompInstProp.getValue() == expectedCompInstProp.getDefaultValue());
191 "actual [Value] parameter " + actualCompInstProp.getName()
192 + "should equal to expected [defaultValue]: " + actualCompInstProp.getValue()
193 + " ,defaultValue: " + actualCompInstProp.getDefaultValue(),
194 actualCompInstProp.getValue().equals(expectedCompInstProp.getDefaultValue()));
196 assertNull("valueId is not null", actualCompInstProp.getValueUniqueUid());
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));
211 public static List<ComponentInstanceProperty> addComponentInstPropertiesToList(Component component,
212 List<ComponentInstanceProperty> listToFill, String componentId) {
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);
224 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = component
225 .getComponentInstancesProperties();
226 for (Map.Entry<String, List<ComponentInstanceProperty>> componentInstanceProperties : componentInstancesProperties
228 for (ComponentInstanceProperty prop : componentInstanceProperties.getValue()) {
229 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(prop, null,
231 componentInstanceProperty.setPath(prop.getPath());
232 componentInstanceProperty.setValueUniqueUid(prop.getValueUniqueUid());
233 componentInstanceProperty.setValue(prop.getValue());
234 listToFill.add(componentInstanceProperty);
239 if (component.getComponentType().getValue().equals("Resource")) {
240 for (PropertyDefinition prop : ((Resource) component).getProperties()) {
241 listToFill.add(new ComponentInstanceProperty(prop, null, null));
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);
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);
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,
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);
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());
292 propDetailsToAdd.setPath(tempPathList);
293 expectedPropertyList.remove(propDetailsToRemove);
294 expectedPropertyList.add(propDetailsToAdd);
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());
311 path.add(componentInstDetails.getUniqueId());
313 updatePropertyListWithPathParameter(resource, path, list);
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);
323 public static RestResponse declareProporties(Component componentObject, Map<String, List<ComponentInstanceInput>> componentInstancesInputs, User sdncModifierDetails)
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) {
342 return declareProportiesResponse;
345 public static RestResponse updateInput(Component componentObject, PropertyObject componentInput, User sdncModifierDetails)
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) {
362 return declareProportiesResponse;