From 2de65567068db12c83bf6225ac0e971705f25d7b Mon Sep 17 00:00:00 2001 From: jbion Date: Thu, 25 May 2017 03:31:48 +0200 Subject: Rework integration tests with new client and API --- .../org/luxons/sevenwonders/SevenWondersTest.java | 66 +++++++++-------- .../org/luxons/sevenwonders/test/TestUtils.java | 86 ---------------------- 2 files changed, 36 insertions(+), 116 deletions(-) (limited to 'backend/src/test/java/org') diff --git a/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java b/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java index f1e68d78..bd120989 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java @@ -1,27 +1,17 @@ package org.luxons.sevenwonders; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingDeque; - import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.luxons.sevenwonders.actions.ChooseNameAction; -import org.luxons.sevenwonders.test.ClientPlayer; -import org.luxons.sevenwonders.test.TestStompFrameHandler; -import org.luxons.sevenwonders.test.TestUtils; +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.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.messaging.simp.stomp.StompSession; -import org.springframework.messaging.simp.stomp.StompSession.Subscription; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.web.socket.messaging.WebSocketStompClient; - -import static java.util.concurrent.TimeUnit.SECONDS; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @@ -29,39 +19,55 @@ public class SevenWondersTest { private static final String WEBSOCKET_URI = "ws://localhost:%d/seven-wonders-websocket"; - private static WebSocketStompClient stompClient; + private static JsonStompClient client; @LocalServerPort private int randomServerPort; @BeforeClass public static void setUp() { - stompClient = TestUtils.createStompClient(); + client = new JsonStompClient(); + } + + private SevenWondersSession connectNewUser() throws Exception { + JsonStompSession session = client.connect(String.format(WEBSOCKET_URI, randomServerPort)); + return new SevenWondersSession(session); + } + + private void disconnect(SevenWondersSession... sessions) { + for (SevenWondersSession session : sessions) { + session.disconnect(); + } } @Test public void testConnection() throws Exception { - StompSession session = TestUtils.connect(stompClient, String.format(WEBSOCKET_URI, randomServerPort)); - BlockingQueue blockingQueue = new LinkedBlockingDeque<>(); - Subscription sub = session.subscribe("/user/queue/nameChoice", - new TestStompFrameHandler<>(blockingQueue, ClientPlayer.class)); + SevenWondersSession session = connectNewUser(); + session.chooseName("Test User"); + session.disconnect(); + } - String testName = "Test User"; - ChooseNameAction chooseNameAction = new ChooseNameAction(); - chooseNameAction.setPlayerName(testName); + @Test + public void testConnection_2users() throws Exception { + SevenWondersSession session1 = connectNewUser(); + SevenWondersSession session2 = connectNewUser(); + session1.chooseName("Player1"); + session2.chooseName( "Player2"); - session.send("/app/chooseName", chooseNameAction); + ApiLobby lobby = session1.createGame("Test Game"); + session2.joinGame(lobby.getId()); - ClientPlayer player = blockingQueue.poll(2, SECONDS); - assertNotNull(player); - assertEquals(testName, player.getDisplayName()); + SevenWondersSession session3 = connectNewUser(); + session3.chooseName("Player3"); + session3.joinGame(lobby.getId()); - sub.unsubscribe(); - session.disconnect(); + session1.startGame(lobby.getId()); + + disconnect(session1, session2, session3); } @AfterClass public static void tearDown() { - stompClient.stop(); + client.stop(); } } diff --git a/backend/src/test/java/org/luxons/sevenwonders/test/TestUtils.java b/backend/src/test/java/org/luxons/sevenwonders/test/TestUtils.java index bc66b338..b29f970e 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/test/TestUtils.java +++ b/backend/src/test/java/org/luxons/sevenwonders/test/TestUtils.java @@ -1,40 +1,11 @@ package org.luxons.sevenwonders.test; -import java.lang.reflect.Type; -import java.security.Principal; -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.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; -import org.springframework.messaging.converter.MappingJackson2MessageConverter; import org.springframework.messaging.simp.SimpMessagingTemplate; -import org.springframework.messaging.simp.stomp.StompCommand; -import org.springframework.messaging.simp.stomp.StompHeaders; -import org.springframework.messaging.simp.stomp.StompSession; -import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter; -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 TestUtils { - private static final Logger logger = LoggerFactory.getLogger(TestUtils.class); - - public static Principal createPrincipal(String username) { - // the Principal interface just contains a getName() method - return () -> username; - } - public static SimpMessagingTemplate createSimpMessagingTemplate() { MessageChannel messageChannel = new MessageChannel() { @Override @@ -49,61 +20,4 @@ public class TestUtils { }; return new SimpMessagingTemplate(messageChannel); } - - public static WebSocketStompClient createStompClient() { - 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 static StompSession connect(WebSocketStompClient stompClient, String url) throws InterruptedException, - ExecutionException, TimeoutException { - StompSession session = stompClient.connect(url, new TestStompSessionHandler()).get(5, TimeUnit.SECONDS); - session.setAutoReceipt(true); - return session; - } - - private static class TestStompSessionHandler extends StompSessionHandlerAdapter { - - @Override - public void afterConnected(StompSession session, StompHeaders connectedHeaders) { - logger.info("Connected under session id " + session.getSessionId()); - } - - @Override - public void handleFrame(StompHeaders headers, Object payload) { - logger.debug("Frame received"); - } - - @Override - public Type getPayloadType(StompHeaders headers) { - return String.class; - } - - @Override - public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, - Throwable exception) { - logger.error("Exception thrown", exception); - } - - @Override - public void handleTransportError(StompSession session, Throwable exception) { - logger.error("Transport exception thrown", exception); - } - } } -- cgit