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.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.Spy;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.onap.logging.filter.base.Constants;
40 import org.onap.logging.filter.base.MDCSetup;
41 import org.onap.logging.filter.base.ONAPComponents;
42 import org.onap.logging.filter.base.SimpleHashMap;
43 import org.onap.logging.filter.base.SimpleJaxrsHeadersMap;
44 import org.onap.logging.filter.base.SimpleMap;
45 import org.onap.logging.ref.slf4j.ONAPLogConstants;
48 @RunWith(MockitoJUnitRunner.class)
49 public class MDCSetupTest {
52 private HttpServletRequest httpServletRequest;
56 private MDCSetup mdcSetup = new MDCSetup();
58 private String requestId = "4d31fe02-4918-4975-942f-fe51a44e6a9b";
59 private String invocationId = "4d31fe02-4918-4975-942f-fe51a44e6a9a";
62 public void tearDown() {
67 public void setElapsedTimeTest() {
68 String expected = "318";
69 MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP, "2019-06-18T02:09:06.024Z");
70 MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP, "2019-06-18T02:09:06.342Z");
72 mdcSetup.setElapsedTime();
73 assertEquals(expected, MDC.get(ONAPLogConstants.MDCs.ELAPSED_TIME));
77 public void setElapsedTimeInvokeTimestampTest() {
78 String expected = "318";
79 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, "2019-06-18T02:09:06.024Z");
80 MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP, "2019-06-18T02:09:06.342Z");
82 mdcSetup.setElapsedTimeInvokeTimestamp();
83 assertEquals(expected, MDC.get(ONAPLogConstants.MDCs.ELAPSED_TIME));
87 public void setRequestIdTest() {
88 HashMap<String, String> headers = new HashMap<>();
89 headers.put(ONAPLogConstants.Headers.REQUEST_ID, requestId);
90 String fetchedRequestId = mdcSetup.getRequestId(new SimpleHashMap(headers));
91 assertEquals(requestId, fetchedRequestId);
95 public void setRequestIdRequestIdHeaderTest() {
96 HashMap<String, String> headers = new HashMap<>();
97 headers.put(Constants.HttpHeaders.HEADER_REQUEST_ID, requestId);
98 String fetchedRequestId = mdcSetup.getRequestId(new SimpleHashMap(headers));
99 assertEquals(requestId, fetchedRequestId);
103 public void setRequestIdTransactionIdHeaderTest() {
104 HashMap<String, String> headers = new HashMap<>();
105 headers.put(Constants.HttpHeaders.TRANSACTION_ID, requestId);
106 String fetchedRequestId = mdcSetup.getRequestId(new SimpleHashMap(headers));
107 assertEquals(requestId, fetchedRequestId);
111 public void setRequestIdEcompRequestIdHeaderTest() {
112 HashMap<String, String> headers = new HashMap<>();
113 headers.put(Constants.HttpHeaders.ECOMP_REQUEST_ID, requestId);
114 String fetchedRequestId = mdcSetup.getRequestId(new SimpleHashMap(headers));
115 assertEquals(requestId, fetchedRequestId);
119 public void setRequestIdNoHeaderTest() {
120 HashMap<String, String> headers = new HashMap<>();
121 String fetchedRequestId = mdcSetup.getRequestId(new SimpleHashMap(headers));
122 assertNotNull(fetchedRequestId);
126 public void setInvocationIdTest() {
127 HashMap<String, String> headers = new HashMap<>();
128 headers.put(ONAPLogConstants.Headers.INVOCATION_ID, invocationId);
129 mdcSetup.setInvocationId(new SimpleHashMap(headers));
130 assertEquals(invocationId, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
134 public void setInvocationIdNoHeaderTest() {
135 HashMap<String, String> headers = new HashMap<>();
136 mdcSetup.setInvocationId(new SimpleHashMap(headers));
137 assertNotNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
141 public void setInvovationIdFromMDCTest() {
142 MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, "7b77143c-9b50-410c-ac2f-05758a68e3e8");
143 mdcSetup.setInvocationIdFromMDC();
144 assertEquals("7b77143c-9b50-410c-ac2f-05758a68e3e8", MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
148 public void setInvocationIdFromMDCNoInvocationIdTest() {
149 mdcSetup.setInvocationIdFromMDC();
150 // InvocationId is set to a random UUID
151 assertNotNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
155 public void setResponseStatusCodeTest() {
156 mdcSetup.setResponseStatusCode(200);
157 assertEquals("COMPLETE", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
161 public void setResponseStatusCodeErrorTest() {
162 mdcSetup.setResponseStatusCode(400);
163 assertEquals("ERROR", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
164 assertEquals("400", MDC.get(ONAPLogConstants.MDCs.ERROR_CODE));
165 assertEquals("Bad Request", MDC.get(ONAPLogConstants.MDCs.ERROR_DESC));
169 public void clearClientMDCsTest() {
170 MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, "7b77143c-9b50-410c-ac2f-05758a68e3e9");
171 MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION, "Bad Gateway");
172 MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, "Bad Gateway");
173 MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, "502");
174 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, "502");
175 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, "502");
176 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO");
177 MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, "SDNC");
178 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, "2019-06-18T02:09:06.024Z");
180 mdcSetup.clearClientMDCs();
181 assertNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
182 assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION));
183 assertNull(MDC.get(ONAPLogConstants.MDCs.ERROR_CODE));
184 assertNull(MDC.get(ONAPLogConstants.MDCs.ERROR_DESC));
185 assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
186 assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_CODE));
187 assertNull(MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
188 assertNull(MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
189 assertNull(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP));
193 public void setTargetEntityTest() {
194 mdcSetup.setTargetEntity(ONAPComponents.SO);
195 assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
199 public void setResponseDescriptionTest() {
200 mdcSetup.setResponseDescription(502);
201 assertEquals("Bad Gateway", MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION));
205 public void setMDCPartnerNameTest() {
206 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
207 headerMap.putSingle(ONAPLogConstants.Headers.PARTNER_NAME, "SO");
208 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
210 mdcSetup.setMDCPartnerName(headers);
212 assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
216 public void setMDCPartnerNameUserAgentHeaderTest() {
217 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
218 headerMap.putSingle(HttpHeaders.USER_AGENT, "Apache-HttpClient/4.5.8 (Java/1.8.0_191)");
219 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
221 mdcSetup.setMDCPartnerName(headers);
223 assertEquals("Apache-HttpClient/4.5.8 (Java/1.8.0_191)", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
227 public void setMDCPartnerNameClientIdHeaderTest() {
228 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
229 headerMap.putSingle(Constants.HttpHeaders.CLIENT_ID, "SO");
230 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
232 mdcSetup.setMDCPartnerName(headers);
234 assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
238 public void setMDCPartnerNameNoHeaderTest() {
239 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
240 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
242 mdcSetup.setMDCPartnerName(headers);
244 assertEquals("UNKNOWN", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
248 public void setServerFQDNTest() {
249 mdcSetup.setServerFQDN();
250 assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_IP_ADDRESS));
251 assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN));
255 public void setClientIPAddressTest() {
256 when(httpServletRequest.getHeader("X-Forwarded-For")).thenReturn("127.0.0.2");
257 mdcSetup.setClientIPAddress(httpServletRequest);
259 assertEquals("127.0.0.2", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS));
263 public void setClientIPAddressNoHeaderTest() {
264 when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1");
265 mdcSetup.setClientIPAddress(httpServletRequest);
267 assertEquals("127.0.0.1", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS));
271 public void setClientIPAddressNullTest() {
272 mdcSetup.setClientIPAddress(null);
274 assertEquals("", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS));
278 public void setEntryTimeStampTest() {
279 mdcSetup.setEntryTimeStamp();
281 assertNotNull(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP));
285 public void setLogTimestampTest() {
286 mdcSetup.setLogTimestamp();
288 assertNotNull(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP));
292 public void setInstanceIDTest() {
293 mdcSetup.setInstanceID();
295 assertNotNull(MDC.get(ONAPLogConstants.MDCs.INSTANCE_UUID));
299 public void setServiceNameTest() {
300 String requestUri = "onap/so/serviceInstances";
301 when(httpServletRequest.getRequestURI()).thenReturn(requestUri);
302 mdcSetup.setServiceName(httpServletRequest);
304 assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME));