diff options
author | Rodrigo Lima <rodrigo.lima@yoppworks.com> | 2020-05-14 10:14:34 -0400 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2020-05-17 14:55:50 +0000 |
commit | 768742ee00920426d9d81aef56dcd0298bf9f1d0 (patch) | |
tree | 810fecbb50ad32e803ea10ebc68e79b1b19e29b8 /openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test | |
parent | 3a28b9af8d08fc2cf8d9af9b38578fd034999dc2 (diff) |
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 <rodrigo.lima@yoppworks.com>
Change-Id: I82519538b763b00f2768801f06929773bfb58013
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test')
-rw-r--r-- | openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java | 117 |
1 files changed, 46 insertions, 71 deletions
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<Void> loggingContextClear = () -> { + lastContext = ContextData.builder().build(); + return null; + }; + + private final Consumer<ContextData> 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<ContextData> 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<ContextData> 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<ContextData> 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<ContextData> 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<ContextData> 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); } } - |