Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring #1455

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>org.telegram</groupId>
<artifactId>Bots</artifactId>
<packaging>pom</packaging>
<version>7.11.0</version>
<version>7.11.1</version>

<modules>
<module>telegrambots-meta</module>
Expand Down
6 changes: 3 additions & 3 deletions telegrambots-abilities/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.telegram</groupId>
<artifactId>Bots</artifactId>
<version>7.11.0</version>
<version>7.11.1</version>
</parent>

<artifactId>telegrambots-abilities</artifactId>
Expand Down Expand Up @@ -104,12 +104,12 @@
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-webhook</artifactId>
<version>7.11.0</version>
<version>7.11.1</version>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-longpolling</artifactId>
<version>7.11.0</version>
<version>7.11.1</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion telegrambots-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.telegram</groupId>
<artifactId>Bots</artifactId>
<version>7.11.0</version>
<version>7.11.1</version>
</parent>

<name>Telegram Bots Client</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public OkHttpTelegramClient(String botToken) {
this(new OkHttpClient.Builder().build(), botToken);
}

@Override
public String getBotToken() {
return botToken;
}

@Override
public <T extends Serializable, Method extends BotApiMethod<T>> CompletableFuture<T> executeAsync(Method method) throws TelegramApiException {
if (method == null) {
Expand Down
6 changes: 3 additions & 3 deletions telegrambots-extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.telegram</groupId>
<artifactId>Bots</artifactId>
<version>7.11.0</version>
<version>7.11.1</version>
</parent>

<artifactId>telegrambots-extensions</artifactId>
Expand Down Expand Up @@ -89,12 +89,12 @@
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-webhook</artifactId>
<version>7.11.0</version>
<version>7.11.1</version>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-longpolling</artifactId>
<version>7.11.0</version>
<version>7.11.1</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion telegrambots-longpolling/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.telegram</groupId>
<artifactId>Bots</artifactId>
<version>7.11.0</version>
<version>7.11.1</version>
</parent>

<artifactId>telegrambots-longpolling</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.telegram.telegrambots.meta.api.methods.updates.GetUpdates;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.longpolling.interfaces.BackOff;
import org.telegram.telegrambots.meta.generics.TelegramClient;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -59,14 +60,15 @@ public TelegramBotsLongPollingApplication(Supplier<ObjectMapper> objectMapperSup
this.backOffSupplier = backOffSupplier;
}

public BotSession registerBot(String botToken, LongPollingUpdateConsumer updatesConsumer) throws TelegramApiException {
return registerBot(botToken, () -> TelegramUrl.DEFAULT_URL, new DefaultGetUpdatesGenerator(), updatesConsumer);
public BotSession registerBot(TelegramClient telegramClient, LongPollingUpdateConsumer updatesConsumer) throws TelegramApiException {
return registerBot(telegramClient, () -> TelegramUrl.DEFAULT_URL, new DefaultGetUpdatesGenerator(), updatesConsumer);
}

public BotSession registerBot(String botToken,
public BotSession registerBot(TelegramClient telegramClient,
Supplier<TelegramUrl> telegramUrlSupplier,
Function<Integer, GetUpdates> getUpdatesGenerator,
LongPollingUpdateConsumer updatesConsumer) throws TelegramApiException {
final String botToken = telegramClient.getBotToken();
if (botSessions.containsKey(botToken)) {
throw new TelegramApiException("Bot is already registered");
} else {
Expand All @@ -88,6 +90,11 @@ public BotSession registerBot(String botToken,
}
}

public void unregisterBot(TelegramClient telegramClient) throws TelegramApiException {
final String botToken = telegramClient.getBotToken();
unregisterBot(botToken);
}

public void unregisterBot(String botToken) throws TelegramApiException {
if (botSessions.containsKey(botToken)) {
BotSession botSession = botSessions.remove(botToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.telegram.telegrambots.longpolling.util.DefaultGetUpdatesGenerator;
Expand All @@ -21,6 +23,7 @@
import org.telegram.telegrambots.meta.api.objects.ApiResponse;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.TelegramClient;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -32,8 +35,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;

public class TestTelegramBotsLongPollingApplicationIntegration {
private MockWebServer webServer;
Expand All @@ -44,11 +46,17 @@ public class TestTelegramBotsLongPollingApplicationIntegration {
@Spy
private ExponentialBackOff exponentialBackOff = new ExponentialBackOff();

@Mock
private TelegramClient mockTelegramClient = Mockito.mock(TelegramClient.class);

private AutoCloseable mockitoCloseable;

@BeforeEach
public void setUp() {
mockitoCloseable = MockitoAnnotations.openMocks(this);

when(mockTelegramClient.getBotToken()).then(invocation -> "TOKEN");

webServer = new MockWebServer();
HttpUrl mockUrl = webServer.url("");
telegramUrl = TelegramUrl.builder().schema(mockUrl.scheme()).host(mockUrl.host()).port(mockUrl.port()).build();
Expand Down Expand Up @@ -76,7 +84,7 @@ public void testIsRunningCheck() {

assertTrue(application.isRunning());

application.registerBot("TOKEN",
application.registerBot(mockTelegramClient,
() -> telegramUrl,
new DefaultGetUpdatesGenerator(),
(LongPollingSingleThreadUpdateConsumer) update -> updateReceived.add(update));
Expand Down Expand Up @@ -134,7 +142,7 @@ public void testStartStop() {

assertFalse(application.isRunning());

application.registerBot("TOKEN",
application.registerBot(mockTelegramClient,
() -> telegramUrl,
new DefaultGetUpdatesGenerator(),
(LongPollingSingleThreadUpdateConsumer) update -> updateReceived.add(update));
Expand Down Expand Up @@ -169,7 +177,7 @@ public void testUnregisterBot() {

application.stop();

application.registerBot("TOKEN",
application.registerBot(mockTelegramClient,
() -> telegramUrl,
new DefaultGetUpdatesGenerator(),
(LongPollingSingleThreadUpdateConsumer) update -> updateReceived.add(update));
Expand All @@ -195,14 +203,14 @@ public void testRegisterSameBotTwiceRaisesException() {

assertTrue(application.isRunning());

application.registerBot("TOKEN",
application.registerBot(mockTelegramClient,
() -> telegramUrl,
new DefaultGetUpdatesGenerator(),
(LongPollingSingleThreadUpdateConsumer) update -> {

});

application.registerBot("TOKEN",
application.registerBot(mockTelegramClient,
() -> telegramUrl,
new DefaultGetUpdatesGenerator(),
(LongPollingSingleThreadUpdateConsumer) update -> {
Expand Down Expand Up @@ -239,7 +247,7 @@ public void testUpdatesAreReceived() {
Dispatcher dispatcher = getDispatcher(List.of(getFakeUpdates1()));
webServer.setDispatcher(dispatcher);

application.registerBot("TOKEN",
application.registerBot(mockTelegramClient,
() -> telegramUrl,
new DefaultGetUpdatesGenerator(),
(LongPollingSingleThreadUpdateConsumer) update -> updateReceived.add(update));
Expand All @@ -258,7 +266,7 @@ public void testOutOfOrderUpdatesAreIgnored() {
Dispatcher dispatcher = getDispatcher(List.of(getFakeUpdates1(), getFakeUpdates1(), getFakeUpdates2()));
webServer.setDispatcher(dispatcher);

application.registerBot("TOKEN",
application.registerBot(mockTelegramClient,
() -> telegramUrl,
new DefaultGetUpdatesGenerator(),
(LongPollingSingleThreadUpdateConsumer) update -> updateReceived.add(update));
Expand All @@ -279,7 +287,7 @@ public void testWhenErrorResponseBackOffIsExecuted() {
Dispatcher dispatcher = getDispatcher(List.of("WRONGRESPONSE", getFakeUpdates1()));
webServer.setDispatcher(dispatcher);

application.registerBot("TOKEN",
application.registerBot(mockTelegramClient,
() -> telegramUrl,
new DefaultGetUpdatesGenerator(),
(LongPollingSingleThreadUpdateConsumer) update -> updateReceived.add(update));
Expand Down
2 changes: 1 addition & 1 deletion telegrambots-meta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.telegram</groupId>
<artifactId>Bots</artifactId>
<version>7.11.0</version>
<version>7.11.1</version>
</parent>

<artifactId>telegrambots-meta</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.concurrent.CompletableFuture;

public interface TelegramClient {
String getBotToken();

<T extends Serializable, Method extends BotApiMethod<T>> CompletableFuture<T> executeAsync(Method method) throws TelegramApiException;

Expand Down
2 changes: 1 addition & 1 deletion telegrambots-springboot-longpolling-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.telegram</groupId>
<artifactId>Bots</artifactId>
<version>7.11.0</version>
<version>7.11.1</version>
</parent>

<artifactId>telegrambots-springboot-longpolling-starter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.telegram.telegrambots.longpolling.starter;

import org.telegram.telegrambots.longpolling.interfaces.LongPollingUpdateConsumer;
import org.telegram.telegrambots.meta.generics.TelegramClient;

public interface SpringLongPollingBot {
String getBotToken();
TelegramClient getTelegramClient();
LongPollingUpdateConsumer getUpdatesConsumer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ public TelegramBotInitializer(@NonNull TelegramBotsLongPollingApplication telegr
public void afterPropertiesSet() {
try {
for (SpringLongPollingBot longPollingBot : longPollingBots) {
BotSession session = telegramBotsApplication.registerBot(longPollingBot.getBotToken(), longPollingBot.getUpdatesConsumer());
BotSession session = telegramBotsApplication.registerBot(longPollingBot.getTelegramClient(), longPollingBot.getUpdatesConsumer());
handleAfterRegistrationHook(longPollingBot, session);
}
} catch (TelegramApiException e) {
throw new RuntimeException(e);
}
}


private void handleAfterRegistrationHook(Object bot, BotSession botSession) {
Stream.of(bot.getClass().getMethods())
.filter(method -> method.getAnnotation(AfterBotRegistration.class) != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@
import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication;
import org.telegram.telegrambots.longpolling.interfaces.LongPollingUpdateConsumer;
import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
import org.telegram.telegrambots.meta.generics.TelegramClient;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.*;

class TestTelegramBotStarterConfiguration {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withAllowBeanDefinitionOverriding(true)
.withConfiguration(AutoConfigurations.of(MockTelegramBotsLongPollingApplication.class,
TelegramBotStarterConfiguration.class));
TelegramBotStarterConfiguration.class));

@Test
void createMockTelegramApplicationWithDefaultSettings() {
Expand All @@ -43,7 +39,7 @@ void createOnlyLongPollingBot() {

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

verify(telegramApplication, times(1)).registerBot(anyString(), any(LongPollingUpdateConsumer.class));
verify(telegramApplication, times(1)).registerBot(any(TelegramClient.class), any(LongPollingUpdateConsumer.class));
verifyNoMoreInteractions(telegramApplication);
});
}
Expand All @@ -62,8 +58,9 @@ static class LongPollingBotConfig {
@Bean
public SpringLongPollingBot longPollingBot() {
SpringLongPollingBot springLongPollingBotMock = mock(SpringLongPollingBot.class);
doReturn("").when(springLongPollingBotMock).getBotToken();
doReturn((LongPollingSingleThreadUpdateConsumer) update -> {}).when(springLongPollingBotMock).getUpdatesConsumer();
doReturn(mock(TelegramClient.class)).when(springLongPollingBotMock).getTelegramClient();
doReturn((LongPollingSingleThreadUpdateConsumer) update -> {
}).when(springLongPollingBotMock).getUpdatesConsumer();
return springLongPollingBotMock;
}
}
Expand Down
Loading