summaryrefslogtreecommitdiffstats
path: root/mod/runtimeapi/runtime-web/src/test/java/org/onap/dcae/runtime/web/ClientMocking.java
blob: 14acf909528b21ea6a00a0f44679460500bef787 (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
/*-
 * ============LICENSE_START=======================================================
 * Copyright (C) 2020 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.onap.dcae.runtime.web;

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.Base64;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.HttpResponse;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;

/**
 * Simulate responses from RestTemplate based clients.
 */

public class ClientMocking implements Answer<HttpResponse> {
	private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
	/**
	 * Replace single quotes with double quotes.
	 * Makes it easy to write JSON strings.
	 *
	 * @param s String with single quotes.
	 * @return String replacing single quotes with double quotes.
	 */
	public static String xq(String s) {
		return s.replaceAll("'", "\"");
	}

	private static class BasicCloseableHttpResponse extends BasicHttpResponse implements CloseableHttpResponse {
		public BasicCloseableHttpResponse(StatusLine line) {
			super(line);
		}

		@Override
		public void close() throws IOException {
			// no op
		}
	}

	/**
	 * Information on request being processed
	 */
	public static class RequestInfo {
		private HttpUriRequest req;
		private String line;

		/**
		 * Collect information on a request.
		 * @param req The request to examine.
		 */
		public RequestInfo(HttpUriRequest req) {
			this.req = req;
			line = getMethod() + " " + getPath() + (req.getURI().getQuery() == null ? "": ("?" + req.getURI().getQuery()));
		}

		/**
		 * Get the method.
		 *
		 * @return The HTTP method of the request.
		 */
		public String getMethod() {
			return req.getMethod();
		}

		/**
		 * Get the method and URI
		 *
		 * @return The method and URI of the request, including any query string.
		 */
		public String getLine() {
			return line;
		}

		/**
		 * Get the path component of the URI
		 *
		 * @return The URI excluding the query string.
		 */
		public String getPath() {
			return req.getURI().getPath();
		}

		/**
		 * Check whether the named header is missing.
		 *
		 * @return true if the header is absent.
		 */
		public boolean lacksHeader(String name) {
			return req.getFirstHeader(name) == null;
		}

		/**
		 * Check whether the header has the specified header value.
		 *
		 * @return true if the header is missing or has the wrong value.
		 */
		public boolean lacksHeaderValue(String name, String value) {
			return req.getFirstHeader(name) == null || !value.equals(req.getFirstHeader(name).getValue());
		}
	}

	private static class Response {
		private Predicate<RequestInfo> matcher;
		private byte[] body;
		private Consumer<RequestInfo> action;
		private int code = 200;
		private String message = "OK";
		private ContentType contentType = ContentType.APPLICATION_JSON;
	}

	private static Predicate<RequestInfo> s2p(String line) {
		return r -> r.getLine().equals(line);
	}

	private ArrayList<Response> responses;
	private HttpClient client;

	/**
	 * Create a responder to handle requests.
	 */
	public ClientMocking() throws IOException {
		responses = new ArrayList<>();
		client = mock(HttpClient.class);
		when(client.execute(any(HttpUriRequest.class), any(HttpContext.class))).thenAnswer(this);
	}

	/**
	 * Redirect the template to this responder.
	 *
	 * @param template The RestTemplate to redirect.
	 * @return This responder.
	 */
	public ClientMocking applyTo(RestTemplate template) {
		template.setRequestFactory(new HttpComponentsClientHttpRequestFactory(client));
		return this;
	}

	/**
	 * Set the identified RestTemplate field to use this responder.
	 *
	 * @param object The object containing the RestTemplate.
	 * @param fieldName The name of the RestTemplate field in the object.
	 * @return This responder.
	 */
	public ClientMocking applyTo(Object object, String fieldName) {
		return applyTo((RestTemplate)ReflectionTestUtils.getField(object, fieldName));
	}

	/**
	 * Set the restTemplate field of an object to use this responder.
	 *
	 * Typically, the object will be a FederationClient or a GatewayClient.
	 * @param object The object with the restTemplate field.
	 * @return This responder.
	 */
	public ClientMocking applyTo(Object object) {
		return applyTo(object, "restTemplate");
	}

	@Override
	public HttpResponse answer(InvocationOnMock invocation) throws Throwable {
		RequestInfo info = new RequestInfo((HttpUriRequest)invocation.getArguments()[0]);
		for (Response r: responses) {
			if (r.matcher.test(info)) {
				if (r.action != null) {
					r.action.accept(info);
				}
				BasicCloseableHttpResponse ret = new BasicCloseableHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), r.code, r.message));
				if (r.body != null) {
					ret.setEntity(new ByteArrayEntity(r.body, r.contentType));
					ret.addHeader("Content-Length", String.valueOf(r.body.length));
					if (r.body.length != 0) {
						ret.addHeader("Content-Type", r.contentType.toString());
					}
				}
				if (log.isInfoEnabled()) {
					log.info("Mock client response to {} is {}", info.getLine(), (r.body == null? "null": new String(r.body)));
				}
				return ret;
			}
		}
		throw new IOException("Mock unhandled " + info.getLine());
	}

	/**
	 * Handle the specified requests.
	 *
	 * @param matcher A predicate for matching requests.
	 * @param body The response body to return.
	 * @param type The content type of the body.
	 * @param action An action to perform, whenever the request is handled.
	 * @return This responder.
	 */
	public ClientMocking on(Predicate<RequestInfo> matcher, byte[] body, ContentType type, Consumer<RequestInfo> action) {
		Response r = new Response();
		r.matcher = matcher;
		r.body = body;
		r.contentType = type;
		r.action = action;
		responses.add(r);
		return this;
	}

	/**
	 * Handle the specified request.
	 *
	 * @param line The HTTP method and URI to handle.
	 * @param body The response body to return.
	 * @param type The content type of the body.
	 * @param action An action to perform, whenever the request is handled.
	 * @return This responder.
	 */
	public ClientMocking on(String line, String body, ContentType type, Consumer<RequestInfo> action) {
		return on(s2p(line), body.getBytes(), type, action);
	}

	/**
	 * Handle the specified request.
	 *
	 * @param line The HTTP method and URI to handle.
	 * @param body The response body to return, as a JSON string.
	 * @param action An action to perform, whenever the request is handled.
	 * @return This responder.
	 */
	public ClientMocking on(String line, String body, Consumer<RequestInfo> action) {
		return on(line, body, ContentType.APPLICATION_JSON, action);
	}

	/**
	 * Handle the specified request.
	 *
	 * @param line The HTTP method and URI to handle.
	 * @param body The response body to return, as a JSON string.
	 * @return This responder.
	 */
	public ClientMocking on(String line, String body) {
		return on(line, body, ContentType.APPLICATION_JSON);
	}

	/**
	 * Handle the specified request.
	 *
	 * @param line The HTTP method and URI to handle.
	 * @param body The response body to return, as a JSON string.
	 * @param type The content type of the body.
	 * @return This responder.
	 */
	public ClientMocking on(String line, String body, ContentType type) {
		return on(line, body, type, null);
	}

	/**
	 * Fail the specified requests.
	 *
	 * @param matcher A predicate for matching requests.
	 * @param code The HTTP error code to return.
	 * @param message The HTTP error status message to return.
	 * @return This responder.
	 */
	public ClientMocking errorOn(Predicate<RequestInfo> matcher, int code, String message) {
		Response r = new Response();
		r.matcher = matcher;
		r.code = code;
		r.message = message;
		responses.add(r);
		return this;
	}

	/**
	 * Fail the specified request.
	 *
	 * @param line The HTTP method and URI to handle.
	 * @param code The HTTP error code to return.
	 * @param message The HTTP error status message to return.
	 * @return This responder.
	 */
	public ClientMocking errorOn(String line, int code, String message) {
		return errorOn(s2p(line), code, message);
	}

	/**
	 * Fail the request if no auth header
	 *
	 * @param code The HTTP error code to return.
	 * @param message The HTTP error status message to return.
	 * @return This responder.
	 */
	public ClientMocking errorOnNoAuth(int code, String message) {
		return errorOn(ri -> ri.lacksHeader("Authorization"), code, message);
	}

	/**
	 * Fail the request if wrong auth header
	 *
	 * @param username The expected user name.
	 * @param password The expected password.
	 * @param code The HTTP error code to return.
	 * @param message The HTTP error status message to return.
	 * @return This responder.
	 */
	public ClientMocking errorOnBadAuth(String username, String password, int code, String message) {
		String authhdr = "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
		return errorOn(ri -> ri.lacksHeaderValue("Authorization", authhdr), code, message);
	}
}