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