d0b7b3721d52e6fff158a5cf48bfc8624d7bf134
[aai/aai-common.git] /
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.when;
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.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.InjectMocks;
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
43 import jakarta.ws.rs.client.ClientRequestContext;
44 import jakarta.ws.rs.core.MultivaluedHashMap;
45 import jakarta.ws.rs.core.MultivaluedMap;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class RestControllerClientLoggingInterceptorTest {
49
50     @Spy
51     private ClientRequestContext clientRequest;
52
53     @Spy
54     @InjectMocks
55     private RestControllerClientRequestLoggingInterceptor restControllerClientLoggingInterceptor;
56
57     @Before
58     public void init() throws URISyntaxException {
59         System.setProperty("jakarta.ws.rs.ext.RuntimeDelegate", "com.sun.ws.rs.ext.RuntimeDelegateImpl");
60         when(clientRequest.getHeaders()).thenReturn(new MultivaluedHashMap<String, Object>());
61         when(clientRequest.getUri())
62                 .thenReturn(new URI("https://localhost:9999/aai/v1/cloud-infrastructure/complexes/complex/complex-1"));
63
64         // clientRequest = ClientRequest.create().build(
65         // new
66         // URI("https://localhost:9999/aai/v1/cloud-infrastructure/complexes/complex/complex-1"),
67         // "GET");
68     }
69
70     @After
71     public void tearDown() {
72         MDC.clear();
73     }
74
75     @Test
76     public void setupHeadersTest() throws java.net.URISyntaxException {
77
78         String transId = "37b3ab2a-e57e-4fe8-8d8f-eee3019efce6";
79         MultivaluedMap<String, Object> requestHeaders = new MultivaluedHashMap<String, Object>();
80         requestHeaders.add(Constants.HttpHeaders.TRANSACTION_ID, transId);
81         when(clientRequest.getHeaders()).thenReturn(requestHeaders);
82         when(clientRequest.getUri())
83                 .thenReturn(new URI("https://localhost:9999/aai/v1/cloud-infrastructure/complexes/complex/complex-1"));
84
85         clientRequest.getHeaders().putAll(requestHeaders);
86         restControllerClientLoggingInterceptor.pre(clientRequest);
87         MultivaluedMap<String, Object> headers = clientRequest.getHeaders();
88
89         assertEquals(transId, headers.getFirst(Constants.HttpHeaders.TRANSACTION_ID));
90         assertEquals(transId, headers.getFirst(Constants.HttpHeaders.ECOMP_REQUEST_ID));
91         assertEquals(transId, headers.getFirst(ONAPLogConstants.Headers.REQUEST_ID));
92         assertEquals(transId, headers.getFirst(Constants.HttpHeaders.HEADER_REQUEST_ID));
93         assertNotNull(headers.getFirst(ONAPLogConstants.Headers.INVOCATION_ID));
94     }
95
96     @Test
97     public void getServiceNameTest() {
98         String serviceName = restControllerClientLoggingInterceptor.getServiceName(clientRequest);
99         assertEquals("/aai/v1/cloud-infrastructure/complexes", serviceName);
100     }
101
102     @Test
103     public void setupMDCTest() throws URISyntaxException {
104         restControllerClientLoggingInterceptor.pre(clientRequest);
105         assertEquals("/aai/v1/cloud-infrastructure/complexes", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
106         assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
107         String serverFQDN = "";
108         InetAddress addr = null;
109         try {
110             addr = InetAddress.getLocalHost();
111             serverFQDN = addr.getCanonicalHostName();
112
113         } catch (UnknownHostException e) {
114             serverFQDN = "";
115         }
116         assertEquals(serverFQDN, MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN));
117     }
118 }