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 setInvocationIdTest() {
109 HashMap<String, String> headers = new HashMap<>();
110 headers.put(ONAPLogConstants.Headers.INVOCATION_ID, invocationId);
111 setInvocationId(new SimpleHashMap(headers));
112 assertEquals(invocationId, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
116 public void setInvocationIdNoHeaderTest() {
117 HashMap<String, String> headers = new HashMap<>();
118 setInvocationId(new SimpleHashMap(headers));
119 assertNotNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
123 public void setResponseStatusCodeTest() {
124 setResponseStatusCode(200);
125 assertEquals("COMPLETE", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
129 public void setResponseStatusCodeErrorTest() {
130 setResponseStatusCode(400);
131 assertEquals("ERROR", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
132 assertEquals("400", MDC.get(ONAPLogConstants.MDCs.ERROR_CODE));
133 assertEquals("Bad Request", MDC.get(ONAPLogConstants.MDCs.ERROR_DESC));
137 public void clearClientMDCsTest() {
138 MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION, "Bad Gateway");
139 MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, "Bad Gateway");
140 MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, "502");
141 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, "502");
142 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, "502");
143 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO");
144 MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, "SDNC");
145 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, "2019-06-18T02:09:06.024Z");
148 assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION));
149 assertNull(MDC.get(ONAPLogConstants.MDCs.ERROR_CODE));
150 assertNull(MDC.get(ONAPLogConstants.MDCs.ERROR_DESC));
151 assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
152 assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_CODE));
153 assertNull(MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
154 assertNull(MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
155 assertNull(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP));
159 public void setTargetEntityTest() {
160 setTargetEntity(ONAPComponents.SO);
161 assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
165 public void setResponseDescriptionTest() {
166 setResponseDescription(502);
167 assertEquals("Bad Gateway", MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION));
171 public void setMDCPartnerNameBearerToken() {
172 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
173 String value = "Bearer some-compex-token";
174 headerMap.putSingle(HttpHeaders.AUTHORIZATION, value);
175 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
177 setMDCPartnerName(headers);
179 assertEquals(Constants.DefaultValues.UNKNOWN, MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
183 public void setMDCPartnerNameTest() {
184 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
185 headerMap.putSingle(ONAPLogConstants.Headers.PARTNER_NAME, "SO");
186 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
188 setMDCPartnerName(headers);
190 assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
194 public void setMDCPartnerNameUserAgentHeaderTest() {
195 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
196 headerMap.putSingle(HttpHeaders.USER_AGENT, "Apache-HttpClient/4.5.8 (Java/1.8.0_191)");
197 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
199 setMDCPartnerName(headers);
201 assertEquals("Apache-HttpClient/4.5.8 (Java/1.8.0_191)", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
205 public void setMDCPartnerNameNoHeaderTest() {
206 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
207 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
209 setMDCPartnerName(headers);
211 assertEquals("UNKNOWN", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
215 public void setServerFQDNTest() {
217 assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_IP_ADDRESS));
218 assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN));
222 public void setClientIPAddressTest() {
223 when(httpServletRequest.getHeader("X-Forwarded-For")).thenReturn("127.0.0.2");
224 setClientIPAddress(httpServletRequest);
226 assertEquals("127.0.0.2", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS));
230 public void setClientIPAddressNoHeaderTest() {
231 when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1");
232 setClientIPAddress(httpServletRequest);
234 assertEquals("127.0.0.1", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS));
238 public void setClientIPAddressNullTest() {
239 setClientIPAddress(null);
241 assertEquals("", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS));
245 public void setEntryTimeStampTest() {
248 assertNotNull(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP));
252 public void setLogTimestampTest() {
255 assertNotNull(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP));
259 public void setInstanceIDTest() {
262 assertNotNull(MDC.get(ONAPLogConstants.MDCs.INSTANCE_UUID));
266 public void getPropertyTest() {
267 System.setProperty("partnerName", "partnerName");
269 String partnerName = getProperty("partnerName");
270 assertEquals("partnerName", partnerName);
274 public void getPropertyNullTest() {
275 String partnerName = getProperty("partner");
276 assertEquals(null, partnerName);