diff --git a/telegrambots-spring-boot-starter/pom.xml b/telegrambots-spring-boot-starter/pom.xml index b40ff9c9..72ed3705 100644 --- a/telegrambots-spring-boot-starter/pom.xml +++ b/telegrambots-spring-boot-starter/pom.xml @@ -80,6 +80,19 @@ spring-boot-autoconfigure ${spring-boot.version} + + + org.mockito + mockito-all + 2.0.2-beta + test + + + junit + junit + 4.11 + test + diff --git a/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java index ec851dc0..11e66af3 100644 --- a/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java +++ b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java @@ -1,49 +1,64 @@ package org.telegram.telegrambots.starter; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.telegram.telegrambots.TelegramBotsApi; -import org.telegram.telegrambots.exceptions.TelegramApiException; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.generics.LongPollingBot; import org.telegram.telegrambots.generics.WebhookBot; +import javax.annotation.PostConstruct; import java.util.List; +import java.util.Optional; /** * Receives all beand which are #LongPollingBot and #WebhookBot and register them in #TelegramBotsApi. * #TelegramBotsApi added to spring context as well */ @Configuration -public class TelegramBotStarterConfiguration implements CommandLineRunner { +public class TelegramBotStarterConfiguration { private final List longPollingBots; private final List webHookBots; - @Autowired private TelegramBotsApi telegramBotsApi; - public TelegramBotStarterConfiguration(List longPollingBots, - List webHookBots) { + @Autowired + public void setTelegramBotsApi(TelegramBotsApi telegramBotsApi) { + this.telegramBotsApi = telegramBotsApi; + } + + public TelegramBotStarterConfiguration(@Autowired(required = false) List longPollingBots, + @Autowired(required = false) List webHookBots) { + this.longPollingBots = longPollingBots; this.webHookBots = webHookBots; } - @Override - public void run(String... args) { - try { - for (LongPollingBot bot : longPollingBots) { - telegramBotsApi.registerBot(bot); - } - for (WebhookBot bot : webHookBots) { - telegramBotsApi.registerBot(bot); - } - } catch (TelegramApiException e) { - e.printStackTrace(); - } + @PostConstruct + public void registerBots() { + Optional.ofNullable(longPollingBots).ifPresent(bots -> { + bots.forEach(bot -> { + try { + telegramBotsApi.registerBot(bot); + } catch (TelegramApiRequestException e) { + throw new RuntimeException(e); + } + }); + }); + + Optional.ofNullable(webHookBots).ifPresent(bots -> { + bots.forEach(bot -> { + try { + telegramBotsApi.registerBot(bot); + } catch (TelegramApiRequestException e) { + throw new RuntimeException(e); + } + }); + }); } diff --git a/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterConfiguration.java b/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterConfiguration.java new file mode 100644 index 00000000..00a1e7cf --- /dev/null +++ b/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterConfiguration.java @@ -0,0 +1,66 @@ +package org.telegram.telegrambots.starter; + +import com.google.common.collect.Lists; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; +import org.telegram.telegrambots.TelegramBotsApi; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.generics.LongPollingBot; +import org.telegram.telegrambots.generics.WebhookBot; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +public class TestTelegramBotStarterConfiguration { + + @Mock + private TelegramBotsApi telegramBotsApi; + + @Rule + public MockitoRule mockitoRule = MockitoJUnit.rule(); + + @Test + public void TestRegisterBotsWithLongPollingBots() throws TelegramApiRequestException { + when(telegramBotsApi.registerBot(any(LongPollingBot.class))).then(Answers.RETURNS_MOCKS.get()); + LongPollingBot longPollingBot = mock(LongPollingBot.class); + TelegramBotStarterConfiguration configuration = new TelegramBotStarterConfiguration(Lists.newArrayList(longPollingBot), null); + configuration.setTelegramBotsApi(telegramBotsApi); + + configuration.registerBots(); + + verify(telegramBotsApi, times(1)).registerBot(longPollingBot); + verifyNoMoreInteractions(telegramBotsApi); + } + + @Test + public void TestRegisterBotsWithWebhookBots() throws TelegramApiRequestException { + doNothing().when(telegramBotsApi).registerBot(any(WebhookBot.class)); + WebhookBot webhookBot = mock(WebhookBot.class); + TelegramBotStarterConfiguration configuration = new TelegramBotStarterConfiguration(null, Lists.newArrayList(webhookBot)); + configuration.setTelegramBotsApi(telegramBotsApi); + + configuration.registerBots(); + + verify(telegramBotsApi, times(1)).registerBot(webhookBot); + verifyNoMoreInteractions(telegramBotsApi); + } + + @Test + public void TestRegisterBotsWithLongPollingBotsAndWebhookBots() throws TelegramApiRequestException { + doNothing().when(telegramBotsApi).registerBot(any(WebhookBot.class)); + LongPollingBot longPollingBot = mock(LongPollingBot.class); + WebhookBot webhookBot = mock(WebhookBot.class); + TelegramBotStarterConfiguration configuration = new TelegramBotStarterConfiguration(Lists.newArrayList(longPollingBot), Lists.newArrayList(webhookBot)); + configuration.setTelegramBotsApi(telegramBotsApi); + + configuration.registerBots(); + + verify(telegramBotsApi, times(1)).registerBot(longPollingBot); + verify(telegramBotsApi, times(1)).registerBot(webhookBot); + verifyNoMoreInteractions(telegramBotsApi); + } +}