summaryrefslogtreecommitdiffstats
path: root/openstack-client-connectors/jersey-connector/src/test/java/com/woorea/openstack/connector/JerseyLoggingFilterTest.java
blob: 3bd07174d1a15b6b8fa89f4f584fee15861c8c15 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2018 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 com.woorea.openstack.connector;

import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import javax.ws.rs.core.MultivaluedMap;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import com.sun.jersey.api.client.ClientHandler;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientRequestAdapter;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.filter.ClientFilter;

public class JerseyLoggingFilterTest {

	private static Logger logger;
	private static LogFormatter logFormatter;
	
	@BeforeClass
	public static void setUpClass() throws Exception {
		logger = Logger.getLogger(JerseyLoggingFilterTest.class.getSimpleName());
		logger.setLevel(Level.ALL);
		logger.setUseParentHandlers(false);

		ConsoleHandler handler = new ConsoleHandler();
		logFormatter = new LogFormatter();
		handler.setFormatter(logFormatter);
		handler.setLevel(Level.ALL);
		logger.addHandler(handler);
	}

	@Before
	public void setUpTest() {
		logFormatter.clearLog();
	}
	
	/**
	 * Tests a scenario with no request content (GET).
	 * @throws Exception for unexpected errors
	 */
	@Test
	public void testGET() throws Exception {
		String responseContent = "<response>Hello, I am Eliza.</response>";
		execute("GET", "http://www.onap.org/eliza", null, responseContent);
	}
	
	/**
	 * Tests a scenario with request content (POST).
	 * @throws Exception for unexpected errors
	 */
	@Test
	public void testPOST() throws Exception {
		String requestContent = "<request>I feel sad.</request>";
		String responseContent = "<response>Do you often feel sad?</response>";
		execute("POST", "http://www.onap.org/eliza", requestContent, responseContent);
	}

	/**
	 * Runs a single test.
	 * @param httpMethod any HTTP method (POST, GET, ...)
	 * @param url any URL
	 * @param requestContent mock request content, possibly null
	 * @param responseContent mock response content, never null
	 * @throws Exception for unexpected errors
	 */
	private void execute(String httpMethod, String url, String requestContent, String responseContent)
			throws Exception {
		JerseyLoggingFilter loggingFilter = new JerseyLoggingFilter(logger);

		// Mock multi-valued and single valued request headers

		HashMap<String, List<Object>> requestHeaderMap = new HashMap<>();
		requestHeaderMap.put("Accept", Arrays.asList(new Object[]{"application/xml","application/json"}));

		if (requestContent != null) {
			requestHeaderMap.put("Content-Type", Arrays.asList(new Object[]{"application/xml"}));
			requestHeaderMap.put("Content-Length", Arrays.asList(new Object[]{String.valueOf(requestContent.length())}));
		}

		@SuppressWarnings("unchecked")
		MultivaluedMap<String, Object> requestHeaders = mock(MultivaluedMap.class);
		when(requestHeaders.entrySet()).thenReturn(requestHeaderMap.entrySet());

		// Mock the request object

		ClientRequest request = mock(TestClientRequest.class);
		when(request.getURI()).thenReturn(new URI(url));
		when(request.getMethod()).thenReturn(httpMethod);
		when(request.getHeaders()).thenReturn(requestHeaders);

		if (requestContent != null) {
			when(request.getEntity()).thenReturn(requestContent.getBytes("UTF-8"));
		}

		doCallRealMethod().when(request).setAdapter(any(ClientRequestAdapter.class));
		when(request.getAdapter()).thenCallRealMethod();
		request.setAdapter(new DefaultClientRequestAdapter());

		// Mock multi-valued and single valued response headers

		HashMap<String, List<String>> responseHeaderMap = new HashMap<>();
		responseHeaderMap.put("Cache-Control", Arrays.asList(new String[]{"no-cache","no-store"}));
		responseHeaderMap.put("Content-Type", Arrays.asList(new String[]{"application/xml"}));
		responseHeaderMap.put("Content-Length", Arrays.asList(new String[]{String.valueOf(responseContent.length())}));
		@SuppressWarnings("unchecked")
		MultivaluedMap<String, String> responseHeaders = mock(MultivaluedMap.class);
		when(responseHeaders.entrySet()).thenReturn(responseHeaderMap.entrySet());

		// Mock the response object

		ClientResponse response = mock(ClientResponse.class);
		when(response.getStatus()).thenReturn(200);
		when(response.getHeaders()).thenReturn(responseHeaders);
		when(response.getEntityInputStream()).thenReturn(
				new ByteArrayInputStream(responseContent.getBytes("UTF-8")));

		// Mock a handler that returns the response object and set
		// it to be the next filter after the logging filter.

		ClientFilter handler = mock(ClientFilter.class);
		when(handler.handle(request)).then(produceResponse(response));
		Method setNext = ClientFilter.class.getDeclaredMethod("setNext", new Class<?>[]{ClientHandler.class});
		setNext.setAccessible(true);
		setNext.invoke(loggingFilter, new Object[]{handler});

		// Run the request into the logging filter

		loggingFilter.handle(request);

		// Validate resulting the log content

		String log = logFormatter.getLog();

		assertContains(log, "* Client out-bound request");
		assertContains(log, "> " + httpMethod + " " + url);

		for (String header : requestHeaderMap.keySet()) {
			assertContains(log, "> " + header + ": ");
		}

		if (requestContent != null) {
			assertContains(log, requestContent);
		}

		assertContains(log, "* Client in-bound response");
		assertContains(log, "< 200");

		for (String header : responseHeaderMap.keySet()) {
			assertContains(log, "< " + header + ": ");
		}

		assertContains(log, responseContent);
	}
	
	private void assertContains(String log, String expect) {
		assertTrue("Log does not contain '" + expect + "'", log.contains(expect));
	}

	private class DefaultClientRequestAdapter implements ClientRequestAdapter {
		@Override
		public OutputStream adapt(ClientRequest request, OutputStream out) throws IOException {
			return out;
		}
	}

	private abstract class TestClientRequest extends ClientRequest {
		private ClientRequestAdapter adapter;

		@Override
		public ClientRequestAdapter getAdapter() {
			return adapter;
		}

		@Override
		public void setAdapter(ClientRequestAdapter adapter) {
			this.adapter = adapter;
		}
	}
	
	private Answer<ClientResponse> produceResponse(final ClientResponse response) { 
		return new Answer<ClientResponse>() {
			public ClientResponse answer(InvocationOnMock invocation) throws IOException {
				ClientRequest request = (ClientRequest) invocation.getArguments()[0];
				byte[] entity = (byte[]) request.getEntity();

				if (entity != null) {
					ClientRequestAdapter adapter = request.getAdapter();
	
					OutputStream nullOutputStream = new OutputStream() {
						@Override
						public void write(int b) {
							// Discard
						}
					};

					OutputStream outputStream = adapter.adapt(request, nullOutputStream);
					outputStream.write(entity);
					outputStream.close();
				}

				return response;
			}
		};
	}

	private static class LogFormatter extends SimpleFormatter {
		StringBuilder buffer = new StringBuilder();

		public synchronized String getLog() {
			return buffer.toString();
		}

		public synchronized void clearLog() {
			buffer.setLength(0);
		}

		@Override
		public synchronized String format(LogRecord record) {
			String logData = super.format(record);
			buffer.append(logData);
			return logData;
		}
	}
}