2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.logging;
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;
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;
40 import org.testng.annotations.BeforeMethod;
41 import org.testng.annotations.Test;
43 public class VidLoggingInterceptorTest {
45 private VidLoggingInterceptor interceptor;
48 private ControllersUtils controllersUtils;
52 MockitoAnnotations.initMocks(this);
53 interceptor = new VidLoggingInterceptor(controllersUtils);
58 public void testAdditionalPreHandling() {
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);
69 interceptor.additionalPreHandling(mock(HttpServletRequest.class));
72 assertEquals(MDC.get(INBOUND_INVO_ID), invoID);
73 assertEquals(MDC.get(MDCs.PARTNER_NAME), myUserId);
77 public void whenNoUserId_previousPartnerNameIsPreserved() {
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)))
86 interceptor.additionalPreHandling(mockedRequest);
89 assertEquals(MDC.get(MDCs.PARTNER_NAME), prevPartnerName);
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));