2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.logging.filter.spring;
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;
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;
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;
47 @RunWith(MockitoJUnitRunner.class)
48 public class SpringClientFilterTest {
51 private MDCSetup mdcSetup;
54 private ClientHttpResponse response;
57 private HttpRequest clientRequest;
60 private ClientHttpRequestExecution execution;
64 private SpringClientFilter springClientFilter;
67 public void tearDown() {
72 public void processResponseTest() throws IOException {
73 String partnerName = springClientFilter.getPartnerName();
75 assertEquals("UNKNOWN", partnerName);
79 public void extractTargetEntityTest() {
80 HttpHeaders headers = new HttpHeaders();
81 headers.add(Constants.HttpHeaders.TARGET_ENTITY_HEADER, "SO");
82 when(clientRequest.getHeaders()).thenReturn(headers);
84 String targetEntity = springClientFilter.extractTargetEntity(clientRequest);
85 assertEquals("SO", targetEntity);
89 public void extractTargetEntityMDCTest() {
90 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO");
91 HttpHeaders headers = new HttpHeaders();
92 when(clientRequest.getHeaders()).thenReturn(headers);
94 String targetEntity = springClientFilter.extractTargetEntity(clientRequest);
95 assertEquals("SO", targetEntity);
99 public void extractTargetEntityNoHeaderTest() {
100 HttpHeaders headers = new HttpHeaders();
101 when(clientRequest.getHeaders()).thenReturn(headers);
103 String targetEntity = springClientFilter.extractTargetEntity(clientRequest);
104 assertEquals("Unknown-Target-Entity", targetEntity);
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);
113 springClientFilter.setupMDC(clientRequest);
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);
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);
130 springClientFilter.setupHeaders(clientRequest);
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));
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);
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);
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);
163 ClientHttpResponse httpResponse = springClientFilter.intercept(clientRequest, body, execution);
164 assertEquals(response, httpResponse);