Skip to content

Commit 13c3ba2

Browse files
Исправил ошибки, дописал тесты на Bot.java
1 parent a14656f commit 13c3ba2

File tree

4 files changed

+122
-11
lines changed

4 files changed

+122
-11
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/ru/untitled_devs/core/client/Bot.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ public void editMessageReplyMarkup(long chatId, int messageId, InlineKeyboardMar
102102
editMessageReplyMarkup.setChatId(chatId);
103103
editMessageReplyMarkup.setMessageId(messageId);
104104
editMessageReplyMarkup.setReplyMarkup(replyKeyboard);
105+
106+
try {
107+
execute(editMessageReplyMarkup);
108+
} catch (TelegramApiException e) {
109+
this.logger.error(e.getMessage());
110+
}
105111
}
106112

107113
@Override
@@ -125,6 +131,12 @@ public void sendPhoto(long chatId, String caption, byte[] photo) {
125131

126132
InputFile file = new InputFile(new ByteArrayInputStream(photo), getImageFileNameWithExtension(photo));
127133
sendPhoto.setPhoto(file);
134+
135+
try {
136+
execute(sendPhoto);
137+
} catch (TelegramApiException e) {
138+
this.logger.error(e.getMessage());
139+
}
128140
}
129141

130142
@Override
@@ -133,6 +145,12 @@ public void answerCallbackQuery(String callbackQueryId, String text, boolean sho
133145
answerCallbackQuery.setCallbackQueryId(callbackQueryId);
134146
answerCallbackQuery.setText(text);
135147
answerCallbackQuery.setShowAlert(showAlert);
148+
149+
try {
150+
execute(answerCallbackQuery);
151+
} catch (TelegramApiException e) {
152+
this.logger.error(e.getMessage());
153+
}
136154
}
137155

138156
public void banChatMember(long chatId, long userId, int duration) {
@@ -148,7 +166,7 @@ public void banChatMember(long chatId, long userId, int duration) {
148166
try {
149167
execute(banChatMember);
150168
} catch (TelegramApiException e) {
151-
System.err.println(e.getMessage());
169+
this.logger.error(e.getMessage());
152170
}
153171

154172
}

src/main/java/ru/untitled_devs/core/routers/handlers/CallbackQueryHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ru.untitled_devs.core.routers.handlers;
22

3-
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
3+
import org.telegram.telegrambots.meta.api.objects.Message;
44
import org.telegram.telegrambots.meta.api.objects.Update;
55
import ru.untitled_devs.core.fsm.State;
66
import ru.untitled_devs.core.fsm.context.FSMContext;
@@ -11,13 +11,13 @@
1111
import java.util.List;
1212
import java.util.function.BiConsumer;
1313

14-
public class CallbackQueryHandler implements Handler{
14+
public class CallbackQueryHandler implements Handler {
1515

1616
private final List<Filter> filters = new ArrayList<>();
17-
private final BiConsumer<CallbackQuery, FSMContext> action;
17+
private final BiConsumer<Message, FSMContext> action;
1818
private final State state;
1919

20-
public CallbackQueryHandler(BiConsumer<CallbackQuery, FSMContext> action, State state, Filter... filters) {
20+
public CallbackQueryHandler(BiConsumer<Message, FSMContext> action, State state, Filter... filters) {
2121
this.filters.addAll(Arrays.asList(filters));
2222
this.action = action;
2323
this.state = state;
@@ -35,11 +35,11 @@ public boolean canHandle(Update update, FSMContext ctx) {
3535

3636
@Override
3737
public void handleUpdate(Update update, FSMContext ctx) {
38-
action.accept(update.getCallbackQuery(), ctx);
38+
action.accept(update.getMessage(), ctx);
3939
}
4040

41-
@Override
4241
public void addFilter(Filter filter) {
4342
this.filters.add(filter);
4443
}
44+
4545
}

src/test/java/ru/untitleddevs/core/client/BotTest.java

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
package ru.untitleddevs.core.client;
22

3-
import org.apache.commons.logging.Log;
43
import org.apache.logging.log4j.Logger;
54
import org.junit.jupiter.api.BeforeEach;
65
import org.junit.jupiter.api.Test;
6+
import org.telegram.telegrambots.meta.api.methods.AnswerCallbackQuery;
77
import org.telegram.telegrambots.meta.api.methods.groupadministration.BanChatMember;
88
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
9+
import org.telegram.telegrambots.meta.api.methods.send.SendPhoto;
10+
import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage;
11+
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageReplyMarkup;
12+
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
913
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
1014
import org.telegram.telegrambots.meta.api.objects.Message;
1115
import org.telegram.telegrambots.meta.api.objects.Update;
1216
import org.telegram.telegrambots.meta.api.objects.User;
17+
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
1318
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
1419
import ru.untitled_devs.core.client.Bot;
1520
import ru.untitled_devs.core.fsm.context.FSMContext;
1621
import ru.untitled_devs.core.fsm.storage.StorageKey;
1722
import ru.untitled_devs.core.fsm.storage.Storage;
1823
import ru.untitled_devs.core.routers.Router;
1924

25+
import java.util.Base64;
26+
2027
import static org.junit.jupiter.api.Assertions.*;
2128
import static org.mockito.ArgumentMatchers.any;
2229
import static org.mockito.Mockito.*;
@@ -26,13 +33,12 @@ class BotTest {
2633
private Bot bot;
2734
private Storage storage;
2835
private Router router;
29-
private Logger logger;
3036

3137
@BeforeEach
3238
void setUp() {
3339
storage = mock(Storage.class);
3440
router = mock(Router.class);
35-
logger = mock(Logger.class);
41+
Logger logger = mock(Logger.class);
3642
bot = new Bot("testToken", "testUsername", storage, logger);
3743
bot.addRouter(router);
3844
}
@@ -50,6 +56,21 @@ void sendMessageSuccessfullySendsMessage() throws TelegramApiException {
5056
verify(bot, times(1)).execute(any(SendMessage.class));
5157
}
5258

59+
@Test
60+
void sendMessageSuccessfullySendsMessageWithMarkup() throws TelegramApiException {
61+
bot = spy(bot);
62+
doReturn(null).when(bot).execute(any(SendMessage.class));
63+
64+
InlineKeyboardMarkup markup = new InlineKeyboardMarkup();
65+
66+
assertDoesNotThrow(
67+
() -> bot.sendMessage(12345L, "Hello, World!", markup),
68+
"sendMessage should not throw any exception when execute(...) returns null"
69+
);
70+
71+
verify(bot, times(1)).execute(any(SendMessage.class));
72+
}
73+
5374
@Test
5475
void sendMessageHandlesTelegramApiException() throws TelegramApiException {
5576
bot = spy(bot);
@@ -89,6 +110,78 @@ void banChatMemberHandlesTelegramApiException() throws TelegramApiException {
89110
verify(bot, times(1)).execute(any(BanChatMember.class));
90111
}
91112

113+
@Test
114+
void seditMessageSuccessfullyEditMessageText() throws TelegramApiException {
115+
bot = spy(bot);
116+
doReturn(null).when(bot).execute(any(EditMessageText.class));
117+
118+
assertDoesNotThrow(
119+
() -> bot.editMessageText(12345L, 12415125,"Hello, World!"),
120+
"sendMessage should not throw any exception when execute(...) returns null"
121+
);
122+
123+
verify(bot, times(1)).execute(any(EditMessageText.class));
124+
}
125+
126+
@Test
127+
void EditMessageMarkupSuccessfullyEditMessageMarkup() throws TelegramApiException {
128+
bot = spy(bot);
129+
doReturn(null).when(bot).execute(any(EditMessageReplyMarkup.class));
130+
131+
InlineKeyboardMarkup markup = new InlineKeyboardMarkup();
132+
133+
assertDoesNotThrow(
134+
() -> bot.editMessageReplyMarkup(12345L, 12415125, markup),
135+
"sendMessage should not throw any exception when execute(...) returns null"
136+
);
137+
138+
verify(bot, times(1)).execute(any(EditMessageReplyMarkup.class));
139+
}
140+
141+
@Test
142+
void deleteMessageSuccessfullyDeletesMessage() throws TelegramApiException {
143+
bot = spy(bot);
144+
doReturn(null).when(bot).execute(any(DeleteMessage.class));
145+
146+
assertDoesNotThrow(
147+
() -> bot.deleteMessage(12345L, 12415125),
148+
"sendMessage should not throw any exception when execute(...) returns null"
149+
);
150+
151+
verify(bot, times(1)).execute(any(DeleteMessage.class));
152+
}
153+
154+
@Test
155+
void sendPhotoSuccessfullySendsPhoto() throws TelegramApiException {
156+
bot = spy(bot);
157+
doReturn(null).when(bot).execute(any(SendPhoto.class));
158+
159+
byte[] photo = Base64.getDecoder().decode(
160+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQI12NgYAAAAAMAASsJTYQAAAAASUVORK5CYII="
161+
);
162+
163+
assertDoesNotThrow(
164+
() -> bot.sendPhoto(12345L, "12415125", photo),
165+
"sendMessage should not throw any exception when execute(...) returns null"
166+
);
167+
168+
verify(bot, times(1)).execute(any(SendPhoto.class));
169+
}
170+
171+
@Test
172+
void answerCallbackQuerySuccessfullyAnswersCallback() throws TelegramApiException {
173+
bot = spy(bot);
174+
doReturn(null).when(bot).execute(any(AnswerCallbackQuery.class));
175+
176+
assertDoesNotThrow(
177+
() -> bot.answerCallbackQuery("21323", "12415125", false),
178+
"sendMessage should not throw any exception when execute(...) returns null"
179+
);
180+
181+
verify(bot, times(1)).execute(any(AnswerCallbackQuery.class));
182+
}
183+
184+
92185
@Test
93186
void onUpdateMessageReceivedRoutesUpdateToAllRouters() {
94187
Update update = mock(Update.class);

0 commit comments

Comments
 (0)