summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/ci/utilities/BaseRestUtils.java
blob: 1246d711a7133e750e40888167ddea947824d166 (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
package org.onap.dcae.ci.utilities;

import org.onap.dcae.ci.config.Configuration;
import org.onap.dcae.ci.entities.RestResponse;
import org.onap.dcae.ci.enums.HttpHeaderEnum;

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

public class BaseRestUtils {

	protected static final String acceptHeaderData = "application/json";
	protected static final String contentTypeHeaderData = "application/json";

	BaseRestUtils() {
	}

	static String getApiUrl(String path) {
		Configuration configuration = SetupReport.getConfiguration();
		return String.format("%s:%s/sdc2/rest/v1/catalog/%s", configuration.getSdcBeHost(), configuration.getSdcBePort(), path);
	}

	private static Map<String, String> prepareHeadersMap(String userId) {
		return prepareHeadersMap(userId, acceptHeaderData);
	}

	private static Map<String, String> prepareHeadersMap(String userId, String accept) {
		Map<String, String> headersMap = new HashMap<>();

		headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);

		if (accept != null) {
			headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), accept);
		}

		if (userId != null) {
			headersMap.put(HttpHeaderEnum.USER_ID.getValue(), userId);
		}

		return headersMap;
	}

	static RestResponse sendGet(String url, String userId) throws IOException {
		return sendGet(url, userId, (Map) null);
	}

	protected static RestResponse sendGet(String url, String userId, Map<String, String> additionalHeaders) throws IOException {
		Map<String, String> headersMap = prepareHeadersMap(userId);
		if (additionalHeaders != null) {
			headersMap.putAll(additionalHeaders);
		}

		HttpRequest http = new HttpRequest();
		return http.httpSendGet(url, headersMap);
	}

	static RestResponse sendPut(String url, String userBodyJson, String userId, String cont) throws IOException {
		Map<String, String> headersMap = prepareHeadersMap(userId, cont);
		HttpRequest http = new HttpRequest();
		return http.httpSendByMethod(url, "PUT", userBodyJson, headersMap);
	}

	static RestResponse sendPost(String url, String userBodyJson, String userId, String accept) throws IOException {
		return sendPost(url, userBodyJson, userId, accept, (Map) null);
	}

	private static RestResponse sendPost(String url, String userBodyJson, String userId, String accept, Map<String, String> additionalHeaders) throws IOException {
		Map<String, String> headersMap = prepareHeadersMap(userId, accept);
		if (additionalHeaders != null) {
			headersMap.putAll(additionalHeaders);
		}

		HttpRequest http = new HttpRequest();
		return http.httpSendPost(url, userBodyJson, headersMap);
	}

	static RestResponse sendDelete(String url, String userId) throws IOException {
		return sendDelete(url, userId, (Map) null);
	}

	private static RestResponse sendDelete(String url, String userId, Map<String, String> additionalHeaders) throws IOException {
		Map<String, String> headersMap = prepareHeadersMap(userId);
		if (additionalHeaders != null) {
			headersMap.putAll(additionalHeaders);
		}

		HttpRequest http = new HttpRequest();
		return http.httpSendDelete(url, headersMap);
	}

}