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