From 930e6ab6d604963ae6a48c58fc1bd3735071d9ee Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 20 Nov 2018 00:07:06 +0300 Subject: getMessage refactoring --- .../java/com/juick/server/tests/ServerTests.java | 79 +++++++++++----------- 1 file changed, 40 insertions(+), 39 deletions(-) (limited to 'src/test/java/com') diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index 69251e10..4345d607 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -257,14 +257,14 @@ public class ServerTests { assertThat(messagesService.getMyFeed(freefd.getUid(), 0, true).get(0), equalTo(mid3)); assertThat(messagesService.getMyFeed(freefd.getUid(), mid2, true).get(0), equalTo(mid0)); assertThat(messagesService.recommendMessage(mid0, ugnich.getUid()), equalTo(MessagesService.RecommendStatus.Added)); - assertThat(messagesService.getMessage(mid0).getLikes(), equalTo(1)); + assertThat(messagesService.getMessage(mid0).orElseThrow(IllegalStateException::new).getLikes(), equalTo(1)); assertThat(messagesService.recommendMessage(mid0, ugnich.getUid()), equalTo(MessagesService.RecommendStatus.Deleted)); - assertThat(messagesService.getMessage(mid0).getLikes(), equalTo(0)); + assertThat(messagesService.getMessage(mid0).get().getLikes(), equalTo(0)); assertThat(messagesService.getAll(ugnich.getUid(), 0).get(0), equalTo(mid3)); Tag yoTag = tagService.getTag("yoyo", true); assertThat(tagService.getTag("YOYO", false), equalTo(yoTag)); int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, Collections.singletonList(yoTag)); - Message msg = messagesService.getMessage(mid); + Message msg = messagesService.getMessage(mid).get(); List subscribers = subscriptionService.getSubscribedUsers(ugnich.getUid(), msg); telegramService.createTelegramUser(12345, "freefd"); @@ -304,7 +304,7 @@ public class ServerTests { 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); + Message msg = messagesService.getMessage(mid).get(); assertEquals("yo", msg.getText()); User me = msg.getUser(); assertEquals("mmmme", me.getName()); @@ -314,7 +314,7 @@ public class ServerTests { List tagList = new ArrayList<>(); tagList.add(tag); int mid2 = messagesService.createMessage(user_id, "yo2", null, tagList); - Message msg2 = messagesService.getMessage(mid2); + Message msg2 = messagesService.getMessage(mid2).get(); assertEquals(1, msg2.getTags().size()); assertEquals("we already have ugnich", -1, userService.createUser("ugnich", "x")); int ugnich_id = userService.createUser("hugnich", "x"); @@ -334,7 +334,7 @@ public class ServerTests { assertThat(replies2.size(), equalTo(2)); assertThat(replies2.get(1), equalTo(reply2)); - Message msg3 = messagesService.getMessage(mid2); + Message msg3 = messagesService.getMessage(mid2).get(); assertEquals(2, msg3.getReplies()); assertEquals("weather", msg3.getTags().get(0).getName()); @@ -357,7 +357,7 @@ public class ServerTests { String dbTagName = jdbcTemplate.queryForObject("select name from tags where name=?", String.class, htmlTagName); assertEquals("db tags should not be escaped", dbTagName, htmlTag.getName()); int mid4 = messagesService.createMessage(user_id, "yoyoyo", null, null); - Message msg4 = messagesService.getMessage(mid4); + Message msg4 = messagesService.getMessage(mid4).get(); assertEquals("tags string should be empty", StringUtils.EMPTY, MessageUtils.getTagsString(msg4)); messagesService.deleteMessage(user_id, mid4); } @@ -373,7 +373,7 @@ public class ServerTests { messagesService.likeMessage(mid, freefdId,3); messagesService.likeMessage(mid, freefdId,1); - Message msg4 = messagesService.getMessage(mid); + Message msg4 = messagesService.getMessage(mid).get(); assertThat(msg4.getLikes(), equalTo(1)); Assert.assertEquals(2, msg4.getReactions().stream().filter(r -> r.getId() == 2) @@ -436,7 +436,7 @@ public class ServerTests { String msgText = "Привет, я - Угнич"; CommandResult result = commandsManager.processCommand(ugnich, msgText, URI.create("http://static.juick.com/settings/facebook.png")); int mid = result.getNewMessage().get().getMid(); - Message msg = messagesService.getMessage(mid); + Message msg = messagesService.getMessage(mid).get(); tagService.createTag("тест"); mockMvc.perform( get("/api/home") @@ -738,7 +738,7 @@ public class ServerTests { Message msgreply = commandsManager.processCommand(user, "#" + msg.getMid() + " yyy", HttpUtils.downloadImage(URI.create("http://static.juick.com/settings/xmpp.png").toURL(), tmpDir)).getNewMessage().get(); assertThat(msgreply.getAttachmentType(), equalTo("png")); assertEquals("text should match", "yoyo", - messagesService.getMessage(msg.getMid()).getText()); + messagesService.getMessage(msg.getMid()).get().getText()); assertEquals("tag should match", "yo", tagService.getMessageTags(msg.getMid()).get(0).getTag().getName()); CommandResult yoyoMsg = commandsManager.processCommand(user, "*yo", URI.create("http://static.juick.com/settings/facebook.png")); @@ -761,16 +761,16 @@ public class ServerTests { int rid = messagesService.createReply(mid, 0, user, "comment", null); assertEquals("number of subscribed users should match", 1, subscriptionService.getUsersSubscribedToComments( - messagesService.getMessage(mid), + messagesService.getMessage(mid).get(), messagesService.getReply(mid, rid)).size()); privacyQueriesService.blacklistUser(user, readerUser); assertEquals("number of subscribed users should match", 0, subscriptionService.getUsersSubscribedToComments( - messagesService.getMessage(mid), + messagesService.getMessage(mid).get(), messagesService.getReply(mid, rid)).size()); assertEquals("number of subscribed users should match", 1, subscriptionService.getUsersSubscribedToComments( - messagesService.getMessage(mid), + messagesService.getMessage(mid).get(), messagesService.getReply(mid, rid), true).size()); assertEquals("should be subscribed", "Subscribed to @" + user.getName(), commandsManager.processCommand(readerUser, "S @" + user.getName(), emptyUri) @@ -801,7 +801,7 @@ public class ServerTests { commandsManager.processCommand(user, "#" + mid + " *there", emptyUri).getText()); assertEquals("number of tags should match", 2, tagService.getMessageTags(mid).size()); - assertThat(messagesService.getMessage(mid).getTags().size(), is(2)); + assertThat(messagesService.getMessage(mid).get().getTags().size(), is(2)); assertEquals("should be blacklisted", "Tag added to your blacklist", commandsManager.processCommand(readerUser, "BL *there", emptyUri).getText()); assertEquals("number of subscribed users should match", 0, @@ -834,7 +834,7 @@ public class ServerTests { assertEquals("should be unsubscribed", "Unsubscribed from #" + mid, commandsManager.processCommand(readerUser, "u #" + mid, emptyUri).getText()); assertEquals("number of subscribed users should match", 0, - subscriptionService.getUsersSubscribedToComments(messagesService.getMessage(mid), + subscriptionService.getUsersSubscribedToComments(messagesService.getMessage(mid).get(), messagesService.getReply(mid, rid)).size()); assertNotEquals("should NOT be deleted", String.format("Message %s deleted", mid), commandsManager.processCommand(readerUser, "D #" + mid, emptyUri).getText()); @@ -938,7 +938,7 @@ public class ServerTests { mockMvc.perform(post("/api/react?mid=" + mid1 + "&hash=" + freefdHash+ "&reactionId=2")) .andExpect(status().isOk()); - Message msg4 = messagesService.getMessage(mid1); + Message msg4 = messagesService.getMessage(mid1).get(); assertThat(msg4.getLikes(), is(0)); assertThat(messagesService.getMessages(AnonymousUser.INSTANCE, Collections.singletonList(mid1)).get(0).getLikes(), is(0)); Assert.assertEquals(1, msg4.getReactions().stream().filter(r -> r.getId() == 2) @@ -947,15 +947,16 @@ public class ServerTests { .andExpect(status().isOk()); mockMvc.perform(post("/api/react?mid=" + mid1 + "&hash=" + freefdHash+ "&reactionId=1")) .andExpect(status().isOk()); - assertThat(messagesService.getMessage(mid1).getLikes(), is(1)); + assertThat(messagesService.getMessage(mid1).get().getLikes(), is(1)); } @Test public void lastReadTests() throws Exception { jdbcTemplate.execute("DELETE FROM bl_users"); + jdbcTemplate.execute("DELETE FROM messages"); assertThat(userService.isInBLAny(ugnich.getUid(), freefd.getUid()), is(false)); int mid = messagesService.createMessage(ugnich.getUid(), "to be watched", null, null); - subscriptionService.subscribeMessage(messagesService.getMessage(mid), ugnich); + subscriptionService.subscribeMessage(messagesService.getMessage(mid).get(), ugnich); messagesService.createReply(mid, 0, freefd, "new reply", null); BiFunction lastRead = (user, m) -> jdbcTemplate.queryForObject( "SELECT last_read_rid FROM subscr_messages WHERE suser_id=? AND message_id=?", @@ -1017,7 +1018,7 @@ public class ServerTests { assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getMyFeed(freefd.getUid(), 0, true)) .stream().noneMatch(m -> m.getTags().contains(banned))); tagService.updateTags(newMid, Collections.singletonList(banned)); - assertThat(messagesService.getMessage(newMid).getTags().size(), is(0)); + assertThat(messagesService.getMessage(newMid).get().getTags().size(), is(0)); privacyQueriesService.blacklistUser(freefd, userService.getUserByUID(newUid).orElse(AnonymousUser.INSTANCE)); assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getMyFeed(freefd.getUid(), 0, true)) .stream().noneMatch(m -> m.getMid() == newMid)); @@ -1127,17 +1128,17 @@ public class ServerTests { commandsManager.processCommand(freefd, String.format("#%d ugnich - dick too", mid), emptyUri); commandsManager.processCommand(juick, String.format("#%d/1 ban for a hour!", mid), emptyUri); commandsManager.processCommand(juick, String.format("#%d freefd is here but it is hidden from you", mid), emptyUri); - assertThat(messagesService.getMessage(mid).getReplies(), is(3)); + assertThat(messagesService.getMessage(mid).get().getReplies(), is(3)); Message reply = messagesService.getReply(mid, 3); assertThat(userService.isReplyToBL(ugnich, reply), is(false)); List replies = messagesService.getReplies(ugnich, mid); assertThat(replies.size(), is(1)); commandsManager.processCommand(freefd, String.format("#%d/3 hahaha!", mid), emptyUri); - assertThat(messagesService.getMessage(mid).getReplies(), is(4)); + assertThat(messagesService.getMessage(mid).get().getReplies(), is(4)); replies = messagesService.getReplies(ugnich, mid); assertThat(replies.size(), is(1)); commandsManager.processCommand(juick, String.format("#%d/4 mmm?!", mid), emptyUri); - assertThat(messagesService.getMessage(mid).getReplies(), is(5)); + assertThat(messagesService.getMessage(mid).get().getReplies(), is(5)); replies = messagesService.getReplies(ugnich, mid); reply = messagesService.getReply(mid, 5); assertThat(userService.isReplyToBL(ugnich, reply), is(true)); @@ -1269,23 +1270,23 @@ public class ServerTests { messagesService.recommendMessage(posterMid, recommenderId); BiFunction> subscribers = (recommId, msg) -> subscriptionService.getUsersSubscribedToUserRecommendations(recommId, msg); - List recommendSubscribers = subscribers.apply(recommenderId, messagesService.getMessage(posterMid)); + List recommendSubscribers = subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get()); assertThat(recommendSubscribers.size(), is(1)); assertThat(recommendSubscribers.get(0).getUid(), is(readerId)); privacyQueriesService.blacklistUser(reader, poster); - assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid)).size(), is(0)); + assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get()).size(), is(0)); privacyQueriesService.blacklistUser(reader, poster); - assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid)).size(), is(1)); + assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get()).size(), is(1)); tagService.blacklistTag(reader, sampleTag); - assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid)).size(), is(0)); + assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get()).size(), is(0)); tagService.blacklistTag(reader, sampleTag); - assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid)).size(), is(1)); + assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get()).size(), is(1)); messagesService.recommendMessage(posterMid, lateRecommenderId); - List lateRecommendSubscribers = subscribers.apply(recommenderId, messagesService.getMessage(posterMid)); + List lateRecommendSubscribers = subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get()); assertThat(lateRecommendSubscribers.size(), is(0)); int readerMid = messagesService.createMessage(readerId, "PEOPLE", null, null); messagesService.recommendMessage(readerMid, recommenderId); - assertThat(subscribers.apply(recommenderId, messagesService.getMessage(readerMid)).size(), is(0)); + assertThat(subscribers.apply(recommenderId, messagesService.getMessage(readerMid).get()).size(), is(0)); } @Test public void mentionsInComments() { @@ -1295,7 +1296,7 @@ public class ServerTests { int mentionerId = userService.createUser("mmm", "secret"); User mentioner = userService.getUserByUID(mentionerId).get(); int mid = messagesService.createMessage(posterId, "who is dick?", null, null); - Message msg = messagesService.getMessage(mid); + Message msg = messagesService.getMessage(mid).get(); int rid = messagesService.createReply(mid, 0, commenter, "@mmm is dick", null); Message reply = messagesService.getReply(mid, rid); @@ -1395,10 +1396,10 @@ public class ServerTests { assertThat(messagesService.recommendMessage(mid, ermineId), is(MessagesService.RecommendStatus.Added)); assertThat(messagesService.recommendMessage(mid, fmapId), is(MessagesService.RecommendStatus.Added)); assertThat(messagesService.recommendMessage(mid, pogoId), is(MessagesService.RecommendStatus.Added)); - assertThat(messagesService.getMessage(mid).getLikes(), is(3)); + assertThat(messagesService.getMessage(mid).get().getLikes(), is(3)); assertThat(CollectionUtils.isEqualCollection(messagesService.getMessageRecommendations(mid), Arrays.asList("fmap", "ermine", "pogo")), is(true)); privacyQueriesService.blacklistUser(userService.getUserByName("monstreek"), userService.getUserByName("pogo")); - assertThat(messagesService.getMessage(mid).getLikes(), is(3)); + assertThat(messagesService.getMessage(mid).get().getLikes(), is(3)); assertThat(CollectionUtils.isEqualCollection(messagesService.getMessageRecommendations(mid), Arrays.asList("fmap", "ermine")), is(true)); } @Test @@ -1526,7 +1527,7 @@ public class ServerTests { assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid), "twitter:card") .getAttribute("content"), equalTo("summary")); int mid2 = messagesService.createMessage(ugnich.getUid(), "with image", "png", null); - Message message = messagesService.getMessage(mid2); + Message message = messagesService.getMessage(mid2).get(); assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "twitter:card") .getAttribute("content"), equalTo("summary_large_image")); assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "og:description") @@ -1576,10 +1577,10 @@ public class ServerTests { String discussionsUrl = "http://localhost:8080/"; HtmlPage discussions = webClient.getPage(discussionsUrl); assertThat(discussions.querySelectorAll("article").size(), is(0)); - subscriptionService.subscribeMessage(messagesService.getMessage(mid), freefd); + subscriptionService.subscribeMessage(messagesService.getMessage(mid).get(), freefd); discussions = (HtmlPage) discussions.refresh(); assertThat(discussions.querySelectorAll("article").size(), is(1)); - subscriptionService.subscribeMessage(messagesService.getMessage(midNew), freefd); + subscriptionService.subscribeMessage(messagesService.getMessage(midNew).get(), freefd); discussions = (HtmlPage) discussions.refresh(); assertThat(discussions.querySelectorAll("article").size(), is(2)); assertThat(discussions.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); @@ -1587,13 +1588,13 @@ public class ServerTests { discussions = (HtmlPage) discussions.refresh(); assertThat(discussions.querySelectorAll("article").size(), is(2)); assertThat(discussions.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid))); - Message msg = messagesService.getMessage(mid); + Message msg = messagesService.getMessage(mid).get(); HtmlPage discussionsOld = webClient.getPage(discussionsUrl + "?to=" + msg.getUpdated().toEpochMilli()); assertThat(discussionsOld.querySelectorAll("article").size(), is(1)); assertThat(discussionsOld.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); List newMids = IntStream.rangeClosed(1, 19).map(i -> messagesService.createMessage(ugnich.getUid(), String.valueOf(i), null, null)).boxed().collect(Collectors.toList()); for (Integer m : newMids) { - subscriptionService.subscribeMessage(messagesService.getMessage(m), freefd); + subscriptionService.subscribeMessage(messagesService.getMessage(m).get(), freefd); } discussions = (HtmlPage) discussions.refresh(); assertThat(discussions.querySelectorAll("article").size(), is(20)); @@ -1603,7 +1604,7 @@ public class ServerTests { discussions = (HtmlPage) discussions.refresh(); assertThat(discussions.querySelectorAll("article") .get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); - Message old = messagesService.getMessage(newMids.get(0)); + Message old = messagesService.getMessage(newMids.get(0)).get(); discussionsOld = webClient.getPage(discussionsUrl + "?to=" + old.getUpdated().toEpochMilli()); assertThat(discussionsOld.querySelectorAll("article").size(), is(1)); assertThat(discussionsOld.querySelectorAll("article") @@ -1653,7 +1654,7 @@ public class ServerTests { loginCookie.getName(), loginCookie.getValue())); int mid = messagesService.createMessage(ugnich.getUid(), "new test", null, null); - subscriptionService.subscribeMessage(messagesService.getMessage(mid), freefd); + subscriptionService.subscribeMessage(messagesService.getMessage(mid).get(), freefd); int rid = messagesService.createReply(mid, 0, ugnich, "new reply", null); HtmlPage discussionsPage = webClient.getPage("http://localhost:8080/?show=discuss"); assertThat(discussionsPage.querySelectorAll("#global a .badge").size(), is(1)); -- cgit v1.2.3