1- package backend .academy .bot .processor ;
1+ package backend .academy .bot .metrics ;
22
33import static org .junit .jupiter .api .Assertions .*;
44import static org .mockito .Mockito .*;
55
66import backend .academy .bot .command .Command ;
77import backend .academy .bot .command .link .TrackCommand ;
8- import backend .academy .bot .state . UserState ;
8+ import backend .academy .bot .processor . UserMessageProcessor ;
99import backend .academy .bot .state .UserStateManager ;
1010import com .pengrad .telegrambot .TelegramBot ;
1111import com .pengrad .telegrambot .model .Chat ;
1212import com .pengrad .telegrambot .model .Message ;
1313import com .pengrad .telegrambot .model .Update ;
1414import com .pengrad .telegrambot .request .SendMessage ;
15+ import io .micrometer .core .instrument .Counter ;
16+ import io .micrometer .core .instrument .MeterRegistry ;
17+ import io .micrometer .core .instrument .simple .SimpleMeterRegistry ;
1518import java .util .List ;
1619import org .junit .jupiter .api .BeforeEach ;
1720import org .junit .jupiter .api .DisplayName ;
@@ -34,50 +37,29 @@ public class UserMessageProcessorTest {
3437 private UserStateManager userStateManager ;
3538
3639 private UserMessageProcessor userMessageProcessor ;
40+ private MeterRegistry meterRegistry ;
3741
3842 @ BeforeEach
3943 void setUp () {
4044 MockitoAnnotations .openMocks (this );
41- userMessageProcessor = new UserMessageProcessor (telegramBot , List .of (command1 , trackCommand ), userStateManager );
42- }
43-
44- @ Test
45- @ DisplayName ("Обработка сообщения: команда найдена и обработана" )
46- void testProcess_CommandFoundAndHandled () {
47- Update update = createUpdateWithText ("/mock" );
48- when (command1 .matchesCommand (update )).thenReturn (true );
49- when (command1 .handle (update )).thenReturn (new SendMessage (123L , "Mock message" ));
50-
51- SendMessage result = userMessageProcessor .process (update );
52- verify (command1 , times (1 )).matchesCommand (update );
53- verify (command1 , times (1 )).handle (update );
54- assertEquals ("Mock message" , result .getParameters ().get ("text" ));
55- }
56-
57- @ Test
58- @ DisplayName ("Обработка сообщения: команда не найдена, состояние WAITING_URL" )
59- void testProcess_NoCommandFound_WaitingUrlState () {
60- Update update = createUpdateWithText ("https://github.com/example" );
61- when (command1 .matchesCommand (update )).thenReturn (false );
62- when (userStateManager .getUserState (123L )).thenReturn (UserState .WAITING_URL );
63- when (trackCommand .handle (update )).thenReturn (new SendMessage (123L , "Track command handled" ));
64-
65- SendMessage result = userMessageProcessor .process (update );
66-
67- verify (command1 , times (1 )).matchesCommand (update );
68- verify (trackCommand , times (1 )).handle (update );
69- assertEquals ("Track command handled" , result .getParameters ().get ("text" ));
45+ meterRegistry = new SimpleMeterRegistry (); // Используем реальный MeterRegistry
46+ userMessageProcessor =
47+ new UserMessageProcessor (telegramBot , List .of (command1 , trackCommand ), userStateManager , meterRegistry );
7048 }
7149
7250 @ Test
7351 @ DisplayName ("Обработка сообщения: пользователь создается, если не существует" )
7452 void testProcess_UserCreatedIfNotExist () {
7553 Update update = createUpdateWithText ("/start" );
7654 when (command1 .matchesCommand (update )).thenReturn (true );
77- when (command1 .handle (update )).thenReturn (new SendMessage (123L , "User created" ));
55+ when (command1 .handle (update )).thenReturn (new SendMessage ("123" , "User created" ));
7856
7957 userMessageProcessor .process (update );
8058
59+ // Проверяем метрику
60+ Counter counter = meterRegistry .counter ("msg_count" );
61+ assertEquals (1 , counter .count ());
62+
8163 verify (userStateManager , times (1 )).createUserIfNotExist (123L );
8264 }
8365
@@ -89,8 +71,6 @@ private Update createUpdateWithText(String text) {
8971 when (update .message ()).thenReturn (message );
9072 when (message .chat ()).thenReturn (chat );
9173 when (chat .id ()).thenReturn (123L );
92- when (message .text ()).thenReturn (text );
93-
9474 return update ;
9575 }
9676}
0 commit comments