Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-els-onap-logging / src / test / java / org / onap / aai / aailog / filter / RestClientLoggingInterceptorTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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.aai.aailog.filter;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Mockito.doReturn;
26
27 import java.net.InetAddress;
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import java.net.UnknownHostException;
31
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;
41 import org.slf4j.MDC;
42 import org.springframework.http.HttpHeaders;
43 import org.springframework.http.HttpRequest;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class RestClientLoggingInterceptorTest {
47
48     @Mock
49     private HttpRequest httpRequest;
50
51     @Spy
52     @InjectMocks
53     private RestClientLoggingInterceptor restClientLoggingInterceptor;
54
55     @After
56     public void tearDown() {
57         MDC.clear();
58     }
59
60     @Test
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);
66
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));
71     }
72
73     @Test
74     public void getServiceNameTest() throws URISyntaxException {
75         URI uri = null;
76         try {
77             uri = new URI("https://localhost:9999/aai/v1/cloud-infrastructure/complexes/complex/complex-1");
78         } catch (URISyntaxException e) {
79             throw e;
80         }
81         doReturn(uri).when(httpRequest).getURI();
82         String serviceName = restClientLoggingInterceptor.getServiceName(httpRequest);
83
84         assertEquals("/aai/v1/cloud-infrastructure/complexes", serviceName);
85     }
86
87     @Test
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;
97         try {
98             addr = InetAddress.getLocalHost();
99             serverFQDN = addr.getCanonicalHostName();
100
101         } catch (UnknownHostException e) {
102             serverFQDN = "";
103         }
104         assertEquals(serverFQDN, MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN));
105         assertNotNull(headers.getFirst(ONAPLogConstants.Headers.INVOCATION_ID));
106     }
107 }