|
1 | 1 | package com.sap.hcp.cf.logging.servlet.filter;
|
2 | 2 |
|
| 3 | +import static org.hamcrest.Matchers.empty; |
3 | 4 | import static org.hamcrest.Matchers.equalTo;
|
4 | 5 | import static org.hamcrest.Matchers.hasEntry;
|
5 | 6 | import static org.hamcrest.Matchers.is;
|
|
36 | 37 | import com.sap.hcp.cf.logging.common.request.HttpHeader;
|
37 | 38 | import com.sap.hcp.cf.logging.common.request.HttpHeaders;
|
38 | 39 |
|
| 40 | +import ch.qos.logback.classic.Level; |
| 41 | +import ch.qos.logback.classic.LoggerContext; |
| 42 | + |
39 | 43 | public class RequestLogTest {
|
40 | 44 |
|
41 | 45 | private static final Logger LOG = LoggerFactory.getLogger(RequestLogTest.class);
|
@@ -73,115 +77,104 @@ public void tearDown() throws Exception {
|
73 | 77 | @Test
|
74 | 78 | public void logsCorrelationIdFromRequestHeader() throws Exception {
|
75 | 79 | 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 |
| - |
| 80 | + HttpGet get = createRequestWithHeader(HttpHeaders.CORRELATION_ID.getName(), correlationId); |
| 81 | + try (CloseableHttpResponse response = client.execute(get)) { |
82 | 82 | assertNull("No correlation_id should be generated.", getCorrelationIdGenerated());
|
83 | 83 |
|
84 | 84 | assertThat("Application log without correlation id.", getRequestMessage(),
|
85 | 85 | hasEntry(Fields.CORRELATION_ID, correlationId));
|
86 | 86 | assertThat("Request log without correlation id.", getRequestLog(),
|
87 | 87 | hasEntry(Fields.CORRELATION_ID, correlationId));
|
88 |
| - } finally { |
89 |
| - if (response != null) { |
90 |
| - response.close(); |
91 |
| - } |
92 | 88 | }
|
93 | 89 | }
|
94 | 90 |
|
| 91 | + private HttpGet createRequestWithHeader(String headerName, String headerValue) { |
| 92 | + HttpGet get = createRequest(); |
| 93 | + get.setHeader(headerName, headerValue); |
| 94 | + return get; |
| 95 | + } |
| 96 | + |
| 97 | + private HttpGet createRequest() { |
| 98 | + return new HttpGet(getBaseUrl() + "/test"); |
| 99 | + } |
| 100 | + |
95 | 101 | @Test
|
96 | 102 | public void logsGeneratedCorrelationId() throws Exception {
|
97 |
| - HttpGet get = new HttpGet(getBaseUrl() + "/test"); |
98 |
| - CloseableHttpResponse response = null; |
99 |
| - try { |
100 |
| - response = client.execute(get); |
101 |
| - |
| 103 | + try (CloseableHttpResponse response = client.execute(createRequest())) { |
102 | 104 | String correlationId = getCorrelationIdGenerated();
|
103 | 105 |
|
104 | 106 | assertThat("Application log without correlation id.", getRequestMessage(),
|
105 | 107 | hasEntry(Fields.CORRELATION_ID, correlationId));
|
106 | 108 | assertThat("Request log without correlation id.", getRequestLog(),
|
107 | 109 | hasEntry(Fields.CORRELATION_ID, correlationId));
|
108 |
| - } finally { |
109 |
| - if (response != null) { |
110 |
| - response.close(); |
111 |
| - } |
112 | 110 | }
|
113 | 111 | }
|
114 | 112 |
|
115 | 113 | @Test
|
116 | 114 | public void logsRequestIdFromRequestHeader() throws Exception {
|
117 | 115 | 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 |
| - |
| 116 | + HttpGet get = createRequestWithHeader(HttpHeaders.X_VCAP_REQUEST_ID.getName(), requestId); |
| 117 | + try (CloseableHttpResponse response = client.execute(get)) { |
124 | 118 | assertThat("Application log without request id.", getRequestMessage(),
|
125 | 119 | hasEntry(Fields.REQUEST_ID, requestId));
|
126 | 120 | assertThat("Request log without request id.", getRequestLog(),
|
127 | 121 | hasEntry(Fields.REQUEST_ID, requestId));
|
128 |
| - } finally { |
129 |
| - if (response != null) { |
130 |
| - response.close(); |
131 |
| - } |
132 | 122 | }
|
133 | 123 | }
|
134 | 124 |
|
135 | 125 | @Test
|
136 | 126 | public void logsTenantIdFromRequestHeader() throws Exception {
|
137 | 127 | 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 |
| - |
| 128 | + HttpGet get = createRequestWithHeader(HttpHeaders.TENANT_ID.getName(), tenantId); |
| 129 | + try (CloseableHttpResponse response = client.execute(get)) { |
144 | 130 | assertThat("Application log without tenant id.", getRequestMessage(),
|
145 | 131 | hasEntry(Fields.TENANT_ID, tenantId));
|
146 | 132 | assertThat("Request log without tenant id.", getRequestLog(), hasEntry(Fields.TENANT_ID, tenantId));
|
147 |
| - } finally { |
148 |
| - if (response != null) { |
149 |
| - response.close(); |
150 |
| - } |
151 | 133 | }
|
152 | 134 | }
|
153 | 135 |
|
154 | 136 | @Test
|
155 | 137 | public void writesCorrelationIdFromHeadersAsResponseHeader() throws Exception {
|
156 | 138 | 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); |
| 139 | + HttpGet get = createRequestWithHeader(HttpHeaders.CORRELATION_ID.getName(), correlationId); |
| 140 | + try (CloseableHttpResponse response = client.execute(get)) { |
162 | 141 | assertFirstHeaderValue(correlationId, response, HttpHeaders.CORRELATION_ID);
|
163 |
| - } finally { |
164 |
| - if (response != null) { |
165 |
| - response.close(); |
166 |
| - } |
167 | 142 | }
|
168 | 143 | }
|
169 | 144 |
|
170 | 145 | @Test
|
171 | 146 | public void writesGeneratedCorrelationIdAsResponseHeader() throws Exception {
|
172 |
| - HttpGet get = new HttpGet(getBaseUrl() + "/test"); |
173 |
| - CloseableHttpResponse response = null; |
174 |
| - try { |
175 |
| - response = client.execute(get); |
176 |
| - |
| 147 | + try (CloseableHttpResponse response = client.execute(createRequest())) { |
177 | 148 | assertFirstHeaderValue(getCorrelationIdGenerated(), response, HttpHeaders.CORRELATION_ID);
|
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void writesNoRequestLogIfNotConfigured() throws Exception { |
| 154 | + setRequestLogLevel(Level.OFF); |
| 155 | + try (CloseableHttpResponse response = client.execute(createRequest())) { |
| 156 | + assertThat(getRequestLog().entrySet(), is(empty())); |
| 157 | + } finally { |
| 158 | + setRequestLogLevel(Level.INFO); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void logCorrelationIdFromHeaderEvenIfRequestLogNotConfigured() throws Exception { |
| 164 | + setRequestLogLevel(Level.OFF); |
| 165 | + String correlationId = UUID.randomUUID().toString(); |
| 166 | + HttpGet get = createRequestWithHeader(HttpHeaders.CORRELATION_ID.getName(), correlationId); |
| 167 | + try (CloseableHttpResponse response = client.execute(get)) { |
| 168 | + assertThat("Application log without correlation id.", getRequestMessage(), |
| 169 | + hasEntry(Fields.CORRELATION_ID, correlationId)); |
178 | 170 | } finally {
|
179 |
| - if (response != null) { |
180 |
| - response.close(); |
181 |
| - } |
| 171 | + setRequestLogLevel(Level.INFO); |
182 | 172 | }
|
183 | 173 | }
|
184 | 174 |
|
| 175 | + private void setRequestLogLevel(Level level) { |
| 176 | + ((LoggerContext) LoggerFactory.getILoggerFactory()).getLogger(RequestLogger.class).setLevel(level); |
| 177 | + } |
185 | 178 |
|
186 | 179 | private String getBaseUrl() {
|
187 | 180 | int port = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
|
|
0 commit comments