From 8d536ddcb0f56ffd38d296b42ff962f7408b88f7 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Tue, 8 Jun 2021 02:47:36 +0200 Subject: [PATCH] test: completed test for Client --- .../server/netconnector/ClientTest.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Server/src/test/java/uulm/teamname/marvelous/server/netconnector/ClientTest.java diff --git a/Server/src/test/java/uulm/teamname/marvelous/server/netconnector/ClientTest.java b/Server/src/test/java/uulm/teamname/marvelous/server/netconnector/ClientTest.java new file mode 100644 index 0000000..ec556a7 --- /dev/null +++ b/Server/src/test/java/uulm/teamname/marvelous/server/netconnector/ClientTest.java @@ -0,0 +1,53 @@ +package uulm.teamname.marvelous.server.netconnector; + +import org.java_websocket.WebSocket; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import uulm.teamname.marvelous.gamelibrary.json.JSON; +import uulm.teamname.marvelous.gamelibrary.messages.ErrorMessage; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.*; + +class ClientTest { + + Client client; + WebSocket socket; + SUID suid; + + @BeforeEach + void setUp() { + socket = mock(WebSocket.class); + suid = new SUID("ClientName", "DeviceID"); + + client = new Client(socket); + } + + @Test + void clientGetsCreatedEmpty() { + assertThat(client.getState()).isEqualTo(ClientState.Blank); + assertThat(client.getId()).isNull(); + assertThat(client.getSocket()).isEqualTo(socket); + } + + @Test + void sendError() { + client.sendError("SomeMessage"); + + verify(socket).send("{\"messageType\":\"ERROR\",\"message\":\"SomeMessage\",\"type\":0}"); + } + + @Test + void sendMessage() { + var stringRepresentingErrorMessage = "{\"messageType\":\"ERROR\",\"message\":\"SomeMessage\",\"type\":0}"; + + var errorMessage = new ErrorMessage(); + errorMessage.message = "SomeMessage"; + errorMessage.type = 0; + + client.sendMessage(errorMessage); + + verify(socket).send(stringRepresentingErrorMessage); + } +}