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=========================================================
20 package org.onap.aai.aailog.filter;
22 import org.junit.After;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.Spy;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.onap.logging.filter.base.Constants;
30 import org.onap.logging.ref.slf4j.ONAPLogConstants;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.http.HttpRequest;
35 import java.net.InetAddress;
37 import java.net.URISyntaxException;
38 import java.net.UnknownHostException;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNotNull;
42 import static org.mockito.Mockito.doReturn;
44 @RunWith(MockitoJUnitRunner.class)
45 public class RestClientLoggingInterceptorTest {
48 private HttpRequest httpRequest;
52 private RestClientLoggingInterceptor restClientLoggingInterceptor;
55 public void tearDown() {
60 public void setupHeadersTest() {
61 String transId="37b3ab2a-e57e-4fe8-8d8f-eee3019efce6";
62 HttpHeaders headers = new HttpHeaders();
63 headers.add(Constants.HttpHeaders.TRANSACTION_ID, transId);
64 restClientLoggingInterceptor.setupHeaders(httpRequest, headers);
66 assertEquals(transId, headers.getFirst(Constants.HttpHeaders.TRANSACTION_ID));
67 assertEquals(transId, headers.getFirst(Constants.HttpHeaders.ECOMP_REQUEST_ID));
68 assertEquals(transId, headers.getFirst(ONAPLogConstants.Headers.REQUEST_ID));
69 assertEquals(transId, headers.getFirst(Constants.HttpHeaders.HEADER_REQUEST_ID));
73 public void getServiceNameTest() throws URISyntaxException {
76 uri = new URI("https://localhost:9999/aai/v1/cloud-infrastructure/complexes/complex/complex-1");
77 } catch (URISyntaxException e) {
80 doReturn(uri).when(httpRequest).getURI();
81 String serviceName = restClientLoggingInterceptor.getServiceName(httpRequest);
83 assertEquals("/aai/v1/cloud-infrastructure/complexes", serviceName);
87 public void setupMDCTest() throws URISyntaxException {
88 URI uri = new URI("https://localhost:9999/aai/v1/cloud-infrastructure/complexes/complex/complex-1");
89 doReturn(uri).when(httpRequest).getURI();
90 HttpHeaders headers = new HttpHeaders();
91 restClientLoggingInterceptor.pre(httpRequest, headers);
92 assertEquals("/aai/v1/cloud-infrastructure/complexes", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME) );
93 assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
94 String serverFQDN = "";
95 InetAddress addr = null;
97 addr = InetAddress.getLocalHost();
98 serverFQDN = addr.getCanonicalHostName();
100 } catch (UnknownHostException e) {
103 assertEquals(serverFQDN, MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN));
104 assertNotNull(headers.getFirst(ONAPLogConstants.Headers.INVOCATION_ID));