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(), resourceId);
51 return sendPost(url, body, user.getUserId(), acceptHeaderData);
54 public static RestResponse updateProperty(String resourceId, String propertyId, String body, User user) throws Exception {
55 Config config = Config.instance();
57 String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId, propertyId);
58 return sendPut(url, body, user.getUserId(), acceptHeaderData);
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());
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);
71 return sendDelete(url, user.getUserId());
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;
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);
88 if (componentInstanceProperty.getName().equals(propNameToUpdate) && componentInstanceProperty.getType().equals(propTypeToUpdate) && path.containsAll(componentInstanceProperty.getPath())) {
89 return componentInstanceProperty;
95 public static ComponentInstanceProperty getPropFromListByPropIdAndPath(List<ComponentInstanceProperty> propList, String propId, List<String> path) {
97 for (ComponentInstanceProperty componentInstanceProperty : propList) {
99 if (componentInstanceProperty.getUniqueId().equals(propId) && componentInstanceProperty.getPath().equals(path)) {
100 return componentInstanceProperty;
103 if (componentInstanceProperty.getUniqueId().equals(propId)) {
104 return componentInstanceProperty;
111 public static void comparePropertyLists(List<ComponentInstanceProperty> expectedList, List<ComponentInstanceProperty> actualList, Boolean isUpdate) {
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);
123 // System.out.println("expected: " + expectedList + ", actual: " +
125 logger.debug("expected: {}, actual: {}",expectedList,actualList);
126 assertTrue("actual lists does not contain all uniqeIds", flag);
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);
138 assertTrue("expected defaulValue is: " + defaulValue + " ,actual: " + actualCompInstProp.getDefaultValue(), actualCompInstProp.getDefaultValue().equals(defaulValue));
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());
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());
149 assertTrue("actual [Value] parameter " + actualCompInstProp.getName() + "should equal to expected [defaultValue]: " + actualCompInstProp.getValue() + " ,defaultValue: " + actualCompInstProp.getDefaultValue(),
150 actualCompInstProp.getValue().equals(expectedCompInstProp.getDefaultValue()));
152 assertNull("valueId is not null", actualCompInstProp.getValueUniqueUid());
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));
166 public static List<ComponentInstanceProperty> addComponentInstPropertiesToList(Component component, List<ComponentInstanceProperty> listToFill, String componentId) {
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);
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);
190 if (component.getComponentType().getValue().equals("Resource")) {
191 for (PropertyDefinition prop : ((Resource) component).getProperties()) {
192 listToFill.add(new ComponentInstanceProperty(prop, null, null));
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);
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);
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));
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);
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());
234 propDetailsToAdd.setPath(tempPathList);
235 expectedPropertyList.remove(propDetailsToRemove);
236 expectedPropertyList.add(propDetailsToAdd);
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());
252 path.add(componentInstDetails.getUniqueId());
254 updatePropertyListWithPathParameter(resource, path, list);
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);