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.mockito.Mockito.doReturn;
27 import java.net.URISyntaxException;
28 import java.time.ZoneOffset;
29 import java.time.ZonedDateTime;
30 import java.time.format.DateTimeFormatter;
31 import javax.ws.rs.client.ClientRequestContext;
32 import javax.ws.rs.core.MultivaluedHashMap;
33 import javax.ws.rs.core.MultivaluedMap;
34 import org.junit.After;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.Spy;
40 import org.mockito.junit.MockitoJUnitRunner;
41 import org.onap.logging.ref.slf4j.ONAPLogConstants;
44 @RunWith(MockitoJUnitRunner.class)
45 public class MetricLogClientFilterTest {
47 private ClientRequestContext clientRequest;
51 private MetricLogClientFilter metricLogClientFilter;
54 public void tearDown() {
59 public void setupHeadersTest() {
60 MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
61 doReturn("0a908a5d-e774-4558-96ff-6edcbba65483").when(metricLogClientFilter).extractRequestID();
63 metricLogClientFilter.setupHeaders(clientRequest, headers);
65 assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(ONAPLogConstants.Headers.REQUEST_ID));
66 assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.HEADER_REQUEST_ID));
67 assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.TRANSACTION_ID));
68 assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.TRANSACTION_ID));
69 assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.ECOMP_REQUEST_ID));
70 assertNotNull(headers.getFirst(ONAPLogConstants.Headers.INVOCATION_ID));
71 assertEquals("UNKNOWN", headers.getFirst(ONAPLogConstants.Headers.PARTNER_NAME));
75 public void setInvocationIdTest() {
76 String invocationId = metricLogClientFilter.setInvocationId();
78 assertEquals(invocationId, MDC.get(ONAPLogConstants.MDCs.CLIENT_INVOCATION_ID));
82 public void setupMDCTest() throws URISyntaxException {
83 // TODO ingest change from upstream
84 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO");
85 URI uri = new URI("onap/so/serviceInstances");
86 doReturn(uri).when(clientRequest).getUri();
88 metricLogClientFilter.setupMDC(clientRequest);
90 assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
91 assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
92 assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
93 assertNotNull(ONAPLogConstants.MDCs.SERVICE_NAME);
94 assertNotNull(ONAPLogConstants.MDCs.SERVER_FQDN);
95 assertNotNull(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
99 public void setupMDCNoTargetEntityTest() throws URISyntaxException {
100 URI uri = new URI("onap/so/serviceInstances");
101 doReturn(uri).when(clientRequest).getUri();
103 metricLogClientFilter.setupMDC(clientRequest);
105 assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
106 assertEquals("Unknown-Target-Entity", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
107 assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
108 assertNotNull(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
112 public void extractRequestIDTest() {
113 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, "0a908a5d-e774-4558-96ff-6edcbba65483");
114 String requestId = metricLogClientFilter.extractRequestID();
115 assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", requestId);
119 public void extractRequestIDNullTest() throws URISyntaxException {
120 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP,
121 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
122 String requestId = metricLogClientFilter.extractRequestID();
123 assertNotNull(requestId);
124 assertNotNull(ONAPLogConstants.MDCs.LOG_TIMESTAMP);
125 assertNotNull(ONAPLogConstants.MDCs.ELAPSED_TIME);