From d1cbf30bf1de274ca33d51e52603c9422b7874d1 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 10 May 2018 14:01:49 +0300 Subject: www: test message read event listener --- juick-www/src/test/java/com/juick/WebAppTests.java | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'juick-www/src/test/java/com/juick') diff --git a/juick-www/src/test/java/com/juick/WebAppTests.java b/juick-www/src/test/java/com/juick/WebAppTests.java index 9fb214ea..b625f7eb 100644 --- a/juick-www/src/test/java/com/juick/WebAppTests.java +++ b/juick-www/src/test/java/com/juick/WebAppTests.java @@ -23,6 +23,7 @@ import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.css.StyleElement; import com.gargoylesoftware.htmlunit.html.DomElement; import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.juick.server.component.MessageReadEvent; import com.juick.service.*; import com.juick.util.MessageUtils; import com.juick.www.WebApp; @@ -38,6 +39,10 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationListener; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; import org.springframework.core.io.ClassPathResource; import org.springframework.http.MediaType; import org.springframework.jdbc.core.JdbcTemplate; @@ -48,6 +53,7 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.util.FileSystemUtils; +import javax.annotation.Nonnull; import javax.inject.Inject; import javax.servlet.http.Cookie; import java.io.FileInputStream; @@ -56,6 +62,7 @@ import java.io.StringWriter; import java.io.Writer; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -74,6 +81,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @AutoConfigureMockMvc @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = { Application.class}) @TestPropertySource(properties = {"ios_app_id=12345678.com.juick.ExampleApp"}) +@Import(WebAppTests.EventListenerConfig.class) public class WebAppTests { @MockBean private ImagesService imagesService; @@ -95,6 +103,10 @@ public class WebAppTests { private JdbcTemplate jdbcTemplate; @Inject private SubscriptionService subscriptionService; + @Inject + private MessageReadListener listener; + @Inject + private ApplicationEventPublisher applicationEventPublisher; @Inject private PebbleEngine pebbleEngine; @@ -392,6 +404,7 @@ public class WebAppTests { } @Test public void notificationsTests() throws Exception { + listener.clear(); MvcResult loginResult = mockMvc.perform(post("/login") .param("username", freefdName) .param("password", freefdPassword)).andReturn(); @@ -408,5 +421,38 @@ public class WebAppTests { assertThat(discussionsPage.querySelectorAll("#global a .badge").size(), is(1)); HtmlPage unreadThread = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); assertThat(unreadThread.querySelectorAll("#global a .badge").size(), is(0)); + assertThat(listener.getUnreadCounts().size(), is(1)); + assertThat(listener.getUnreadCounts().get(0), is(0)); + messagesService.createReply(mid, 0, ugnich, "new reply", null); + assertThat(messagesService.getUnread(freefd).size(), is(1)); + unreadThread.refresh(); + assertThat(listener.getUnreadCounts().size(), is(2)); + assertThat(listener.getUnreadCounts().get(1), is(0)); + } + + public static class EventListenerConfig { + @Bean + public MessageReadListener listener() { + return new MessageReadListener(); + } + } + + private static class MessageReadListener implements ApplicationListener { + @Inject + private MessagesService messagesService; + private List unreadCounts = new ArrayList<>(); + @Override + public void onApplicationEvent(@Nonnull MessageReadEvent event) { + if (event.getUser().equals(freefd)) { + unreadCounts.add(messagesService.getUnread(event.getUser()).size()); + } + } + + public List getUnreadCounts() { + return unreadCounts; + } + public void clear() { + unreadCounts.clear(); + } } } -- cgit v1.2.3