From 7067e1a65ebcfe916835c6515de452110774e15a Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Sat, 29 Jan 2022 17:21:54 +0100 Subject: [PATCH] Deduplicate code --- .../abilitybots/api/bot/AbilityBotI18nTest.java | 4 ++-- .../abilitybots/api/bot/AbilityBotTest.java | 2 +- .../telegram/abilitybots/api/bot/TestUtils.java | 4 ++-- .../telegrambots/updatesreceivers/RestApi.java | 15 +++++++++++---- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotI18nTest.java b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotI18nTest.java index 3175f03d..25d5181d 100644 --- a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotI18nTest.java +++ b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotI18nTest.java @@ -19,8 +19,8 @@ import static org.telegram.abilitybots.api.bot.TestUtils.mockContext; import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance; class AbilityBotI18nTest { - private static final User NO_LANGUAGE_USER = new User(1L, "first", false, "last", "username", null, false, false, false); - private static final User ITALIAN_USER = new User(2L, "first", false, "last", "username", "it-IT", false, false, false); + private static final User NO_LANGUAGE_USER = new User(1L, "first", false, "last", "username", null, false, false, false, null, null, null, null, null); + private static final User ITALIAN_USER = new User(2L, "first", false, "last", "username", "it-IT", false, false, false, null, null, null, null, null); private DBContext db; private NoPublicCommandsBot bot; diff --git a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotTest.java b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotTest.java index 17edd4de..20a53d13 100644 --- a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotTest.java +++ b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotTest.java @@ -374,7 +374,7 @@ public class AbilityBotTest { String newFirstName = USER.getFirstName() + "-test"; String newLastName = USER.getLastName() + "-test"; long sameId = USER.getId(); - User changedUser = new User(sameId, newFirstName, false, newLastName, newUsername, "en", false, false, false); + User changedUser = new User(sameId, newFirstName, false, newLastName, newUsername, "en", false, false, false, null, null, null, null, null); mockAlternateUser(update, message, changedUser); diff --git a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/TestUtils.java b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/TestUtils.java index ea36c023..6e364eed 100644 --- a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/TestUtils.java +++ b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/TestUtils.java @@ -11,8 +11,8 @@ import static org.mockito.Mockito.when; import static org.telegram.abilitybots.api.objects.MessageContext.newContext; public final class TestUtils { - public static final User USER = new User(1L, "first", false, "last", "username", null, false, false, false); - public static final User CREATOR = new User(1337L, "creatorFirst", false, "creatorLast", "creatorUsername", null, false, false, false); + public static final User USER = new User(1L, "first", false, "last", "username", null, false, false, false, null, null, null, null, null); + public static final User CREATOR = new User(1337L, "creatorFirst", false, "creatorLast", "creatorUsername", null, false, false, false, null, null, null, null, null); private TestUtils() { diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/RestApi.java b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/RestApi.java index 2c5b68b0..807a0027 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/RestApi.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/RestApi.java @@ -50,8 +50,7 @@ public class RestApi { } return Response.ok(response).build(); } catch (MalformedUpdateException e) { - log.error("Malformed update", e.getCause());; - return Response.status(Status.BAD_REQUEST).build(); + return handleMalformedUpdate(e); } catch (TooManyRequestsException e) { if (e.getProcessedRequestsCount().isPresent()) { return Response @@ -87,8 +86,7 @@ public class RestApi { } return Response.ok(responses).build(); } catch (MalformedUpdateException e) { - log.error("Malformed update", e.getCause()); - return Response.status(Status.BAD_REQUEST).build(); + return handleMalformedUpdate(e); } catch (TooManyRequestsException e) { if (e.getProcessedRequestsCount().isPresent()) { return Response @@ -107,6 +105,15 @@ public class RestApi { return Response.status(Response.Status.NOT_FOUND).build(); } + private Response handleMalformedUpdate(MalformedUpdateException e) { + log.error("Malformed update", e.getCause());; + return Response + .status(Status.BAD_REQUEST) + .entity(e.getCause() == null ? "" : e.getCause().getMessage()) + .type(MediaType.TEXT_PLAIN) + .build(); + } + @GET @Path("/{botPath}") @Produces(MediaType.APPLICATION_JSON)