Merge pull request #456 from valinz/spring-boot-start-config
Spring boot start config
This commit is contained in:
commit
570895ac11
@ -80,6 +80,19 @@
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<version>2.0.2-beta</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -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<LongPollingBot> longPollingBots;
|
||||
private final List<WebhookBot> webHookBots;
|
||||
|
||||
@Autowired
|
||||
private TelegramBotsApi telegramBotsApi;
|
||||
|
||||
public TelegramBotStarterConfiguration(List<LongPollingBot> longPollingBots,
|
||||
List<WebhookBot> webHookBots) {
|
||||
@Autowired
|
||||
public void setTelegramBotsApi(TelegramBotsApi telegramBotsApi) {
|
||||
this.telegramBotsApi = telegramBotsApi;
|
||||
}
|
||||
|
||||
public TelegramBotStarterConfiguration(@Autowired(required = false) List<LongPollingBot> longPollingBots,
|
||||
@Autowired(required = false) List<WebhookBot> 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);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user