Set InvocationID value from server and client invocationIds
[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 java.net.URI;
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;
42 import org.slf4j.MDC;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class MetricLogClientFilterTest {
46     @Mock
47     private ClientRequestContext clientRequest;
48
49     @Spy
50     @InjectMocks
51     private MetricLogClientFilter metricLogClientFilter;
52
53     @After
54     public void tearDown() {
55         MDC.clear();
56     }
57
58     @Test
59     public void setupHeadersTest() {
60         MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
61         doReturn("0a908a5d-e774-4558-96ff-6edcbba65483").when(metricLogClientFilter).extractRequestID();
62
63         metricLogClientFilter.setupHeaders(clientRequest, headers);
64
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));
72     }
73
74     @Test
75     public void setInvocationIdTest() {
76         String invocationId = metricLogClientFilter.setInvocationId();
77
78         assertEquals(invocationId, MDC.get(ONAPLogConstants.MDCs.CLIENT_INVOCATION_ID));
79         assertEquals(invocationId, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
80     }
81
82     @Test
83     public void setupMDCTest() throws URISyntaxException {
84         // TODO ingest change from upstream
85         MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO");
86         URI uri = new URI("onap/so/serviceInstances");
87         doReturn(uri).when(clientRequest).getUri();
88
89         metricLogClientFilter.setupMDC(clientRequest);
90
91         assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
92         assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
93         assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
94         assertNotNull(ONAPLogConstants.MDCs.SERVICE_NAME);
95         assertNotNull(ONAPLogConstants.MDCs.SERVER_FQDN);
96         assertNotNull(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
97     }
98
99     @Test
100     public void setupMDCNoTargetEntityTest() throws URISyntaxException {
101         URI uri = new URI("onap/so/serviceInstances");
102         doReturn(uri).when(clientRequest).getUri();
103
104         metricLogClientFilter.setupMDC(clientRequest);
105
106         assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
107         assertEquals("Unknown-Target-Entity", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
108         assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
109         assertNotNull(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
110     }
111
112     @Test
113     public void extractRequestIDTest() {
114         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, "0a908a5d-e774-4558-96ff-6edcbba65483");
115         String requestId = metricLogClientFilter.extractRequestID();
116         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", requestId);
117     }
118
119     @Test
120     public void extractRequestIDNullTest() throws URISyntaxException {
121         MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP,
122                 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
123         String requestId = metricLogClientFilter.extractRequestID();
124         assertNotNull(requestId);
125         assertNotNull(ONAPLogConstants.MDCs.LOG_TIMESTAMP);
126         assertNotNull(ONAPLogConstants.MDCs.ELAPSED_TIME);
127
128     }
129 }