Generate InvocationID UUID on outgoing requests and use X-InvocationID
[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         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, "8819bfb4-69d2-43fc-b0d6-81d2690533ea");
61         MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
62         doReturn("0a908a5d-e774-4558-96ff-6edcbba65483").when(metricLogClientFilter).extractRequestID();
63
64         metricLogClientFilter.setupHeaders(clientRequest, headers);
65
66         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(ONAPLogConstants.Headers.REQUEST_ID));
67         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.HEADER_REQUEST_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.TRANSACTION_ID));
70         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.ECOMP_REQUEST_ID));
71         assertNotNull(headers.getFirst(ONAPLogConstants.Headers.INVOCATION_ID));
72         assertEquals("UNKNOWN", headers.getFirst(ONAPLogConstants.Headers.PARTNER_NAME));
73     }
74
75     @Test
76     public void setupMDCTest() throws URISyntaxException {
77         // TODO ingest change from upstream
78         MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO");
79         URI uri = new URI("onap/so/serviceInstances");
80         doReturn(uri).when(clientRequest).getUri();
81
82         metricLogClientFilter.setupMDC(clientRequest);
83
84         assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
85         assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
86         assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
87         assertNotNull(ONAPLogConstants.MDCs.SERVICE_NAME);
88         assertNotNull(ONAPLogConstants.MDCs.SERVER_FQDN);
89         assertNotNull(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
90     }
91
92     @Test
93     public void setupMDCNoTargetEntityTest() throws URISyntaxException {
94         URI uri = new URI("onap/so/serviceInstances");
95         doReturn(uri).when(clientRequest).getUri();
96
97         metricLogClientFilter.setupMDC(clientRequest);
98
99         assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
100         assertEquals("Unknown-Target-Entity", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
101         assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
102         assertNotNull(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
103     }
104
105     @Test
106     public void extractRequestIDTest() {
107         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, "0a908a5d-e774-4558-96ff-6edcbba65483");
108         String requestId = metricLogClientFilter.extractRequestID();
109         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", requestId);
110     }
111
112     @Test
113     public void extractRequestIDNullTest() throws URISyntaxException {
114         MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP,
115                 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
116         String requestId = metricLogClientFilter.extractRequestID();
117         assertNotNull(requestId);
118         assertNotNull(ONAPLogConstants.MDCs.LOG_TIMESTAMP);
119         assertNotNull(ONAPLogConstants.MDCs.ELAPSED_TIME);
120
121     }
122 }