diff options
Diffstat (limited to 'vid-app-common/src/test')
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/logging/VidLoggingInterceptorTest.java | 61 |
1 files changed, 57 insertions, 4 deletions
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<String, String> hashMap = new HashMap<>(); + hashMap.put("Authorization","abcdefghi"); + SimpleMap headers = new SimpleHashMap(hashMap); + assertNull(interceptor.getBasicAuthUserName(headers)); } } |