Skip to content

Commit 92ae4fd

Browse files
Добавлен тест для LocalisationMiddleware, проверяющий обработку обновлений и переходы между сценами
1 parent 94f8b54 commit 92ae4fd

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package ru.untitled_devs.bot.features.localisation;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import org.telegram.telegrambots.meta.api.objects.Message;
6+
import org.telegram.telegrambots.meta.api.objects.Update;
7+
import ru.untitled_devs.core.context.UpdateContext;
8+
import ru.untitled_devs.core.context.UpdateContextFactory;
9+
import ru.untitled_devs.core.exceptions.StopMiddlewareException;
10+
import ru.untitled_devs.core.exceptions.StopRoutingException;
11+
import ru.untitled_devs.core.fsm.DataKey;
12+
import ru.untitled_devs.core.fsm.FSMContext;
13+
import ru.untitled_devs.core.routers.scenes.SceneManager;
14+
15+
import java.util.Locale;
16+
17+
import static org.mockito.Mockito.*;
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
public class LocalisationMiddlewareTest {
21+
DataKey<Locale> langKey = DataKey.of("lang", Locale.class);
22+
LocalisationMiddleware middleware;
23+
24+
FSMContext context;
25+
Update update;
26+
SceneManager sceneManager;
27+
28+
@BeforeEach
29+
void setup() {
30+
context = mock(FSMContext.class);
31+
update = mock(Update.class);
32+
sceneManager = mock(SceneManager.class);
33+
middleware = new LocalisationMiddleware(sceneManager);
34+
35+
Message message = mock(Message.class);
36+
when(message.getChatId()).thenReturn(124512L);
37+
when(message.hasText()).thenReturn(true);
38+
when(message.getText()).thenReturn("Test");
39+
when(update.hasMessage()).thenReturn(true);
40+
when(update.getMessage()).thenReturn(message);
41+
}
42+
43+
@Test
44+
void preHandleSwitchToLocalisationScene() {
45+
when(context.getData(langKey)).thenReturn(null);
46+
47+
UpdateContext updateContext = UpdateContextFactory.create(update);
48+
assertThrows(StopRoutingException.class,
49+
() -> middleware.preHandle(updateContext, context));
50+
verify(sceneManager).enterScene(any(Long.class), eq("lang"), eq(context));
51+
}
52+
53+
@Test
54+
void preHandleSkipMiddlewareChainScene() {
55+
when(context.getState()).thenReturn(LocalisationStates.START);
56+
57+
UpdateContext updateContext = UpdateContextFactory.create(update);
58+
assertThrows(StopMiddlewareException.class,
59+
() -> middleware.preHandle(updateContext, context));
60+
}
61+
62+
@Test
63+
void preHandleSkipLocalisationScene() {
64+
when(context.getData(langKey)).thenReturn(Locale.of("ru_RU"));
65+
66+
UpdateContext updateContext = UpdateContextFactory.create(update);
67+
assertDoesNotThrow(() -> middleware.preHandle(updateContext, context));
68+
}
69+
}

0 commit comments

Comments
 (0)