|
| 1 | +package com.sap.hcp.cf.logging.servlet.filter; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.equalTo; |
| 4 | +import static org.hamcrest.Matchers.hasEntry; |
| 5 | +import static org.hamcrest.Matchers.is; |
| 6 | +import static org.junit.Assert.assertNull; |
| 7 | +import static org.junit.Assert.assertThat; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.EnumSet; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.UUID; |
| 13 | + |
| 14 | +import javax.servlet.DispatcherType; |
| 15 | +import javax.servlet.ServletException; |
| 16 | +import javax.servlet.http.HttpServlet; |
| 17 | +import javax.servlet.http.HttpServletRequest; |
| 18 | +import javax.servlet.http.HttpServletResponse; |
| 19 | + |
| 20 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 21 | +import org.apache.http.client.methods.HttpGet; |
| 22 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 23 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 24 | +import org.eclipse.jetty.server.Server; |
| 25 | +import org.eclipse.jetty.server.ServerConnector; |
| 26 | +import org.eclipse.jetty.servlet.ServletContextHandler; |
| 27 | +import org.junit.After; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.Rule; |
| 30 | +import org.junit.Test; |
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.slf4j.LoggerFactory; |
| 33 | + |
| 34 | +import com.sap.hcp.cf.logging.common.Fields; |
| 35 | +import com.sap.hcp.cf.logging.common.LogContext; |
| 36 | +import com.sap.hcp.cf.logging.common.request.HttpHeader; |
| 37 | +import com.sap.hcp.cf.logging.common.request.HttpHeaders; |
| 38 | + |
| 39 | +public class RequestLogTest { |
| 40 | + |
| 41 | + private static final Logger LOG = LoggerFactory.getLogger(RequestLogTest.class); |
| 42 | + private static final String REQUEST_RECEIVED = "Request received"; |
| 43 | + |
| 44 | + @Rule |
| 45 | + public SystemOutRule systemOut = new SystemOutRule(); |
| 46 | + |
| 47 | + private Server server; |
| 48 | + private CloseableHttpClient client; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setUp() throws Exception { |
| 52 | + this.server = initJetty(); |
| 53 | + this.client = HttpClientBuilder.create().build(); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + private Server initJetty() throws Exception { |
| 58 | + Server server = new Server(0); |
| 59 | + ServletContextHandler handler = new ServletContextHandler(server, null); |
| 60 | + handler.addFilter(RequestLoggingFilter.class, "/*", EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST, |
| 61 | + DispatcherType.ERROR, DispatcherType.FORWARD, DispatcherType.ASYNC)); |
| 62 | + handler.addServlet(TestServlet.class, "/test"); |
| 63 | + server.start(); |
| 64 | + return server; |
| 65 | + } |
| 66 | + |
| 67 | + @After |
| 68 | + public void tearDown() throws Exception { |
| 69 | + client.close(); |
| 70 | + server.stop(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void logsCorrelationIdFromRequestHeader() throws Exception { |
| 75 | + String correlationId = UUID.randomUUID().toString(); |
| 76 | + HttpGet get = new HttpGet(getBaseUrl() + "/test"); |
| 77 | + get.setHeader(HttpHeaders.CORRELATION_ID.getName(), correlationId); |
| 78 | + CloseableHttpResponse response = null; |
| 79 | + try { |
| 80 | + response = client.execute(get); |
| 81 | + |
| 82 | + assertNull("No correlation_id should be generated.", getCorrelationIdGenerated()); |
| 83 | + |
| 84 | + assertThat("Application log without correlation id.", getRequestMessage(), |
| 85 | + hasEntry(Fields.CORRELATION_ID, correlationId)); |
| 86 | + assertThat("Request log without correlation id.", getRequestLog(), |
| 87 | + hasEntry(Fields.CORRELATION_ID, correlationId)); |
| 88 | + } finally { |
| 89 | + if (response != null) { |
| 90 | + response.close(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void logsGeneratedCorrelationId() throws Exception { |
| 97 | + HttpGet get = new HttpGet(getBaseUrl() + "/test"); |
| 98 | + CloseableHttpResponse response = null; |
| 99 | + try { |
| 100 | + response = client.execute(get); |
| 101 | + |
| 102 | + String correlationId = getCorrelationIdGenerated(); |
| 103 | + |
| 104 | + assertThat("Application log without correlation id.", getRequestMessage(), |
| 105 | + hasEntry(Fields.CORRELATION_ID, correlationId)); |
| 106 | + assertThat("Request log without correlation id.", getRequestLog(), |
| 107 | + hasEntry(Fields.CORRELATION_ID, correlationId)); |
| 108 | + } finally { |
| 109 | + if (response != null) { |
| 110 | + response.close(); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void logsRequestIdFromRequestHeader() throws Exception { |
| 117 | + String requestId = UUID.randomUUID().toString(); |
| 118 | + HttpGet get = new HttpGet(getBaseUrl() + "/test"); |
| 119 | + get.setHeader(HttpHeaders.X_VCAP_REQUEST_ID.getName(), requestId); |
| 120 | + CloseableHttpResponse response = null; |
| 121 | + try { |
| 122 | + response = client.execute(get); |
| 123 | + |
| 124 | + assertThat("Application log without request id.", getRequestMessage(), |
| 125 | + hasEntry(Fields.REQUEST_ID, requestId)); |
| 126 | + assertThat("Request log without request id.", getRequestLog(), |
| 127 | + hasEntry(Fields.REQUEST_ID, requestId)); |
| 128 | + } finally { |
| 129 | + if (response != null) { |
| 130 | + response.close(); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void logsTenantIdFromRequestHeader() throws Exception { |
| 137 | + String tenantId = UUID.randomUUID().toString(); |
| 138 | + HttpGet get = new HttpGet(getBaseUrl() + "/test"); |
| 139 | + get.setHeader(HttpHeaders.TENANT_ID.getName(), tenantId); |
| 140 | + CloseableHttpResponse response = null; |
| 141 | + try { |
| 142 | + response = client.execute(get); |
| 143 | + |
| 144 | + assertThat("Application log without tenant id.", getRequestMessage(), |
| 145 | + hasEntry(Fields.TENANT_ID, tenantId)); |
| 146 | + assertThat("Request log without tenant id.", getRequestLog(), hasEntry(Fields.TENANT_ID, tenantId)); |
| 147 | + } finally { |
| 148 | + if (response != null) { |
| 149 | + response.close(); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void writesCorrelationIdFromHeadersAsResponseHeader() throws Exception { |
| 156 | + String correlationId = UUID.randomUUID().toString(); |
| 157 | + HttpGet get = new HttpGet(getBaseUrl() + "/test"); |
| 158 | + get.setHeader(HttpHeaders.CORRELATION_ID.getName(), correlationId); |
| 159 | + CloseableHttpResponse response = null; |
| 160 | + try { |
| 161 | + response = client.execute(get); |
| 162 | + assertFirstHeaderValue(correlationId, response, HttpHeaders.CORRELATION_ID); |
| 163 | + } finally { |
| 164 | + if (response != null) { |
| 165 | + response.close(); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void writesGeneratedCorrelationIdAsResponseHeader() throws Exception { |
| 172 | + HttpGet get = new HttpGet(getBaseUrl() + "/test"); |
| 173 | + CloseableHttpResponse response = null; |
| 174 | + try { |
| 175 | + response = client.execute(get); |
| 176 | + |
| 177 | + assertFirstHeaderValue(getCorrelationIdGenerated(), response, HttpHeaders.CORRELATION_ID); |
| 178 | + } finally { |
| 179 | + if (response != null) { |
| 180 | + response.close(); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + |
| 186 | + private String getBaseUrl() { |
| 187 | + int port = ((ServerConnector) server.getConnectors()[0]).getLocalPort(); |
| 188 | + return "http://localhost:" + port; |
| 189 | + } |
| 190 | + |
| 191 | + private String getCorrelationIdGenerated() throws IOException { |
| 192 | + Map<Object, Object> generationLog = systemOut.fineLineAsMapWith("logger", LogContext.class.getName()); |
| 193 | + if (generationLog == null) { |
| 194 | + return null; |
| 195 | + } |
| 196 | + return generationLog.get(Fields.CORRELATION_ID) == null ? null |
| 197 | + : generationLog.get(Fields.CORRELATION_ID).toString(); |
| 198 | + } |
| 199 | + |
| 200 | + private Map<Object, Object> getRequestMessage() throws IOException { |
| 201 | + return systemOut.fineLineAsMapWith("msg", REQUEST_RECEIVED); |
| 202 | + } |
| 203 | + |
| 204 | + private Map<Object, Object> getRequestLog() throws IOException { |
| 205 | + return systemOut.fineLineAsMapWith("layer", "[SERVLET]"); |
| 206 | + } |
| 207 | + |
| 208 | + private static void assertFirstHeaderValue(String expected, CloseableHttpResponse response, HttpHeader header) { |
| 209 | + String headerValue = response.getFirstHeader(header.getName()).getValue(); |
| 210 | + assertThat(headerValue, is(equalTo(expected))); |
| 211 | + } |
| 212 | + |
| 213 | + @SuppressWarnings("serial") |
| 214 | + public static class TestServlet extends HttpServlet { |
| 215 | + |
| 216 | + @Override |
| 217 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 218 | + LOG.info(REQUEST_RECEIVED); |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments