From 06e518055d9758ee3ecc2e40efe13c7a5d982a2a Mon Sep 17 00:00:00 2001 From: Eylon Malin Date: Wed, 6 Nov 2019 08:16:08 +0200 Subject: print user-id in partner name record of audit log Issue-ID: VID-253 Also add more verifications for audit log format Change-Id: I00976a63fd1a795a972c13ad975d6a71248e5c8b Signed-off-by: Eylon Malin --- .../vid/logging/VidLoggingInterceptorTest.java | 61 ++++++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) (limited to 'vid-app-common/src/test/java/org') diff --git a/vid-app-common/src/test/java/org/onap/vid/logging/VidLoggingInterceptorTest.java b/vid-app-common/src/test/java/org/onap/vid/logging/VidLoggingInterceptorTest.java index fc78ed714..c31d00df8 100644 --- a/vid-app-common/src/test/java/org/onap/vid/logging/VidLoggingInterceptorTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/logging/VidLoggingInterceptorTest.java @@ -20,10 +20,22 @@ package org.onap.vid.logging; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import static org.onap.vid.logging.VidLoggingInterceptor.INBOUND_INVO_ID; import static org.testng.Assert.assertEquals; +import static org.testng.AssertJUnit.assertNull; +import java.util.HashMap; +import javax.servlet.http.HttpServletRequest; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.logging.filter.base.SimpleHashMap; +import org.onap.logging.filter.base.SimpleMap; import org.onap.logging.ref.slf4j.ONAPLogConstants; +import org.onap.logging.ref.slf4j.ONAPLogConstants.MDCs; +import org.onap.vid.controller.ControllersUtils; import org.slf4j.MDC; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -32,17 +44,58 @@ public class VidLoggingInterceptorTest { private VidLoggingInterceptor interceptor; + @Mock + private ControllersUtils controllersUtils; + @BeforeMethod public void setup() { - interceptor = new VidLoggingInterceptor(); + MockitoAnnotations.initMocks(this); + interceptor = new VidLoggingInterceptor(controllersUtils); MDC.clear(); } @Test public void testAdditionalPreHandling() { - MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, "987"); - interceptor.additionalPreHandling(null); - assertEquals(MDC.get(INBOUND_INVO_ID), "987"); + + //given + final String invoID = "987"; + MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invoID); + MDC.put(MDCs.PARTNER_NAME, "wrongPartnerName"); + final String myUserId = "myUserId"; + when(controllersUtils.extractUserId(any(HttpServletRequest.class))) + .thenReturn(myUserId); + + //when + interceptor.additionalPreHandling(mock(HttpServletRequest.class)); + + //then + assertEquals(MDC.get(INBOUND_INVO_ID), invoID); + assertEquals(MDC.get(MDCs.PARTNER_NAME), myUserId); + } + + @Test + public void whenNoUserId_previousPartnerNameIsPreserved() { + //given + final String prevPartnerName = "prevPartnerName"; + MDC.put(MDCs.PARTNER_NAME, prevPartnerName); + final HttpServletRequest mockedRequest = mock(HttpServletRequest.class); + when(controllersUtils.extractUserId(any(HttpServletRequest.class))) + .thenReturn(""); + + //when + interceptor.additionalPreHandling(mockedRequest); + + //then + assertEquals(MDC.get(MDCs.PARTNER_NAME), prevPartnerName); + + } + + @Test + public void givenNotValidAuthorizationHeader_whenGetBasicAuthUserName_noExceptionIsThrown() { + HashMap hashMap = new HashMap<>(); + hashMap.put("Authorization","abcdefghi"); + SimpleMap headers = new SimpleHashMap(hashMap); + assertNull(interceptor.getBasicAuthUserName(headers)); } } -- cgit 1.2.3-korg