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.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;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
41 import static org.testng.AssertJUnit.*;
43 public class PropertyRestUtils extends BaseRestUtils {
44 private static Logger logger = LoggerFactory.getLogger(PropertyRestUtils.class.getName());
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);
50 return sendPost(url, body, user.getUserId(), acceptHeaderData);
53 public static RestResponse updateProperty(String resourceId, String propertyId, String body, User user)
55 Config config = Config.instance();
57 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
58 resourceId, propertyId);
59 return sendPut(url, body, user.getUserId(), acceptHeaderData);
62 public static RestResponse updateGroupProperty(Component component, String groupId, String body, User user)
64 Config config = Config.instance();
66 String url = String.format(Urls.RESOURCE_GROUP_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(), component.getUniqueId(), groupId);
67 return sendPut(url, body, user.getUserId(), acceptHeaderData);
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,
74 return sendGet(url, user.getUserId());
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);
82 return sendDelete(url, user.getUserId());
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;
96 public static ComponentInstanceProperty getPropFromListByPropNameTypeAndPath(
97 List<ComponentInstanceProperty> propList, String propNameToUpdate, String propTypeToUpdate,
99 for (ComponentInstanceProperty componentInstanceProperty : propList) {
100 if (componentInstanceProperty.getPath() == null) {
101 return getPropFromListByPropNameAndType(propList, propNameToUpdate, propTypeToUpdate);
103 if (componentInstanceProperty.getName().equals(propNameToUpdate)
104 && componentInstanceProperty.getType().equals(propTypeToUpdate)
105 && path.containsAll(componentInstanceProperty.getPath())) {
106 return componentInstanceProperty;
112 public static ComponentInstanceProperty getPropFromListByPropIdAndPath(List<ComponentInstanceProperty> propList,
113 String propId, List<String> path) {
115 for (ComponentInstanceProperty componentInstanceProperty : propList) {
117 if (componentInstanceProperty.getUniqueId().equals(propId)
118 && componentInstanceProperty.getPath().equals(path)) {
119 return componentInstanceProperty;
122 if (componentInstanceProperty.getUniqueId().equals(propId)) {
123 return componentInstanceProperty;
130 public static void comparePropertyLists(List<ComponentInstanceProperty> expectedList,
131 List<ComponentInstanceProperty> actualList, Boolean isUpdate) {
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);
145 // System.out.println("expected: " + expectedList + ", actual: " +
147 logger.debug("expected: {}, actual: {}", expectedList, actualList);
148 assertTrue("actual lists does not contain all uniqeIds", flag);
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) {
162 "expected defaulValue is: " + defaulValue + " ,actual: " + actualCompInstProp.getDefaultValue(),
163 actualCompInstProp.getDefaultValue() == defaulValue);
166 "expected defaulValue is: " + defaulValue + " ,actual: " + actualCompInstProp.getDefaultValue(),
167 actualCompInstProp.getDefaultValue().equals(defaulValue));
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());
177 if (defaulValue == null) {
179 "actual [Value] parameter " + actualCompInstProp.getName()
180 + "should equal to expected [defaultValue]: " + actualCompInstProp.getValue()
181 + " ,defaultValue: " + actualCompInstProp.getDefaultValue(),
182 actualCompInstProp.getValue() == expectedCompInstProp.getDefaultValue());
185 "actual [Value] parameter " + actualCompInstProp.getName()
186 + "should equal to expected [defaultValue]: " + actualCompInstProp.getValue()
187 + " ,defaultValue: " + actualCompInstProp.getDefaultValue(),
188 actualCompInstProp.getValue().equals(expectedCompInstProp.getDefaultValue()));
190 assertNull("valueId is not null", actualCompInstProp.getValueUniqueUid());
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));
205 public static List<ComponentInstanceProperty> addComponentInstPropertiesToList(Component component,
206 List<ComponentInstanceProperty> listToFill, String componentId) {
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);
218 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = component
219 .getComponentInstancesProperties();
220 for (Map.Entry<String, List<ComponentInstanceProperty>> componentInstanceProperties : componentInstancesProperties
222 for (ComponentInstanceProperty prop : componentInstanceProperties.getValue()) {
223 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(prop, null,
225 componentInstanceProperty.setPath(prop.getPath());
226 componentInstanceProperty.setValueUniqueUid(prop.getValueUniqueUid());
227 componentInstanceProperty.setValue(prop.getValue());
228 listToFill.add(componentInstanceProperty);
233 if (component.getComponentType().getValue().equals("Resource")) {
234 for (PropertyDefinition prop : ((Resource) component).getProperties()) {
235 listToFill.add(new ComponentInstanceProperty(prop, null, null));
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);
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);
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,
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);
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());
286 propDetailsToAdd.setPath(tempPathList);
287 expectedPropertyList.remove(propDetailsToRemove);
288 expectedPropertyList.add(propDetailsToAdd);
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());
305 path.add(componentInstDetails.getUniqueId());
307 updatePropertyListWithPathParameter(resource, path, list);
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);
317 public static RestResponse declareProporties(Component componentObject, Map<String, List<ComponentInstanceInput>> componentInstancesInputs, User sdncModifierDetails)
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) {
336 return declareProportiesResponse;
339 public static RestResponse updateInput(Component componentObject, PropertyObject componentInput, User sdncModifierDetails)
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) {
356 return declareProportiesResponse;