From 6f85b661374ea0f3320dcff966fa7d76762ce742 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 11 May 2018 13:14:40 +0300 Subject: AnonymousUser.INSTANCE where possible --- .../src/main/java/com/juick/server/MessengerManager.java | 3 ++- .../src/main/java/com/juick/server/TelegramBotManager.java | 3 ++- .../src/test/java/com/juick/server/tests/ServerTests.java | 14 +++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) (limited to 'juick-server/src') diff --git a/juick-server/src/main/java/com/juick/server/MessengerManager.java b/juick-server/src/main/java/com/juick/server/MessengerManager.java index 303e08ce..8bfcafd0 100644 --- a/juick-server/src/main/java/com/juick/server/MessengerManager.java +++ b/juick-server/src/main/java/com/juick/server/MessengerManager.java @@ -15,6 +15,7 @@ import com.github.messenger4j.webhook.event.TextMessageEvent; import com.juick.Message; import com.juick.User; import com.juick.server.component.MessageEvent; +import com.juick.server.helpers.AnonymousUser; import com.juick.service.MessagesService; import com.juick.service.MessengerService; import com.juick.service.SubscriptionService; @@ -76,7 +77,7 @@ public class MessengerManager implements ApplicationListener { final String senderId = event.senderId(); final Instant timestamp = event.timestamp(); - User user_from = userService.getUserByUID(messengerService.getUserId(senderId)).orElse(new User()); + User user_from = userService.getUserByUID(messengerService.getUserId(senderId)).orElse(AnonymousUser.INSTANCE); logger.info("Found juick user {}", user_from.getUid()); if (user_from.isAnonymous()) { try { diff --git a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java index c9097cb2..6e6198a8 100644 --- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java +++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java @@ -19,6 +19,7 @@ package com.juick.server; import com.juick.User; import com.juick.server.component.*; +import com.juick.server.helpers.AnonymousUser; import com.juick.server.helpers.CommandResult; import com.juick.server.util.HttpUtils; import com.juick.service.MessagesService; @@ -113,7 +114,7 @@ public class TelegramBotManager implements NotificationListener { return; } } - User user_from = userService.getUserByUID(telegramService.getUser(message.chat().id())).orElse(new User()); + User user_from = userService.getUserByUID(telegramService.getUser(message.chat().id())).orElse(AnonymousUser.INSTANCE); logger.info("Found juick user {}", user_from.getUid()); List chats = telegramService.getChats(); diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java index d3e7b7a5..abdabf4e 100644 --- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java +++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java @@ -256,7 +256,7 @@ public class ServerTests { @Test public void messageTests() { int user_id = userService.createUser("mmmme", "secret"); - User user = userService.getUserByUID(user_id).orElse(new User()); + User user = userService.getUserByUID(user_id).orElse(AnonymousUser.INSTANCE); assertEquals("it should be me", "mmmme", user.getName()); int mid = messagesService.createMessage(user_id, "yo", null, new ArrayList<>()); Message msg = messagesService.getMessage(mid); @@ -273,7 +273,7 @@ public class ServerTests { assertEquals(1, msg2.getTags().size()); assertEquals("we already have ugnich", -1, userService.createUser("ugnich", "x")); int ugnich_id = userService.createUser("hugnich", "x"); - User ugnich = userService.getUserByUID(ugnich_id).orElse(new User()); + User ugnich = userService.getUserByUID(ugnich_id).orElse(AnonymousUser.INSTANCE); int rid = messagesService.createReply(msg2.getMid(), 0, ugnich, "bla-bla", null); assertEquals(1, rid); assertThat(msg2.getTo(), equalTo(null)); @@ -659,16 +659,16 @@ public class ServerTests { @Test public void botCommandsTests() throws Exception { URI emptyUri = URI.create(StringUtils.EMPTY); - assertThat(commandsManager.processCommand(new User(), "PING", emptyUri).getText(), is("PONG")); + assertThat(commandsManager.processCommand(AnonymousUser.INSTANCE, "PING", emptyUri).getText(), is("PONG")); // subscription commands have two lines, others have 1 - assertThat(commandsManager.processCommand(new User(), "help", emptyUri).getText().split("\n").length, is(32)); + assertThat(commandsManager.processCommand(AnonymousUser.INSTANCE, "help", emptyUri).getText().split("\n").length, is(32)); } @Test public void protocolTests() throws Exception { URI emptyUri = URI.create(StringUtils.EMPTY); int uid = userService.createUser("me", "secret"); - User user = userService.getUserByUID(uid).orElse(new User()); + User user = userService.getUserByUID(uid).orElse(AnonymousUser.INSTANCE); Tag yo = tagService.getTag("yo", true); Message msg = commandsManager.processCommand(user, "*yo yoyo", URI.create("http://static.juick.com/settings/facebook.png")).getNewMessage().get(); assertThat(msg.getAttachmentType(), is("png")); @@ -687,7 +687,7 @@ public class ServerTests { assertEquals("should be message", true, commandsManager.processCommand(user, String.format("#%d", mid), emptyUri).getText().startsWith("@me")); int readerUid = userService.createUser("dummyReader", "dummySecret"); - User readerUser = userService.getUserByUID(readerUid).orElse(new User()); + User readerUser = userService.getUserByUID(readerUid).orElse(AnonymousUser.INSTANCE); assertThat(commandsManager.processCommand(readerUser, "s", emptyUri).getText().startsWith("You are subscribed to"), is(true)); assertThat(commandsManager.processCommand(readerUser, "S", emptyUri).getText().startsWith("You are subscribed to"), is(true)); assertEquals("should be subscribed", "Subscribed", @@ -747,7 +747,7 @@ public class ServerTests { assertEquals("number of tags should match", 1, tagService.getMessageTags(mid).size()); int taggerUid = userService.createUser("dummyTagger", "dummySecret"); - User taggerUser = userService.getUserByUID(taggerUid).orElse(new User()); + User taggerUser = userService.getUserByUID(taggerUid).orElse(AnonymousUser.INSTANCE); assertEquals("should be subscribed", "Subscribed", commandsManager.processCommand(taggerUser, "S *yo", emptyUri).getText()); assertEquals("number of subscribed users should match", 2, -- cgit v1.2.3