aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/test/java/com/juick
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-03-21 11:33:25 +0300
committerGravatar Vitaly Takmazov2018-03-21 11:33:25 +0300
commit7509917609578ef535311f5df6e1492a6357851b (patch)
treece0ec6904cea2c045eb2c3185e511d582028b534 /juick-server/src/test/java/com/juick
parent5d139bd9d7b3f65453eef29ef9806c9738891f90 (diff)
server: websocket unit testing
Diffstat (limited to 'juick-server/src/test/java/com/juick')
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/ServerTests.java28
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/WebsocketClientConfig.java52
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/WebsocketClientHandler.java18
3 files changed, 98 insertions, 0 deletions
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 f831141bc..fcc1a7773 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
@@ -30,12 +30,23 @@ import com.juick.server.XMPPServer;
import com.juick.server.helpers.TagStats;
import com.juick.service.*;
import com.juick.util.DateFormattersHolder;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
+import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
+import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.TestPropertySource;
@@ -44,11 +55,16 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.socket.WebSocketSession;
+import org.springframework.web.socket.client.WebSocketConnectionManager;
+import org.springframework.web.socket.client.standard.StandardWebSocketClient;
+import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.stanza.model.Stanza;
import rocks.xmpp.core.stanza.model.server.ServerMessage;
+import sun.java2d.SurfaceDataProxy;
import javax.inject.Inject;
import java.lang.reflect.InvocationTargetException;
@@ -57,6 +73,8 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -493,4 +511,14 @@ public class ServerTests extends AbstractJUnit4SpringContextTests {
mockMvc.perform(post("/mail").with(httpBasic(juickName, juickPassword)).content(mail))
.andExpect(status().isOk());
}
+ @Test
+ public void websocketsTest() throws Exception {
+ ConfigurableApplicationContext context = new SpringApplicationBuilder(
+ WebsocketClientConfig.class, PropertyPlaceholderAutoConfiguration.class)
+ .run("--spring.main.web_environment=false");
+ long count = context.getBean(WebsocketClientConfig.class).getLatch().getCount();
+ context.close();
+ Assert.assertThat(count, equalTo(0L));
+ mockMvc.perform(get("/ws/_all")).andExpect(status().isBadRequest());
+ }
}
diff --git a/juick-server/src/test/java/com/juick/server/tests/WebsocketClientConfig.java b/juick-server/src/test/java/com/juick/server/tests/WebsocketClientConfig.java
new file mode 100644
index 000000000..6be8e3c64
--- /dev/null
+++ b/juick-server/src/test/java/com/juick/server/tests/WebsocketClientConfig.java
@@ -0,0 +1,52 @@
+package com.juick.server.tests;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
+import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.socket.client.WebSocketConnectionManager;
+import org.springframework.web.socket.client.standard.StandardWebSocketClient;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+@Configuration
+public class WebsocketClientConfig implements CommandLineRunner {
+ private static Logger logger = LoggerFactory.getLogger(ServerTests.class);
+ @Bean
+ public WebsocketClientHandler handler() {
+ return new WebsocketClientHandler(latch);
+ }
+ @Bean
+ ServletWebServerFactory servletWebServerFactory(){
+ return new TomcatServletWebServerFactory();
+ }
+ @Bean
+ public WebSocketConnectionManager connectionManager() {
+ WebSocketConnectionManager manager = new WebSocketConnectionManager(client(),
+ handler(),
+ "ws://localhost:8080/ws/");
+ manager.setAutoStartup(true);
+ return manager;
+ }
+
+ @Bean
+ public StandardWebSocketClient client() {
+ return new StandardWebSocketClient();
+ }
+ private final CountDownLatch latch = new CountDownLatch(1);
+
+ @Override
+ public void run(String... args) throws Exception {
+ if (latch.await(10, TimeUnit.SECONDS)) {
+ logger.info("client connected");
+ }
+ }
+
+ public CountDownLatch getLatch() {
+ return latch;
+ }
+}
diff --git a/juick-server/src/test/java/com/juick/server/tests/WebsocketClientHandler.java b/juick-server/src/test/java/com/juick/server/tests/WebsocketClientHandler.java
new file mode 100644
index 000000000..7d896a04c
--- /dev/null
+++ b/juick-server/src/test/java/com/juick/server/tests/WebsocketClientHandler.java
@@ -0,0 +1,18 @@
+package com.juick.server.tests;
+
+import org.springframework.web.socket.WebSocketSession;
+import org.springframework.web.socket.handler.TextWebSocketHandler;
+
+import java.util.concurrent.CountDownLatch;
+
+class WebsocketClientHandler extends TextWebSocketHandler {
+ private final CountDownLatch latch;
+ public WebsocketClientHandler(CountDownLatch latch) {
+ this.latch = latch;
+ }
+ @Override
+ public void afterConnectionEstablished(WebSocketSession session) throws Exception {
+ super.afterConnectionEstablished(session);
+ latch.countDown();
+ }
+}