Skip to content

Commit 3ae66bc

Browse files
committed
Configurable GetUpdates for SpringLongPollingBot
1 parent b92311f commit 3ae66bc

2 files changed

Lines changed: 77 additions & 30 deletions

File tree

telegrambots-longpolling/src/main/java/org/telegram/telegrambots/longpolling/util/DefaultGetUpdatesGenerator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
@AllArgsConstructor
1010
public class DefaultGetUpdatesGenerator implements Function<Integer, GetUpdates> {
1111

12-
public static final DefaultGetUpdatesGenerator INSTANCE = new DefaultGetUpdatesGenerator(List.of());
12+
public static final DefaultGetUpdatesGenerator INSTANCE = new DefaultGetUpdatesGenerator();
1313

1414
private static final int GET_UPDATES_LIMIT = 100;
1515
private static final int GET_UPDATES_TIMEOUT = 50;
1616

1717
private final List<String> allowedUpdates;
1818

19+
public DefaultGetUpdatesGenerator() {
20+
this(List.of());
21+
}
22+
1923
@Override
2024
public GetUpdates apply(Integer lastReceivedUpdate) {
2125
return GetUpdates
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.telegram.telegrambots.longpolling.starter;
22

3-
import lombok.Getter;
43
import org.junit.jupiter.api.Test;
54
import org.mockito.ArgumentCaptor;
65
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -26,7 +25,6 @@
2625
import static org.mockito.Mockito.mock;
2726
import static org.mockito.Mockito.times;
2827
import static org.mockito.Mockito.verify;
29-
import static org.mockito.Mockito.verifyNoMoreInteractions;
3028
import static org.mockito.Mockito.when;
3129

3230
class TestTelegramBotStarterWithCustomUpdatesGenerator {
@@ -35,9 +33,9 @@ class TestTelegramBotStarterWithCustomUpdatesGenerator {
3533
private static final TelegramBotsLongPollingApplication mockApplication = mock(TelegramBotsLongPollingApplication.class);
3634

3735
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
38-
.withAllowBeanDefinitionOverriding(true)
36+
.withAllowBeanDefinitionOverriding(true)
3937
.withConfiguration(AutoConfigurations.of(MockTelegramBotsApi.class,
40-
TelegramBotStarterConfiguration.class));
38+
TelegramBotStarterConfiguration.class));
4139

4240
@Test
4341
void longPollingBotWithCustomUpdates() throws TelegramApiException {
@@ -46,16 +44,15 @@ void longPollingBotWithCustomUpdates() throws TelegramApiException {
4644

4745
this.contextRunner.withUserConfiguration(CustomUpdatesLongPollingBotConfig.class)
4846
.run((context) -> {
49-
assertThat(context).hasSingleBean(CustomUpdatesLongPollingBot.class);
47+
assertThat(context).hasSingleBean(CustomUpdatesLongPollingBotConfig.Bot.class);
5048

5149
final SpringLongPollingBot bot = context.getBean(SpringLongPollingBot.class);
5250
final TelegramBotsLongPollingApplication telegramBotsApi = context.getBean(TelegramBotsLongPollingApplication.class);
5351

54-
assertInstanceOf(CustomUpdatesLongPollingBot.class, bot);
52+
assertInstanceOf(CustomUpdatesLongPollingBotConfig.Bot.class, bot);
5553

56-
final ArgumentCaptor<Function<Integer,GetUpdates>> getUpdatesGeneratorCaptor = ArgumentCaptor.captor();
54+
final ArgumentCaptor<Function<Integer, GetUpdates>> getUpdatesGeneratorCaptor = ArgumentCaptor.captor();
5755
verify(telegramBotsApi, times(1)).registerBot(eq(bot.getBotToken()), any(), getUpdatesGeneratorCaptor.capture(), any(LongPollingUpdateConsumer.class));
58-
verifyNoMoreInteractions(telegramBotsApi);
5956

6057
final Function<Integer, GetUpdates> getUpdatesGenerator = getUpdatesGeneratorCaptor.getValue();
6158
final GetUpdates getUpdates = getUpdatesGenerator.apply(123);
@@ -67,6 +64,32 @@ void longPollingBotWithCustomUpdates() throws TelegramApiException {
6764
});
6865
}
6966

67+
@Test
68+
void longPollingBotWithDefaultUpdates() throws TelegramApiException {
69+
70+
when(mockApplication.registerBot(anyString(), any(), any(), any(LongPollingUpdateConsumer.class))).thenReturn(someBotSession);
71+
72+
this.contextRunner.withUserConfiguration(DefaultUpdatesLongPollingBotConfig.class)
73+
.run((context) -> {
74+
assertThat(context).hasSingleBean(DefaultUpdatesLongPollingBotConfig.Bot.class);
75+
76+
final SpringLongPollingBot bot = context.getBean(SpringLongPollingBot.class);
77+
final TelegramBotsLongPollingApplication telegramBotsApi = context.getBean(TelegramBotsLongPollingApplication.class);
78+
79+
assertInstanceOf(DefaultUpdatesLongPollingBotConfig.Bot.class, bot);
80+
81+
final ArgumentCaptor<Function<Integer, GetUpdates>> getUpdatesGeneratorCaptor = ArgumentCaptor.captor();
82+
verify(telegramBotsApi, times(1)).registerBot(eq(bot.getBotToken()), any(), getUpdatesGeneratorCaptor.capture(), any(LongPollingUpdateConsumer.class));
83+
84+
final Function<Integer, GetUpdates> getUpdatesGenerator = getUpdatesGeneratorCaptor.getValue();
85+
final GetUpdates getUpdates = getUpdatesGenerator.apply(123);
86+
assertNotNull(getUpdates);
87+
assertEquals(100, getUpdates.getLimit());
88+
assertEquals(124, getUpdates.getOffset());
89+
assertEquals(50, getUpdates.getTimeout());
90+
assertEquals(List.of(), getUpdates.getAllowedUpdates());
91+
});
92+
}
7093

7194
@Configuration
7295
public static class MockTelegramBotsApi {
@@ -77,36 +100,56 @@ public TelegramBotsLongPollingApplication telegramBotsApplication() {
77100
}
78101

79102
@Configuration
80-
public static class CustomUpdatesLongPollingBotConfig {
103+
static class CustomUpdatesLongPollingBotConfig {
81104
@Bean
82105
public SpringLongPollingBot longPollingBot() {
83-
return new CustomUpdatesLongPollingBot();
106+
return new Bot();
84107
}
85-
}
86108

87-
@Getter
88-
public static class CustomUpdatesLongPollingBot implements SpringLongPollingBot {
89-
public CustomUpdatesLongPollingBot() {
109+
static class Bot implements SpringLongPollingBot {
110+
111+
@Override
112+
public String getBotToken() {
113+
return "custom";
114+
}
115+
116+
@Override
117+
public LongPollingUpdateConsumer getUpdatesConsumer() {
118+
return update -> {
119+
};
120+
}
121+
122+
@Override
123+
public GetUpdates getUpdates(final int offset) {
124+
return GetUpdates.builder()
125+
.limit(42)
126+
.timeout(555)
127+
.offset(offset + 1)
128+
.allowedUpdates(List.of("message", "callback_query", "message_reaction"))
129+
.build();
130+
}
90131
}
132+
}
91133

92-
@Override
93-
public String getBotToken() {
94-
return "token";
134+
@Configuration
135+
static class DefaultUpdatesLongPollingBotConfig {
136+
@Bean
137+
public SpringLongPollingBot longPollingBot() {
138+
return new Bot();
95139
}
96140

97-
@Override
98-
public LongPollingUpdateConsumer getUpdatesConsumer() {
99-
return update -> { };
100-
}
141+
static class Bot implements SpringLongPollingBot {
142+
143+
@Override
144+
public String getBotToken() {
145+
return "default";
146+
}
101147

102-
@Override
103-
public GetUpdates getUpdates(final int offset) {
104-
return GetUpdates.builder()
105-
.limit(42)
106-
.timeout(555)
107-
.offset(offset + 1)
108-
.allowedUpdates(List.of("message", "callback_query", "message_reaction"))
109-
.build();
148+
@Override
149+
public LongPollingUpdateConsumer getUpdatesConsumer() {
150+
return update -> {
151+
};
152+
}
110153
}
111154
}
112155
}

0 commit comments

Comments
 (0)