Skip to content

Commit

Permalink
fix: logservice tests to be independent of logback rule
Browse files Browse the repository at this point in the history
  • Loading branch information
mebo4b committed Aug 17, 2020
1 parent 396b84e commit 87905bc
Showing 1 changed file with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
package de.caritas.cob.messageservice.api.service;

import static net.therore.logback.EventMatchers.text;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.powermock.reflect.Whitebox.setInternalState;

import de.caritas.cob.messageservice.api.exception.RocketChatGetGroupMessagesException;
import java.io.PrintWriter;
import net.therore.logback.LogbackRule;
import org.junit.Rule;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;

@RunWith(MockitoJUnitRunner.class)
public class LogServiceTest {

private final String ERROR_MESSAGE = "error";
private final String RC_SERVICE_ERROR_TEXT = "Rocket.Chat service error: ";
private final String INTERNAL_SERVER_ERROR_TEXT = "Internal Server Error: ";
private final String BAD_REQUEST_TEXT = "Bad Request: ";
private static final String ERROR_MESSAGE = "error";
private static final String RC_SERVICE_ERROR_TEXT = "Rocket.Chat service error: {}";
private static final String INTERNAL_SERVER_ERROR_TEXT = "Internal Server Error: ";
private static final String BAD_REQUEST_TEXT = "Bad Request: {}";

@Mock
private RocketChatGetGroupMessagesException rocketChatGetGroupMessagesException;

@Mock
Exception exception;

@Rule
public LogbackRule rule = new LogbackRule();
@Mock
private Logger logger;

@Before
public void setup() {
setInternalState(LogService.class, "LOGGER", logger);
}

@Test
public void logRocketChatServiceError_Should_LogExceptionStackTrace() {
Expand All @@ -46,14 +51,14 @@ public void logRocketChatServiceError_Should_LogExceptionStackTrace() {
public void logRocketChatServiceError_Should_LogErrorMessage() {

LogService.logRocketChatServiceError(ERROR_MESSAGE);
verify(rule.getLog(), times(1)).contains(argThat(text(RC_SERVICE_ERROR_TEXT + ERROR_MESSAGE)));
verify(logger, times(1)).error(eq(RC_SERVICE_ERROR_TEXT), eq(ERROR_MESSAGE));
}

@Test
public void logRocketChatServiceError_Should_LogErrorMessageAndExceptionStackTrace() {

LogService.logRocketChatServiceError(ERROR_MESSAGE, rocketChatGetGroupMessagesException);
verify(rule.getLog(), times(1)).contains(argThat(text(RC_SERVICE_ERROR_TEXT + ERROR_MESSAGE)));
verify(logger, times(1)).error(eq(RC_SERVICE_ERROR_TEXT), eq(ERROR_MESSAGE));
verify(rocketChatGetGroupMessagesException, atLeastOnce())
.printStackTrace(any(PrintWriter.class));
}
Expand All @@ -70,7 +75,7 @@ public void logUserServiceHelperError_Should_LogExceptionStackTrace() {
public void logInfo_Should_LogMessage() {

LogService.logInfo(ERROR_MESSAGE);
verify(rule.getLog(), times(1)).contains(argThat(text(ERROR_MESSAGE)));
verify(logger, times(1)).info(eq(ERROR_MESSAGE));
}

@Test
Expand Down Expand Up @@ -99,15 +104,15 @@ public void logRocketChatBadRequestError_Should_LogExceptionStackTrace() {
public void logInternalServerError_Should_LogErrorMessageAndExceptionStackTrace() {

LogService.logInternalServerError(ERROR_MESSAGE, exception);
verify(rule.getLog(), times(1))
.contains(argThat(text(INTERNAL_SERVER_ERROR_TEXT + ERROR_MESSAGE)));
verify(logger, times(1))
.error(eq("{}{}"), eq(INTERNAL_SERVER_ERROR_TEXT), eq(ERROR_MESSAGE));
verify(exception, atLeastOnce()).printStackTrace(any(PrintWriter.class));
}

@Test
public void logBadRequest_Should_LogMessage() {

LogService.logBadRequest(ERROR_MESSAGE);
verify(rule.getLog(), times(1)).contains(argThat(text(BAD_REQUEST_TEXT + ERROR_MESSAGE)));
verify(logger, times(1)).error(eq(BAD_REQUEST_TEXT), eq(ERROR_MESSAGE));
}
}

0 comments on commit 87905bc

Please sign in to comment.