|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.*; |
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.google.protobuf.Value; |
6 | 10 | import com.spotify.confidence.ConfidenceValue.Struct; |
7 | 11 | import com.spotify.confidence.shaded.flags.resolver.v1.ResolveFlagsResponse; |
|
15 | 19 | import java.util.List; |
16 | 20 | import java.util.Map; |
17 | 21 | import java.util.concurrent.CompletableFuture; |
| 22 | +import org.junit.jupiter.api.AfterEach; |
18 | 23 | import org.junit.jupiter.api.BeforeEach; |
19 | 24 | import org.junit.jupiter.api.Test; |
| 25 | +import org.slf4j.LoggerFactory; |
20 | 26 |
|
21 | 27 | final class ConfidenceTest { |
22 | 28 | private final FakeEventSenderEngine fakeEngine = new FakeEventSenderEngine(new FakeClock()); |
23 | 29 | private final ResolverClientTestUtils.FakeFlagResolverClient fakeFlagResolverClient = |
24 | 30 | new ResolverClientTestUtils.FakeFlagResolverClient(); |
25 | 31 | private static Confidence confidence; |
| 32 | + private ListAppender<ILoggingEvent> listAppender; |
| 33 | + private Logger confidenceLogger; |
26 | 34 |
|
27 | 35 | @BeforeEach |
28 | 36 | void beforeEach() { |
29 | 37 | confidence = Confidence.create(fakeEngine, fakeFlagResolverClient, "clientKey"); |
| 38 | + confidenceLogger = (Logger) LoggerFactory.getLogger(Confidence.class); |
| 39 | + |
| 40 | + listAppender = new ListAppender<>(); |
| 41 | + listAppender.start(); |
| 42 | + // Add the appender to the logger |
| 43 | + confidenceLogger.addAppender(listAppender); |
| 44 | + } |
| 45 | + |
| 46 | + @AfterEach |
| 47 | + void afterEach() { |
| 48 | + confidenceLogger.detachAppender(listAppender); |
30 | 49 | } |
31 | 50 |
|
32 | 51 | @Test |
@@ -259,6 +278,22 @@ void internalError() { |
259 | 278 | evaluation.getErrorMessage().get().startsWith("Crashing while performing network call")); |
260 | 279 | } |
261 | 280 |
|
| 281 | + @Test |
| 282 | + void shouldLogResolverHint() { |
| 283 | + confidence |
| 284 | + .withContext(Map.of("my_context_value", ConfidenceValue.of(42))) |
| 285 | + .logResolveTesterHint(ResolvedFlag.newBuilder().setFlag("FlagName").build()); |
| 286 | + final List<ILoggingEvent> loggingEvents = listAppender.list; |
| 287 | + assertTrue(loggingEvents.size() > 0, "No log message was captured."); |
| 288 | + final ILoggingEvent lastLogEvent = loggingEvents.get(loggingEvents.size() - 1); |
| 289 | + assertEquals(Level.DEBUG, lastLogEvent.getLevel()); // Or whatever level you expect |
| 290 | + assertEquals( |
| 291 | + "Check your flag evaluation for 'FlagName' by copy pasting the payload to the Resolve tester " |
| 292 | + + "'ewogICJjbGllbnRLZXkiOiAiY2xpZW50S2V5IiwKICAiZmxhZyI6ICJGbGFnTmFtZSIsCiAgImN" |
| 293 | + + "vbnRleHQiOiB7CiAgICAibXlfY29udGV4dF92YWx1ZSI6IDQyLjAKICB9Cn0='", |
| 294 | + lastLogEvent.getFormattedMessage()); |
| 295 | + } |
| 296 | + |
262 | 297 | public static class FailingFlagResolverClient implements FlagResolverClient { |
263 | 298 |
|
264 | 299 | @Override |
|
0 commit comments