Merge "Exclude unneccessary artifact - jackson-databind"
[logging-analytics.git] / reference / logging-filter / logging-filter-spring / src / test / java / org / onap / logging / filter / spring / SpringClientFilterTest.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.spring;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.when;
28 import java.io.IOException;
29 import java.net.URI;
30 import java.net.URISyntaxException;
31 import org.junit.After;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.Spy;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.onap.logging.filter.base.Constants;
39 import org.onap.logging.filter.base.MDCSetup;
40 import org.onap.logging.ref.slf4j.ONAPLogConstants;
41 import org.slf4j.MDC;
42 import org.springframework.http.HttpHeaders;
43 import org.springframework.http.HttpRequest;
44 import org.springframework.http.client.ClientHttpRequestExecution;
45 import org.springframework.http.client.ClientHttpResponse;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class SpringClientFilterTest {
49
50     @Mock
51     private MDCSetup mdcSetup;
52
53     @Mock
54     private ClientHttpResponse response;
55
56     @Mock
57     private HttpRequest clientRequest;
58
59     @Mock
60     private ClientHttpRequestExecution execution;
61
62     @Spy
63     @InjectMocks
64     private SpringClientFilter springClientFilter;
65
66     @After
67     public void tearDown() {
68         MDC.clear();
69     }
70
71     @Test
72     public void processResponseTest() throws IOException {
73         String partnerName = springClientFilter.getPartnerName();
74
75         assertEquals("UNKNOWN", partnerName);
76     }
77
78     @Test
79     public void extractTargetEntityTest() {
80         HttpHeaders headers = new HttpHeaders();
81         headers.add(Constants.HttpHeaders.TARGET_ENTITY_HEADER, "SO");
82         when(clientRequest.getHeaders()).thenReturn(headers);
83
84         String targetEntity = springClientFilter.extractTargetEntity(clientRequest);
85         assertEquals("SO", targetEntity);
86     }
87
88     @Test
89     public void extractTargetEntityMDCTest() {
90         MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO");
91         HttpHeaders headers = new HttpHeaders();
92         when(clientRequest.getHeaders()).thenReturn(headers);
93
94         String targetEntity = springClientFilter.extractTargetEntity(clientRequest);
95         assertEquals("SO", targetEntity);
96     }
97
98     @Test
99     public void extractTargetEntityNoHeaderTest() {
100         HttpHeaders headers = new HttpHeaders();
101         when(clientRequest.getHeaders()).thenReturn(headers);
102
103         String targetEntity = springClientFilter.extractTargetEntity(clientRequest);
104         assertEquals("Unknown-Target-Entity", targetEntity);
105     }
106
107     @Test
108     public void setupMDCTest() throws URISyntaxException {
109         doReturn("SO").when(springClientFilter).extractTargetEntity(clientRequest);
110         URI uri = new URI("onap/so/serviceInstances");
111         when(clientRequest.getURI()).thenReturn(uri);
112
113         springClientFilter.setupMDC(clientRequest);
114
115         assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
116         assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
117         assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
118         assertNotNull(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
119         assertNotNull(ONAPLogConstants.MDCs.SERVICE_NAME);
120         assertNotNull(ONAPLogConstants.MDCs.SERVER_FQDN);
121     }
122
123     @Test
124     public void setupHeadersTest() {
125         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, "8819bfb4-69d2-43fc-b0d6-81d2690533ea");
126         HttpHeaders headers = new HttpHeaders();
127         when(clientRequest.getHeaders()).thenReturn(headers);
128         doReturn("0a908a5d-e774-4558-96ff-6edcbba65483").when(springClientFilter).extractRequestID(clientRequest);
129
130         springClientFilter.setupHeaders(clientRequest);
131
132         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(ONAPLogConstants.Headers.REQUEST_ID));
133         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.HEADER_REQUEST_ID));
134         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.TRANSACTION_ID));
135         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.TRANSACTION_ID));
136         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.ECOMP_REQUEST_ID));
137         assertEquals("8819bfb4-69d2-43fc-b0d6-81d2690533ea", headers.getFirst(ONAPLogConstants.Headers.INVOCATION_ID));
138         assertEquals("UNKNOWN", headers.getFirst(ONAPLogConstants.Headers.PARTNER_NAME));
139     }
140
141     @Test
142     public void extractRequestIDTest() {
143         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, "0a908a5d-e774-4558-96ff-6edcbba65483");
144         String requestId = springClientFilter.extractRequestID(clientRequest);
145         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", requestId);
146     }
147
148     @Test
149     public void extractRequestIDNullTest() {
150         String requestId = springClientFilter.extractRequestID(clientRequest);
151         assertNotNull(requestId);
152         assertNotNull(ONAPLogConstants.MDCs.LOG_TIMESTAMP);
153         assertNotNull(ONAPLogConstants.MDCs.ELAPSED_TIME);
154     }
155
156     @Test
157     public void interceptTest() throws IOException {
158         byte[] body = new byte[3];
159         doNothing().when(springClientFilter).processRequest(clientRequest, body);
160         doReturn(response).when(execution).execute(clientRequest, body);
161         doNothing().when(springClientFilter).processResponse(response);
162
163         ClientHttpResponse httpResponse = springClientFilter.intercept(clientRequest, body, execution);
164         assertEquals(response, httpResponse);
165     }
166 }