2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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.aai.aailog.filter;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Mockito.doReturn;
27 import java.net.InetAddress;
29 import java.net.URISyntaxException;
30 import java.net.UnknownHostException;
32 import org.junit.After;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.Spy;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.onap.logging.filter.base.Constants;
40 import org.onap.logging.ref.slf4j.ONAPLogConstants;
42 import org.springframework.http.HttpHeaders;
43 import org.springframework.http.HttpRequest;
45 @RunWith(MockitoJUnitRunner.class)
46 public class RestClientLoggingInterceptorTest {
49 private HttpRequest httpRequest;
53 private RestClientLoggingInterceptor restClientLoggingInterceptor;
56 public void tearDown() {
61 public void setupHeadersTest() {
62 String transId = "37b3ab2a-e57e-4fe8-8d8f-eee3019efce6";
63 HttpHeaders headers = new HttpHeaders();
64 headers.add(Constants.HttpHeaders.TRANSACTION_ID, transId);
65 restClientLoggingInterceptor.setupHeaders(httpRequest, headers);
67 assertEquals(transId, headers.getFirst(Constants.HttpHeaders.TRANSACTION_ID));
68 assertEquals(transId, headers.getFirst(Constants.HttpHeaders.ECOMP_REQUEST_ID));
69 assertEquals(transId, headers.getFirst(ONAPLogConstants.Headers.REQUEST_ID));
70 assertEquals(transId, headers.getFirst(Constants.HttpHeaders.HEADER_REQUEST_ID));
74 public void getServiceNameTest() throws URISyntaxException {
77 uri = new URI("https://localhost:9999/aai/v1/cloud-infrastructure/complexes/complex/complex-1");
78 } catch (URISyntaxException e) {
81 doReturn(uri).when(httpRequest).getURI();
82 String serviceName = restClientLoggingInterceptor.getServiceName(httpRequest);
84 assertEquals("/aai/v1/cloud-infrastructure/complexes", serviceName);
88 public void setupMDCTest() throws URISyntaxException {
89 URI uri = new URI("https://localhost:9999/aai/v1/cloud-infrastructure/complexes/complex/complex-1");
90 doReturn(uri).when(httpRequest).getURI();
91 HttpHeaders headers = new HttpHeaders();
92 restClientLoggingInterceptor.pre(httpRequest, headers);
93 assertEquals("/aai/v1/cloud-infrastructure/complexes", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
94 assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
95 String serverFQDN = "";
96 InetAddress addr = null;
98 addr = InetAddress.getLocalHost();
99 serverFQDN = addr.getCanonicalHostName();
101 } catch (UnknownHostException e) {
104 assertEquals(serverFQDN, MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN));
105 assertNotNull(headers.getFirst(ONAPLogConstants.Headers.INVOCATION_ID));