From 91f5dc539958743b6a2a110681e76cd9c3a250f3 Mon Sep 17 00:00:00 2001 From: jbion Date: Thu, 25 May 2017 03:24:39 +0200 Subject: Add a java API for SevenWonders using the simple JSON/STOMP client This API is intended to be used for integration tests, but could later be extracted and used for AI actions or machine learning. --- .../org/luxons/sevenwonders/test/ClientPlayer.java | 30 ----------- .../org/luxons/sevenwonders/test/api/ApiLobby.java | 24 +++++++++ .../luxons/sevenwonders/test/api/ApiPlayer.java | 30 +++++++++++ .../sevenwonders/test/api/SevenWondersSession.java | 61 ++++++++++++++++++++++ 4 files changed, 115 insertions(+), 30 deletions(-) delete mode 100644 backend/src/test/java/org/luxons/sevenwonders/test/ClientPlayer.java create mode 100644 backend/src/test/java/org/luxons/sevenwonders/test/api/ApiLobby.java create mode 100644 backend/src/test/java/org/luxons/sevenwonders/test/api/ApiPlayer.java create mode 100644 backend/src/test/java/org/luxons/sevenwonders/test/api/SevenWondersSession.java (limited to 'backend/src/test') diff --git a/backend/src/test/java/org/luxons/sevenwonders/test/ClientPlayer.java b/backend/src/test/java/org/luxons/sevenwonders/test/ClientPlayer.java deleted file mode 100644 index 4f5eebcf..00000000 --- a/backend/src/test/java/org/luxons/sevenwonders/test/ClientPlayer.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.luxons.sevenwonders.test; - -public class ClientPlayer { - - private String username; - - private String displayName; - - private int index; - - public String getUsername() { - return username; - } - - public String getDisplayName() { - return displayName; - } - - public void setDisplayName(String displayName) { - this.displayName = displayName; - } - - public int getIndex() { - return index; - } - - public void setIndex(int index) { - this.index = index; - } -} diff --git a/backend/src/test/java/org/luxons/sevenwonders/test/api/ApiLobby.java b/backend/src/test/java/org/luxons/sevenwonders/test/api/ApiLobby.java new file mode 100644 index 00000000..9ce9a5d7 --- /dev/null +++ b/backend/src/test/java/org/luxons/sevenwonders/test/api/ApiLobby.java @@ -0,0 +1,24 @@ +package org.luxons.sevenwonders.test.api; + +public class ApiLobby { + + private long id; + + private String name; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/backend/src/test/java/org/luxons/sevenwonders/test/api/ApiPlayer.java b/backend/src/test/java/org/luxons/sevenwonders/test/api/ApiPlayer.java new file mode 100644 index 00000000..dcf7ec03 --- /dev/null +++ b/backend/src/test/java/org/luxons/sevenwonders/test/api/ApiPlayer.java @@ -0,0 +1,30 @@ +package org.luxons.sevenwonders.test.api; + +public class ApiPlayer { + + private String username; + + private String displayName; + + private int index; + + public String getUsername() { + return username; + } + + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + public int getIndex() { + return index; + } + + public void setIndex(int index) { + this.index = index; + } +} 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 new file mode 100644 index 00000000..4f67c49f --- /dev/null +++ b/backend/src/test/java/org/luxons/sevenwonders/test/api/SevenWondersSession.java @@ -0,0 +1,61 @@ +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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class SevenWondersSession { + + private final JsonStompSession session; + + public SevenWondersSession(JsonStompSession session) { + this.session = session; + } + + public void disconnect() { + session.disconnect(); + } + + public ApiPlayer chooseName(String displayName) throws Exception { + ChooseNameAction action = new ChooseNameAction(); + action.setPlayerName(displayName); + + ApiPlayer player = session.request("/app/chooseName", action, "/user/queue/nameChoice", ApiPlayer.class); + assertNotNull(player); + assertEquals(displayName, player.getDisplayName()); + + return player; + } + + public ApiLobby createGame(String gameName) throws InterruptedException { + CreateGameAction action = new CreateGameAction(); + action.setGameName(gameName); + + ApiLobby lobby = session.request("/app/lobby/create", action, "/user/queue/lobby/joined", ApiLobby.class); + assertNotNull(lobby); + assertEquals(gameName, lobby.getName()); + return lobby; + } + + public ApiLobby joinGame(long gameId) throws InterruptedException { + JoinGameAction action = new JoinGameAction(); + action.setGameId(gameId); + + ApiLobby lobby = session.request("/app/lobby/join", action, "/user/queue/lobby/joined", ApiLobby.class); + assertNotNull(lobby); + assertEquals(gameId, lobby.getId()); + return lobby; + } + + public void startGame(long gameId) throws InterruptedException { + String sendDestination = "/app/lobby/startGame"; + String receiveDestination = "/topic/lobby/" + gameId + "/started"; + boolean received = session.request(sendDestination, null, receiveDestination); + assertTrue(received); + } +} -- cgit