forked from eclipse-ee4j/jersey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggingFeatureTest.java
508 lines (423 loc) · 20.8 KB
/
LoggingFeatureTest.java
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
* Copyright (c) 2013, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.tests.e2e.common;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.client.ClientResponseFilter;
import javax.ws.rs.client.Entity;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.annotation.Priority;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThan;
/**
* {@link LoggingFeature} end-to-end tests.
*
* @author Michal Gajdos
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
LoggingFeatureTest.ClientTest.class,
LoggingFeatureTest.ContainerTest.class,
LoggingFeatureTest.ContainerAutodiscoveryTest.class,
LoggingFeatureTest.FiltersOrderTest.class
})
public class LoggingFeatureTest {
private static final String LOGGER_NAME = "org.glassfish.jersey.logging.feature";
private static final String BINARY_MEDIA_TYPE = "application/binary";
private static final String TEXT_MEDIA_TYPE = MediaType.TEXT_PLAIN;
private static final String ENTITY = "This entity must (not) be logged";
private static final String SEPARATOR = "!-------!";
@Path("/")
public static class MyResource {
@GET
@Produces(BINARY_MEDIA_TYPE)
public Response getHeadersAndBinaryPayload() {
return Response
.ok(ENTITY)
.header("001", "First Header Value")
.header("002", "Second Header Value")
.header("003", "Third Header Value")
.header("004", "Fourth Header Value")
.header("005", "Fifth Header Value")
.build();
}
@Path("/text")
@GET
@Produces(TEXT_MEDIA_TYPE)
public Response getHeadersAndTextPayload() {
return Response
.ok(ENTITY)
.header("001", "First Header Value")
.header("002", "Second Header Value")
.header("003", "Third Header Value")
.header("004", "Fourth Header Value")
.header("005", "Fifth Header Value")
.build();
}
@Path("/text")
@POST
@Produces(TEXT_MEDIA_TYPE)
public Response post(String text) {
return Response
.ok(ENTITY)
.build();
}
}
/**
* General client side tests.
*/
public static class ClientTest extends JerseyTest {
@Override
protected Application configure() {
set(TestProperties.RECORD_LOG_LEVEL, Level.FINE.intValue());
return new ResourceConfig(MyResource.class);
}
@Test
public void testFilterAsClientRequestFilter() throws Exception {
final Response response = target()
.register(new LoggingFeature(Logger.getLogger(LOGGER_NAME)))
.request()
.get();
// Correct response status.
assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for proper id.
assertThat(getLoggingFilterRequestLogRecord(getLoggedRecords()).getMessage(), containsString("1 *"));
}
@Test
public void testOrderOfHeadersOnClient() throws Exception {
final Response response = target()
.register(new LoggingFeature(Logger.getLogger(LOGGER_NAME)))
.request()
.get();
assertThat(response.readEntity(String.class), equalTo(ENTITY));
final LogRecord record = getLoggingFilterResponseLogRecord(getLoggedRecords());
final String message = record.getMessage();
int i = 1;
do {
final String h1 = "00" + i++;
final String h2 = "00" + i;
final int i1 = message.indexOf(h1);
final int i2 = message.indexOf(h2);
assertThat("Header " + h1 + " has been logged sooner than header " + h2, i1, lessThan(i2));
} while (i < 5);
}
@Test
public void testVerbosityAnyPayload() throws Exception {
final Response response = target()
.register(new LoggingFeature(Logger.getLogger(LOGGER_NAME), LoggingFeature.Verbosity.PAYLOAD_ANY))
.request()
.get();
// Correct response status.
assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for proper id.
assertThat(getLoggingFilterLogRecord(getLoggedRecords()).get(1).getMessage(), containsString(ENTITY));
}
@Test
public void testVerbosityAnyPayloadSetVerbosityAsText() throws Exception {
final Response response = target()
.register(new LoggingFeature(Logger.getLogger(LOGGER_NAME)))
.property(LoggingFeature.LOGGING_FEATURE_VERBOSITY, "PAYLOAD_ANY")
.request()
.get();
// Correct response status.
assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for proper id.
assertThat(getLoggingFilterLogRecord(getLoggedRecords()).get(1).getMessage(), containsString(ENTITY));
}
@Test
public void testVerbosityTextPayloadBinaryFiltered() throws Exception {
final Response response = target()
.register(new LoggingFeature(Logger.getLogger(LOGGER_NAME), LoggingFeature.Verbosity.PAYLOAD_TEXT))
.request()
.get();
// Correct response status.
assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for proper id.
assertThat(getLoggingFilterLogRecord(getLoggedRecords()).get(1).getMessage(), not(containsString(ENTITY)));
}
@Test
public void testVerbosityTextPayload() throws Exception {
final Response response = target("/text")
.register(new LoggingFeature(Logger.getLogger(LOGGER_NAME), LoggingFeature.Verbosity.PAYLOAD_TEXT))
.request()
.get();
// Correct response status.
assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for proper id.
assertThat(getLoggingFilterLogRecord(getLoggedRecords()).get(1).getMessage(), containsString(ENTITY));
}
@Test
public void testVerbosityHeadersPayload() throws Exception {
final Response response = target()
.register(new LoggingFeature(Logger.getLogger(LOGGER_NAME), LoggingFeature.Verbosity.HEADERS_ONLY))
.request()
.get();
// Correct response status.
assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for proper id.
assertThat(getLoggingFilterLogRecord(getLoggedRecords()).get(1).getMessage(), not(containsString(ENTITY)));
}
@Test
public void testPostedEntityLogged() throws Exception {
final Response response = target("/text")
.register(new LoggingFeature(Logger.getLogger(LOGGER_NAME), LoggingFeature.Verbosity.PAYLOAD_TEXT))
.request()
.post(Entity.text(ENTITY));
// Correct response status.
assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for proper id.
assertThat(getLoggingFilterLogRecord(getLoggedRecords()).get(0).getMessage(), containsString(ENTITY));
}
@Test
public void testLoggingFeatureBuilderSeparator() {
final Response response = target("/text")
.register(LoggingFeature
.builder()
.withLogger(Logger.getLogger(LOGGER_NAME))
.verbosity(LoggingFeature.Verbosity.PAYLOAD_ANY)
.separator(SEPARATOR)
.build()
).request()
.post(Entity.text(ENTITY));
// Correct response status.
assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for proper separator.
final LogRecord record = getLoggingFilterResponseLogRecord(getLoggedRecords());
assertThat(record.getMessage(), containsString(SEPARATOR));
}
@Test
public void testLoggingFeatureBuilderProperty() {
final Response response = target("/text")
.register(LoggingFeature
.builder()
.withLogger(Logger.getLogger(LOGGER_NAME))
.build()
).property(LoggingFeature.LOGGING_FEATURE_SEPARATOR, SEPARATOR)
.request()
.post(Entity.text(ENTITY));
// Correct response status.
assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for proper separator.
final LogRecord record = getLoggingFilterResponseLogRecord(getLoggedRecords());
assertThat(record.getMessage(), containsString(SEPARATOR));
}
@Test
public void testLoggingFeatureMaxEntitySize() {
final Response response = target("/text")
.register(LoggingFeature.class)
.property(LoggingFeature.LOGGING_FEATURE_LOGGER_NAME, LOGGER_NAME)
.property(LoggingFeature.LOGGING_FEATURE_MAX_ENTITY_SIZE, 1)
.request()
.post(Entity.text(ENTITY));
// Correct response status.
assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for trimmedEntity.
String trimmedEntity = ENTITY.charAt(0) + "...more...";
List<LogRecord> logRecords = getLoggedRecords();
assertThat(getLoggingFilterRequestLogRecord(logRecords).getMessage(), containsString(trimmedEntity));
assertThat(getLoggingFilterResponseLogRecord(logRecords).getMessage(), containsString(trimmedEntity));
}
}
/**
* General client side tests.
*/
public static class ContainerTest extends JerseyTest {
@Override
protected Application configure() {
set(TestProperties.RECORD_LOG_LEVEL, Level.FINE.intValue());
return new ResourceConfig(MyResource.class)
.register(LoggingFeature.class)
.property(LoggingFeature.LOGGING_FEATURE_LOGGER_NAME_SERVER, LOGGER_NAME);
}
@Test
public void testLoggingAsContainer() throws Exception {
// Correct response status.
assertThat(target().request().get().getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for proper id.
assertThat(getLoggingFilterRequestLogRecord(getLoggedRecords()).getMessage(), containsString("1 *"));
assertThat(getLoggingFilterResponseLogRecord(getLoggedRecords()).getMessage(), containsString("1 *"));
}
@Test
public void testLoggingAsContainerTextPayload() throws Exception {
// Correct response status.
assertThat(target("/text").request().get().getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for proper id.
assertThat(getLoggingFilterRequestLogRecord(getLoggedRecords()).getMessage(), containsString("1 *"));
assertThat(getLoggingFilterResponseLogRecord(getLoggedRecords()).getMessage(), containsString("1 *"));
assertThat(getLoggingFilterLogRecord(getLoggedRecords()).get(1).getMessage(), containsString(ENTITY));
}
@Test
public void testLoggingAsContainerBinaryPayload() throws Exception {
// Correct response status.
assertThat(target().request().get().getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for proper id.
assertThat(getLoggingFilterRequestLogRecord(getLoggedRecords()).getMessage(), containsString("1 *"));
assertThat(getLoggingFilterResponseLogRecord(getLoggedRecords()).getMessage(), containsString("1 *"));
assertThat(getLoggingFilterLogRecord(getLoggedRecords()).get(1).getMessage(), not(containsString(ENTITY)));
}
@Test
public void testPostedEntityLogged() throws Exception {
final Response response = target("/text")
.request()
.post(Entity.text(ENTITY));
// Correct response status.
assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for proper id.
assertThat(getLoggingFilterLogRecord(getLoggedRecords()).get(0).getMessage(), containsString(ENTITY));
}
}
/**
* General client side tests.
*/
public static class ContainerAutodiscoveryTest extends JerseyTest {
@Override
protected Application configure() {
set(TestProperties.RECORD_LOG_LEVEL, Level.INFO.intValue());
return new ResourceConfig(MyResource.class)
.property(LoggingFeature.LOGGING_FEATURE_LOGGER_NAME_SERVER, LOGGER_NAME)
.property(LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL_SERVER, "INFO");
}
@Test
public void testFilterAsContainerFilter() throws Exception {
// Correct response status.
assertThat(target().request().get().getStatus(), is(Response.Status.OK.getStatusCode()));
// Check logs for proper id.
assertThat(getLoggingFilterRequestLogRecord(getLoggedRecords()).getMessage(), containsString("1 *"));
assertThat(getLoggingFilterResponseLogRecord(getLoggedRecords()).getMessage(), containsString("1 *"));
}
}
private static LogRecord getLoggingFilterRequestLogRecord(final List<LogRecord> records) {
return getLoggingFilterLogRecord(records, true);
}
private static LogRecord getLoggingFilterResponseLogRecord(final List<LogRecord> records) {
return getLoggingFilterLogRecord(records, false);
}
private static LogRecord getLoggingFilterLogRecord(final List<LogRecord> records, final boolean requestQuery) {
for (final LogRecord record : getLoggingFilterLogRecord(records)) {
if (record.getMessage().contains(requestQuery ? "request" : "response")) {
return record;
}
}
throw new AssertionError("Unable to find proper log record.");
}
private static List<LogRecord> getLoggingFilterLogRecord(final List<LogRecord> records) {
final List<LogRecord> loggingFilterRecords = new ArrayList<>(records.size());
for (final LogRecord record : records) {
if (record.getLoggerName().startsWith(LOGGER_NAME)) {
loggingFilterRecords.add(record);
}
}
return loggingFilterRecords;
}
public static class FiltersOrderTest extends JerseyTest {
@Priority(1000)
private static class CustomFilter implements ClientRequestFilter, ClientResponseFilter,
ContainerRequestFilter, ContainerResponseFilter {
static final String CUSTOM_HEADER = "custom_header";
@Override
public void filter(final ClientRequestContext requestContext) throws IOException {
requestContext.getHeaders().add(CUSTOM_HEADER, "client/request");
}
@Override
public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext)
throws IOException {
responseContext.getHeaders().add(CUSTOM_HEADER, "client/response");
}
@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
requestContext.getHeaders().add(CUSTOM_HEADER, "container/request");
}
@Override
public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext)
throws IOException {
responseContext.getHeaders().add(CUSTOM_HEADER, "container/response");
}
}
@Override
protected Application configure() {
set(TestProperties.RECORD_LOG_LEVEL, Level.INFO.intValue());
return new ResourceConfig(MyResource.class)
.property(LoggingFeature.LOGGING_FEATURE_LOGGER_NAME, LOGGER_NAME)
.property(LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL, "INFO")
.property(ServerProperties.WADL_FEATURE_DISABLE, true)
.register(CustomFilter.class);
}
@Test
public void testFilterAsContainerFilter() throws Exception {
// Correct response status.
assertThat(target()
.register(CustomFilter.class)
.register(new LoggingFeature(Logger.getLogger(LOGGER_NAME),
Level.INFO,
LoggingFeature.Verbosity.HEADERS_ONLY,
0))
.request().get().getStatus(), is(Response.Status.OK.getStatusCode()));
for (LogRecord record : getLoggedRecords()) {
System.out.println(record.getMessage());
}
// --- client request log entry
// client added header before request has sent (and logged)
assertThat(getLoggedRecords().get(0).getMessage(),
containsString("1 > custom_header: client/request\n"));
// --- container request log entry
// container receives header from client request
assertThat(getLoggedRecords().get(1).getMessage(),
containsString("1 > custom_header: client/request\n"));
// container has added its own header after logging filter logged message
assertThat(getLoggedRecords().get(1).getMessage(),
not(containsString("1 > custom_header: container/request\n")));
// --- container response log entry
// container added header to the response and it was logged
assertThat(getLoggedRecords().get(2).getMessage(),
containsString("1 < custom_header: container/response\n"));
// --- client response log entry
// client received header
assertThat(getLoggedRecords().get(3).getMessage(),
containsString("1 < custom_header: container/response\n"));
assertThat(getLoggedRecords().get(3).getMessage(),
not(containsString("1 < custom_header: client/response\n")));
}
}
}