diff --git a/server/src/test/java/org/moparforia/server/ServerTest.java b/server/src/test/java/org/moparforia/server/ServerTest.java new file mode 100644 index 00000000..65dbe18e --- /dev/null +++ b/server/src/test/java/org/moparforia/server/ServerTest.java @@ -0,0 +1,117 @@ +package org.moparforia.server; + +import org.junit.jupiter.api.Test; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.Socket; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ServerTest { + + private void sendMessage(BufferedWriter writer, String message) throws IOException { + writer.write(message); + writer.newLine(); + writer.flush(); + } + + @Test + void testSinglePlayerFlow() throws IOException, InterruptedException { + Server server = new Server("127.0.0.1", 4243, Optional.empty()); + server.start(); + + Socket socket = new Socket("127.0.0.1", 4243); + InputStream in = socket.getInputStream(); + OutputStream out = socket.getOutputStream(); + InputStreamReader r = new InputStreamReader(in); + OutputStreamWriter w = new OutputStreamWriter(out); + BufferedReader reader = new BufferedReader(r); + BufferedWriter writer = new BufferedWriter(w); + + // init + String h = reader.readLine(); + String cIo = reader.readLine(); + String cCrt = reader.readLine(); + String cCtr = reader.readLine(); + assertEquals("h 1", h); + assertTrue(cIo.matches("c io \\d+")); + assertEquals("c crt 250", cCrt); + assertEquals("c ctr", cCtr); + + String nickname = "nick"; + + // client id + this.sendMessage(writer, "c new"); + String cId = reader.readLine(); + assertEquals("c id 0", cId); + + // version + this.sendMessage(writer, "d 0 version\t35"); + String statusLogin = reader.readLine(); + assertEquals("d 0 status\tlogin", statusLogin); + + // login + this.sendMessage(writer, "d 1 ttlogin\t" + nickname + "\t"); + String basicInfo = reader.readLine(); + String statusLobbyselect = reader.readLine(); + assertEquals("d 1 basicinfo\tt\t0\tt\tf", basicInfo); + assertEquals("d 2 status\tlobbyselect\t300", statusLobbyselect); + + // number of players + this.sendMessage(writer, "d 2 lobbyselect\trnop"); + String lobbySelectNop = reader.readLine(); + assertEquals("d 3 lobbyselect\tnop\t0\t0\t0", lobbySelectNop); + + // select single player + this.sendMessage(writer, "d 3 lobbyselect\tselect\t1"); + String statusLobby = reader.readLine(); + String lobbyUsers = reader.readLine(); + String lobbyOwnjoin = reader.readLine(); + assertEquals("d 4 status\tlobby\t1", statusLobby); + assertEquals("d 5 lobby\tusers", lobbyUsers); + assertEquals("d 6 lobby\townjoin\t3:" + nickname + "^w^10000^-^-^-", lobbyOwnjoin); + this.sendMessage(writer, "d 4 lobby\ttracksetlist"); + String tracksetlist = reader.readLine(); + assertEquals(tracksetlist, "d 7 lobby\ttracksetlist\tBirchwood\t1\t9\tNo one\t1\tNo one\t1\tNo one\t1\tNo one\t1\tOak Park\t1\t18\tNo one\t1\tNo one\t1\tNo one\t1\tNo one\t1\tOne by One\t2\t18\tNo one\t1\tNo one\t1\tNo one\t1\tNo one\t1\tScary Set\t3\t9\tNo one\t1\tNo one\t1\tNo one\t1\tNo one\t1\tSpruce Corpse\t2\t9\tNo one\t1\tNo one\t1\tNo one\t1\tNo one\t1\tThe First\t2\t18\tNo one\t1\tNo one\t1\tNo one\t1\tNo one\t1\tTorment Fields\t3\t18\tNo one\t1\tNo one\t1\tNo one\t1\tNo one\t1"); + + // start championship game + this.sendMessage(writer, "d 5 lobby\tcspc\t6"); + String statusGame = reader.readLine(); + String gameInfo = reader.readLine(); + String players = reader.readLine(); + String ownInfo = reader.readLine(); + String gameStart = reader.readLine(); + String resetVoteSkip = reader.readLine(); + String startTrack = reader.readLine(); + String startTurn = reader.readLine(); + assertEquals("d 8 status\tgame", statusGame); + assertEquals("d 9 game\tgameinfo\tderp\tf\t0\t1\t18\t0\t0\t0\t0\t1\t0\t0\tf", gameInfo); + assertEquals("d 10 game\tplayers", players); + assertEquals("d 11 game\towninfo\t0\t" + nickname + "\t-", ownInfo); + assertEquals("d 12 game\tstart", gameStart); + assertEquals("d 13 game\tresetvoteskip", resetVoteS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B d2b,\\d+\tR 633,173,118,129,125,546,495,461,467,556,4472")); + assertEquals("d 15 game\tstartturn\t0", startTurn); + + // leave game + this.sendMessage(writer, "d 6 game\tback"); + statusLobby = reader.readLine(); + lobbyUsers = reader.readLine(); + lobbyOwnjoin = reader.readLine(); + assertEquals("d 16 status\tlobby\t1", statusLobby); + assertEquals("d 17 lobby\tusers", lobbyUsers); + assertEquals("d 18 lobby\townjoin\t3:" + nickname + "^w^10000^-^-^-", lobbyOwnjoin); + + // quit game + this.sendMessage(writer, "d 7 lobby\tquit"); + server.stop(); + } +}