From 7ce16fd81f66e6e9a9f85ed1f9d09eb964278045 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Sat, 5 Jun 2021 19:38:28 +0200 Subject: [PATCH] test: partially TDDed UserManager --- .../server/netconnector/UserManagerTest.java | 64 ++++++++++++++----- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/Server/src/test/java/uulm/teamname/marvelous/server/netconnector/UserManagerTest.java b/Server/src/test/java/uulm/teamname/marvelous/server/netconnector/UserManagerTest.java index b15c491..ab02016 100644 --- a/Server/src/test/java/uulm/teamname/marvelous/server/netconnector/UserManagerTest.java +++ b/Server/src/test/java/uulm/teamname/marvelous/server/netconnector/UserManagerTest.java @@ -2,28 +2,21 @@ package uulm.teamname.marvelous.server.netconnector; import org.java_websocket.WebSocket; import org.java_websocket.handshake.ClientHandshake; -import org.java_websocket.handshake.ClientHandshakeBuilder; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; import uulm.teamname.marvelous.gamelibrary.json.JSON; import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; import uulm.teamname.marvelous.gamelibrary.messages.client.*; import uulm.teamname.marvelous.server.lobbymanager.Participant; -import java.util.HashMap; -import java.util.Iterator; - import static org.mockito.Mockito.*; -import static org.junit.jupiter.api.Assertions.*; import static org.assertj.core.api.Assertions.*; class UserManagerTest { UserManager manager; WebSocket connection; + SUID clientID; Participant participant; ClientHandshake handshake; @@ -36,14 +29,16 @@ class UserManagerTest { handshake = mock(ClientHandshake.class); when(handshake.getResourceDescriptor()).thenReturn("/someDescriptor"); - participant = spy(new Participant(connection, ParticipantType.PlayerOne, false)); + clientID = new SUID("Awesome Name", "Awesome deviceID"); + + participant = spy(new Participant(connection, ParticipantType.PlayerOne, "Awesome Name")); } @Test void userIsConnectedTest() { assertThat(manager.getUserCount()).isEqualTo(0); assertThat(manager.getNewUsers()).doesNotContain(connection); - manager.connectUser(connection, handshake); + manager.connectUser(connection); assertThat(manager.getUserCount()).isEqualTo(1); assertThat(manager.isUserConnected(connection)).isTrue(); assertThat(manager.getNewUsers()).containsOnly(connection); @@ -62,7 +57,9 @@ class UserManagerTest { @Test void handshakeGetsCompletedProperlyTest() { - manager.connectUser(connection, handshake); + // manager.connectUser(connection, handshake); + + manager.getNewUsers().add(connection); assertThat(manager.getUserCount()).isEqualTo(1); assertThat(manager.getNewUsers()).containsOnly(connection); assertThat(manager.getReadyToConnect()).isEmpty(); @@ -76,9 +73,9 @@ class UserManagerTest { assertThat(manager.getUserCount()).isEqualTo(1); assertThat(manager.getNewUsers()).isEmpty(); assertThat(manager.getReadyToConnect()).containsOnlyKeys(connection); - } + @Test void reconnectMessagesGetAssignedProperly() { manager.messageReceived( connection, @@ -90,15 +87,50 @@ class UserManagerTest { } @Test - @Disabled void reconnectWorksProperly() { - // TODO: create test for this case + manager.getActiveParticipants().put(clientID, participant); + manager.getReadyToReconnect().put(connection, clientID); + + assertThat(manager.getInGame()).isEmpty(); + + var message = new ReconnectMessage(); + message.reconnect = true; + + manager.reconnectClient(connection, message); + + assertThat(manager.getReadyToReconnect()).isEmpty(); + + assertThat(manager.getInGame()).containsOnlyKeys(connection); + assertThat(manager.getInGame().get(connection)).isEqualTo(participant); + + verify(participant).setConnection(connection); } + @Test + void notReconnectTransfersPlayerToReadyToConnect() { + manager.getReadyToReconnect().put(connection, clientID); + + assertThat(manager.getReadyToReconnect()).containsOnlyKeys(connection); + assertThat(manager.getReadyToConnect()).isEmpty(); + assertThat(manager.getInGame()).isEmpty(); + + var message = new ReconnectMessage(); + message.reconnect = false; + + manager.reconnectClient(connection, message); + + assertThat(manager.getReadyToReconnect()).isEmpty(); + assertThat(manager.getReadyToConnect()).containsOnlyKeys(connection); + assertThat(manager.getReadyToConnect().get(connection)).isEqualTo(clientID); + assertThat(manager.getInGame()).isEmpty(); + + } + + @Test void playerReadyMessagesGetAssignedProperly() { manager.messageReceived( connection, - "{\"messageType\":\"PLAYER_READY\",\"startGame\":\"false\",\"role\":\"Player\"}"); + "{\"messageType\":\"PLAYER_READY\",\"startGame\":\"false\",\"role\":\"PLAYER\"}"); verify(manager).assignLobby( eq(connection), @@ -106,6 +138,7 @@ class UserManagerTest { } + @Test void characterSelectionMessageGetsAssignedProperly() { manager.messageReceived( connection, @@ -117,6 +150,7 @@ class UserManagerTest { any(CharacterSelectionMessage.class)); } + @Test void requestMessagesGetRelayedProperly() { manager.messageReceived( connection,