Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-els-onap-logging / 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
27 import java.net.URI;
28 import java.net.URISyntaxException;
29 import java.time.ZoneOffset;
30 import java.time.ZonedDateTime;
31 import java.time.format.DateTimeFormatter;
32
33 import javax.ws.rs.client.ClientRequestContext;
34 import javax.ws.rs.core.MultivaluedHashMap;
35 import javax.ws.rs.core.MultivaluedMap;
36
37 import org.junit.After;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.Spy;
43 import org.mockito.junit.MockitoJUnitRunner;
44 import org.onap.logging.ref.slf4j.ONAPLogConstants;
45 import org.slf4j.MDC;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class MetricLogClientFilterTest {
49     @Mock
50     private ClientRequestContext clientRequest;
51
52     @Spy
53     @InjectMocks
54     private MetricLogClientFilter metricLogClientFilter;
55
56     @After
57     public void tearDown() {
58         MDC.clear();
59     }
60
61     @Test
62     public void setupHeadersTest() {
63         MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
64         doReturn("0a908a5d-e774-4558-96ff-6edcbba65483").when(metricLogClientFilter).extractRequestID();
65         metricLogClientFilter.setupHeaders(clientRequest, headers);
66
67         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(ONAPLogConstants.Headers.REQUEST_ID));
68         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.HEADER_REQUEST_ID));
69         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.TRANSACTION_ID));
70         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.TRANSACTION_ID));
71         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.ECOMP_REQUEST_ID));
72     }
73
74     @Test
75     public void setupMDCTest() throws URISyntaxException {
76         // TODO ingest change from upstream
77         MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO");
78         URI uri = new URI("onap/so/serviceInstances");
79         doReturn(uri).when(clientRequest).getUri();
80
81         metricLogClientFilter.setupMDC(clientRequest);
82
83         assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
84         assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
85         assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
86         assertNotNull(ONAPLogConstants.MDCs.SERVICE_NAME);
87         assertNotNull(ONAPLogConstants.MDCs.SERVER_FQDN);
88         assertNotNull(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
89     }
90
91     @Test
92     public void setupMDCNoTargetEntityTest() throws URISyntaxException {
93         URI uri = new URI("onap/so/serviceInstances");
94         doReturn(uri).when(clientRequest).getUri();
95
96         metricLogClientFilter.setupMDC(clientRequest);
97
98         assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
99         assertEquals("Unknown-Target-Entity", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
100         assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
101         assertNotNull(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
102     }
103
104     @Test
105     public void extractRequestIDTest() {
106         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, "0a908a5d-e774-4558-96ff-6edcbba65483");
107         String requestId = metricLogClientFilter.extractRequestID();
108         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", requestId);
109     }
110
111     @Test
112     public void extractRequestIDNullTest() throws URISyntaxException {
113         MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP,
114                 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
115         String requestId = metricLogClientFilter.extractRequestID();
116         assertNotNull(requestId);
117         assertNotNull(ONAPLogConstants.MDCs.LOG_TIMESTAMP);
118         assertNotNull(ONAPLogConstants.MDCs.ELAPSED_TIME);
119
120     }
121 }