|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat; |
4 | 4 |
|
| 5 | +import ch.qos.logback.classic.Level; |
| 6 | +import ch.qos.logback.classic.Logger; |
| 7 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 8 | +import ch.qos.logback.core.read.ListAppender; |
5 | 9 | import com.retailsvc.http.spec.HttpMethod; |
6 | 10 | import com.retailsvc.http.validate.ValidationError; |
7 | 11 | import java.nio.charset.StandardCharsets; |
8 | 12 | import java.util.Map; |
9 | 13 | import java.util.Set; |
| 14 | +import org.junit.jupiter.api.AfterEach; |
| 15 | +import org.junit.jupiter.api.BeforeEach; |
10 | 16 | import org.junit.jupiter.api.Test; |
| 17 | +import org.slf4j.LoggerFactory; |
11 | 18 |
|
12 | 19 | class HandlersDefaultExceptionTest { |
13 | 20 |
|
14 | 21 | private static final TypeMapper JSON = new GsonTypeMapper(); |
15 | 22 |
|
| 23 | + private Logger handlersLogger; |
| 24 | + private Level originalLevel; |
| 25 | + private ListAppender<ILoggingEvent> appender; |
| 26 | + |
| 27 | + @BeforeEach |
| 28 | + void attachAppender() { |
| 29 | + handlersLogger = (Logger) LoggerFactory.getLogger(Handlers.class); |
| 30 | + originalLevel = handlersLogger.getLevel(); |
| 31 | + handlersLogger.setLevel(Level.DEBUG); |
| 32 | + appender = new ListAppender<>(); |
| 33 | + appender.start(); |
| 34 | + handlersLogger.addAppender(appender); |
| 35 | + } |
| 36 | + |
| 37 | + @AfterEach |
| 38 | + void detachAppender() { |
| 39 | + handlersLogger.detachAppender(appender); |
| 40 | + handlersLogger.setLevel(originalLevel); |
| 41 | + } |
| 42 | + |
16 | 43 | @Test |
17 | 44 | void validationExceptionRendersProblemJson() { |
18 | 45 | Response resp = |
@@ -70,6 +97,50 @@ void methodNotAllowedReturns405WithAllowHeader() { |
70 | 97 | assertThat(resp.headers().get("Allow")).contains("GET").contains("POST"); |
71 | 98 | } |
72 | 99 |
|
| 100 | + @Test |
| 101 | + void badRequestCauseLoggedAtDebug() { |
| 102 | + Throwable cause = new IllegalStateException("root"); |
| 103 | + |
| 104 | + Handlers.defaultExceptionHandler().handle(new BadRequestException("bad", cause)); |
| 105 | + |
| 106 | + assertThat(appender.list) |
| 107 | + .anySatisfy( |
| 108 | + event -> { |
| 109 | + assertThat(event.getLevel()).isEqualTo(Level.DEBUG); |
| 110 | + assertThat(event.getThrowableProxy().getClassName()) |
| 111 | + .isEqualTo(IllegalStateException.class.getName()); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void badRequestWithoutCauseDoesNotLog() { |
| 117 | + Handlers.defaultExceptionHandler().handle(new BadRequestException("bad")); |
| 118 | + |
| 119 | + assertThat(appender.list).isEmpty(); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void notFoundCauseLoggedAtDebug() { |
| 124 | + Throwable cause = new IllegalStateException("root"); |
| 125 | + |
| 126 | + Handlers.defaultExceptionHandler().handle(new NotFoundException("missing", cause)); |
| 127 | + |
| 128 | + assertThat(appender.list) |
| 129 | + .anySatisfy( |
| 130 | + event -> { |
| 131 | + assertThat(event.getLevel()).isEqualTo(Level.DEBUG); |
| 132 | + assertThat(event.getThrowableProxy().getClassName()) |
| 133 | + .isEqualTo(IllegalStateException.class.getName()); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void notFoundWithoutCauseDoesNotLog() { |
| 139 | + Handlers.defaultExceptionHandler().handle(new NotFoundException("missing")); |
| 140 | + |
| 141 | + assertThat(appender.list).isEmpty(); |
| 142 | + } |
| 143 | + |
73 | 144 | @Test |
74 | 145 | void unknownExceptionReturns500() { |
75 | 146 | Response resp = Handlers.defaultExceptionHandler().handle(new RuntimeException("kaboom")); |
|
0 commit comments