Increase code coverage on aai-common: aai-els-onap-logging library
[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 package org.onap.aai.aailog.filter;
21
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;
31 import org.slf4j.MDC;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.http.HttpRequest;
34
35 import java.net.InetAddress;
36 import java.net.URI;
37 import java.net.URISyntaxException;
38 import java.net.UnknownHostException;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNotNull;
42 import static org.mockito.Mockito.doReturn;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class RestClientLoggingInterceptorTest {
46
47         @Mock
48         private HttpRequest httpRequest;
49
50         @Spy
51         @InjectMocks
52         private RestClientLoggingInterceptor restClientLoggingInterceptor;
53
54         @After
55         public void tearDown() {
56             MDC.clear();
57         }
58
59         @Test
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);
65
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));
70         }
71
72         @Test
73         public void getServiceNameTest() throws URISyntaxException {
74             URI uri = null;
75             try {
76                 uri = new URI("https://localhost:9999/aai/v1/cloud-infrastructure/complexes/complex/complex-1");
77             } catch (URISyntaxException e) {
78                 throw e;
79             }
80             doReturn(uri).when(httpRequest).getURI();
81             String serviceName = restClientLoggingInterceptor.getServiceName(httpRequest);
82
83             assertEquals("/aai/v1/cloud-infrastructure/complexes", serviceName);
84         }
85
86        @Test
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;
96            try {
97                addr = InetAddress.getLocalHost();
98                serverFQDN = addr.getCanonicalHostName();
99
100            } catch (UnknownHostException e) {
101                serverFQDN = "";
102            }
103            assertEquals(serverFQDN, MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN));
104            assertNotNull(headers.getFirst(ONAPLogConstants.Headers.INVOCATION_ID));
105         }
106 }