Skip to content

Commit 3ce3b34

Browse files
SuLG-ikrubenlagus
andcommitted
Add ability to override TelegramUrl in TelegramBotInitializer (#1482)
Co-authored-by: Ruben Bermudez <[email protected]>
1 parent de14334 commit 3ce3b34

File tree

5 files changed

+107
-7
lines changed

5 files changed

+107
-7
lines changed

telegrambots-springboot-longpolling-starter/src/main/java/org/telegram/telegrambots/longpolling/starter/TelegramBotInitializer.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.springframework.beans.factory.InitializingBean;
66
import org.telegram.telegrambots.longpolling.BotSession;
77
import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication;
8+
import org.telegram.telegrambots.longpolling.util.DefaultGetUpdatesGenerator;
9+
import org.telegram.telegrambots.meta.TelegramUrl;
810
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
911

1012
import java.lang.reflect.InvocationTargetException;
@@ -17,18 +19,26 @@ public class TelegramBotInitializer implements InitializingBean {
1719

1820
private final TelegramBotsLongPollingApplication telegramBotsApplication;
1921
private final List<SpringLongPollingBot> longPollingBots;
22+
private final TelegramUrl telegramUrl;
2023

2124
public TelegramBotInitializer(@NonNull TelegramBotsLongPollingApplication telegramBotsApplication,
2225
@NonNull List<SpringLongPollingBot> longPollingBots) {
26+
this(telegramBotsApplication, longPollingBots, TelegramUrl.DEFAULT_URL);
27+
}
28+
29+
public TelegramBotInitializer(@NonNull TelegramBotsLongPollingApplication telegramBotsApplication,
30+
@NonNull List<SpringLongPollingBot> longPollingBots,
31+
TelegramUrl telegramUrl) {
2332
this.telegramBotsApplication = telegramBotsApplication;
2433
this.longPollingBots = longPollingBots;
34+
this.telegramUrl = telegramUrl;
2535
}
2636

2737
@Override
28-
public void afterPropertiesSet() {
38+
public void afterPropertiesSet() {
2939
try {
3040
for (SpringLongPollingBot longPollingBot : longPollingBots) {
31-
BotSession session = telegramBotsApplication.registerBot(longPollingBot.getBotToken(), longPollingBot.getUpdatesConsumer());
41+
BotSession session = telegramBotsApplication.registerBot(longPollingBot.getBotToken(), () -> telegramUrl, new DefaultGetUpdatesGenerator(), longPollingBot.getUpdatesConsumer());
3242
handleAfterRegistrationHook(longPollingBot, session);
3343
}
3444
} catch (TelegramApiException e) {

telegrambots-springboot-longpolling-starter/src/main/java/org/telegram/telegrambots/longpolling/starter/TelegramBotStarterConfiguration.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.context.annotation.Bean;
77
import org.springframework.context.annotation.Configuration;
88
import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication;
9+
import org.telegram.telegrambots.meta.TelegramUrl;
910

1011
import java.util.Collections;
1112
import java.util.List;
@@ -22,8 +23,11 @@ public TelegramBotsLongPollingApplication telegramBotsApplication() {
2223
@Bean
2324
@ConditionalOnMissingBean
2425
public TelegramBotInitializer telegramBotInitializer(TelegramBotsLongPollingApplication telegramBotsApplication,
25-
ObjectProvider<List<SpringLongPollingBot>> longPollingBots) {
26+
ObjectProvider<List<SpringLongPollingBot>> longPollingBots,
27+
ObjectProvider<TelegramUrl> telegramUrl) {
2628
return new TelegramBotInitializer(telegramBotsApplication,
27-
longPollingBots.getIfAvailable(Collections::emptyList));
29+
longPollingBots.getIfAvailable(Collections::emptyList),
30+
telegramUrl.getIfAvailable(() -> TelegramUrl.DEFAULT_URL)
31+
);
2832
}
2933
}

telegrambots-springboot-longpolling-starter/src/test/java/org/telegram/telegrambots/longpolling/starter/TestTelegramBotStarterConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication;
99
import org.telegram.telegrambots.longpolling.interfaces.LongPollingUpdateConsumer;
1010
import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
11+
import org.telegram.telegrambots.meta.TelegramUrl;
1112

1213
import static org.assertj.core.api.Assertions.assertThat;
1314
import static org.mockito.ArgumentMatchers.any;
@@ -31,6 +32,7 @@ void createMockTelegramApplicationWithDefaultSettings() {
3132
assertThat(context).hasSingleBean(TelegramBotsLongPollingApplication.class);
3233
assertThat(context).hasSingleBean(TelegramBotInitializer.class);
3334
assertThat(context).doesNotHaveBean(SpringLongPollingBot.class);
35+
assertThat(context).doesNotHaveBean(TelegramUrl.class);
3436
verifyNoMoreInteractions(context.getBean(TelegramBotsLongPollingApplication.class));
3537
});
3638
}
@@ -43,7 +45,7 @@ void createOnlyLongPollingBot() {
4345

4446
TelegramBotsLongPollingApplication telegramApplication = context.getBean(TelegramBotsLongPollingApplication.class);
4547

46-
verify(telegramApplication, times(1)).registerBot(anyString(), any(LongPollingUpdateConsumer.class));
48+
verify(telegramApplication, times(1)).registerBot(anyString(), any(), any(), any(LongPollingUpdateConsumer.class));
4749
verifyNoMoreInteractions(telegramApplication);
4850
});
4951
}

telegrambots-springboot-longpolling-starter/src/test/java/org/telegram/telegrambots/longpolling/starter/TestTelegramBotStarterRegistrationHooks.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class TestTelegramBotStarterRegistrationHooks {
3737
@Test
3838
void longPollingBotWithAnnotatedMethodshouldBeCalled() throws TelegramApiException {
3939

40-
when(mockApplication.registerBot(anyString(), any(LongPollingUpdateConsumer.class))).thenReturn(someBotSession);
40+
when(mockApplication.registerBot(anyString(), any(), any(), any(LongPollingUpdateConsumer.class))).thenReturn(someBotSession);
4141

4242
this.contextRunner.withUserConfiguration(LongPollingBotConfig.class)
4343
.run((context) -> {
@@ -49,7 +49,7 @@ void longPollingBotWithAnnotatedMethodshouldBeCalled() throws TelegramApiExcepti
4949
assertInstanceOf(AnnotatedLongPollingBot.class, bot);
5050
assertTrue(((AnnotatedLongPollingBot) bot).isHookCalled());
5151
assertEquals(someBotSession, ((AnnotatedLongPollingBot) bot).getHookCalledWithSession());
52-
verify(telegramBotsApi, times(1)).registerBot(eq(bot.getBotToken()), any(LongPollingUpdateConsumer.class));
52+
verify(telegramBotsApi, times(1)).registerBot(eq(bot.getBotToken()), any(), any(), any(LongPollingUpdateConsumer.class));
5353
verifyNoMoreInteractions(telegramBotsApi);
5454
});
5555
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)