From 768742ee00920426d9d81aef56dcd0298bf9f1d0 Mon Sep 17 00:00:00 2001 From: Rodrigo Lima Date: Thu, 14 May 2020 10:14:34 -0400 Subject: Remove powermock from ContextTracker - Remove powermock from ContextTrackerTest - Add package level constructor to ContextTracker to pass clear and put functions Issue-ID: SDC-3058 Signed-off-by: Rodrigo Lima Change-Id: I82519538b763b00f2768801f06929773bfb58013 --- .../sdc/logging/servlet/ContextTrackerTest.java | 117 ++++++++------------- 1 file changed, 46 insertions(+), 71 deletions(-) (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test') diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java index f1066c447b..272dfdd164 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java @@ -16,23 +16,19 @@ package org.openecomp.sdc.logging.servlet; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static org.powermock.api.mockito.PowerMockito.verifyStatic; -import static org.powermock.api.mockito.PowerMockito.when; - +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.function.Consumer; +import java.util.function.Supplier; import javax.servlet.http.HttpServletRequest; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; +import org.junit.jupiter.api.Test; + import org.openecomp.sdc.logging.api.ContextData; -import org.openecomp.sdc.logging.api.LoggingContext; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; /** * Populating context from request data. @@ -40,8 +36,6 @@ import org.powermock.modules.junit4.PowerMockRunner; * @author evitaliy * @since 01 Aug 2018 */ -@RunWith(PowerMockRunner.class) -@PrepareForTest(LoggingContext.class) public class ContextTrackerTest { private static final String X_REQUEST_ID = "X-REQUEST-ID"; @@ -50,50 +44,52 @@ public class ContextTrackerTest { private static final String X_PARTNER_NAME = "X-PARTNER-NAME"; private static final HttpHeader PARTNER_NAME_HEADER = new HttpHeader(X_PARTNER_NAME); - @Test(expected = NullPointerException.class) + private static final String PROVIDER_PATH = "org.openecomp.sdc.logging.servlet.TestLogginContextService"; + + private ContextData lastContext = ContextData.builder().build(); + + private final Supplier loggingContextClear = () -> { + lastContext = ContextData.builder().build(); + return null; + }; + + private final Consumer loggingContextPut = (contextData) -> lastContext = contextData; + + private final ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, + REQUEST_ID_HEADER, + loggingContextClear, + loggingContextPut); + + @Test public void throwExceptionWhenPartnerNamesNull() { - new ContextTracker(null, REQUEST_ID_HEADER); + assertThrows(NullPointerException.class, + () -> new ContextTracker(null, REQUEST_ID_HEADER)); } - @Test(expected = NullPointerException.class) + @Test public void throwExceptionWhenRequestIdsNull() { - new ContextTracker(PARTNER_NAME_HEADER, null); + assertThrows(NullPointerException.class, + () -> new ContextTracker(PARTNER_NAME_HEADER, null)); } @Test public void requestIdCopiedWhenGiven() { - mockStatic(LoggingContext.class); - final String requestId = "request-id-for-unit-testing"; HttpServletRequest request = mock(HttpServletRequest.class); when(request.getHeader(X_REQUEST_ID)).thenReturn(requestId); - ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER); tracker.preRequest(request); - ArgumentCaptor contextDataCaptor = ArgumentCaptor.forClass(ContextData.class); - verifyStatic(LoggingContext.class); - - LoggingContext.put(contextDataCaptor.capture()); - - assertEquals(requestId, contextDataCaptor.getValue().getRequestId()); + assertEquals(requestId, lastContext.getRequestId()); } @Test public void requestIdGeneratedWhenNotGiven() { - mockStatic(LoggingContext.class); - - ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER); tracker.preRequest(mock(HttpServletRequest.class)); - ArgumentCaptor contextDataCaptor = ArgumentCaptor.forClass(ContextData.class); - verifyStatic(LoggingContext.class); - - LoggingContext.put(contextDataCaptor.capture()); - - String requestId = contextDataCaptor.getValue().getRequestId(); + String requestId = lastContext.getRequestId(); assertNotNull(requestId); assertFalse(requestId.isEmpty()); } @@ -101,65 +97,44 @@ public class ContextTrackerTest { @Test public void partnerNameCopiedWhenGiven() { - mockStatic(LoggingContext.class); - final String partner = "partner-name-for-unit-testing"; HttpServletRequest request = mock(HttpServletRequest.class); when(request.getHeader(X_PARTNER_NAME)).thenReturn(partner); - ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER); tracker.preRequest(request); - ArgumentCaptor contextDataCaptor = ArgumentCaptor.forClass(ContextData.class); - verifyStatic(LoggingContext.class); - - LoggingContext.put(contextDataCaptor.capture()); - - assertEquals(partner, contextDataCaptor.getValue().getPartnerName()); + assertEquals(partner, lastContext.getPartnerName()); } @Test public void partnerNameIsUnknownWhenNotGiven() { - mockStatic(LoggingContext.class); - - ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER); tracker.preRequest(mock(HttpServletRequest.class)); - ArgumentCaptor contextDataCaptor = ArgumentCaptor.forClass(ContextData.class); - verifyStatic(LoggingContext.class); - - LoggingContext.put(contextDataCaptor.capture()); - - assertEquals(contextDataCaptor.getValue().getPartnerName(), "UNKNOWN"); + assertEquals("UNKNOWN", lastContext.getPartnerName()); } @Test public void serviceNameGenerated() { - mockStatic(LoggingContext.class); - - ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER); - tracker.preRequest(mock(HttpServletRequest.class)); - - ArgumentCaptor contextDataCaptor = ArgumentCaptor.forClass(ContextData.class); - verifyStatic(LoggingContext.class); + HttpServletRequest request = mock(HttpServletRequest.class); - LoggingContext.put(contextDataCaptor.capture()); + when(request.getMethod()).thenReturn("GET"); + when(request.getRequestURI()).thenReturn("/testUri"); + tracker.preRequest(request); - assertNotNull(contextDataCaptor.getValue().getServiceName()); + assertEquals("GET: /testUri", lastContext .getServiceName()); } @Test public void contextClearedWhenRequestFinished() { - mockStatic(LoggingContext.class); + tracker.preRequest(mock(HttpServletRequest.class)); + + assertNotNull(lastContext.getRequestId()); - ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER); tracker.postRequest(mock(RequestProcessingResult.class)); - verifyStatic(LoggingContext.class); - LoggingContext.clear(); + assertEquals(ContextData.builder().build(), lastContext); } } - -- cgit 1.2.3-korg