|
1 | 1 | package ubic.gemma.web.logging;
|
2 | 2 |
|
| 3 | +import com.slack.api.Slack; |
| 4 | +import com.slack.api.methods.MethodsClient; |
| 5 | +import com.slack.api.methods.SlackApiException; |
| 6 | +import com.slack.api.methods.request.chat.ChatPostMessageRequest; |
3 | 7 | import org.apache.commons.logging.Log;
|
4 | 8 | import org.apache.commons.logging.LogFactory;
|
| 9 | +import org.junit.After; |
| 10 | +import org.junit.Before; |
5 | 11 | import org.junit.Test;
|
| 12 | +import org.mockito.MockedStatic; |
| 13 | +import org.mockito.Mockito; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.Objects; |
6 | 17 |
|
7 | 18 | import static org.junit.Assert.*;
|
8 | 19 | import static org.junit.Assume.assumeNotNull;
|
9 | 20 | import static org.junit.Assume.assumeTrue;
|
| 21 | +import static org.mockito.Mockito.*; |
10 | 22 |
|
11 | 23 | public class SlackAppenderTest {
|
12 | 24 |
|
13 | 25 | private static Log log = LogFactory.getLog( SlackAppenderTest.class );
|
14 | 26 |
|
15 |
| - @Test |
16 |
| - public void test() { |
17 |
| - String slackToken = System.getProperty( "gemma.slack.token" ); |
18 |
| - String slackChannel = System.getProperty( "gemma.slack.channel" ); |
| 27 | + private Slack mockedSlack; |
| 28 | + |
| 29 | + private String slackToken; |
| 30 | + private String slackChannel; |
| 31 | + |
| 32 | + @Before |
| 33 | + public void setUp() { |
| 34 | + slackToken = System.getProperty( "gemma.slack.token" ); |
| 35 | + slackChannel = System.getProperty( "gemma.slack.channel" ); |
| 36 | + mockedSlack = mock( Slack.class ); |
| 37 | + MethodsClient mockedMethodClient = mock( MethodsClient.class ); |
| 38 | + when( mockedSlack.methods( any( String.class ) ) ).thenReturn( mockedMethodClient ); |
| 39 | + assumeTrue( "This test must be run with -Dlog4j1.compatibility=true.", |
| 40 | + Objects.equals( System.getProperty( "log4j1.compatibility" ), "true" ) ); |
19 | 41 | assumeTrue( "Both -Dgemma.slack.token and -Dgemma.slack.channel must be set for this test.",
|
20 | 42 | slackToken != null && slackChannel != null );
|
21 |
| - try { |
22 |
| - raiseStackTrace(); |
23 |
| - fail( "This should not be reached." ); |
24 |
| - } catch ( IllegalArgumentException e ) { |
25 |
| - log.error( "This is just a test for the Log4j Slack appender.", e ); |
| 43 | + } |
| 44 | + |
| 45 | + @After |
| 46 | + public void tearDown() { |
| 47 | + reset( mockedSlack ); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void test() throws SlackApiException, IOException { |
| 52 | + try ( MockedStatic<Slack> slack = Mockito.mockStatic( Slack.class ) ) { |
| 53 | + slack.when( Slack::getInstance ).thenReturn( mockedSlack ); |
| 54 | + try { |
| 55 | + raiseStackTrace(); |
| 56 | + fail( "This should not be reached." ); |
| 57 | + } catch ( IllegalArgumentException e ) { |
| 58 | + log.error( "This is just a test for the Log4j Slack appender.", e ); |
| 59 | + } |
| 60 | + verify( mockedSlack ).methods( slackToken ); |
| 61 | + verify( mockedSlack.methods( slackToken ) ).chatPostMessage( any( ChatPostMessageRequest.class ) ); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testWarnLogsShouldNotBeAppended() { |
| 67 | + try ( MockedStatic<Slack> slack = Mockito.mockStatic( Slack.class ) ) { |
| 68 | + slack.when( Slack::getInstance ).thenReturn( mockedSlack ); |
| 69 | + log.warn( "This should not be captured by the Slack appender." ); |
| 70 | + verifyNoInteractions( mockedSlack ); |
26 | 71 | }
|
27 | 72 | }
|
28 | 73 |
|
|
0 commit comments