|
33 | 33 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
34 | 34 | import static org.junit.jupiter.api.Assertions.assertEquals; |
35 | 35 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 36 | +import static org.mockito.ArgumentMatchers.any; |
| 37 | +import static org.mockito.ArgumentMatchers.anyString; |
| 38 | +import static org.mockito.Mockito.mock; |
| 39 | +import static org.mockito.Mockito.never; |
36 | 40 | import static org.mockito.Mockito.verify; |
| 41 | +import static org.mockito.Mockito.when; |
37 | 42 |
|
38 | 43 | import com.google.api.gax.logging.LoggingUtils.ThrowingRunnable; |
| 44 | +import java.util.Collections; |
| 45 | +import java.util.Map; |
| 46 | +import org.junit.jupiter.api.AfterEach; |
39 | 47 | import org.junit.jupiter.api.Test; |
40 | 48 | import org.mockito.Mockito; |
| 49 | +import org.slf4j.Logger; |
41 | 50 |
|
42 | 51 | class LoggingUtilsTest { |
43 | 52 |
|
@@ -77,4 +86,37 @@ void testExecuteWithTryCatch_WithNoSuchMethodError() throws Throwable { |
77 | 86 | // Verify that the action was executed (despite the error) |
78 | 87 | verify(action).run(); |
79 | 88 | } |
| 89 | + |
| 90 | + @AfterEach |
| 91 | + void tearDown() { |
| 92 | + LoggingUtils.setLoggingEnabled(false); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testLogActionableError_loggingDisabled() { |
| 97 | + LoggingUtils.setLoggingEnabled(false); |
| 98 | + LoggerProvider loggerProvider = mock(LoggerProvider.class); |
| 99 | + |
| 100 | + LoggingUtils.logActionableError( |
| 101 | + Collections.emptyMap(), loggerProvider, org.slf4j.event.Level.INFO, "message"); |
| 102 | + |
| 103 | + verify(loggerProvider, never()).getLogger(); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void testLogActionableError_success() { |
| 108 | + LoggingUtils.setLoggingEnabled(true); |
| 109 | + LoggerProvider loggerProvider = mock(LoggerProvider.class); |
| 110 | + Logger logger = mock(Logger.class); |
| 111 | + when(loggerProvider.getLogger()).thenReturn(logger); |
| 112 | + |
| 113 | + org.slf4j.spi.LoggingEventBuilder eventBuilder = mock(org.slf4j.spi.LoggingEventBuilder.class); |
| 114 | + when(logger.atInfo()).thenReturn(eventBuilder); |
| 115 | + when(eventBuilder.addKeyValue(anyString(), any())).thenReturn(eventBuilder); |
| 116 | + |
| 117 | + Map<String, Object> context = Collections.singletonMap("key", "value"); |
| 118 | + LoggingUtils.logActionableError(context, loggerProvider, org.slf4j.event.Level.INFO, "message"); |
| 119 | + |
| 120 | + verify(loggerProvider).getLogger(); |
| 121 | + } |
80 | 122 | } |
0 commit comments