diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/Command.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/Command.java index cbfb3ab917..872739d2ad 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/Command.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/Command.java @@ -24,6 +24,13 @@ public abstract class Command { description = HELP_DESCRIPTION) private boolean help; + @Parameter( + names = {"--failure-slack-notification-channel", "-fsnc"}, + arity = 1, + description = + "Slack channel to which notifications are sent, required if sending Slack notifications when CLI fails.") + private String failureSlackNotificationChannel; + /** * Method to be overridden to implement the business logic of this command * @@ -114,4 +121,8 @@ public String getName() { public void setOriginalArgs(List originalArgs) { this.originalArgs = originalArgs; } + + public String getFailureSlackNotificationChannel() { + return this.failureSlackNotificationChannel; + } } diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/CommandException.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/CommandException.java index cfac2e184e..fa33c66336 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/CommandException.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/CommandException.java @@ -11,6 +11,8 @@ */ public class CommandException extends RuntimeException { + private boolean expected; + public CommandException(Throwable cause) { super(cause); } @@ -22,4 +24,13 @@ public CommandException(String message) { public CommandException(String message, Throwable cause) { super(message, cause); } + + public CommandException(String message, boolean expected) { + super(message); + this.expected = expected; + } + + public boolean isExpected() { + return this.expected; + } } diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/ExtractionCheckCommand.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/ExtractionCheckCommand.java index c1a975c158..d1e155ed2d 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/ExtractionCheckCommand.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/ExtractionCheckCommand.java @@ -499,7 +499,7 @@ private void checkForHardFail(List results) { sendFailureNotifications( results.stream().filter(result -> !result.isSuccessful()).collect(Collectors.toList()), true); - throw new CommandException(cliFailureString); + throw new CommandException(cliFailureString, true); } } diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommand.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommand.java index b2fcedf094..b9e97c1420 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommand.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommand.java @@ -6,18 +6,19 @@ import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; import com.box.l10n.mojito.cli.command.extraction.AssetExtractionDiff; -import com.box.l10n.mojito.cli.command.extraction.ExtractionDiffNotificationSender; import com.box.l10n.mojito.cli.command.extraction.ExtractionDiffPaths; import com.box.l10n.mojito.cli.command.extraction.ExtractionDiffService; import com.box.l10n.mojito.cli.command.extraction.ExtractionDiffStatistics; import com.box.l10n.mojito.cli.command.extraction.ExtractionPaths; import com.box.l10n.mojito.cli.command.extraction.MissingExtractionDirectoryException; import com.box.l10n.mojito.cli.command.param.Param; +import com.box.l10n.mojito.cli.command.utils.SlackNotificationSender; import com.box.l10n.mojito.cli.console.ConsoleWriter; import com.box.l10n.mojito.json.ObjectMapper; import com.box.l10n.mojito.rest.entity.Repository; import com.box.l10n.mojito.rest.entity.SourceAsset; import com.box.l10n.mojito.shell.Shell; +import com.box.l10n.mojito.slack.SlackClient; import com.google.common.base.Strings; import java.nio.file.Path; import java.util.Collections; @@ -237,12 +238,15 @@ public class ExtractionDiffCommand extends Command { @Autowired PushService pushService; - private Optional notificationSender; + @Autowired(required = false) + private SlackClient slackClient; + + private Optional notificationSender; // Method for testing purposes - protected Optional getNotificationSender() { + protected Optional getNotificationSender() { if (!Strings.isNullOrEmpty(this.slackNotificationChannel)) { - return of(new ExtractionDiffNotificationSender(this.slackNotificationChannel)); + return of(new SlackNotificationSender(this.slackClient)); } return empty(); } @@ -271,6 +275,7 @@ private void checkMaxStringsBlock(ExtractionDiffPaths extractionDiffPaths) this.notificationSender.ifPresent( notificationSender -> notificationSender.sendMessage( + this.slackNotificationChannel, String.format( MAX_STRINGS_ADDED_BLOCK_MESSAGE, this.pushToBranchName, @@ -284,6 +289,7 @@ private void checkMaxStringsBlock(ExtractionDiffPaths extractionDiffPaths) this.notificationSender.ifPresent( notificationSender -> notificationSender.sendMessage( + this.slackNotificationChannel, String.format( MAX_STRINGS_REMOVED_BLOCK_MESSAGE, this.pushToBranchName, diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/L10nJCommander.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/L10nJCommander.java index 1e58a1f6c7..8cda3211af 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/L10nJCommander.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/L10nJCommander.java @@ -1,8 +1,10 @@ package com.box.l10n.mojito.cli.command; import com.beust.jcommander.JCommander; +import com.box.l10n.mojito.cli.command.utils.SlackNotificationSender; import com.box.l10n.mojito.cli.console.ConsoleWriter; import com.box.l10n.mojito.rest.resttemplate.AuthenticatedRestTemplate; +import com.box.l10n.mojito.slack.SlackClient; import com.google.common.base.Strings; import jakarta.annotation.PostConstruct; import java.util.Arrays; @@ -43,6 +45,9 @@ public class L10nJCommander { @Autowired AuthenticatedRestTemplate authenticatedRestTemplate; + @Autowired(required = false) + SlackClient slackClient; + static final String PROGRAM_NAME = "mojito"; boolean systemExitEnabled = true; @@ -92,6 +97,24 @@ public void init() { createJCommanderForRun(); } + private void notifyFailure(Command command, String[] args, String errorMessage) { + if (!Strings.isNullOrEmpty(command.getFailureSlackNotificationChannel())) { + new SlackNotificationSender(this.slackClient) + .sendMessage( + command.getFailureSlackNotificationChannel(), + String.format( + ":warning: The following command has failed: *%s*\n\n*ARGUMENTS:*\n%s\n\n*ERROR MESSAGE:*\n%s", + command.getName(), String.join(", ", args), errorMessage)); + } + } + + private void notifyFailure(Command command, String[] args, Throwable throwable) { + this.notifyFailure( + command, + args, + String.format("%s\n%s", throwable.getMessage(), ExceptionUtils.getStackTrace(throwable))); + } + public void run(String... args) { Exception parsingException = null; @@ -124,21 +147,30 @@ public void run(String... args) { try { command.run(); } catch (SessionAuthenticationException ae) { + String message = "Invalid username or password"; logger.debug("Exit with Invalid username or password", ae); - printErrorMessage("Invalid username or password"); + printErrorMessage(message); + this.notifyFailure(command, args, message); exitWithError(); } catch (CommandWithExitStatusException cwese) { - logger.error("Exit with error for command: " + command.getName(), cwese); + String message = "Exit with error for command: " + command.getName(); + logger.error(message, cwese); + this.notifyFailure(command, args, cwese); exitWithError(cwese.getExitCode()); } catch (CommandException ce) { + String message = "Exit with error for command: " + command.getName(); printErrorMessage(ce.getMessage()); - logger.error("Exit with error for command: " + command.getName(), ce); + logger.error(message, ce); + if (!ce.isExpected()) { + this.notifyFailure(command, args, ce); + } exitWithError(); } catch (ResourceAccessException rae) { String msg = "Is a server running on: " + authenticatedRestTemplate.getURIForResource("") + "?"; printErrorMessage(msg); logger.error(msg, rae); + this.notifyFailure(command, args, msg); exitWithError(); } catch (HttpClientErrorException | HttpServerErrorException e) { String msg = @@ -151,11 +183,13 @@ public void run(String... args) { printErrorMessage(msg); logger.error("Unexpected error", e); logger.error(e.getResponseBodyAsString()); + this.notifyFailure(command, args, msg); exitWithError(); } catch (Throwable t) { String msg = "Unexpected error: " + t.getMessage() + "\n" + ExceptionUtils.getStackTrace(t); printErrorMessage(msg); logger.error("Unexpected error", t); + this.notifyFailure(command, args, msg); exitWithError(); } } diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/extraction/ExtractionDiffNotificationSender.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/extraction/ExtractionDiffNotificationSender.java deleted file mode 100644 index 2f2a1bedd2..0000000000 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/extraction/ExtractionDiffNotificationSender.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.box.l10n.mojito.cli.command.extraction; - -import static com.box.l10n.mojito.slack.SlackClient.COLOR_WARNING; -import static com.box.l10n.mojito.slack.request.Attachment.MRKDWNIN_TEXT; - -import com.box.l10n.mojito.slack.SlackClient; -import com.box.l10n.mojito.slack.SlackClientException; -import com.box.l10n.mojito.slack.request.Attachment; -import com.box.l10n.mojito.slack.request.Message; -import com.google.common.base.Strings; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Configurable; - -@Configurable -public class ExtractionDiffNotificationSender { - @Autowired SlackClient slackClient; - - private String channel; - - public ExtractionDiffNotificationSender(String channel) { - this.channel = channel; - } - - private Message getSlackMessage(String message) throws SlackClientException { - Message slackMessage = new Message(); - if (Strings.isNullOrEmpty(this.channel)) { - throw new ExtractionDiffNotificationSenderException( - "Channel cannot be empty for a Slack notification"); - } - slackMessage.setChannel(this.channel); - Attachment attachment = new Attachment(); - attachment.setText(message); - attachment.setColor(COLOR_WARNING); - attachment.getMrkdwnIn().add(MRKDWNIN_TEXT); - slackMessage.getAttachments().add(attachment); - return slackMessage; - } - - public void sendMessage(String message) { - if (!Strings.isNullOrEmpty(message)) { - try { - this.slackClient.sendInstantMessage(this.getSlackMessage(message)); - } catch (SlackClientException e) { - throw new ExtractionDiffNotificationSenderException( - "Error sending Slack notification: " + e.getMessage(), e); - } - } - } - - public void setChannel(String channel) { - this.channel = channel; - } -} diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/extraction/ExtractionDiffNotificationSenderException.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/extraction/ExtractionDiffNotificationSenderException.java deleted file mode 100644 index 183825185d..0000000000 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/extraction/ExtractionDiffNotificationSenderException.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.box.l10n.mojito.cli.command.extraction; - -public class ExtractionDiffNotificationSenderException extends RuntimeException { - public ExtractionDiffNotificationSenderException(String message) { - super(message); - } - - public ExtractionDiffNotificationSenderException(String message, Throwable t) { - super(message, t); - } -} diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/utils/SlackNotificationSender.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/utils/SlackNotificationSender.java new file mode 100644 index 0000000000..183d963f78 --- /dev/null +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/utils/SlackNotificationSender.java @@ -0,0 +1,41 @@ +package com.box.l10n.mojito.cli.command.utils; + +import static com.box.l10n.mojito.slack.SlackClient.COLOR_WARNING; +import static com.box.l10n.mojito.slack.request.Attachment.MRKDWNIN_TEXT; + +import com.box.l10n.mojito.slack.SlackClient; +import com.box.l10n.mojito.slack.SlackClientException; +import com.box.l10n.mojito.slack.request.Attachment; +import com.box.l10n.mojito.slack.request.Message; +import com.google.common.base.Strings; + +public class SlackNotificationSender { + private final SlackClient slackClient; + + public SlackNotificationSender(SlackClient slackClient) { + this.slackClient = slackClient; + } + + public void sendMessage(String channel, String message) { + if (!Strings.isNullOrEmpty(message)) { + try { + Message slackMessage = new Message(); + if (Strings.isNullOrEmpty(channel)) { + throw new SlackNotificationSenderException( + "Channel cannot be empty for a Slack notification"); + } + slackMessage.setChannel(channel); + Attachment attachment = new Attachment(); + attachment.setText(message); + attachment.setColor(COLOR_WARNING); + attachment.getMrkdwnIn().add(MRKDWNIN_TEXT); + slackMessage.getAttachments().add(attachment); + + this.slackClient.sendInstantMessage(slackMessage); + } catch (SlackClientException e) { + throw new SlackNotificationSenderException( + "Error sending Slack notification: " + e.getMessage(), e); + } + } + } +} diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/utils/SlackNotificationSenderException.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/utils/SlackNotificationSenderException.java new file mode 100644 index 0000000000..3b5a9a7b37 --- /dev/null +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/utils/SlackNotificationSenderException.java @@ -0,0 +1,11 @@ +package com.box.l10n.mojito.cli.command.utils; + +public class SlackNotificationSenderException extends RuntimeException { + public SlackNotificationSenderException(String message) { + super(message); + } + + public SlackNotificationSenderException(String message, Throwable t) { + super(message, t); + } +} diff --git a/cli/src/test/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommandForTest.java b/cli/src/test/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommandForTest.java index 45fca5df73..cacd337537 100644 --- a/cli/src/test/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommandForTest.java +++ b/cli/src/test/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommandForTest.java @@ -5,7 +5,7 @@ import static org.mockito.Mockito.doNothing; import com.beust.jcommander.Parameters; -import com.box.l10n.mojito.cli.command.extraction.ExtractionDiffNotificationSender; +import com.box.l10n.mojito.cli.command.utils.SlackNotificationSender; import java.util.Optional; import org.mockito.Mockito; import org.springframework.context.annotation.Scope; @@ -17,19 +17,19 @@ commandNames = {"extract-diff-test"}, commandDescription = "Class to test Slack notifications for the extract-diff command") public class ExtractionDiffCommandForTest extends ExtractionDiffCommand { - private Optional mockedNotificationSender = empty(); + private Optional mockedNotificationSender = empty(); @Override - protected Optional getNotificationSender() { - Optional notificationSender = super.getNotificationSender(); + protected Optional getNotificationSender() { + Optional notificationSender = super.getNotificationSender(); this.mockedNotificationSender = notificationSender.map(Mockito::spy); this.mockedNotificationSender.ifPresent( - extractionDiffNotificationSender -> - doNothing().when(extractionDiffNotificationSender).sendMessage(anyString())); + slackNotificationSender -> + doNothing().when(slackNotificationSender).sendMessage(anyString(), anyString())); return this.mockedNotificationSender; } - public Optional getMockedNotificationSender() { + public Optional getMockedNotificationSender() { return this.mockedNotificationSender; } } diff --git a/cli/src/test/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommandTest.java b/cli/src/test/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommandTest.java index 312d2de73c..ebfacc78e4 100644 --- a/cli/src/test/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommandTest.java +++ b/cli/src/test/java/com/box/l10n/mojito/cli/command/ExtractionDiffCommandTest.java @@ -7,7 +7,7 @@ import static org.mockito.Mockito.verify; import com.box.l10n.mojito.cli.CLITestBase; -import com.box.l10n.mojito.cli.command.extraction.ExtractionDiffNotificationSender; +import com.box.l10n.mojito.cli.command.utils.SlackNotificationSender; import com.box.l10n.mojito.entity.Repository; import com.box.l10n.mojito.service.tm.search.TextUnitDTO; import com.box.l10n.mojito.service.tm.search.TextUnitSearcher; @@ -549,11 +549,12 @@ public void testMaxStringsBlockFailure() throws Exception { ExtractionDiffCommandForTest extractionDiffCommandForTest = l10nJCommander.getCommand(ExtractionDiffCommandForTest.class); - Optional mockedNotificationSender = + Optional mockedNotificationSender = extractionDiffCommandForTest.getMockedNotificationSender(); assertTrue(mockedNotificationSender.isPresent()); verify(mockedNotificationSender.get(), times(1)) .sendMessage( + "CHANNEL_ID", String.format(ExtractionDiffCommand.MAX_STRINGS_ADDED_BLOCK_MESSAGE, "branch", 3, 2)); Assert.assertEquals(1L, l10nJCommander.getExitCode()); Mockito.verify(l10nJCommander.consoleWriter, Mockito.times(1)) @@ -607,6 +608,7 @@ public void testMaxStringsBlockFailure() throws Exception { assertTrue(mockedNotificationSender.isPresent()); verify(mockedNotificationSender.get(), times(1)) .sendMessage( + "CHANNEL_ID", String.format(ExtractionDiffCommand.MAX_STRINGS_REMOVED_BLOCK_MESSAGE, "branch", 3, 2)); Assert.assertEquals(1L, l10nJCommander.getExitCode()); Mockito.verify(l10nJCommander.consoleWriter, Mockito.times(1)) diff --git a/cli/src/test/java/com/box/l10n/mojito/cli/command/L10nJCommanderTest.java b/cli/src/test/java/com/box/l10n/mojito/cli/command/L10nJCommanderTest.java new file mode 100644 index 0000000000..6a3827b1ec --- /dev/null +++ b/cli/src/test/java/com/box/l10n/mojito/cli/command/L10nJCommanderTest.java @@ -0,0 +1,111 @@ +package com.box.l10n.mojito.cli.command; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.box.l10n.mojito.cli.CLITestBase; +import com.box.l10n.mojito.slack.SlackClient; +import com.box.l10n.mojito.slack.SlackClientException; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.springframework.http.HttpStatusCode; +import org.springframework.security.web.authentication.session.SessionAuthenticationException; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.ResourceAccessException; + +public class L10nJCommanderTest extends CLITestBase { + SlackClient slackClientMock; + + Command commandMock; + + @Before + public void before() { + this.slackClientMock = Mockito.mock(SlackClient.class); + this.commandMock = Mockito.mock(Command.class); + } + + private L10nJCommander getL10nJCommanderSpy() { + Mockito.reset(this.slackClientMock); + Mockito.reset(this.commandMock); + when(this.commandMock.getName()).thenReturn("repo-create"); + when(this.commandMock.getFailureSlackNotificationChannel()).thenReturn("@testslackchannel"); + L10nJCommander commander = Mockito.spy(this.getL10nJCommander()); + when(commander.getCommand(anyString())).thenReturn(this.commandMock); + commander.slackClient = this.slackClientMock; + return commander; + } + + @Test + public void testRunSlackClientIsCalled() throws SlackClientException { + String[] args = + new String[] { + "repo-create", "-n", "test_repo", "-l", "ar-SA", "-fsnc", "@testslackchannel" + }; + + L10nJCommander commander = this.getL10nJCommanderSpy(); + doThrow(new SessionAuthenticationException("error message")).when(this.commandMock).run(); + commander.run(args); + + verify(this.slackClientMock, times(1)).sendInstantMessage(any()); + assertEquals(1, commander.getExitCode()); + + commander = this.getL10nJCommanderSpy(); + doThrow(new CommandWithExitStatusException(2)).when(this.commandMock).run(); + commander.run(args); + + verify(this.slackClientMock, times(1)).sendInstantMessage(any()); + assertEquals(2, commander.getExitCode()); + + commander = this.getL10nJCommanderSpy(); + doThrow(new CommandException("error message")).when(this.commandMock).run(); + commander.run(args); + + verify(this.slackClientMock, times(1)).sendInstantMessage(any()); + assertEquals(1, commander.getExitCode()); + + commander = this.getL10nJCommanderSpy(); + doThrow(new ResourceAccessException("error message")).when(this.commandMock).run(); + commander.run(args); + + verify(this.slackClientMock, times(1)).sendInstantMessage(any()); + assertEquals(1, commander.getExitCode()); + + commander = this.getL10nJCommanderSpy(); + doThrow(new HttpClientErrorException(HttpStatusCode.valueOf(500))).when(this.commandMock).run(); + commander.run(args); + + verify(this.slackClientMock, times(1)).sendInstantMessage(any()); + assertEquals(1, commander.getExitCode()); + + commander = this.getL10nJCommanderSpy(); + doThrow(new ArrayIndexOutOfBoundsException()).when(this.commandMock).run(); + commander.run(args); + + verify(this.slackClientMock, times(1)).sendInstantMessage(any()); + assertEquals(1, commander.getExitCode()); + } + + @Test + public void testRunSlackClientIsNotCalled() throws SlackClientException { + L10nJCommander commander = this.getL10nJCommanderSpy(); + doThrow(new CommandException("error message", true)).when(this.commandMock).run(); + commander.run("repo-create", "-n", "test_repo", "-l", "ar-SA", "-fsnc", "@testslackchannel"); + + verify(this.slackClientMock, times(0)).sendInstantMessage(any()); + assertEquals(1, commander.getExitCode()); + + commander = this.getL10nJCommanderSpy(); + doThrow(new ResourceAccessException("error message")).when(this.commandMock).run(); + when(this.commandMock.getFailureSlackNotificationChannel()).thenReturn(null); + commander.run("repo-create", "-n", "test_repo", "-l", "ar-SA"); + + verify(this.slackClientMock, times(0)).sendInstantMessage(any()); + assertEquals(1, commander.getExitCode()); + } +} diff --git a/cli/src/test/java/com/box/l10n/mojito/cli/command/extraction/ExtractionDiffNotificationSenderTest.java b/cli/src/test/java/com/box/l10n/mojito/cli/command/utils/SlackNotificationSenderTest.java similarity index 68% rename from cli/src/test/java/com/box/l10n/mojito/cli/command/extraction/ExtractionDiffNotificationSenderTest.java rename to cli/src/test/java/com/box/l10n/mojito/cli/command/utils/SlackNotificationSenderTest.java index d553312248..d92f0b772c 100644 --- a/cli/src/test/java/com/box/l10n/mojito/cli/command/extraction/ExtractionDiffNotificationSenderTest.java +++ b/cli/src/test/java/com/box/l10n/mojito/cli/command/utils/SlackNotificationSenderTest.java @@ -1,4 +1,4 @@ -package com.box.l10n.mojito.cli.command.extraction; +package com.box.l10n.mojito.cli.command.utils; import static com.box.l10n.mojito.slack.SlackClient.COLOR_WARNING; import static org.mockito.ArgumentMatchers.any; @@ -26,10 +26,10 @@ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest( classes = { - ExtractionDiffNotificationSenderTest.class, - ExtractionDiffNotificationSenderTest.ExtractionDiffNotificationSenderTestConfiguration.class + SlackNotificationSenderTest.class, + SlackNotificationSenderTest.ExtractionDiffNotificationSenderTestConfiguration.class }) -public class ExtractionDiffNotificationSenderTest { +public class SlackNotificationSenderTest { @TestConfiguration static class ExtractionDiffNotificationSenderTestConfiguration { @Bean(name = "mockedSlackClient") @@ -48,18 +48,17 @@ public SlackClient slackClient() { @Captor ArgumentCaptor messageArgumentCaptor; - ExtractionDiffNotificationSender extractionDiffNotificationSender; + SlackNotificationSender slackNotificationSender; @Before public void setup() { - this.extractionDiffNotificationSender = new ExtractionDiffNotificationSender(CHANNEL_ID); - this.extractionDiffNotificationSender.slackClient = this.slackClientMock; + this.slackNotificationSender = new SlackNotificationSender(this.slackClientMock); Mockito.reset(this.slackClientMock); } @Test public void testSendMessageSuccess() throws SlackClientException { - this.extractionDiffNotificationSender.sendMessage(MESSAGE); + this.slackNotificationSender.sendMessage(CHANNEL_ID, MESSAGE); verify(this.slackClientMock, times(1)).sendInstantMessage(this.messageArgumentCaptor.capture()); Message slackMessage = this.messageArgumentCaptor.getValue(); Assert.assertEquals(CHANNEL_ID, slackMessage.getChannel()); @@ -70,22 +69,20 @@ public void testSendMessageSuccess() throws SlackClientException { } @Test - public void testSendMessageWithoutChannel() throws SlackClientException { - this.extractionDiffNotificationSender.setChannel(null); + public void testSendMessageWithoutChannel() { Assert.assertThrows( - ExtractionDiffNotificationSenderException.class, - () -> this.extractionDiffNotificationSender.sendMessage(MESSAGE)); - this.extractionDiffNotificationSender.setChannel(""); + SlackNotificationSenderException.class, + () -> this.slackNotificationSender.sendMessage(null, MESSAGE)); Assert.assertThrows( - ExtractionDiffNotificationSenderException.class, - () -> this.extractionDiffNotificationSender.sendMessage(MESSAGE)); + SlackNotificationSenderException.class, + () -> this.slackNotificationSender.sendMessage("", MESSAGE)); } @Test public void testSendMessageWithoutMessage() throws SlackClientException { - this.extractionDiffNotificationSender.sendMessage(null); + this.slackNotificationSender.sendMessage(CHANNEL_ID, null); verify(this.slackClientMock, times(0)).sendInstantMessage(any()); - this.extractionDiffNotificationSender.sendMessage(""); + this.slackNotificationSender.sendMessage(CHANNEL_ID, ""); verify(this.slackClientMock, times(0)).sendInstantMessage(any()); } }