|
| 1 | +package com.stackify.slf4j.guide.controllers; |
| 2 | + |
| 3 | +import static org.powermock.api.mockito.PowerMockito.doNothing; |
| 4 | +import static org.powermock.api.mockito.PowerMockito.spy; |
| 5 | + |
| 6 | +import java.util.Collections; |
| 7 | + |
| 8 | +import org.apache.log4j.Level; |
| 9 | +import org.apache.log4j.spi.LoggingEvent; |
| 10 | +import org.assertj.core.api.Condition; |
| 11 | +import org.assertj.core.api.SoftAssertions; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 16 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 17 | +import org.slf4j.MDC; |
| 18 | + |
| 19 | +import com.stackify.slf4j.guide.utils.ListAppender; |
| 20 | + |
| 21 | +@RunWith(PowerMockRunner.class) |
| 22 | +@PrepareForTest(MDC.class) |
| 23 | +public class SimpleControllerIntegrationTest { |
| 24 | + |
| 25 | + private SimpleController controller = new SimpleController(); |
| 26 | + |
| 27 | + @Before |
| 28 | + public void clearLogList() { |
| 29 | + ListAppender.clearEventList(); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void whenSimpleRequestMade_thenAllRegularMessagesLogged() { |
| 34 | + String output = controller.processList(Collections.emptyList()); |
| 35 | + |
| 36 | + SoftAssertions errorCollector = new SoftAssertions(); |
| 37 | + errorCollector.assertThat(ListAppender.getEvents()) |
| 38 | + .haveAtLeastOne(eventContains("Client requested process the following list: []", Level.INFO)) |
| 39 | + .haveAtLeastOne(eventContains("Starting process", Level.DEBUG)) |
| 40 | + .haveAtLeastOne(eventContains("Finished processing", Level.INFO)) |
| 41 | + .haveExactly(0, eventOfLevel(Level.ERROR)); |
| 42 | + errorCollector.assertThat(output) |
| 43 | + .isEqualTo("done"); |
| 44 | + errorCollector.assertAll(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void givenClientId_whenMDCRequestMade_thenMessagesWithClientIdLogged() throws Exception { |
| 49 | + // We avoid cleaning the context so tht we can check it afterwards |
| 50 | + spy(MDC.class); |
| 51 | + doNothing().when(MDC.class); |
| 52 | + MDC.clear(); |
| 53 | + String clientId = "id-1234"; |
| 54 | + |
| 55 | + String output = controller.clientMDCRequest(clientId); |
| 56 | + |
| 57 | + SoftAssertions errorCollector = new SoftAssertions(); |
| 58 | + errorCollector.assertThat(ListAppender.getEvents()) |
| 59 | + .allMatch(entry -> { |
| 60 | + return clientId.equals(entry.getMDC("clientId")); |
| 61 | + }) |
| 62 | + .haveAtLeastOne(eventContains("Client id-1234 has made a request", Level.INFO)) |
| 63 | + .haveAtLeastOne(eventContains("Starting request", Level.INFO)) |
| 64 | + .haveAtLeastOne(eventContains("Finished request", Level.INFO)); |
| 65 | + errorCollector.assertThat(output) |
| 66 | + .isEqualTo("finished"); |
| 67 | + errorCollector.assertAll(); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + private Condition<LoggingEvent> eventOfLevel(Level level) { |
| 72 | + return eventContains(null, level); |
| 73 | + } |
| 74 | + |
| 75 | + private Condition<LoggingEvent> eventContains(String substring, Level level) { |
| 76 | + |
| 77 | + return new Condition<LoggingEvent>(entry -> (substring == null || (entry.getRenderedMessage() != null && entry.getRenderedMessage() |
| 78 | + .contains(substring))) && (level == null || level.equals(entry.getLevel())), String.format("entry with message '%s', level %s", substring, level)); |
| 79 | + } |
| 80 | +} |
0 commit comments