Slavomir Hustaty 2021-04-19 14:09:08 +02:00
parent 4882040822
commit 58822f5113
6 changed files with 38 additions and 29 deletions

View File

@ -185,6 +185,10 @@ public final class TelegramBotsHelper {
return "{\"ok\": false,\"error_code\": 400,\"description\": \"Error descriptions\",\"parameters\": {\"migrate_to_chat_id\": 12345,\"retry_after\": 12}}"; return "{\"ok\": false,\"error_code\": 400,\"description\": \"Error descriptions\",\"parameters\": {\"migrate_to_chat_id\": 12345,\"retry_after\": 12}}";
} }
public static String getResponseWithError409() {
return "{\"ok\": false,\"error_code\": 409,\"description\": \"Conflict: terminated by other getUpdates request; make sure that only one bot instance is running\"}";
}
public static String GetSetGameScoreBooleanResponse() { public static String GetSetGameScoreBooleanResponse() {
return "{\"ok\": true,\"result\": true}"; return "{\"ok\": true,\"result\": true}";
} }

View File

@ -51,7 +51,7 @@ class TestDeserialization {
} }
@Test @Test
void TestListUpdates() throws Exception { void testListUpdates() throws Exception {
String updateText = "{\"ok\":true,\"result\":[{\"update_id\":79995144,\n" + String updateText = "{\"ok\":true,\"result\":[{\"update_id\":79995144,\n" +
"\"message\":{\"message_id\":90,\"from\":{\"id\":1234567,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"en\"},\"chat\":{\"id\":1234567,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154223,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":79995145,\n" + "\"message\":{\"message_id\":90,\"from\":{\"id\":1234567,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"en\"},\"chat\":{\"id\":1234567,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154223,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":79995145,\n" +
"\"message\":{\"message_id\":91,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"it\"},\"chat\":{\"id\":12345678,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154306,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":79995146,\n" + "\"message\":{\"message_id\":91,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"it\"},\"chat\":{\"id\":12345678,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154306,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":79995146,\n" +
@ -89,7 +89,7 @@ class TestDeserialization {
} }
@Test @Test
void TestListUpdates2() throws Exception { void testListUpdates2() throws Exception {
String updateText = "{\"ok\":true,\"result\":[{\"update_id\":259894298,\n" + String updateText = "{\"ok\":true,\"result\":[{\"update_id\":259894298,\n" +
"\"message\":{\"message_id\":101,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\"},\"chat\":{\"id\":12345678,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\",\"type\":\"private\"},\"date\":1604171814,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":259894299,\n" + "\"message\":{\"message_id\":101,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\"},\"chat\":{\"id\":12345678,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\",\"type\":\"private\"},\"date\":1604171814,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":259894299,\n" +
"\"message\":{\"message_id\":102,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\",\"language_code\":\"en\"},\"chat\":{\"id\":12345678,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\",\"type\":\"private\"},\"date\":1604188661,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":259894300,\n" + "\"message\":{\"message_id\":102,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\",\"language_code\":\"en\"},\"chat\":{\"id\":12345678,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\",\"type\":\"private\"},\"date\":1604188661,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":259894300,\n" +
@ -106,7 +106,7 @@ class TestDeserialization {
} }
@Test @Test
void TestListUpdates3() throws Exception { void testListUpdates3() throws Exception {
String updateText = "{\"ok\":true,\"result\":[{\"update_id\":259894302,\n" + String updateText = "{\"ok\":true,\"result\":[{\"update_id\":259894302,\n" +
"\"message\":{\"message_id\":105,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FirstName\",\"username\":\"Username\"},\"chat\":{\"id\":12345678,\"first_name\":\"FirstName\",\"username\":\"Username\",\"type\":\"private\"},\"date\":1604226451,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":259894303,\n" + "\"message\":{\"message_id\":105,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FirstName\",\"username\":\"Username\"},\"chat\":{\"id\":12345678,\"first_name\":\"FirstName\",\"username\":\"Username\",\"type\":\"private\"},\"date\":1604226451,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":259894303,\n" +
"\"message\":{\"message_id\":106,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FirstName\",\"username\":\"Username\",\"language_code\":\"en\"},\"chat\":{\"id\":12345678,\"first_name\":\"FirstName\",\"username\":\"Username\",\"type\":\"private\"},\"date\":1604226480,\"document\":{\"file_name\":\"aaa.txt\",\"mime_type\":\"text/plain\",\"file_id\":\"FILEID\",\"file_unique_id\":\"AgADiQEAAjTe-VQ\",\"file_size\":2}}},{\"update_id\":259894304,\n" + "\"message\":{\"message_id\":106,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FirstName\",\"username\":\"Username\",\"language_code\":\"en\"},\"chat\":{\"id\":12345678,\"first_name\":\"FirstName\",\"username\":\"Username\",\"type\":\"private\"},\"date\":1604226480,\"document\":{\"file_name\":\"aaa.txt\",\"mime_type\":\"text/plain\",\"file_id\":\"FILEID\",\"file_unique_id\":\"AgADiQEAAjTe-VQ\",\"file_size\":2}}},{\"update_id\":259894304,\n" +
@ -165,7 +165,7 @@ class TestDeserialization {
} }
@Test @Test
void TestListUpdatesVoiceChat() throws Exception { void testListUpdatesVoiceChat() throws Exception {
String updateText = "{\n" + String updateText = "{\n" +
" \"ok\": true,\n" + " \"ok\": true,\n" +
" \"result\": [\n" + " \"result\": [\n" +
@ -289,7 +289,7 @@ class TestDeserialization {
} }
@Test @Test
void TestDeserializationCloseMethod() throws Exception { void testDeserializationCloseMethod() throws Exception {
String updateText = "{\"ok\":true,\"result\": true}"; String updateText = "{\"ok\":true,\"result\": true}";
Boolean response = new Close().deserializeResponse(updateText); Boolean response = new Close().deserializeResponse(updateText);
@ -298,7 +298,7 @@ class TestDeserialization {
} }
@Test @Test
void TestDeserializationChatMember() throws Exception { void testDeserializationChatMember() throws Exception {
String updateText = "{\n" + String updateText = "{\n" +
" \"ok\": true,\n" + " \"ok\": true,\n" +
" \"result\": {\n" + " \"result\": {\n" +
@ -344,7 +344,7 @@ class TestDeserialization {
} }
@Test @Test
void TestDeserializationLogoutMethod() throws Exception { void testDeserializationLogoutMethod() throws Exception {
String updateText = "{\"ok\":true,\"result\": true}"; String updateText = "{\"ok\":true,\"result\": true}";
Boolean response = new LogOut().deserializeResponse(updateText); Boolean response = new LogOut().deserializeResponse(updateText);
@ -353,7 +353,7 @@ class TestDeserialization {
} }
@Test @Test
void TestDeserializationGetMyCommandsMethod() throws Exception { void testDeserializationGetMyCommandsMethod() throws Exception {
String updateText = "{\n" + String updateText = "{\n" +
" \"ok\": true,\n" + " \"ok\": true,\n" +
" \"result\": [\n" + " \"result\": [\n" +

View File

@ -28,7 +28,7 @@ class TestTelegramApi {
} }
@Test @Test
void TestTelegramApiMustBeInitializableForLongPolling() { void testTelegramApiMustBeInitializableForLongPolling() {
try { try {
new TelegramBotsApi(BotSession.class); new TelegramBotsApi(BotSession.class);
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
@ -37,7 +37,7 @@ class TestTelegramApi {
} }
@Test @Test
void TestTelegramApiMustBeInitializableForWebhook() { void testTelegramApiMustBeInitializableForWebhook() {
try { try {
new TelegramBotsApi(BotSession.class, webhook); new TelegramBotsApi(BotSession.class, webhook);
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
@ -46,7 +46,7 @@ class TestTelegramApi {
} }
@Test @Test
void TestTelegramApiMustBeThrowIfNotCreatedForWebhook() { void testTelegramApiMustBeThrowIfNotCreatedForWebhook() {
try { try {
TelegramBotsApi telegramBotsApi = new TelegramBotsApi(BotSession.class, null); TelegramBotsApi telegramBotsApi = new TelegramBotsApi(BotSession.class, null);
telegramBotsApi.registerBot(new WebhookBot() { telegramBotsApi.registerBot(new WebhookBot() {

View File

@ -22,7 +22,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestInlineQueryIdMustBePresent() { void testInlineQueryIdMustBePresent() {
try { try {
answerInlineQuery.validate(); answerInlineQuery.validate();
} catch (TelegramApiValidationException e) { } catch (TelegramApiValidationException e) {
@ -31,7 +31,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestInlineQueryIdCanNotBeEmpty() { void testInlineQueryIdCanNotBeEmpty() {
answerInlineQuery.setInlineQueryId(""); answerInlineQuery.setInlineQueryId("");
try { try {
answerInlineQuery.validate(); answerInlineQuery.validate();
@ -41,7 +41,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestResultsMustBePresent() { void testResultsMustBePresent() {
answerInlineQuery.setInlineQueryId("RANDOMEID"); answerInlineQuery.setInlineQueryId("RANDOMEID");
try { try {
answerInlineQuery.validate(); answerInlineQuery.validate();
@ -51,7 +51,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestSwitchPmTextCanNotBeEmpty() { void testSwitchPmTextCanNotBeEmpty() {
answerInlineQuery.setInlineQueryId("RANDOMEID"); answerInlineQuery.setInlineQueryId("RANDOMEID");
answerInlineQuery.setResults(new ArrayList<>()); answerInlineQuery.setResults(new ArrayList<>());
answerInlineQuery.setSwitchPmText(""); answerInlineQuery.setSwitchPmText("");
@ -64,7 +64,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestSwitchPmParameterIsMandatoryIfSwitchPmTextIsPresent() { void testSwitchPmParameterIsMandatoryIfSwitchPmTextIsPresent() {
answerInlineQuery.setInlineQueryId("RANDOMEID"); answerInlineQuery.setInlineQueryId("RANDOMEID");
answerInlineQuery.setResults(new ArrayList<>()); answerInlineQuery.setResults(new ArrayList<>());
answerInlineQuery.setSwitchPmText("Test Text"); answerInlineQuery.setSwitchPmText("Test Text");
@ -77,7 +77,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestSwitchPmParameterCanNotBeEmptyIfSwitchPmTextIsPresent() { void testSwitchPmParameterCanNotBeEmptyIfSwitchPmTextIsPresent() {
answerInlineQuery.setInlineQueryId("RANDOMEID"); answerInlineQuery.setInlineQueryId("RANDOMEID");
answerInlineQuery.setResults(new ArrayList<>()); answerInlineQuery.setResults(new ArrayList<>());
answerInlineQuery.setSwitchPmText("Test Text"); answerInlineQuery.setSwitchPmText("Test Text");
@ -91,7 +91,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestSwitchPmParameterContainsUpTo64Chars() { void testSwitchPmParameterContainsUpTo64Chars() {
answerInlineQuery.setInlineQueryId("RANDOMEID"); answerInlineQuery.setInlineQueryId("RANDOMEID");
answerInlineQuery.setResults(new ArrayList<>()); answerInlineQuery.setResults(new ArrayList<>());
answerInlineQuery.setSwitchPmText("Test Text"); answerInlineQuery.setSwitchPmText("Test Text");
@ -105,7 +105,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestSwitchPmParameterOnlyContainsAcceptedCharacters() { void testSwitchPmParameterOnlyContainsAcceptedCharacters() {
answerInlineQuery.setInlineQueryId("RANDOMEID"); answerInlineQuery.setInlineQueryId("RANDOMEID");
answerInlineQuery.setResults(new ArrayList<>()); answerInlineQuery.setResults(new ArrayList<>());
answerInlineQuery.setSwitchPmText("Test Text"); answerInlineQuery.setSwitchPmText("Test Text");

View File

@ -11,8 +11,7 @@ import org.telegram.telegrambots.meta.test.TelegramBotsHelper;
import java.util.ArrayList; import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/** /**
* @author Ruben Bermudez * @author Ruben Bermudez
@ -32,14 +31,14 @@ class TestGetUpdates {
} }
@Test @Test
void TestGetUpdatesMustBeSerializable() throws Exception { void testGetUpdatesMustBeSerializable() throws Exception {
String json = mapper.writeValueAsString(getUpdates); String json = mapper.writeValueAsString(getUpdates);
assertNotNull(json); assertNotNull(json);
assertEquals("{\"offset\":15,\"limit\":100,\"timeout\":50,\"method\":\"getupdates\"}", json); assertEquals("{\"offset\":15,\"limit\":100,\"timeout\":50,\"method\":\"getupdates\"}", json);
} }
@Test @Test
void TestGetUpdatesMustDeserializeCorrectResponse() throws Exception { void testGetUpdatesMustDeserializeCorrectResponse() throws Exception {
ArrayList<Update> result = ArrayList<Update> result =
getUpdates.deserializeResponse(TelegramBotsHelper.GetResponseWithoutError()); getUpdates.deserializeResponse(TelegramBotsHelper.GetResponseWithoutError());
assertNotNull(result); assertNotNull(result);
@ -47,7 +46,7 @@ class TestGetUpdates {
} }
@Test @Test
void TestGetUpdatesMustThrowAnExceptionForInCorrectResponse() { void testGetUpdatesMustThrowAnExceptionForInCorrectResponse() {
try { try {
getUpdates.deserializeResponse(TelegramBotsHelper.GetResponseWithError()); getUpdates.deserializeResponse(TelegramBotsHelper.GetResponseWithError());
} catch (TelegramApiRequestException e) { } catch (TelegramApiRequestException e) {
@ -56,4 +55,15 @@ class TestGetUpdates {
assertEquals("Error descriptions", e.getApiResponse()); assertEquals("Error descriptions", e.getApiResponse());
} }
} }
@Test
void testGetUpdatesMustThrowAnExceptionForInCorrectResponse409() {
try {
getUpdates.deserializeResponse(TelegramBotsHelper.getResponseWithError409());
} catch (TelegramApiRequestException e) {
assertNull(e.getParameters());
assertEquals(Integer.valueOf(409), e.getErrorCode());
assertEquals("Conflict: terminated by other getUpdates request; make sure that only one bot instance is running", e.getApiResponse());
}
}
} }

View File

@ -18,9 +18,4 @@
}, },
"text": "Original" "text": "Original"
} }
}
{
"ok": true,
"result": true
} }