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 static org.testng.AssertJUnit.assertNotNull;
24 import static org.testng.AssertJUnit.assertNull;
25 import static org.testng.AssertJUnit.assertTrue;
27 import java.util.ArrayList;
28 import java.util.List;
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;
44 public class PropertyRestUtils extends BaseRestUtils {
45 private static Logger logger = LoggerFactory.getLogger(PropertyRestUtils.class.getName());
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(),
52 return sendPost(url, body, user.getUserId(), acceptHeaderData);
55 public static RestResponse updateProperty(String resourceId, String propertyId, String body, User user)
57 Config config = Config.instance();
59 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
60 resourceId, propertyId);
61 return sendPut(url, body, user.getUserId(), acceptHeaderData);
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,
68 return sendGet(url, user.getUserId());
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);
76 return sendDelete(url, user.getUserId());
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;
90 public static ComponentInstanceProperty getPropFromListByPropNameTypeAndPath(
91 List<ComponentInstanceProperty> propList, String propNameToUpdate, String propTypeToUpdate,
93 for (ComponentInstanceProperty componentInstanceProperty : propList) {
94 if (componentInstanceProperty.getPath() == null) {
95 return getPropFromListByPropNameAndType(propList, propNameToUpdate, propTypeToUpdate);
97 if (componentInstanceProperty.getName().equals(propNameToUpdate)
98 && componentInstanceProperty.getType().equals(propTypeToUpdate)
99 && path.containsAll(componentInstanceProperty.getPath())) {
100 return componentInstanceProperty;
106 public static ComponentInstanceProperty getPropFromListByPropIdAndPath(List<ComponentInstanceProperty> propList,
107 String propId, List<String> path) {
109 for (ComponentInstanceProperty componentInstanceProperty : propList) {
111 if (componentInstanceProperty.getUniqueId().equals(propId)
112 && componentInstanceProperty.getPath().equals(path)) {
113 return componentInstanceProperty;
116 if (componentInstanceProperty.getUniqueId().equals(propId)) {
117 return componentInstanceProperty;
124 public static void comparePropertyLists(List<ComponentInstanceProperty> expectedList,
125 List<ComponentInstanceProperty> actualList, Boolean isUpdate) {
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);
139 // System.out.println("expected: " + expectedList + ", actual: " +
141 logger.debug("expected: {}, actual: {}",expectedList,actualList);
142 assertTrue("actual lists does not contain all uniqeIds", flag);
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) {
156 "expected defaulValue is: " + defaulValue + " ,actual: " + actualCompInstProp.getDefaultValue(),
157 actualCompInstProp.getDefaultValue() == defaulValue);
160 "expected defaulValue is: " + defaulValue + " ,actual: " + actualCompInstProp.getDefaultValue(),
161 actualCompInstProp.getDefaultValue().equals(defaulValue));
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());
171 if (defaulValue == null) {
173 "actual [Value] parameter " + actualCompInstProp.getName()
174 + "should equal to expected [defaultValue]: " + actualCompInstProp.getValue()
175 + " ,defaultValue: " + actualCompInstProp.getDefaultValue(),
176 actualCompInstProp.getValue() == expectedCompInstProp.getDefaultValue());
179 "actual [Value] parameter " + actualCompInstProp.getName()
180 + "should equal to expected [defaultValue]: " + actualCompInstProp.getValue()
181 + " ,defaultValue: " + actualCompInstProp.getDefaultValue(),
182 actualCompInstProp.getValue().equals(expectedCompInstProp.getDefaultValue()));
184 assertNull("valueId is not null", actualCompInstProp.getValueUniqueUid());
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));
199 public static List<ComponentInstanceProperty> addComponentInstPropertiesToList(Component component,
200 List<ComponentInstanceProperty> listToFill, String componentId) {
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);
212 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = component
213 .getComponentInstancesProperties();
214 for (Map.Entry<String, List<ComponentInstanceProperty>> componentInstanceProperties : componentInstancesProperties
216 for (ComponentInstanceProperty prop : componentInstanceProperties.getValue()) {
217 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(prop, null,
219 componentInstanceProperty.setPath(prop.getPath());
220 componentInstanceProperty.setValueUniqueUid(prop.getValueUniqueUid());
221 componentInstanceProperty.setValue(prop.getValue());
222 listToFill.add(componentInstanceProperty);
227 if (component.getComponentType().getValue().equals("Resource")) {
228 for (PropertyDefinition prop : ((Resource) component).getProperties()) {
229 listToFill.add(new ComponentInstanceProperty(prop, null, null));
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);
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);
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,
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);
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());
280 propDetailsToAdd.setPath(tempPathList);
281 expectedPropertyList.remove(propDetailsToRemove);
282 expectedPropertyList.add(propDetailsToAdd);
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());
299 path.add(componentInstDetails.getUniqueId());
301 updatePropertyListWithPathParameter(resource, path, list);
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);