2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.logging.filter.base;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.mockito.Mockito.when;
27 import java.util.HashMap;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.ws.rs.core.HttpHeaders;
30 import javax.ws.rs.core.MultivaluedHashMap;
31 import javax.ws.rs.core.MultivaluedMap;
32 import org.junit.After;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.logging.ref.slf4j.ONAPLogConstants;
40 @RunWith(MockitoJUnitRunner.class)
41 public class MDCSetupTest extends MDCSetup {
44 private HttpServletRequest httpServletRequest;
46 private String requestId = "4d31fe02-4918-4975-942f-fe51a44e6a9b";
47 private String invocationId = "4d31fe02-4918-4975-942f-fe51a44e6a9a";
50 public void tearDown() {
52 System.clearProperty("partnerName");
56 public void setElapsedTimeTest() {
57 String expected = "318";
58 MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP, "2019-06-18T02:09:06.024Z");
59 MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP, "2019-06-18T02:09:06.342Z");
62 assertEquals(expected, MDC.get(ONAPLogConstants.MDCs.ELAPSED_TIME));
66 public void setElapsedTimeInvokeTimestampTest() {
67 String expected = "318";
68 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, "2019-06-18T02:09:06.024Z");
69 MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP, "2019-06-18T02:09:06.342Z");
71 setElapsedTimeInvokeTimestamp();
72 assertEquals(expected, MDC.get(ONAPLogConstants.MDCs.ELAPSED_TIME));
76 public void setRequestIdTest() {
77 HashMap<String, String> headers = new HashMap<>();
78 headers.put(ONAPLogConstants.Headers.REQUEST_ID, requestId);
79 String fetchedRequestId = getRequestId(new SimpleHashMap(headers));
80 assertEquals(requestId, fetchedRequestId);
84 public void setRequestIdRequestIdHeaderTest() {
85 HashMap<String, String> headers = new HashMap<>();
86 headers.put(Constants.HttpHeaders.HEADER_REQUEST_ID, requestId);
87 String fetchedRequestId = getRequestId(new SimpleHashMap(headers));
88 assertEquals(requestId, fetchedRequestId);
92 public void setRequestIdTransactionIdHeaderTest() {
93 HashMap<String, String> headers = new HashMap<>();
94 headers.put(Constants.HttpHeaders.TRANSACTION_ID, requestId);
95 String fetchedRequestId = getRequestId(new SimpleHashMap(headers));
96 assertEquals(requestId, fetchedRequestId);
100 public void setRequestIdEcompRequestIdHeaderTest() {
101 HashMap<String, String> headers = new HashMap<>();
102 headers.put(Constants.HttpHeaders.ECOMP_REQUEST_ID, requestId);
103 String fetchedRequestId = getRequestId(new SimpleHashMap(headers));
104 assertEquals(requestId, fetchedRequestId);
108 public void setRequestIdNoHeaderTest() {
109 HashMap<String, String> headers = new HashMap<>();
110 String fetchedRequestId = getRequestId(new SimpleHashMap(headers));
111 assertNotNull(fetchedRequestId);
115 public void setInvocationIdTest() {
116 HashMap<String, String> headers = new HashMap<>();
117 headers.put(ONAPLogConstants.Headers.INVOCATION_ID, invocationId);
118 setInvocationId(new SimpleHashMap(headers));
119 assertEquals(invocationId, MDC.get(ONAPLogConstants.MDCs.SERVER_INVOCATION_ID));
120 assertEquals(invocationId, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
124 public void setInvocationIdNoHeaderTest() {
125 HashMap<String, String> headers = new HashMap<>();
126 setInvocationId(new SimpleHashMap(headers));
127 assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_INVOCATION_ID));
128 assertNotNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
132 public void setResponseStatusCodeTest() {
133 setResponseStatusCode(200);
134 assertEquals("COMPLETE", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
138 public void setResponseStatusCodeErrorTest() {
139 setResponseStatusCode(400);
140 assertEquals("ERROR", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
141 assertEquals("400", MDC.get(ONAPLogConstants.MDCs.ERROR_CODE));
142 assertEquals("Bad Request", MDC.get(ONAPLogConstants.MDCs.ERROR_DESC));
146 public void clearClientMDCsTest() {
147 MDC.put(ONAPLogConstants.MDCs.CLIENT_INVOCATION_ID, "7b77143c-9b50-410c-ac2f-05758a68e3e9");
148 MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION, "Bad Gateway");
149 MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, "Bad Gateway");
150 MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, "502");
151 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, "502");
152 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, "502");
153 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO");
154 MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, "SDNC");
155 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, "2019-06-18T02:09:06.024Z");
158 assertNull(MDC.get(ONAPLogConstants.MDCs.CLIENT_INVOCATION_ID));
159 assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION));
160 assertNull(MDC.get(ONAPLogConstants.MDCs.ERROR_CODE));
161 assertNull(MDC.get(ONAPLogConstants.MDCs.ERROR_DESC));
162 assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
163 assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_CODE));
164 assertNull(MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
165 assertNull(MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
166 assertNull(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP));
170 public void setTargetEntityTest() {
171 setTargetEntity(ONAPComponents.SO);
172 assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
176 public void setResponseDescriptionTest() {
177 setResponseDescription(502);
178 assertEquals("Bad Gateway", MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION));
182 public void setMDCPartnerNameBearerToken() {
183 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
184 String value = "Bearer some-compex-token";
185 headerMap.putSingle(HttpHeaders.AUTHORIZATION, value);
186 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
188 setMDCPartnerName(headers);
190 assertEquals(Constants.DefaultValues.UNKNOWN, MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
194 public void setMDCPartnerNameFromBasicAuth() {
195 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
196 String value = "Basic dXNlcjpwYXNz"; // decodes to user:pass
197 headerMap.putSingle(HttpHeaders.AUTHORIZATION, value);
198 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
200 setMDCPartnerName(headers);
202 assertEquals("user", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
206 public void setMDCPartnerNameTest() {
207 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
208 headerMap.putSingle(ONAPLogConstants.Headers.PARTNER_NAME, "SO");
209 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
211 setMDCPartnerName(headers);
213 assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
217 public void setMDCPartnerNameUserAgentHeaderTest() {
218 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
219 headerMap.putSingle(HttpHeaders.USER_AGENT, "Apache-HttpClient/4.5.8 (Java/1.8.0_191)");
220 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
222 setMDCPartnerName(headers);
224 assertEquals("Apache-HttpClient/4.5.8 (Java/1.8.0_191)", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
228 public void setMDCPartnerNameNoHeaderTest() {
229 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
230 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
232 setMDCPartnerName(headers);
234 assertEquals("UNKNOWN", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
238 public void setServerFQDNTest() {
240 assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_IP_ADDRESS));
241 assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN));
245 public void setClientIPAddressTest() {
246 when(httpServletRequest.getHeader("X-Forwarded-For")).thenReturn("127.0.0.2");
247 setClientIPAddress(httpServletRequest);
249 assertEquals("127.0.0.2", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS));
253 public void setClientIPAddressNoHeaderTest() {
254 when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1");
255 setClientIPAddress(httpServletRequest);
257 assertEquals("127.0.0.1", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS));
261 public void setClientIPAddressNullTest() {
262 setClientIPAddress(null);
264 assertEquals("", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS));
268 public void setEntryTimeStampTest() {
271 assertNotNull(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP));
275 public void setLogTimestampTest() {
278 assertNotNull(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP));
282 public void setInstanceIDTest() {
285 assertNotNull(MDC.get(ONAPLogConstants.MDCs.INSTANCE_UUID));
289 public void getPropertyTest() {
290 System.setProperty("partnerName", "partnerName");
292 String partnerName = getProperty("partnerName");
293 assertEquals("partnerName", partnerName);
297 public void getPropertyNullTest() {
298 String partnerName = getProperty("partner");
299 assertEquals("UNKNOWN", partnerName);
303 public void overrideServerIp() {
304 String ip = "127.0.0.1";
305 System.setProperty(serverIpAddressOverride, ip);
306 MDCSetup m = new MDCSetup();
307 assertEquals(ip, m.serverIpAddress);
311 public void overrideServerFqdn() {
312 String nodeName = "node300";
313 System.setProperty(serverFqdnOverride, nodeName);
314 MDCSetup m = new MDCSetup();
315 assertEquals(nodeName, m.serverFqdn);