Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / test / java / org / onap / vid / logging / VidLoggingInterceptorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.vid.logging;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26 import static org.onap.vid.logging.VidLoggingInterceptor.INBOUND_INVO_ID;
27 import static org.testng.Assert.assertEquals;
28 import static org.testng.AssertJUnit.assertNull;
29
30 import java.util.HashMap;
31 import javax.servlet.http.HttpServletRequest;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.logging.filter.base.SimpleHashMap;
35 import org.onap.logging.filter.base.SimpleMap;
36 import org.onap.logging.ref.slf4j.ONAPLogConstants;
37 import org.onap.logging.ref.slf4j.ONAPLogConstants.MDCs;
38 import org.onap.vid.controller.ControllersUtils;
39 import org.slf4j.MDC;
40 import org.testng.annotations.BeforeMethod;
41 import org.testng.annotations.Test;
42
43 public class VidLoggingInterceptorTest {
44
45     private VidLoggingInterceptor interceptor;
46
47     @Mock
48     private ControllersUtils controllersUtils;
49
50     @BeforeMethod
51     public void setup() {
52         MockitoAnnotations.initMocks(this);
53         interceptor = new VidLoggingInterceptor(controllersUtils);
54         MDC.clear();
55     }
56
57     @Test
58     public void testAdditionalPreHandling() {
59
60         //given
61         final String invoID = "987";
62         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invoID);
63         MDC.put(MDCs.PARTNER_NAME, "wrongPartnerName");
64         final String myUserId = "myUserId";
65         when(controllersUtils.extractUserId(any(HttpServletRequest.class)))
66             .thenReturn(myUserId);
67
68         //when
69         interceptor.additionalPreHandling(mock(HttpServletRequest.class));
70
71         //then
72         assertEquals(MDC.get(INBOUND_INVO_ID), invoID);
73         assertEquals(MDC.get(MDCs.PARTNER_NAME), myUserId);
74     }
75
76     @Test
77     public void whenNoUserId_previousPartnerNameIsPreserved() {
78         //given
79         final String prevPartnerName = "prevPartnerName";
80         MDC.put(MDCs.PARTNER_NAME, prevPartnerName);
81         final HttpServletRequest mockedRequest = mock(HttpServletRequest.class);
82         when(controllersUtils.extractUserId(any(HttpServletRequest.class)))
83             .thenReturn("");
84
85         //when
86         interceptor.additionalPreHandling(mockedRequest);
87
88         //then
89         assertEquals(MDC.get(MDCs.PARTNER_NAME), prevPartnerName);
90
91     }
92
93     @Test
94     public void givenNotValidAuthorizationHeader_whenGetBasicAuthUserName_noExceptionIsThrown() {
95         HashMap<String, String> hashMap = new HashMap<>();
96         hashMap.put("Authorization","abcdefghi");
97         SimpleMap headers = new SimpleHashMap(hashMap);
98         assertNull(interceptor.getBasicAuthUserName(headers));
99     }
100
101 }