Skip to content

Commit d538fa6

Browse files
Avoid catching exceptions in the unit test of RequestLoggingFilter
Change-Id: I80cb92261066eb4f3c9ecadda25125a104f46720
1 parent dc82398 commit d538fa6

File tree

2 files changed

+182
-233
lines changed

2 files changed

+182
-233
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package com.sap.hcp.cf.logging.servlet.filter;
2+
3+
import static org.hamcrest.core.Is.is;
4+
import static org.hamcrest.core.IsNot.not;
5+
import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString;
6+
import static org.junit.Assert.assertThat;
7+
import static org.mockito.Mockito.mock;
8+
import static org.mockito.Mockito.when;
9+
10+
import java.io.BufferedReader;
11+
import java.io.ByteArrayOutputStream;
12+
import java.io.IOException;
13+
import java.io.PrintStream;
14+
import java.io.PrintWriter;
15+
16+
import javax.servlet.FilterChain;
17+
import javax.servlet.ServletException;
18+
import javax.servlet.ServletInputStream;
19+
import javax.servlet.ServletRequest;
20+
import javax.servlet.ServletResponse;
21+
import javax.servlet.http.HttpServletRequest;
22+
import javax.servlet.http.HttpServletResponse;
23+
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
import com.fasterxml.jackson.jr.ob.JSON;
29+
import com.fasterxml.jackson.jr.ob.JSONObjectException;
30+
import com.sap.hcp.cf.logging.common.Defaults;
31+
import com.sap.hcp.cf.logging.common.Fields;
32+
import com.sap.hcp.cf.logging.common.HttpHeaders;
33+
34+
public class RequestLoggingFilterTest {
35+
36+
protected final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
37+
protected final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
38+
private PrintStream previousOut;
39+
private PrintStream previousErr;
40+
41+
private static final String REQUEST_ID = "1234-56-7890-xxx";
42+
private static final String CORRELATION_ID = "xxx-56-7890-xxx";
43+
private static final String REQUEST = "/foobar";
44+
private static final String QUERY_STRING = "baz=bla";
45+
private static final String FULL_REQUEST = REQUEST + "?" + QUERY_STRING;
46+
private static final String REMOTE_HOST = "acme.org";
47+
private static final String REFERER = "my.fancy.com";
48+
49+
@Before
50+
public void setupStreams() {
51+
previousOut = System.out;
52+
System.setOut(new PrintStream(outContent));
53+
previousErr = System.err;
54+
System.setErr(new PrintStream(errContent));
55+
}
56+
57+
@After
58+
public void teardownStreams() {
59+
System.setOut(previousOut);
60+
System.setErr(previousErr);
61+
}
62+
63+
@Test
64+
public void testSimple() throws IOException, ServletException {
65+
HttpServletRequest mockReq = mock(HttpServletRequest.class);
66+
HttpServletResponse mockResp = mock(HttpServletResponse.class);
67+
PrintWriter mockWriter = mock(PrintWriter.class);
68+
when(mockResp.getWriter()).thenReturn(mockWriter);
69+
FilterChain mockFilterChain = mock(FilterChain.class);
70+
new RequestLoggingFilter().doFilter(mockReq, mockResp, mockFilterChain);
71+
assertThat(getField(Fields.REQUEST), is(Defaults.UNKNOWN));
72+
assertThat(getField(Fields.CORRELATION_ID), not(isEmptyOrNullString()));
73+
assertThat(getField(Fields.REQUEST_ID), is(Defaults.UNKNOWN));
74+
assertThat(getField(Fields.REMOTE_HOST), is(Defaults.UNKNOWN));
75+
assertThat(getField(Fields.COMPONENT_ID), is(Defaults.UNKNOWN));
76+
assertThat(getField(Fields.CONTAINER_ID), is(Defaults.UNKNOWN));
77+
assertThat(getField(Fields.REFERER), is(Defaults.UNKNOWN));
78+
assertThat(getField(Fields.REQUEST_SIZE_B), is("-1"));
79+
}
80+
81+
@Test
82+
public void testInputStream() throws IOException, ServletException {
83+
HttpServletRequest mockReq = mock(HttpServletRequest.class);
84+
HttpServletResponse mockResp = mock(HttpServletResponse.class);
85+
PrintWriter mockWriter = mock(PrintWriter.class);
86+
ServletInputStream mockStream = mock(ServletInputStream.class);
87+
88+
when(mockResp.getWriter()).thenReturn(mockWriter);
89+
when(mockReq.getInputStream()).thenReturn(mockStream);
90+
when(mockStream.read()).thenReturn(1);
91+
FilterChain mockFilterChain = new FilterChain() {
92+
@Override
93+
public void doFilter(ServletRequest request, ServletResponse response) throws IOException,
94+
ServletException {
95+
request.getInputStream().read();
96+
}
97+
};
98+
new RequestLoggingFilter().doFilter(mockReq, mockResp, mockFilterChain);
99+
assertThat(getField(Fields.REQUEST), is(Defaults.UNKNOWN));
100+
assertThat(getField(Fields.CORRELATION_ID), not(isEmptyOrNullString()));
101+
assertThat(getField(Fields.REQUEST_ID), is(Defaults.UNKNOWN));
102+
assertThat(getField(Fields.REMOTE_HOST), is(Defaults.UNKNOWN));
103+
assertThat(getField(Fields.COMPONENT_ID), is(Defaults.UNKNOWN));
104+
assertThat(getField(Fields.CONTAINER_ID), is(Defaults.UNKNOWN));
105+
assertThat(getField(Fields.REFERER), is(Defaults.UNKNOWN));
106+
assertThat(getField(Fields.REQUEST_SIZE_B), is("1"));
107+
}
108+
109+
@Test
110+
public void testReader() throws IOException, ServletException {
111+
HttpServletRequest mockReq = mock(HttpServletRequest.class);
112+
HttpServletResponse mockResp = mock(HttpServletResponse.class);
113+
PrintWriter mockWriter = mock(PrintWriter.class);
114+
BufferedReader mockReader = mock(BufferedReader.class);
115+
116+
when(mockResp.getWriter()).thenReturn(mockWriter);
117+
when(mockReq.getReader()).thenReturn(mockReader);
118+
when(mockReader.read()).thenReturn(1);
119+
FilterChain mockFilterChain = new FilterChain() {
120+
@Override
121+
public void doFilter(ServletRequest request, ServletResponse response) throws IOException,
122+
ServletException {
123+
request.getReader().read();
124+
}
125+
};
126+
new RequestLoggingFilter().doFilter(mockReq, mockResp, mockFilterChain);
127+
assertThat(getField(Fields.REQUEST), is(Defaults.UNKNOWN));
128+
assertThat(getField(Fields.CORRELATION_ID), not(isEmptyOrNullString()));
129+
assertThat(getField(Fields.REQUEST_ID), is(Defaults.UNKNOWN));
130+
assertThat(getField(Fields.REMOTE_HOST), is(Defaults.UNKNOWN));
131+
assertThat(getField(Fields.COMPONENT_ID), is(Defaults.UNKNOWN));
132+
assertThat(getField(Fields.CONTAINER_ID), is(Defaults.UNKNOWN));
133+
assertThat(getField(Fields.REFERER), is(Defaults.UNKNOWN));
134+
assertThat(getField(Fields.REQUEST_SIZE_B), is("1"));
135+
}
136+
137+
@Test
138+
public void testWithSettings() throws IOException, ServletException {
139+
HttpServletRequest mockReq = mock(HttpServletRequest.class);
140+
HttpServletResponse mockResp = mock(HttpServletResponse.class);
141+
PrintWriter mockWriter = mock(PrintWriter.class);
142+
when(mockResp.getWriter()).thenReturn(mockWriter);
143+
when(mockReq.getRequestURI()).thenReturn(REQUEST);
144+
when(mockReq.getQueryString()).thenReturn(QUERY_STRING);
145+
when(mockReq.getRemoteHost()).thenReturn(REMOTE_HOST);
146+
// will also set correlation id
147+
when(mockReq.getHeader(HttpHeaders.X_VCAP_REQUEST_ID)).thenReturn(REQUEST_ID);
148+
when(mockReq.getHeader(HttpHeaders.REFERER)).thenReturn(REFERER);
149+
FilterChain mockFilterChain = mock(FilterChain.class);
150+
new RequestLoggingFilter().doFilter(mockReq, mockResp, mockFilterChain);
151+
assertThat(getField(Fields.REQUEST), is(FULL_REQUEST));
152+
assertThat(getField(Fields.CORRELATION_ID), is(REQUEST_ID));
153+
assertThat(getField(Fields.REQUEST_ID), is(REQUEST_ID));
154+
assertThat(getField(Fields.REMOTE_HOST), is(REMOTE_HOST));
155+
assertThat(getField(Fields.COMPONENT_ID), is(Defaults.UNKNOWN));
156+
assertThat(getField(Fields.CONTAINER_ID), is(Defaults.UNKNOWN));
157+
assertThat(getField(Fields.REFERER), is(REFERER));
158+
}
159+
160+
@Test
161+
public void testExplicitCorrelationId() throws IOException, ServletException {
162+
HttpServletRequest mockReq = mock(HttpServletRequest.class);
163+
HttpServletResponse mockResp = mock(HttpServletResponse.class);
164+
PrintWriter mockWriter = mock(PrintWriter.class);
165+
when(mockResp.getWriter()).thenReturn(mockWriter);
166+
when(mockReq.getHeader(HttpHeaders.CORRELATION_ID)).thenReturn(CORRELATION_ID);
167+
when(mockReq.getHeader(HttpHeaders.X_VCAP_REQUEST_ID)).thenReturn(REQUEST_ID);
168+
FilterChain mockFilterChain = mock(FilterChain.class);
169+
new RequestLoggingFilter().doFilter(mockReq, mockResp, mockFilterChain);
170+
assertThat(getField(Fields.CORRELATION_ID), is(CORRELATION_ID));
171+
assertThat(getField(Fields.CORRELATION_ID), not(REQUEST_ID));
172+
}
173+
174+
protected String getField(String fieldName) throws JSONObjectException, IOException {
175+
return JSON.std.mapFrom(getLastLine()).get(fieldName).toString();
176+
}
177+
178+
private String getLastLine() {
179+
String[] lines = outContent.toString().split("\n");
180+
return lines[lines.length - 1];
181+
}
182+
}

0 commit comments

Comments
 (0)