From 6a80c19f3578e0ecfc9bfe97a3d3f5a3580470e7 Mon Sep 17 00:00:00 2001 From: jbion Date: Thu, 25 May 2017 06:03:13 +0200 Subject: Re-order params and rename to Jackstomp --- .../org/luxons/sevenwonders/SevenWondersTest.java | 10 +-- .../sevenwonders/test/api/SevenWondersSession.java | 12 +-- .../sevenwonders/test/client/JackstompClient.java | 68 ++++++++++++++++ .../sevenwonders/test/client/JackstompSession.java | 93 ++++++++++++++++++++++ .../sevenwonders/test/client/JsonStompClient.java | 68 ---------------- .../sevenwonders/test/client/JsonStompSession.java | 93 ---------------------- 6 files changed, 172 insertions(+), 172 deletions(-) create mode 100644 backend/src/test/java/org/luxons/sevenwonders/test/client/JackstompClient.java create mode 100644 backend/src/test/java/org/luxons/sevenwonders/test/client/JackstompSession.java delete mode 100644 backend/src/test/java/org/luxons/sevenwonders/test/client/JsonStompClient.java delete mode 100644 backend/src/test/java/org/luxons/sevenwonders/test/client/JsonStompSession.java (limited to 'backend/src') diff --git a/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java b/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java index bd120989..78d85ca4 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java @@ -6,8 +6,8 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.luxons.sevenwonders.test.api.ApiLobby; import org.luxons.sevenwonders.test.api.SevenWondersSession; -import org.luxons.sevenwonders.test.client.JsonStompClient; -import org.luxons.sevenwonders.test.client.JsonStompSession; +import org.luxons.sevenwonders.test.client.JackstompClient; +import org.luxons.sevenwonders.test.client.JackstompSession; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -19,18 +19,18 @@ public class SevenWondersTest { private static final String WEBSOCKET_URI = "ws://localhost:%d/seven-wonders-websocket"; - private static JsonStompClient client; + private static JackstompClient client; @LocalServerPort private int randomServerPort; @BeforeClass public static void setUp() { - client = new JsonStompClient(); + client = new JackstompClient(); } private SevenWondersSession connectNewUser() throws Exception { - JsonStompSession session = client.connect(String.format(WEBSOCKET_URI, randomServerPort)); + JackstompSession session = client.connect(String.format(WEBSOCKET_URI, randomServerPort)); return new SevenWondersSession(session); } diff --git a/backend/src/test/java/org/luxons/sevenwonders/test/api/SevenWondersSession.java b/backend/src/test/java/org/luxons/sevenwonders/test/api/SevenWondersSession.java index 4f67c49f..ad8e41ba 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/test/api/SevenWondersSession.java +++ b/backend/src/test/java/org/luxons/sevenwonders/test/api/SevenWondersSession.java @@ -3,7 +3,7 @@ package org.luxons.sevenwonders.test.api; import org.luxons.sevenwonders.actions.ChooseNameAction; import org.luxons.sevenwonders.actions.CreateGameAction; import org.luxons.sevenwonders.actions.JoinGameAction; -import org.luxons.sevenwonders.test.client.JsonStompSession; +import org.luxons.sevenwonders.test.client.JackstompSession; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -11,9 +11,9 @@ import static org.junit.Assert.assertTrue; public class SevenWondersSession { - private final JsonStompSession session; + private final JackstompSession session; - public SevenWondersSession(JsonStompSession session) { + public SevenWondersSession(JackstompSession session) { this.session = session; } @@ -25,7 +25,7 @@ public class SevenWondersSession { ChooseNameAction action = new ChooseNameAction(); action.setPlayerName(displayName); - ApiPlayer player = session.request("/app/chooseName", action, "/user/queue/nameChoice", ApiPlayer.class); + ApiPlayer player = session.request(action, ApiPlayer.class, "/app/chooseName", "/user/queue/nameChoice"); assertNotNull(player); assertEquals(displayName, player.getDisplayName()); @@ -36,7 +36,7 @@ public class SevenWondersSession { CreateGameAction action = new CreateGameAction(); action.setGameName(gameName); - ApiLobby lobby = session.request("/app/lobby/create", action, "/user/queue/lobby/joined", ApiLobby.class); + ApiLobby lobby = session.request(action, ApiLobby.class, "/app/lobby/create", "/user/queue/lobby/joined"); assertNotNull(lobby); assertEquals(gameName, lobby.getName()); return lobby; @@ -46,7 +46,7 @@ public class SevenWondersSession { JoinGameAction action = new JoinGameAction(); action.setGameId(gameId); - ApiLobby lobby = session.request("/app/lobby/join", action, "/user/queue/lobby/joined", ApiLobby.class); + ApiLobby lobby = session.request(action, ApiLobby.class, "/app/lobby/join", "/user/queue/lobby/joined"); assertNotNull(lobby); assertEquals(gameId, lobby.getId()); return lobby; diff --git a/backend/src/test/java/org/luxons/sevenwonders/test/client/JackstompClient.java b/backend/src/test/java/org/luxons/sevenwonders/test/client/JackstompClient.java new file mode 100644 index 00000000..3388e002 --- /dev/null +++ b/backend/src/test/java/org/luxons/sevenwonders/test/client/JackstompClient.java @@ -0,0 +1,68 @@ +package org.luxons.sevenwonders.test.client; + +import java.util.Collections; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.springframework.messaging.converter.MappingJackson2MessageConverter; +import org.springframework.messaging.simp.stomp.StompSession; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.web.socket.client.WebSocketClient; +import org.springframework.web.socket.client.standard.StandardWebSocketClient; +import org.springframework.web.socket.messaging.WebSocketStompClient; +import org.springframework.web.socket.sockjs.client.SockJsClient; +import org.springframework.web.socket.sockjs.client.Transport; +import org.springframework.web.socket.sockjs.client.WebSocketTransport; + +public class JackstompClient { + + private static final long DEFAULT_CONNECTION_TIMEOUT_IN_SECONDS = 15; + + private final WebSocketStompClient client; + + public JackstompClient() { + this(createDefaultStompClient()); + } + + public JackstompClient(WebSocketStompClient client) { + this.client = client; + } + + private static WebSocketStompClient createDefaultStompClient() { + WebSocketStompClient stompClient = new WebSocketStompClient(createWebSocketClient()); + stompClient.setMessageConverter(new MappingJackson2MessageConverter()); // for custom object exchanges + stompClient.setTaskScheduler(createTaskScheduler()); // for heartbeats + return stompClient; + } + + private static WebSocketClient createWebSocketClient() { + return new SockJsClient(createWsTransports()); + } + + private static List createWsTransports() { + return Collections.singletonList(new WebSocketTransport(new StandardWebSocketClient())); + } + + private static ThreadPoolTaskScheduler createTaskScheduler() { + ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); + taskScheduler.afterPropertiesSet(); + return taskScheduler; + } + + public JackstompSession connect(String url) throws InterruptedException, ExecutionException, TimeoutException { + return connect(url, DEFAULT_CONNECTION_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS); + } + + public JackstompSession connect(String url, long timeout, TimeUnit unit) + throws InterruptedException, ExecutionException, TimeoutException { + StompSession session = client.connect(url, new LoggingStompSessionHandler()).get(timeout, unit); + session.setAutoReceipt(true); + return new JackstompSession(session); + } + + public void stop() { + client.stop(); + } +} diff --git a/backend/src/test/java/org/luxons/sevenwonders/test/client/JackstompSession.java b/backend/src/test/java/org/luxons/sevenwonders/test/client/JackstompSession.java new file mode 100644 index 00000000..6ab7fb7d --- /dev/null +++ b/backend/src/test/java/org/luxons/sevenwonders/test/client/JackstompSession.java @@ -0,0 +1,93 @@ +package org.luxons.sevenwonders.test.client; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingDeque; + +import org.springframework.messaging.simp.stomp.StompFrameHandler; +import org.springframework.messaging.simp.stomp.StompHeaders; +import org.springframework.messaging.simp.stomp.StompSession; + +public class JackstompSession implements StompSession { + + private final StompSession stompSession; + + public JackstompSession(StompSession stompSession) { + this.stompSession = stompSession; + } + + @Override + public Receiptable send(String destination, Object payload) { + return stompSession.send(destination, payload); + } + + @Override + public Receiptable send(StompHeaders headers, Object payload) { + return stompSession.send(headers, payload); + } + + @Override + public Subscription subscribe(String destination, StompFrameHandler handler) { + return stompSession.subscribe(destination, handler); + } + + @Override + public Subscription subscribe(StompHeaders headers, StompFrameHandler handler) { + return stompSession.subscribe(headers, handler); + } + + public Channel subscribe(String destination, Class payloadType) { + BlockingQueue blockingQueue = new LinkedBlockingDeque<>(); + StompFrameHandler frameHandler = new QueuedStompFrameHandler<>(blockingQueue, payloadType); + Subscription sub = stompSession.subscribe(destination, frameHandler); + return new Channel<>(sub, blockingQueue); + } + + public Channel subscribeEmptyMsgs(String destination) { + BlockingQueue blockingQueue = new LinkedBlockingDeque<>(); + StompFrameHandler frameHandler = new EmptyMsgStompFrameHandler(blockingQueue); + Subscription sub = stompSession.subscribe(destination, frameHandler); + return new Channel<>(sub, blockingQueue); + } + @Override + public Receiptable acknowledge(String messageId, boolean consumed) { + return stompSession.acknowledge(messageId, consumed); + } + + @Override + public String getSessionId() { + return stompSession.getSessionId(); + } + + @Override + public boolean isConnected() { + return stompSession.isConnected(); + } + + @Override + public void setAutoReceipt(boolean enabled) { + stompSession.setAutoReceipt(enabled); + } + + @Override + public void disconnect() { + stompSession.disconnect(); + } + + public T request(Object payload, Class responseType, String requestDestination, String responseDestination) + throws InterruptedException { + Channel channel = subscribe(responseDestination, responseType); + send(requestDestination, payload); + T msg = channel.next(); + channel.unsubscribe(); + return msg; + } + + public boolean request(String requestDestination, Object payload, String responseDestination) + throws InterruptedException { + Channel channel = subscribeEmptyMsgs(responseDestination); + send(requestDestination, payload); + Object msg = channel.next(); + channel.unsubscribe(); + return msg != null; + } +} diff --git a/backend/src/test/java/org/luxons/sevenwonders/test/client/JsonStompClient.java b/backend/src/test/java/org/luxons/sevenwonders/test/client/JsonStompClient.java deleted file mode 100644 index 1e9a5c3e..00000000 --- a/backend/src/test/java/org/luxons/sevenwonders/test/client/JsonStompClient.java +++ /dev/null @@ -1,68 +0,0 @@ -package org.luxons.sevenwonders.test.client; - -import java.util.Collections; -import java.util.List; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.springframework.messaging.converter.MappingJackson2MessageConverter; -import org.springframework.messaging.simp.stomp.StompSession; -import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; -import org.springframework.web.socket.client.WebSocketClient; -import org.springframework.web.socket.client.standard.StandardWebSocketClient; -import org.springframework.web.socket.messaging.WebSocketStompClient; -import org.springframework.web.socket.sockjs.client.SockJsClient; -import org.springframework.web.socket.sockjs.client.Transport; -import org.springframework.web.socket.sockjs.client.WebSocketTransport; - -public class JsonStompClient { - - private static final long DEFAULT_CONNECTION_TIMEOUT_IN_SECONDS = 15; - - private final WebSocketStompClient client; - - public JsonStompClient() { - this(createDefaultStompClient()); - } - - public JsonStompClient(WebSocketStompClient client) { - this.client = client; - } - - private static WebSocketStompClient createDefaultStompClient() { - WebSocketStompClient stompClient = new WebSocketStompClient(createWebSocketClient()); - stompClient.setMessageConverter(new MappingJackson2MessageConverter()); // for custom object exchanges - stompClient.setTaskScheduler(createTaskScheduler()); // for heartbeats - return stompClient; - } - - private static WebSocketClient createWebSocketClient() { - return new SockJsClient(createWsTransports()); - } - - private static List createWsTransports() { - return Collections.singletonList(new WebSocketTransport(new StandardWebSocketClient())); - } - - private static ThreadPoolTaskScheduler createTaskScheduler() { - ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); - taskScheduler.afterPropertiesSet(); - return taskScheduler; - } - - public JsonStompSession connect(String url) throws InterruptedException, ExecutionException, TimeoutException { - return connect(url, DEFAULT_CONNECTION_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS); - } - - public JsonStompSession connect(String url, long timeout, TimeUnit unit) - throws InterruptedException, ExecutionException, TimeoutException { - StompSession session = client.connect(url, new LoggingStompSessionHandler()).get(timeout, unit); - session.setAutoReceipt(true); - return new JsonStompSession(session); - } - - public void stop() { - client.stop(); - } -} diff --git a/backend/src/test/java/org/luxons/sevenwonders/test/client/JsonStompSession.java b/backend/src/test/java/org/luxons/sevenwonders/test/client/JsonStompSession.java deleted file mode 100644 index d2ded856..00000000 --- a/backend/src/test/java/org/luxons/sevenwonders/test/client/JsonStompSession.java +++ /dev/null @@ -1,93 +0,0 @@ -package org.luxons.sevenwonders.test.client; - -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingDeque; - -import org.springframework.messaging.simp.stomp.StompFrameHandler; -import org.springframework.messaging.simp.stomp.StompHeaders; -import org.springframework.messaging.simp.stomp.StompSession; - -public class JsonStompSession implements StompSession { - - private final StompSession stompSession; - - public JsonStompSession(StompSession stompSession) { - this.stompSession = stompSession; - } - - @Override - public Receiptable send(String destination, Object payload) { - return stompSession.send(destination, payload); - } - - @Override - public Receiptable send(StompHeaders headers, Object payload) { - return stompSession.send(headers, payload); - } - - @Override - public Subscription subscribe(String destination, StompFrameHandler handler) { - return stompSession.subscribe(destination, handler); - } - - @Override - public Subscription subscribe(StompHeaders headers, StompFrameHandler handler) { - return stompSession.subscribe(headers, handler); - } - - public Channel subscribe(String destination, Class payloadType) { - BlockingQueue blockingQueue = new LinkedBlockingDeque<>(); - StompFrameHandler frameHandler = new QueuedStompFrameHandler<>(blockingQueue, payloadType); - Subscription sub = stompSession.subscribe(destination, frameHandler); - return new Channel<>(sub, blockingQueue); - } - - public Channel subscribeEmptyMsgs(String destination) { - BlockingQueue blockingQueue = new LinkedBlockingDeque<>(); - StompFrameHandler frameHandler = new EmptyMsgStompFrameHandler(blockingQueue); - Subscription sub = stompSession.subscribe(destination, frameHandler); - return new Channel<>(sub, blockingQueue); - } - @Override - public Receiptable acknowledge(String messageId, boolean consumed) { - return stompSession.acknowledge(messageId, consumed); - } - - @Override - public String getSessionId() { - return stompSession.getSessionId(); - } - - @Override - public boolean isConnected() { - return stompSession.isConnected(); - } - - @Override - public void setAutoReceipt(boolean enabled) { - stompSession.setAutoReceipt(enabled); - } - - @Override - public void disconnect() { - stompSession.disconnect(); - } - - public T request(String requestDestination, Object payload, String responseDestination, Class responseType) - throws InterruptedException { - Channel channel = subscribe(responseDestination, responseType); - send(requestDestination, payload); - T msg = channel.next(); - channel.unsubscribe(); - return msg; - } - - public boolean request(String requestDestination, Object payload, String responseDestination) - throws InterruptedException { - Channel channel = subscribeEmptyMsgs(responseDestination); - send(requestDestination, payload); - Object msg = channel.next(); - channel.unsubscribe(); - return msg != null; - } -} -- cgit