9a9f8837112ef3fe5762cab798e955c8a07ad4f6
[logging-analytics.git] / reference / logging-filter / logging-filter-base / src / test / java / org / onap / logging / filter / base / MetricLogClientFilterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - Logging
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.logging.filter.base;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.when;
27 import java.net.URI;
28 import java.net.URISyntaxException;
29 import javax.ws.rs.client.ClientRequestContext;
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.MetricLogClientFilter;
42 import org.onap.logging.ref.slf4j.ONAPLogConstants;
43 import org.slf4j.MDC;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class MetricLogClientFilterTest {
47
48     @Mock
49     private MDCSetup mdcSetup;
50
51     @Mock
52     private ClientRequestContext clientRequest;
53
54     @Spy
55     @InjectMocks
56     private MetricLogClientFilter metricLogClientFilter;
57
58     @After
59     public void tearDown() {
60         MDC.clear();
61     }
62
63     @Test
64     public void setupHeadersTest() {
65         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, "8819bfb4-69d2-43fc-b0d6-81d2690533ea");
66         MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
67         when(clientRequest.getHeaders()).thenReturn(headers);
68         doReturn("0a908a5d-e774-4558-96ff-6edcbba65483").when(metricLogClientFilter).extractRequestID(clientRequest);
69
70         metricLogClientFilter.setupHeaders(clientRequest);
71
72         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(ONAPLogConstants.Headers.REQUEST_ID));
73         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.HEADER_REQUEST_ID));
74         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.TRANSACTION_ID));
75         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.TRANSACTION_ID));
76         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.ECOMP_REQUEST_ID));
77         assertEquals("8819bfb4-69d2-43fc-b0d6-81d2690533ea", headers.getFirst(ONAPLogConstants.Headers.INVOCATION_ID));
78         assertEquals("UNKNOWN", headers.getFirst(ONAPLogConstants.Headers.PARTNER_NAME));
79     }
80
81     @Test
82     public void setupMDCTest() throws URISyntaxException {
83         MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO");
84         URI uri = new URI("onap/so/serviceInstances");
85         doReturn(uri).when(clientRequest).getUri();
86
87         metricLogClientFilter.setupMDC(clientRequest);
88
89         assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
90         assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
91         assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
92         assertNotNull(ONAPLogConstants.MDCs.SERVICE_NAME);
93         assertNotNull(ONAPLogConstants.MDCs.SERVER_FQDN);
94         assertNotNull(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
95     }
96
97     @Test
98     public void setupMDCNoTargetEntityTest() throws URISyntaxException {
99         URI uri = new URI("onap/so/serviceInstances");
100         doReturn(uri).when(clientRequest).getUri();
101
102         metricLogClientFilter.setupMDC(clientRequest);
103
104         assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
105         assertEquals("Unknown-Target-Entity", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
106         assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
107         assertNotNull(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
108     }
109
110     @Test
111     public void extractRequestIDTest() {
112         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, "0a908a5d-e774-4558-96ff-6edcbba65483");
113         String requestId = metricLogClientFilter.extractRequestID(clientRequest);
114         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", requestId);
115     }
116
117     @Test
118     public void extractRequestIDNullTest() throws URISyntaxException {
119         URI uri = new URI("onap/so/serviceInstances");
120         doReturn(uri).when(clientRequest).getUri();
121         String requestId = metricLogClientFilter.extractRequestID(clientRequest);
122         assertNotNull(requestId);
123         assertNotNull(ONAPLogConstants.MDCs.LOG_TIMESTAMP);
124         assertNotNull(ONAPLogConstants.MDCs.ELAPSED_TIME);
125
126     }
127 }