diff options
-rw-r--r-- | juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java | 7 | ||||
-rw-r--r-- | juick-server/src/test/java/com/juick/server/tests/ServerTests.java | 42 |
2 files changed, 26 insertions, 23 deletions
diff --git a/juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java b/juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java index 26dafa69..c94e70f3 100644 --- a/juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java +++ b/juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java @@ -40,6 +40,7 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; import rocks.xmpp.addr.Jid; +import javax.annotation.Nonnull; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.lang.reflect.InvocationTargetException; @@ -84,7 +85,7 @@ public class CommandsManager { } - public Optional<CommandResult> processCommand(User user, Jid from, String input, URI attachment) throws InvocationTargetException, + public Optional<CommandResult> processCommand(User user, Jid from, String input, @Nonnull URI attachment) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { Optional<Method> cmd = MethodUtils.getMethodsListWithAnnotation(getClass(), UserCommand.class).stream() .filter(m -> Pattern.compile(m.getAnnotation(UserCommand.class).pattern(), @@ -452,7 +453,7 @@ public class CommandsManager { } @UserCommand(pattern = "^(#|\\.)(\\d+)((\\.|\\-|\\/)(\\d+))?\\s([\\s\\S]+)", help = "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message") - public CommandResult EditOrReply(User user, Jid from, URI attachment, String... args) throws Exception { + public CommandResult EditOrReply(User user, Jid from, @Nonnull URI attachment, String... args) throws Exception { int mid = NumberUtils.toInt(args[1]); int rid = NumberUtils.toInt(args[4], 0); String txt = args[5]; @@ -464,7 +465,7 @@ public class CommandsManager { tagService.updateTags(mid, messageTags); return CommandResult.fromString("Tags are updated"); } else { - String attachmentType = attachment != null && StringUtils.isNotEmpty(attachment.toString()) ? attachment.toString().substring(attachment.toString().length() - 3) : null; + String attachmentType = StringUtils.isNotEmpty(attachment.toString()) ? attachment.toString().substring(attachment.toString().length() - 3) : null; int newrid = messagesService.createReply(mid, rid, user.getUid(), txt, attachmentType); if (StringUtils.isNotEmpty(attachmentType)) { String attachmentFName = attachment.getScheme().equals("juick") ? attachment.getHost() 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 2930d61b..4dba2523 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 @@ -540,13 +540,15 @@ public class ServerTests { } @Test public void botCommandsTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { - assertThat(commandsManager.processCommand(new User(), Jid.of("test@localhost"), "PING", null).get().getText(), is("PONG")); + URI emptyUri = URI.create(StringUtils.EMPTY); + assertThat(commandsManager.processCommand(new User(), Jid.of("test@localhost"), "PING", emptyUri).get().getText(), is("PONG")); // subscription commands have two lines, others have 1 - assertThat(commandsManager.processCommand(new User(), Jid.of("test@localhost"), "help", null).get().getText().split("\n").length, is(32)); + assertThat(commandsManager.processCommand(new User(), Jid.of("test@localhost"), "help", emptyUri).get().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()); Tag yo = tagService.getTag("yo", true); @@ -556,7 +558,7 @@ public class ServerTests { assertThat(msgreply.getAttachmentType(), equalTo("png")); int mid = messagesService.createMessage(uid, "yoyo", null, Collections.singletonList(yo)); assertEquals("should be message", true, - commandsManager.processCommand(user, Jid.of("test@localhost"), String.format("#%d", mid), null).get().getText().startsWith("@me")); + commandsManager.processCommand(user, Jid.of("test@localhost"), String.format("#%d", mid), emptyUri).get().getText().startsWith("@me")); assertEquals("text should match", "yoyo", messagesService.getMessage(mid).getText()); assertEquals("tag should match", "yo", @@ -565,9 +567,9 @@ public class ServerTests { User readerUser = userService.getUserByUID(readerUid).orElse(new User()); Jid dummyJid = Jid.of("dummy@localhost"); assertEquals("should be subscribed", "Subscribed", - commandsManager.processCommand(readerUser, dummyJid, "S #" + mid, null).get().getText()); + commandsManager.processCommand(readerUser, dummyJid, "S #" + mid, emptyUri).get().getText()); assertEquals("should be favorited", "Message is added to your recommendations", - commandsManager.processCommand(readerUser, dummyJid, "! #" + mid, null).get().getText()); + commandsManager.processCommand(readerUser, dummyJid, "! #" + mid, emptyUri).get().getText()); int rid = messagesService.createReply(mid, 0, uid, "comment", null); assertEquals("number of subscribed users should match", 1, subscriptionService.getUsersSubscribedToComments( @@ -579,7 +581,7 @@ public class ServerTests { messagesService.getMessage(mid), messagesService.getReply(mid, rid)).size()); assertEquals("should be subscribed", "Subscribed to @" + user.getName(), - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "S @" + user.getName(), null).get().getText()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "S @" + user.getName(), emptyUri).get().getText()); List<User> friends = userService.getUserFriends(readerUid); assertEquals("number of friend users should match", 2, friends.size()); @@ -590,57 +592,57 @@ public class ServerTests { String expectedThirdReply = "Reply posted.\n#" + mid + "/3 " + "https://juick.com/" + mid + "#3"; assertEquals("should be second reply", expectedSecondReply, - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " yoyo", null).get().getText()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " yoyo", emptyUri).get().getText()); assertEquals("should be third reply", expectedThirdReply, - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + "/2 yoyo", null).get().getText()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + "/2 yoyo", emptyUri).get().getText()); Message reply = messagesService.getReplies(mid).stream().filter(m -> m.getRid() == 3).findFirst() .orElse(new Message()); assertEquals("should be reply to second comment", 2, reply.getReplyto()); assertEquals("tags should NOT be updated", "It is not your message", - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "#" + mid + " *yo *there", null).get().getText()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "#" + mid + " *yo *there", emptyUri).get().getText()); assertEquals("tags should be updated", "Tags are updated", - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there", null).get().getText()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there", emptyUri).get().getText()); assertEquals("number of tags should match", 2, tagService.getMessageTags(mid).size()); assertEquals("should be blacklisted", "Tag added to your blacklist", - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "BL *there", null).get().getText()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "BL *there", emptyUri).get().getText()); assertEquals("number of subscribed users should match", 0, subscriptionService.getSubscribedUsers(uid, mid).size()); assertEquals("tags should be updated", "Tags are updated", - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there", null).get().getText()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there", emptyUri).get().getText()); 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()); assertEquals("should be subscribed", "Subscribed", - commandsManager.processCommand(taggerUser, Jid.of("tagger@localhost"), "S *yo", null).get().getText()); + commandsManager.processCommand(taggerUser, Jid.of("tagger@localhost"), "S *yo", emptyUri).get().getText()); assertEquals("number of subscribed users should match", 2, subscriptionService.getSubscribedUsers(uid, mid).size()); assertEquals("should be unsubscribed", "Unsubscribed from yo", - commandsManager.processCommand(taggerUser, Jid.of("tagger@localhost"), "U *yo", null).get().getText()); + commandsManager.processCommand(taggerUser, Jid.of("tagger@localhost"), "U *yo", emptyUri).get().getText()); assertEquals("number of subscribed users should match", 1, subscriptionService.getSubscribedUsers(uid, mid).size()); assertEquals("number of readers should match", 1, userService.getUserReaders(uid).size()); - String readerFeed = commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "#", null).get().getText(); + String readerFeed = commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "#", emptyUri).get().getText(); assertEquals("description should match", true, readerFeed.startsWith("Your feed")); assertEquals("should be unsubscribed", "Unsubscribed from @" + user.getName(), - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "U @" + user.getName(), null).get().getText()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "U @" + user.getName(), emptyUri).get().getText()); assertEquals("number of readers should match", 0, userService.getUserReaders(uid).size()); assertEquals("number of friends should match", 1, userService.getUserFriends(uid).size()); assertEquals("should be unsubscribed", "Unsubscribed from #" + mid, - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "u #" + mid, null).get().getText()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "u #" + mid, emptyUri).get().getText()); assertEquals("number of subscribed users should match", 0, subscriptionService.getUsersSubscribedToComments(messagesService.getMessage(mid), messagesService.getReply(mid, rid)).size()); assertNotEquals("should NOT be deleted", String.format("Message %s deleted", mid), - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "D #" + mid, null).get().getText()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "D #" + mid, emptyUri).get().getText()); assertEquals("should be deleted", "Message deleted", - commandsManager.processCommand(user, Jid.of("test@localhost"), "D #" + mid, null).get().getText()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "D #" + mid, emptyUri).get().getText()); assertEquals("should be not found", "Message not found", - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid, null).get().getText()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid, emptyUri).get().getText()); } @Test public void mailParserTest() throws Exception { |