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;
28 import java.util.HashMap;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.ws.rs.core.HttpHeaders;
32 import javax.ws.rs.core.MultivaluedHashMap;
33 import javax.ws.rs.core.MultivaluedMap;
35 import org.junit.After;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.logging.ref.slf4j.ONAPLogConstants;
43 @RunWith(MockitoJUnitRunner.class)
44 public class MDCSetupTest extends MDCSetup {
47 private HttpServletRequest httpServletRequest;
49 private String requestId = "4d31fe02-4918-4975-942f-fe51a44e6a9b";
50 private String invocationId = "4d31fe02-4918-4975-942f-fe51a44e6a9a";
53 public void tearDown() {
55 System.clearProperty("partnerName");
59 public void setElapsedTimeTest() {
60 String expected = "318";
61 MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP, "2019-06-18T02:09:06.024Z");
62 MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP, "2019-06-18T02:09:06.342Z");
65 assertEquals(expected, MDC.get(ONAPLogConstants.MDCs.ELAPSED_TIME));
69 public void setElapsedTimeInvokeTimestampTest() {
70 String expected = "318";
71 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, "2019-06-18T02:09:06.024Z");
72 MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP, "2019-06-18T02:09:06.342Z");
74 setElapsedTimeInvokeTimestamp();
75 assertEquals(expected, MDC.get(ONAPLogConstants.MDCs.ELAPSED_TIME));
79 public void setRequestIdTest() {
80 HashMap<String, String> headers = new HashMap<>();
81 headers.put(ONAPLogConstants.Headers.REQUEST_ID, requestId);
82 String fetchedRequestId = getRequestId(new SimpleHashMap(headers));
83 assertEquals(requestId, fetchedRequestId);
87 public void setRequestIdRequestIdHeaderTest() {
88 HashMap<String, String> headers = new HashMap<>();
89 headers.put(Constants.HttpHeaders.HEADER_REQUEST_ID, requestId);
90 String fetchedRequestId = getRequestId(new SimpleHashMap(headers));
91 assertEquals(requestId, fetchedRequestId);
95 public void setRequestIdTransactionIdHeaderTest() {
96 HashMap<String, String> headers = new HashMap<>();
97 headers.put(Constants.HttpHeaders.TRANSACTION_ID, requestId);
98 String fetchedRequestId = getRequestId(new SimpleHashMap(headers));
99 assertEquals(requestId, fetchedRequestId);
103 public void setRequestIdEcompRequestIdHeaderTest() {
104 HashMap<String, String> headers = new HashMap<>();
105 headers.put(Constants.HttpHeaders.ECOMP_REQUEST_ID, requestId);
106 String fetchedRequestId = getRequestId(new SimpleHashMap(headers));
107 assertEquals(requestId, fetchedRequestId);
111 public void setInvocationIdTest() {
112 HashMap<String, String> headers = new HashMap<>();
113 headers.put(ONAPLogConstants.Headers.INVOCATION_ID, invocationId);
114 setInvocationId(new SimpleHashMap(headers));
115 assertEquals(invocationId, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
119 public void setInvocationIdNoHeaderTest() {
120 HashMap<String, String> headers = new HashMap<>();
121 setInvocationId(new SimpleHashMap(headers));
122 assertNotNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
126 public void setResponseStatusCodeTest() {
127 setResponseStatusCode(200);
128 assertEquals("COMPLETE", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
132 public void setResponseStatusCodeErrorTest() {
133 setResponseStatusCode(400);
134 assertEquals("ERROR", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
135 assertEquals("400", MDC.get(ONAPLogConstants.MDCs.ERROR_CODE));
136 assertEquals("Bad Request", MDC.get(ONAPLogConstants.MDCs.ERROR_DESC));
140 public void clearClientMDCsTest() {
141 MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION, "Bad Gateway");
142 MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, "Bad Gateway");
143 MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, "502");
144 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, "502");
145 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, "502");
146 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO");
147 MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, "SDNC");
148 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, "2019-06-18T02:09:06.024Z");
151 assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION));
152 assertNull(MDC.get(ONAPLogConstants.MDCs.ERROR_CODE));
153 assertNull(MDC.get(ONAPLogConstants.MDCs.ERROR_DESC));
154 assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
155 assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_CODE));
156 assertNull(MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
157 assertNull(MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
158 assertNull(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP));
162 public void setTargetEntityTest() {
163 setTargetEntity(ONAPComponents.SO);
164 assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
168 public void setResponseDescriptionTest() {
169 setResponseDescription(502);
170 assertEquals("Bad Gateway", MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION));
174 public void setMDCPartnerNameBearerToken() {
175 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
176 String value = "Bearer some-compex-token";
177 headerMap.putSingle(HttpHeaders.AUTHORIZATION, value);
178 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
180 setMDCPartnerName(headers);
182 assertEquals(Constants.DefaultValues.UNKNOWN, MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
186 public void setMDCPartnerNameTest() {
187 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
188 headerMap.putSingle(ONAPLogConstants.Headers.PARTNER_NAME, "SO");
189 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
191 setMDCPartnerName(headers);
193 assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
197 public void setMDCPartnerNameUserAgentHeaderTest() {
198 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
199 headerMap.putSingle(HttpHeaders.USER_AGENT, "Apache-HttpClient/4.5.8 (Java/1.8.0_191)");
200 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
202 setMDCPartnerName(headers);
204 assertEquals("Apache-HttpClient/4.5.8 (Java/1.8.0_191)", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
208 public void setMDCPartnerNameNoHeaderTest() {
209 MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
210 SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap);
212 setMDCPartnerName(headers);
214 assertEquals("UNKNOWN", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
218 public void setServerFQDNTest() {
220 assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_IP_ADDRESS));
221 assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN));
225 public void setClientIPAddressTest() {
226 when(httpServletRequest.getHeader("X-Forwarded-For")).thenReturn("127.0.0.2");
227 setClientIPAddress(httpServletRequest);
229 assertEquals("127.0.0.2", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS));
233 public void setClientIPAddressNoHeaderTest() {
234 when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1");
235 setClientIPAddress(httpServletRequest);
237 assertEquals("127.0.0.1", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS));
241 public void setClientIPAddressNullTest() {
242 setClientIPAddress(null);
244 assertEquals("", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS));
248 public void setEntryTimeStampTest() {
251 assertNotNull(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP));
255 public void setLogTimestampTest() {
258 assertNotNull(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP));
262 public void setInstanceIDTest() {
265 assertNotNull(MDC.get(ONAPLogConstants.MDCs.INSTANCE_UUID));
269 public void getPropertyTest() {
270 System.setProperty("partnerName", "partnerName");
272 String partnerName = getProperty("partnerName");
273 assertEquals("partnerName", partnerName);
277 public void getPropertyNullTest() {
278 String partnerName = getProperty("partner");
279 assertEquals(null, partnerName);