Skip to content

Commit 6358070

Browse files
author
Alex Wenckus
committed
Tests for building appenders
1 parent d03ae09 commit 6358070

File tree

5 files changed

+162
-4
lines changed

5 files changed

+162
-4
lines changed

pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939

4040
<java.version>1.8</java.version>
4141

42+
<awaitility.version>2.0.0</awaitility.version>
4243
<dropwizard.version>1.0.2</dropwizard.version>
43-
<immutables.version>2.3.9</immutables.version>
4444
<logstash-logback-encoder.version>4.8</logstash-logback-encoder.version>
4545
</properties>
4646

@@ -95,5 +95,17 @@
9595
<artifactId>logstash-logback-encoder</artifactId>
9696
<version>${logstash-logback-encoder.version}</version>
9797
</dependency>
98+
99+
<dependency>
100+
<groupId>io.dropwizard</groupId>
101+
<artifactId>dropwizard-testing</artifactId>
102+
<scope>test</scope>
103+
</dependency>
104+
<dependency>
105+
<groupId>org.awaitility</groupId>
106+
<artifactId>awaitility</artifactId>
107+
<version>${awaitility.version}</version>
108+
<scope>test</scope>
109+
</dependency>
98110
</dependencies>
99111
</project>

src/main/java/io/dropwizard/bundles/jsonlog/JsonRequestLogFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
import net.logstash.logback.encoder.LogstashAccessEncoder;
2121
import net.logstash.logback.fieldnames.LogstashAccessFieldNames;
2222
import org.eclipse.jetty.server.RequestLog;
23-
import org.hibernate.validator.constraints.NotEmpty;
23+
import org.hibernate.validator.constraints.NotBlank;
2424
import org.slf4j.LoggerFactory;
2525

2626
/**
2727
* Creates a Logback access request logger which will log access requests as JSON messages.
2828
*/
2929
@SuppressWarnings("WeakerAccess")
3030
@JsonTypeName("json-logback-access")
31-
public abstract class JsonRequestLogFactory
31+
public class JsonRequestLogFactory
3232
extends AbstractAppenderFactory<IAccessEvent>
3333
implements RequestLogFactory<RequestLog> {
3434

@@ -111,7 +111,7 @@ public static class Fields {
111111
private String statusCode = "response";
112112
private String hostname = null;
113113
private String requestedUrl = null;
114-
@NotEmpty
114+
@NotBlank
115115
private String message = "message";
116116

117117
@JsonProperty
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.dropwizard.bundles.jsonlog;
2+
3+
import ch.qos.logback.classic.spi.ILoggingEvent;
4+
import ch.qos.logback.core.Appender;
5+
import io.dropwizard.logging.LoggingUtil;
6+
import io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory;
7+
import io.dropwizard.logging.filter.ThresholdLevelFilterFactory;
8+
import io.dropwizard.logging.layout.DropwizardLayoutFactory;
9+
import org.junit.Test;
10+
11+
import static org.junit.Assert.assertNotNull;
12+
13+
/**
14+
*
15+
*/
16+
public class JsonConsoleAppenderFactoryTest {
17+
18+
@Test
19+
public void build() throws Exception {
20+
final JsonConsoleAppenderFactory factory = new JsonConsoleAppenderFactory();
21+
22+
final Appender<ILoggingEvent> appender = factory.build(LoggingUtil.getLoggerContext(), "app",
23+
new DropwizardLayoutFactory(), new ThresholdLevelFilterFactory(),
24+
new AsyncLoggingEventAppenderFactory());
25+
assertNotNull(appender);
26+
}
27+
28+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package io.dropwizard.bundles.jsonlog;
2+
3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.google.common.collect.ImmutableList;
6+
import io.dropwizard.bundles.jsonlog.test.ConsoleInterceptor;
7+
import io.dropwizard.jackson.Jackson;
8+
import java.util.Collections;
9+
import java.util.Map;
10+
import java.util.Optional;
11+
import java.util.concurrent.TimeUnit;
12+
import org.eclipse.jetty.http.HttpFields;
13+
import org.eclipse.jetty.server.Request;
14+
import org.eclipse.jetty.server.RequestLog;
15+
import org.eclipse.jetty.server.Response;
16+
import org.junit.Rule;
17+
import org.junit.Test;
18+
19+
import static org.awaitility.Awaitility.await;
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertTrue;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.when;
25+
26+
/**
27+
*
28+
*/
29+
public class JsonRequestLogFactoryTest {
30+
31+
private static final ObjectMapper OBJECT_MAPPER = Jackson.newObjectMapper();
32+
33+
@Rule
34+
public ConsoleInterceptor interceptor = new ConsoleInterceptor();
35+
36+
@Test
37+
public void build() throws Exception {
38+
final JsonRequestLogFactory logFactory = new JsonRequestLogFactory();
39+
40+
final RequestLog requestLog = logFactory.build("test");
41+
assertNotNull(requestLog);
42+
43+
final String expectedVerb = "GET";
44+
final String requestUri = "/test";
45+
final String expectedProtocol = "HTTP/1.1";
46+
final int expectedStatus = 419;
47+
48+
final Request request = mock(Request.class);
49+
when(request.getRequestURI()).thenReturn(requestUri);
50+
when(request.getMethod()).thenReturn(expectedVerb);
51+
when(request.getProtocol()).thenReturn(expectedProtocol);
52+
when(request.getAttributeNames()).thenReturn(Collections.enumeration(ImmutableList.of()));
53+
final Response response = mock(Response.class);
54+
when(response.getHttpFields()).thenReturn(new HttpFields());
55+
when(response.getStatus()).thenReturn(expectedStatus);
56+
57+
requestLog.log(request, response);
58+
59+
await().atMost(5, TimeUnit.SECONDS).until(interceptor.contains(s -> s.contains(requestUri)));
60+
61+
final Optional<String> opt = interceptor.getLogs().stream()
62+
.filter(s -> s.contains(requestUri)).findFirst();
63+
64+
assertTrue(opt.isPresent());
65+
final Map<String, String> accessMap = OBJECT_MAPPER.readValue(opt.get(),
66+
new TypeReference<Map<String, String>>() {});
67+
68+
assertEquals(expectedVerb, accessMap.get("verb"));
69+
assertEquals(requestUri, accessMap.get("request"));
70+
assertEquals(expectedProtocol, accessMap.get("protocol"));
71+
assertEquals(expectedStatus, Integer.parseInt(accessMap.get("response")));
72+
}
73+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.dropwizard.bundles.jsonlog.test;
2+
3+
import java.io.IOException;
4+
import java.io.PrintStream;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.concurrent.Callable;
8+
import java.util.concurrent.CopyOnWriteArrayList;
9+
import java.util.function.Predicate;
10+
import org.junit.rules.ExternalResource;
11+
12+
public class ConsoleInterceptor extends ExternalResource {
13+
private List<String> logs = new CopyOnWriteArrayList<>();
14+
15+
@Override
16+
protected void before() throws Throwable {
17+
System.setOut(new PrintStream(System.out) {
18+
@Override
19+
public void write(byte[] b) throws IOException {
20+
logs.add(new String(b));
21+
super.write(b);
22+
}
23+
24+
@Override
25+
public void write(byte[] buf, int off, int len) {
26+
logs.add(new String(buf, off, len));
27+
28+
super.write(buf, off, len);
29+
}
30+
});
31+
}
32+
33+
public Callable<Boolean> contains(Predicate<String> test) {
34+
return () -> logs.stream().anyMatch(test);
35+
}
36+
37+
public List<String> getLogs() {
38+
return Collections.unmodifiableList(logs);
39+
}
40+
41+
@Override
42+
protected void after() {
43+
System.setOut(null);
44+
}
45+
}

0 commit comments

Comments
 (0)