From 085ec083c71ba227fd977431f828001c53b2cc07 Mon Sep 17 00:00:00 2001 From: jbion Date: Thu, 25 May 2017 06:45:03 +0200 Subject: Add SevenWondersClient for ease of use in tests --- .../org/luxons/sevenwonders/SevenWondersTest.java | 34 ++++++++++++---------- .../sevenwonders/test/api/SevenWondersClient.java | 32 ++++++++++++++++++++ 2 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 backend/src/test/java/org/luxons/sevenwonders/test/api/SevenWondersClient.java (limited to 'backend/src/test') diff --git a/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java b/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java index 78d85ca4..e2e2f84f 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/SevenWondersTest.java @@ -1,13 +1,13 @@ package org.luxons.sevenwonders; -import org.junit.AfterClass; +import org.junit.After; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.luxons.sevenwonders.test.api.ApiLobby; +import org.luxons.sevenwonders.test.api.SevenWondersClient; import org.luxons.sevenwonders.test.api.SevenWondersSession; -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; @@ -17,21 +17,23 @@ import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class SevenWondersTest { - private static final String WEBSOCKET_URI = "ws://localhost:%d/seven-wonders-websocket"; + private static final String WEBSOCKET_URI = "ws://localhost:%d"; - private static JackstompClient client; + private static SevenWondersClient client; @LocalServerPort private int randomServerPort; + private String serverUrl; + @BeforeClass - public static void setUp() { - client = new JackstompClient(); + public static void createClient() { + client = new SevenWondersClient(); } - private SevenWondersSession connectNewUser() throws Exception { - JackstompSession session = client.connect(String.format(WEBSOCKET_URI, randomServerPort)); - return new SevenWondersSession(session); + @Before + public void setServerUrl() { + serverUrl = String.format(WEBSOCKET_URI, randomServerPort); } private void disconnect(SevenWondersSession... sessions) { @@ -42,22 +44,22 @@ public class SevenWondersTest { @Test public void testConnection() throws Exception { - SevenWondersSession session = connectNewUser(); + SevenWondersSession session = client.connect(serverUrl); session.chooseName("Test User"); session.disconnect(); } @Test public void testConnection_2users() throws Exception { - SevenWondersSession session1 = connectNewUser(); - SevenWondersSession session2 = connectNewUser(); + SevenWondersSession session1 = client.connect(serverUrl); + SevenWondersSession session2 = client.connect(serverUrl); session1.chooseName("Player1"); session2.chooseName( "Player2"); ApiLobby lobby = session1.createGame("Test Game"); session2.joinGame(lobby.getId()); - SevenWondersSession session3 = connectNewUser(); + SevenWondersSession session3 = client.connect(serverUrl); session3.chooseName("Player3"); session3.joinGame(lobby.getId()); @@ -66,8 +68,8 @@ public class SevenWondersTest { disconnect(session1, session2, session3); } - @AfterClass - public static void tearDown() { + @After + public void tearDown() { client.stop(); } } diff --git a/backend/src/test/java/org/luxons/sevenwonders/test/api/SevenWondersClient.java b/backend/src/test/java/org/luxons/sevenwonders/test/api/SevenWondersClient.java new file mode 100644 index 00000000..cf7f46c7 --- /dev/null +++ b/backend/src/test/java/org/luxons/sevenwonders/test/api/SevenWondersClient.java @@ -0,0 +1,32 @@ +package org.luxons.sevenwonders.test.api; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; + +import org.luxons.sevenwonders.test.client.JackstompClient; +import org.luxons.sevenwonders.test.client.JackstompSession; + +public class SevenWondersClient { + + private static final String WEBSOCKET_ENDPOINT = "/seven-wonders-websocket"; + + private final JackstompClient client; + + public SevenWondersClient() { + this(new JackstompClient()); + } + + public SevenWondersClient(JackstompClient client) { + this.client = client; + } + + public SevenWondersSession connect(String serverUrl) + throws InterruptedException, ExecutionException, TimeoutException { + JackstompSession session = client.connect(serverUrl + WEBSOCKET_ENDPOINT); + return new SevenWondersSession(session); + } + + public void stop() { + client.stop(); + } +} -- cgit