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>
|
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||||
<version>${spring-boot.version}</version>
|
<version>${spring-boot.version}</version>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -1,49 +1,64 @@
|
|||||||
package org.telegram.telegrambots.starter;
|
package org.telegram.telegrambots.starter;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.CommandLineRunner;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.telegram.telegrambots.TelegramBotsApi;
|
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.LongPollingBot;
|
||||||
import org.telegram.telegrambots.generics.WebhookBot;
|
import org.telegram.telegrambots.generics.WebhookBot;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Receives all beand which are #LongPollingBot and #WebhookBot and register them in #TelegramBotsApi.
|
* Receives all beand which are #LongPollingBot and #WebhookBot and register them in #TelegramBotsApi.
|
||||||
* #TelegramBotsApi added to spring context as well
|
* #TelegramBotsApi added to spring context as well
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class TelegramBotStarterConfiguration implements CommandLineRunner {
|
public class TelegramBotStarterConfiguration {
|
||||||
|
|
||||||
|
|
||||||
private final List<LongPollingBot> longPollingBots;
|
private final List<LongPollingBot> longPollingBots;
|
||||||
private final List<WebhookBot> webHookBots;
|
private final List<WebhookBot> webHookBots;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private TelegramBotsApi telegramBotsApi;
|
private TelegramBotsApi telegramBotsApi;
|
||||||
|
|
||||||
public TelegramBotStarterConfiguration(List<LongPollingBot> longPollingBots,
|
@Autowired
|
||||||
List<WebhookBot> webHookBots) {
|
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.longPollingBots = longPollingBots;
|
||||||
this.webHookBots = webHookBots;
|
this.webHookBots = webHookBots;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@PostConstruct
|
||||||
public void run(String... args) {
|
public void registerBots() {
|
||||||
try {
|
Optional.ofNullable(longPollingBots).ifPresent(bots -> {
|
||||||
for (LongPollingBot bot : longPollingBots) {
|
bots.forEach(bot -> {
|
||||||
telegramBotsApi.registerBot(bot);
|
try {
|
||||||
}
|
telegramBotsApi.registerBot(bot);
|
||||||
for (WebhookBot bot : webHookBots) {
|
} catch (TelegramApiRequestException e) {
|
||||||
telegramBotsApi.registerBot(bot);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
} catch (TelegramApiException e) {
|
});
|
||||||
e.printStackTrace();
|
});
|
||||||
}
|
|
||||||
|
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