summaryrefslogtreecommitdiffstats
path: root/ui-ci/src/main/java/org/openecomp/sdc/ci/tests/utilities/RestCDUtils.java
blob: 10535e0157ab6fa6548413dfa8cb1c4a63f78fcb (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
/*-
 * ============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.utilities;

import static org.testng.AssertJUnit.assertTrue;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.ci.tests.datatypes.ComponentReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.ProductReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.ci.tests.utils.rest.ProductRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;

public class RestCDUtils {

	private static void setResourceUniqueIdAndUUID(ComponentReqDetails element, RestResponse getResourceResponse) {
		element.setUniqueId(ResponseParser.getUniqueIdFromResponse(getResourceResponse));
		element.setUUID(ResponseParser.getUuidFromResponse(getResourceResponse));
	}

	public static RestResponse getResource(ResourceReqDetails resource, User user) {
		try {
			System.out.println("trying to get resource");
			GeneralUIUtils.sleep(1000);
			RestResponse getResourceResponse = null;
			String reourceUniqueId = resource.getUniqueId();
			if (reourceUniqueId != null) {
				getResourceResponse = ResourceRestUtils.getResource(reourceUniqueId);
				if (getResourceResponse.getErrorCode().intValue() == 200) {
					System.out.println("succeeded to get resource");
				}
				return getResourceResponse;
			}
			JSONObject getResourceJSONObject = null;
			getResourceResponse = ResourceRestUtils.getResourceByNameAndVersion(user.getUserId(), resource.getName(),
					resource.getVersion());
			if (getResourceResponse.getErrorCode().intValue() == 200) {
				JSONArray jArray = new JSONArray(getResourceResponse.getResponse());
				for (int i = 0; i < jArray.length(); i++) {
					getResourceJSONObject = jArray.getJSONObject(i);
					String resourceType = ResponseParser.getValueFromJsonResponse(getResourceJSONObject.toString(),
							"resourceType");
					if (resourceType.equals(resource.getResourceType())) {
						getResourceResponse.setResponse(getResourceJSONObject.toString());
						setResourceUniqueIdAndUUID(resource, getResourceResponse);
						System.out.println("succeeded to get resource");
						return getResourceResponse;
					}
				}
			}

			return getResourceResponse;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static RestResponse getService(ServiceReqDetails service, User user) {
		try {
			Thread.sleep(3500);
			RestResponse getServiceResponse = ServiceRestUtils.getServiceByNameAndVersion(user, service.getName(),
					service.getVersion());
			if (getServiceResponse.getErrorCode().intValue() == 200) {
				setResourceUniqueIdAndUUID(service, getServiceResponse);
			}
			return getServiceResponse;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	public static RestResponse getProduct(ProductReqDetails product, User user) {
		try {
			Thread.sleep(3500);
			RestResponse getProductResponse = ProductRestUtils.getProductByNameAndVersion(product.getName(),
					product.getVersion(), user.getUserId());
			if (getProductResponse.getErrorCode().intValue() == 200) {
				setResourceUniqueIdAndUUID(product, getProductResponse);
			}
			return getProductResponse;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static Map<String, String> getAllElementVersionsFromResponse(RestResponse getResource) throws Exception {
		Map<String, String> versionsMap = new HashMap<String, String>();
		try {
			ObjectMapper mapper = new ObjectMapper();

			JSONObject object = new JSONObject(getResource.getResponse());
			versionsMap = mapper.readValue(object.get("allVersions").toString(), Map.class);

		} catch (Exception e) {
			e.printStackTrace();
			return versionsMap;

		}

		return versionsMap;
	}

	public static void deleteElementVersions(Map<String, String> elementVersions, boolean isBeforeTest, Object clazz,
			User user) throws Exception {
		Iterator<String> iterator = elementVersions.keySet().iterator();
		while (iterator.hasNext()) {
			String singleVersion = iterator.next();
			String uniqueId = elementVersions.get(singleVersion);
			RestResponse deleteResponse = null;
			if (clazz instanceof ServiceReqDetails) {
				deleteResponse = ServiceRestUtils.deleteServiceById(uniqueId, user.getUserId());
			} else if (clazz instanceof ResourceReqDetails) {
				deleteResponse = ResourceRestUtils.deleteResource(uniqueId, user.getUserId());
			} else if (clazz instanceof ProductReqDetails) {
				deleteResponse = ProductRestUtils.deleteProduct(uniqueId, user.getUserId());
			}

			if (isBeforeTest) {
				assertTrue(deleteResponse.getErrorCode().intValue() == 204
						|| deleteResponse.getErrorCode().intValue() == 404);
			} else {
				assertTrue(deleteResponse.getErrorCode().intValue() == 204);
			}
		}
	}

	public static void deleteAllResourceVersionsAfterTest(ComponentReqDetails componentDetails,
			RestResponse getObjectResponse, User user) {
		try {
			deleteAllComponentVersion(false, componentDetails, getObjectResponse, user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void deleteAllResourceVersionsBeforeTest(ComponentReqDetails componentDetails,
			RestResponse getObjectResponse, User user) throws Exception {
		deleteAllComponentVersion(true, componentDetails, getObjectResponse, user);
	}

	public static void deleteAllComponentVersion(boolean isBeforeTest, ComponentReqDetails componentDetails,
			RestResponse getObjectResponse, User user) throws Exception {
		if (getObjectResponse.getErrorCode().intValue() == 404)
			return;
		Map<String, String> componentVersionsMap = getAllElementVersionsFromResponse(getObjectResponse);
		System.out.println("deleting...");
		deleteElementVersions(componentVersionsMap, isBeforeTest, componentDetails, user);
		componentDetails.setUniqueId(null);
	}

}