Skip to content

Commit

Permalink
Refactored the test to stablize the test
Browse files Browse the repository at this point in the history
  • Loading branch information
lijamie98 committed Sep 6, 2023
1 parent 4aa31af commit 6deb82b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,9 @@ public RequestLoggerFilter(AppLoggingConfig appLoggingConfig) {
this.appLoggingConfig = appLoggingConfig;
}

@Override
protected void doFilterInternal(
protected void doFilterWithLogging(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {

if (!appLoggingConfig.isRequestLoggerEnabled()) {
filterChain.doFilter(request, response);
return;
}

long startTime = System.currentTimeMillis();

// ========= Log request and response payload ("body") ========
Expand Down Expand Up @@ -77,6 +70,19 @@ protected void doFilterInternal(
wrappedResponse.copyBodyToResponse();
}

@Override
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {

if (!appLoggingConfig.isRequestLoggerEnabled()) {
filterChain.doFilter(request, response);
return;
}

doFilterWithLogging(request, response, filterChain);
}

/**
* getBody will get the response body (if it's an error) or omit it if it's not an error.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class RequestLoggerFilterTest {

@BeforeEach
fun setUp() {
appLoggingConfig = AppLoggingConfig()
appLoggingConfig = mockk<AppLoggingConfig>(relaxed = true)
filterChain = mockk<FilterChain>(relaxed = true)
requestLoggerFilter = RequestLoggerFilter(appLoggingConfig)
request = MockHttpServletRequest()
response = MockHttpServletResponse()
filterChain = mockk<FilterChain>(relaxed = true)
mockkStatic(Log::class)
}

Expand All @@ -36,28 +36,26 @@ class RequestLoggerFilterTest {
@Test
fun `doFilterInternal when RequestLogger disabled should not send debug and trace logs`() {
// Arrange
appLoggingConfig.isRequestLoggerEnabled = false
every { appLoggingConfig.isRequestLoggerEnabled } returns false

// Act
requestLoggerFilter.doFilterInternal(request, response, filterChain)

// Assert
verify(exactly = 1) { filterChain.doFilter(any(), any()) }
verify(exactly = 0) { Log.debugF(any(), *varargAny { true }) }
verify(exactly = 0) { Log.trace(any()) }
verify(exactly = 0) { requestLoggerFilter.doFilterWithLogging(any(), any(), any()) }
}

@Test
fun `doFilterInternal when RequestLogger enabled should send debug and trace logs`() {
// Arrange
appLoggingConfig.isRequestLoggerEnabled = true
every { appLoggingConfig.isRequestLoggerEnabled } returns false

// Act
requestLoggerFilter.doFilterInternal(request, response, filterChain)

// Assert
verify(exactly = 1) { filterChain.doFilter(any(), any()) }
verify(exactly = 1) { Log.debugF(any(), *varargAny { true }) }
verify(exactly = 1) { Log.trace(any()) }
verify(exactly = 1) { requestLoggerFilter.doFilterWithLogging(any(), any(), any()) }
}
}

0 comments on commit 6deb82b

Please sign in to comment.