|
| 1 | +package org.telegram.telegrambots.longpolling.starter; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.mockito.ArgumentCaptor; |
| 5 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 6 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | +import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication; |
| 10 | +import org.telegram.telegrambots.longpolling.interfaces.LongPollingUpdateConsumer; |
| 11 | +import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer; |
| 12 | +import org.telegram.telegrambots.meta.TelegramUrl; |
| 13 | + |
| 14 | +import java.util.function.Supplier; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.mockito.ArgumentMatchers.any; |
| 18 | +import static org.mockito.ArgumentMatchers.anyString; |
| 19 | +import static org.mockito.Mockito.doReturn; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.times; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 24 | + |
| 25 | +class TestTelegramBotStarterWithCustomTelegramUrlConfiguration { |
| 26 | + |
| 27 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 28 | + .withAllowBeanDefinitionOverriding(true) |
| 29 | + .withConfiguration(AutoConfigurations.of(MockTelegramBotsLongPollingApplication.class, |
| 30 | + TelegramBotStarterConfiguration.class)); |
| 31 | + |
| 32 | + @Test |
| 33 | + void createMockTelegramApplicationWithDefaultSettings() { |
| 34 | + this.contextRunner.run((context) -> { |
| 35 | + assertThat(context).hasSingleBean(TelegramBotsLongPollingApplication.class); |
| 36 | + assertThat(context).hasSingleBean(TelegramBotInitializer.class); |
| 37 | + assertThat(context).doesNotHaveBean(SpringLongPollingBot.class); |
| 38 | + assertThat(context).hasSingleBean(TelegramUrl.class); |
| 39 | + verifyNoMoreInteractions(context.getBean(TelegramBotsLongPollingApplication.class)); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void createOnlyLongPollingBot() { |
| 45 | + this.contextRunner.withUserConfiguration(LongPollingBotConfig.class) |
| 46 | + .run((context) -> { |
| 47 | + assertThat(context).hasSingleBean(SpringLongPollingBot.class); |
| 48 | + |
| 49 | + TelegramBotsLongPollingApplication telegramApplication = context.getBean(TelegramBotsLongPollingApplication.class); |
| 50 | + TelegramUrl telegramUrl = context.getBean(TelegramUrl.class); |
| 51 | + |
| 52 | + ArgumentCaptor<Supplier<TelegramUrl>> telegramUrlCaptor = ArgumentCaptor.captor(); |
| 53 | + |
| 54 | + verify(telegramApplication, times(1)).registerBot(anyString(), telegramUrlCaptor.capture(), any(), any(LongPollingUpdateConsumer.class)); |
| 55 | + verifyNoMoreInteractions(telegramApplication); |
| 56 | + assertThat(telegramUrlCaptor.getValue().get()).isEqualTo(telegramUrl); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + @Configuration |
| 61 | + static class MockTelegramBotsLongPollingApplication { |
| 62 | + |
| 63 | + @Bean |
| 64 | + public TelegramBotsLongPollingApplication telegramBotsApplication() { |
| 65 | + return mock(TelegramBotsLongPollingApplication.class); |
| 66 | + } |
| 67 | + |
| 68 | + @Bean |
| 69 | + public TelegramUrl telegramUrl() { |
| 70 | + return mock(TelegramUrl.class); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Configuration |
| 75 | + static class LongPollingBotConfig { |
| 76 | + @Bean |
| 77 | + public SpringLongPollingBot longPollingBot() { |
| 78 | + SpringLongPollingBot springLongPollingBotMock = mock(SpringLongPollingBot.class); |
| 79 | + doReturn("").when(springLongPollingBotMock).getBotToken(); |
| 80 | + doReturn((LongPollingSingleThreadUpdateConsumer) update -> {}).when(springLongPollingBotMock).getUpdatesConsumer(); |
| 81 | + return springLongPollingBotMock; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments