aboutsummaryrefslogtreecommitdiffstats
path: root/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/rest/PropertyRestUtils.java
blob: 34a158fe74e02545d18420ea90e6039aff8b5687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.ci.tests.utils.rest;

import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertNull;
import static org.testng.AssertJUnit.assertTrue;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.ComponentInstanceInput;
import org.openecomp.sdc.be.model.ComponentInstanceProperty;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.Service;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.ci.tests.api.Urls;
import org.openecomp.sdc.ci.tests.config.Config;
import org.openecomp.sdc.ci.tests.datatypes.PropertyObject;
import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.common.util.GeneralUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PropertyRestUtils extends BaseRestUtils {
	private static Logger logger = LoggerFactory.getLogger(PropertyRestUtils.class.getName());

	public static RestResponse createProperty(String resourceId, String body, User user) throws Exception {
		Config config = Config.instance();
		String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId);

		return sendPost(url, body, user.getUserId(), acceptHeaderData);
	}

	public static RestResponse addValueToProperty(String resourceId, String body, User user) throws Exception {
		Config config = Config.instance();
		String url = String.format(Urls.CREATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId);

		return sendPut(url, body, user.getUserId(), acceptHeaderData);
	}

	public static RestResponse createServiceProperty(String resourceId, String body, User user) throws Exception {
		Config config = Config.instance();
		String url = String.format(Urls.CREATE_SERVICE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
				resourceId);

		return sendPost(url, body, user.getUserId(), acceptHeaderData);
	}

	public static RestResponse updateProperty(String resourceId, String propertyId, String body, User user)
			throws Exception {
		Config config = Config.instance();

		String url = String.format(Urls.UPDATE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
				resourceId, propertyId);
		return sendPut(url, body, user.getUserId(), acceptHeaderData);
	}

	public static RestResponse updateGroupProperty(Component component, String groupId, String body, User user)
			throws Exception {
		Config config = Config.instance();

		String url = String.format(Urls.RESOURCE_GROUP_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(), component.getUniqueId(), groupId);
		return sendPut(url, body, user.getUserId(), acceptHeaderData);
	}

	public static RestResponse getProperty(String resourceId, String propertyId, User user) throws Exception {
		Config config = Config.instance();
		String url = String.format(Urls.GET_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId,
				propertyId);
		return sendGet(url, user.getUserId());
	}

	public static RestResponse deleteProperty(String resourceId, String propertyId, User user) throws Exception {
		Config config = Config.instance();
		String url = String.format(Urls.DELETE_PROPERTY, config.getCatalogBeHost(), config.getCatalogBePort(),
				resourceId, propertyId);

		return sendDelete(url, user.getUserId());
	}

	public static ComponentInstanceProperty getPropFromListByPropNameAndType(List<ComponentInstanceProperty> propList,
																			 String propNameToUpdate, String propTypeToUpdate) {
		for (ComponentInstanceProperty componentInstanceProperty : propList) {
			if (componentInstanceProperty.getName().equals(propNameToUpdate)
					&& componentInstanceProperty.getType().equals(propTypeToUpdate)) {
				return componentInstanceProperty;
			}
		}
		return null;
	}

	public static ComponentInstanceProperty getPropFromListByPropNameTypeAndPath(
			List<ComponentInstanceProperty> propList, String propNameToUpdate, String propTypeToUpdate,
			List<String> path) {
		for (ComponentInstanceProperty componentInstanceProperty : propList) {
			if (componentInstanceProperty.getPath() == null) {
				return getPropFromListByPropNameAndType(propList, propNameToUpdate, propTypeToUpdate);
			}
			if (componentInstanceProperty.getName().equals(propNameToUpdate)
					&& componentInstanceProperty.getType().equals(propTypeToUpdate)
					&& path.containsAll(componentInstanceProperty.getPath())) {
				return componentInstanceProperty;
			}
		}
		return null;
	}

	public static ComponentInstanceProperty getPropFromListByPropIdAndPath(List<ComponentInstanceProperty> propList,
																		   String propId, List<String> path) {

		for (ComponentInstanceProperty componentInstanceProperty : propList) {
			if (path != null) {
				if (componentInstanceProperty.getUniqueId().equals(propId)
						&& componentInstanceProperty.getPath().equals(path)) {
					return componentInstanceProperty;
				}
			} else {
				if (componentInstanceProperty.getUniqueId().equals(propId)) {
					return componentInstanceProperty;
				}
			}
		}
		return null;
	}

	public static void comparePropertyLists(List<ComponentInstanceProperty> expectedList,
											List<ComponentInstanceProperty> actualList, Boolean isUpdate) {

		assertTrue(
				"list size are not equals, expected size is: " + expectedList.size() + " ,actual: " + actualList.size(),
				expectedList.size() == actualList.size());
		Boolean flag = false;
		for (ComponentInstanceProperty expectedcompInstProp : expectedList) {
			for (ComponentInstanceProperty actualcompInstProp : actualList) {
				flag = comparePropertyObjects(expectedcompInstProp, actualcompInstProp, isUpdate);
				if (flag) {
					break;
				}
			}
		}
		// System.out.println("expected: " + expectedList + ", actual: " +
		// actualList);
		logger.debug("expected: {}, actual: {}", expectedList, actualList);
		assertTrue("actual lists does not contain all uniqeIds", flag);
	}

	public static Boolean comparePropertyObjects(ComponentInstanceProperty expectedCompInstProp,
												 ComponentInstanceProperty actualCompInstProp, Boolean isUpdate) {
		String uniqueId = expectedCompInstProp.getUniqueId();
		String type = expectedCompInstProp.getType();
		String defaulValue = (String) expectedCompInstProp.getDefaultValue();
		if (actualCompInstProp.getUniqueId().equals(uniqueId)
				&& actualCompInstProp.getPath().equals(expectedCompInstProp.getPath())) {
			assertTrue("expected type is: " + type + " ,actual: " + actualCompInstProp.getType(),
					actualCompInstProp.getType().equals(type));
			if (defaulValue == null) {
				assertTrue(
						"expected defaulValue is: " + defaulValue + " ,actual: " + actualCompInstProp.getDefaultValue(),
						actualCompInstProp.getDefaultValue() == defaulValue);
			} else {
				assertTrue(
						"expected defaulValue is: " + defaulValue + " ,actual: " + actualCompInstProp.getDefaultValue(),
						actualCompInstProp.getDefaultValue().equals(defaulValue));
			}
			if (isUpdate) {
				assertTrue(
						"actual [Value] parameter " + actualCompInstProp.getName()
								+ "should equal to expected [Value]: " + actualCompInstProp.getValue() + " ,Value: "
								+ actualCompInstProp.getValue(),
						actualCompInstProp.getValue().equals(expectedCompInstProp.getValue()));
				assertNotNull("valueId is null", actualCompInstProp.getValueUniqueUid());
			} else {
				if (defaulValue == null) {
					assertTrue(
							"actual [Value] parameter " + actualCompInstProp.getName()
									+ "should equal to expected [defaultValue]: " + actualCompInstProp.getValue()
									+ " ,defaultValue: " + actualCompInstProp.getDefaultValue(),
							actualCompInstProp.getValue() == expectedCompInstProp.getDefaultValue());
				} else {
					assertTrue(
							"actual [Value] parameter " + actualCompInstProp.getName()
									+ "should equal to expected [defaultValue]: " + actualCompInstProp.getValue()
									+ " ,defaultValue: " + actualCompInstProp.getDefaultValue(),
							actualCompInstProp.getValue().equals(expectedCompInstProp.getDefaultValue()));
				}
				assertNull("valueId is not null", actualCompInstProp.getValueUniqueUid());
			}
			return true;
		}
		return false;
	}

	public static List<ComponentInstanceProperty> addResourcePropertiesToList(Resource resource,
																			  List<ComponentInstanceProperty> listToFill) {
		for (PropertyDefinition prop : resource.getProperties()) {
			listToFill.add(new ComponentInstanceProperty(prop, null, null));
		}
		return listToFill;
	}

	public static List<ComponentInstanceProperty> addComponentInstPropertiesToList(Component component,
																				   List<ComponentInstanceProperty> listToFill, String componentId) {

		if (componentId != null) {
			List<ComponentInstanceProperty> list = component.getComponentInstancesProperties().get(componentId);
			for (ComponentInstanceProperty prop : list) {
				ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(prop, null, null);
				componentInstanceProperty.setPath(prop.getPath());
				componentInstanceProperty.setValueUniqueUid(prop.getValueUniqueUid());
				componentInstanceProperty.setValue(prop.getValue());
				listToFill.add(componentInstanceProperty);
			}
		} else {
			Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = component
					.getComponentInstancesProperties();
			for (Map.Entry<String, List<ComponentInstanceProperty>> componentInstanceProperties : componentInstancesProperties
					.entrySet()) {
				for (ComponentInstanceProperty prop : componentInstanceProperties.getValue()) {
					ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(prop, null,
							null);
					componentInstanceProperty.setPath(prop.getPath());
					componentInstanceProperty.setValueUniqueUid(prop.getValueUniqueUid());
					componentInstanceProperty.setValue(prop.getValue());
					listToFill.add(componentInstanceProperty);
				}
			}
		}

		if (component.getComponentType().getValue().equals("Resource")) {
			for (PropertyDefinition prop : ((Resource) component).getProperties()) {
				listToFill.add(new ComponentInstanceProperty(prop, null, null));
			}
		}
		return listToFill;
	}

	public static ComponentInstanceProperty getCompPropInstListByInstIdAndPropName(Component component,
																				   ComponentInstance componentInstanceDetails, String name, String type) {
		List<ComponentInstanceProperty> propList = component.getComponentInstancesProperties()
				.get(componentInstanceDetails.getUniqueId());
		if (propList != null) {
			return getPropFromListByPropNameAndType(propList, name, type);
		}
		return null;
	}

	private static void updatePropertyListWithPathParameter(Resource resource, List<String> path,
															List<ComponentInstanceProperty> expectedPropertyList) {
		List<PropertyDefinition> propertyList = resource.getProperties();
		for (PropertyDefinition propertyDefinition : propertyList) {
			ComponentInstanceProperty propDetailsToRemove = PropertyRestUtils.getPropFromListByPropNameAndType(
					expectedPropertyList, propertyDefinition.getName(), propertyDefinition.getType());
			ComponentInstanceProperty propDetailsToAdd = propDetailsToRemove;
			propDetailsToAdd.setPath(path);
			expectedPropertyList.remove(propDetailsToRemove);
			expectedPropertyList.add(propDetailsToAdd);
		}
	}

	private static void updatePropertyListWithPathParameterOnCompInst(Service service, List<String> path,
																	  List<ComponentInstanceProperty> expectedPropertyList) {
		List<ComponentInstanceProperty> servicePropertyList = new ArrayList<>();
		servicePropertyList = PropertyRestUtils.addComponentInstPropertiesToList(service, servicePropertyList,
				path.get(0));

		for (ComponentInstanceProperty serviceCompInstProperty : servicePropertyList) {
			ComponentInstanceProperty propDetailsToRemove = PropertyRestUtils.getPropFromListByPropNameTypeAndPath(
					expectedPropertyList, serviceCompInstProperty.getName(), serviceCompInstProperty.getType(),
					serviceCompInstProperty.getPath());
			ComponentInstanceProperty propDetailsToAdd = propDetailsToRemove;
			List<String> tempPathList = new ArrayList<String>();
			for (String tempPath : path) {
				tempPathList.add(tempPath);
			}
			// path parameter can not contain the same service unique ID twice
			if (propDetailsToAdd.getPath() != null
					&& !propDetailsToAdd.getPath().get(0).contains(service.getUniqueId())) {
				if (!propDetailsToAdd.getPath().containsAll(tempPathList)) {
					tempPathList.addAll(propDetailsToAdd.getPath());
				}
			}
			propDetailsToAdd.setPath(tempPathList);
			expectedPropertyList.remove(propDetailsToRemove);
			expectedPropertyList.add(propDetailsToAdd);
		}
	}

	public static void updatePropertyListWithPathOnResource(ComponentInstance componentInstDetails, Resource resource,
															List<ComponentInstanceProperty> list, Component container) {
		List<String> path = new ArrayList<>();
		if (container != null) {
			List<ComponentInstance> componentInstances = container.getComponentInstances();
			for (ComponentInstance componentInstance : componentInstances) {
				if (componentInstance.getNormalizedName().equals(componentInstDetails.getNormalizedName())) {
					path.add(componentInstance.getUniqueId());
					break;
				}
			}

		} else {
			path.add(componentInstDetails.getUniqueId());
		}
		updatePropertyListWithPathParameter(resource, path, list);
	}

	public static void updatePropertyListWithPathOnComponentInstance(ComponentInstance componentInstDetails,
																	 Service service, List<ComponentInstanceProperty> list) {
		List<String> path = new ArrayList<>();
		path.add(componentInstDetails.getUniqueId());
		updatePropertyListWithPathParameterOnCompInst(service, path, list);
	}

	public static RestResponse declareProporties(Component componentObject, Map<String, List<ComponentInstanceInput>> componentInstancesInputs, User sdncModifierDetails)
			throws Exception {
		Config config = Config.instance();
		String url = String.format(Urls.DECLARE_PROPERTIES, config.getCatalogBeHost(), config.getCatalogBePort(), ComponentTypeEnum.findParamByType(componentObject.getComponentType()), componentObject.getUniqueId());
		String userId = sdncModifierDetails.getUserId();
		Map<String, String> headersMap = prepareHeadersMap(userId);
		Map<String, Object> jsonBuilder = new HashMap<>();
		jsonBuilder.put("componentInstanceInputsMap", componentInstancesInputs);
		Gson gson = new Gson();
		String userBodyJson = gson.toJson(jsonBuilder);
		String calculateMD5 = GeneralUtility.calculateMD5Base64EncodedByString(userBodyJson);
		headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
		HttpRequest http = new HttpRequest();
		// System.out.println(url);
		// System.out.println(userBodyJson);
		RestResponse declareProportiesResponse = http.httpSendPost(url, userBodyJson, headersMap);
		if (declareProportiesResponse.getErrorCode() == STATUS_CODE_GET_SUCCESS) {

		}
		return declareProportiesResponse;
	}

	public static RestResponse updateInput(Component componentObject, PropertyObject componentInput, User sdncModifierDetails)
			throws Exception {
		Config config = Config.instance();
		String url = String.format(Urls.UPDATE_INPUT, config.getCatalogBeHost(), config.getCatalogBePort(), componentObject.getUniqueId());
		String userId = sdncModifierDetails.getUserId();
		Map<String, String> headersMap = prepareHeadersMap(userId);
		Gson gson = new Gson();
		String userBodyJson = gson.toJson(componentInput);
		String calculateMD5 = GeneralUtility.calculateMD5Base64EncodedByString(userBodyJson);
		headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
		HttpRequest http = new HttpRequest();
		// System.out.println(url);
		// System.out.println(userBodyJson);
		RestResponse declareProportiesResponse = http.httpSendPost(url, userBodyJson, headersMap);
		if (declareProportiesResponse.getErrorCode() == STATUS_CODE_GET_SUCCESS) {

		}
		return declareProportiesResponse;
	}
}