From 3bbf2004183058d0476c25b5ff2a3f50c80165c9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 2 Nov 2017 17:26:31 +0300 Subject: reorganize tests --- .../com/juick/xmpp/server/XMPPServerTests.java | 123 +++++++++++++++++++-- 1 file changed, 114 insertions(+), 9 deletions(-) (limited to 'juick-xmpp/src/test/java') diff --git a/juick-xmpp/src/test/java/com/juick/xmpp/server/XMPPServerTests.java b/juick-xmpp/src/test/java/com/juick/xmpp/server/XMPPServerTests.java index 11ca75cf..dca09189 100644 --- a/juick-xmpp/src/test/java/com/juick/xmpp/server/XMPPServerTests.java +++ b/juick-xmpp/src/test/java/com/juick/xmpp/server/XMPPServerTests.java @@ -1,17 +1,21 @@ package com.juick.xmpp.server; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.juick.Message; import com.juick.User; import com.juick.components.JuickBot; import com.juick.components.XMPPServer; import com.juick.components.configuration.XmppAppConfiguration; -import com.juick.configuration.MockDataConfiguration; +import com.juick.configuration.RepositoryConfiguration; import com.juick.server.configuration.BaseWebConfiguration; -import com.juick.service.UserService; +import com.juick.service.*; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @@ -24,24 +28,32 @@ import rocks.xmpp.core.stanza.model.server.ServerMessage; import javax.inject.Inject; import java.lang.reflect.InvocationTargetException; +import java.text.ParseException; +import java.util.List; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration -@ContextConfiguration +@ContextConfiguration(classes = { + BaseWebConfiguration.class, XmppAppConfiguration.class, RepositoryConfiguration.class +}) public class XMPPServerTests { @Configuration - @Import(value = { - BaseWebConfiguration.class, XmppAppConfiguration.class, MockDataConfiguration.class - }) - static class Config { } - + @ComponentScan(basePackages = "com.juick.www.controllers") + static class Config { + @Bean + public ImagesService imagesService() { + return new MockImagesService(); + } + } @Inject private WebApplicationContext wac; @Inject @@ -50,6 +62,14 @@ public class XMPPServerTests { private JuickBot bot; @Inject private UserService userService; + @Inject + private MessagesService messagesService; + @Inject + private TagService tagService; + @Inject + private SubscriptionService subscriptionService; + @Inject + private JdbcTemplate jdbcTemplate; private MockMvc mockMvc; @@ -76,4 +96,89 @@ public class XMPPServerTests { // tag help have two lines, others have 1 assertThat(bot.processCommand(new User(), Jid.of("test@localhost"), "help").get().split("\n").length, is(17)); } + + @Test + public void protocolTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, ParseException, JsonProcessingException { + int uid = userService.createUser("me", "secret"); + User user = userService.getUserByUID(uid).orElse(new User()); + int mid = messagesService.createMessage(uid, "*yo yoyo", null, null); + assertEquals("should be message", true, + bot.processCommand(user, Jid.of("test@localhost"), String.format("#%d", mid)).get().startsWith("@me")); + mid = messagesService.getUserBlog(user.getUid(), -1, 0).stream().reduce((first, second) -> second).get(); + assertEquals("text should match", "yoyo", + messagesService.getMessage(mid).getText()); + assertEquals("tag should match", "yo", + tagService.getMessageTags(mid).get(0).getTag().getName()); + int readerUid = userService.createUser("dummyReader", "dummySecret"); + User readerUser = userService.getUserByUID(readerUid).orElse(new User()); + assertEquals("should be subscribed", "Subscribed", + bot.processCommand(readerUser, Jid.of("dummy@localhost"), "S #" + mid).get()); + /* TODO: move from juick-legacy + assertEquals("should be favorited", "Message added to your recommendations", + juickProtocol.getReply(readerUser, "! #" + mid)); + */ + assertEquals("number of subscribed users should match", 1, + subscriptionService.getUsersSubscribedToComments(mid, uid).size()); + assertEquals("should be subscribed", "Subscribed", + bot.processCommand(readerUser, Jid.of("dummy@localhost"), "S @" + user.getName()).get()); + List friends = userService.getUserFriends(readerUid); + assertEquals("number of friend users should match", 2, + friends.size()); + assertEquals("number of reader users should match", 1, + userService.getUserReaders(uid).size()); + String expectedReply = "Reply posted.\n#" + mid + "/1 " + + "http://juick.com/" + mid + "#1"; + String expectedSecondReply = "Reply posted.\n#" + mid + "/2 " + + "http://juick.com/" + mid + "#2"; + assertEquals("should be reply", expectedReply, + bot.processCommand(user, Jid.of("test@localhost"), "#" + mid + " yoyo").get()); + assertEquals("should be second reply", expectedSecondReply, + bot.processCommand(user, Jid.of("test@localhost"), "#" + mid + "/1 yoyo").get()); + Message reply = messagesService.getReplies(mid).stream().filter(m -> m.getRid() == 2).findFirst() + .orElse(new Message()); + assertEquals("should be reply to first comment", 1, reply.getReplyto()); + assertNotEquals("tags should NOT be updated", "Tags are updated", + bot.processCommand(readerUser, Jid.of("dummy@localhost"), "#" + mid + " *yo *there").get()); + assertEquals("tags should be updated", "Tags are updated", + bot.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there").get()); + assertEquals("number of tags should match", 2, + tagService.getMessageTags(mid).size()); + assertEquals("should be blacklisted", "Tag added to your blacklist", + bot.processCommand(readerUser, Jid.of("dummy@localhost"), "BL *there").get()); + assertEquals("number of subscribed users should match", 0, + subscriptionService.getSubscribedUsers(uid, mid).size()); + assertEquals("tags should be updated", "Tags are updated", + bot.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there").get()); + 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", + bot.processCommand(taggerUser, Jid.of("tagger@localhost"), "S *yo").get()); + assertEquals("number of subscribed users should match", 2, + subscriptionService.getSubscribedUsers(uid, mid).size()); + assertEquals("should be unsubscribed", "Unsubscribed from yo", + bot.processCommand(taggerUser, Jid.of("tagger@localhost"), "U *yo").get()); + 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 = bot.processCommand(readerUser, Jid.of("dummy@localhost"), "#").get(); + assertEquals("description should match", true, readerFeed.startsWith("Your feed")); + assertEquals("should be unsubscribed", "Unsubscribed from @" + user.getName(), + bot.processCommand(readerUser, Jid.of("dummy@localhost"), "U @" + user.getName()).get()); + 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, + bot.processCommand(readerUser, Jid.of("dummy@localhost"), "u #" + mid).get()); + assertEquals("number of subscribed users should match", 0, + subscriptionService.getUsersSubscribedToComments(mid, uid).size()); + assertNotEquals("should NOT be deleted", String.format("Message %s deleted", mid), + bot.processCommand(readerUser, Jid.of("dummy@localhost"), "D #" + mid).get()); + assertEquals("should be deleted", String.format("Message %s deleted", mid), + bot.processCommand(user, Jid.of("test@localhost"), "D #" + mid).get()); + assertEquals("should not have messages", 0, messagesService.getAll(user.getUid(), 0).size()); + } } -- cgit v1.2.3