diff --git a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnectionTest.java b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnectionTest.java index cca745a..0ffad7f 100644 --- a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnectionTest.java +++ b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnectionTest.java @@ -23,8 +23,12 @@ import static org.junit.jupiter.api.Assertions.*; import static org.assertj.core.api.Assertions.*; class LobbyConnectionTest { + @BeforeEach + void setUp() { + + } + - /* BiConsumer sendMessageCallback; BiConsumer sendErrorCallback; @@ -39,17 +43,19 @@ class LobbyConnectionTest { sendMessageCallback = mock(BiConsumer.class); sendErrorCallback = mock(BiConsumer.class); - player1 = mock(WebSocket.class); - player2 = mock(WebSocket.class); - spectator = mock(WebSocket.class); - - player1Participant = new Participant(player1, ParticipantType.PlayerOne, "Player1"); - player2Participant = new Participant(player2, ParticipantType.PlayerTwo, "Player2"); - spectatorParticipant = new Participant(spectator, ParticipantType.Spectator, "Spectator"); - - connection = new LobbyConnection("/AwesomeGame", sendMessageCallback, sendErrorCallback); +// player1 = mock(WebSocket.class); +// player2 = mock(WebSocket.class); +// spectator = mock(WebSocket.class); +// +// player1Participant = new Participant(player1, ParticipantType.PlayerOne, "Player1"); +// player2Participant = new Participant(player2, ParticipantType.PlayerTwo, "Player2"); +// spectatorParticipant = new Participant(spectator, ParticipantType.Spectator, "Spectator"); +// +// connection = new LobbyConnection("/AwesomeGame", sendMessageCallback, sendErrorCallback); } + /* + @Test void lobbyIsFullTest() { assertThat(connection.isFull()).isFalse(); 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 0e71a3a..50e8ae1 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 @@ -15,6 +15,108 @@ import static org.assertj.core.api.Assertions.*; class UserManagerTest { + UserManager manager; + + Client client1, client2; + WebSocket socket1, socket2; + SUID suid1, suid2; + + @BeforeEach + void beforeEach() + throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + socket1 = mock(WebSocket.class); + client1 = spy(new Client(socket1)); + suid1 = new SUID("name1", "devID1"); + when(client1.getId()).thenReturn(suid1); + when(client1.getSocket()).thenReturn(socket1); + + socket2 = mock(WebSocket.class); + client2 = spy(new Client(socket2)); + suid2 = new SUID("name2", "devID2"); + when(client2.getId()).thenReturn(suid2); + when(client2.getSocket()).thenReturn(socket2); + + var c = UserManager.class.getDeclaredConstructor(); + c.setAccessible(true); + manager = spy(c.newInstance()); + } + + @Test + void userIsConnectedTest() { + assertThat(manager.getUserCount()).isEqualTo(0); + assertThat(manager.containsConnection(socket1)).isFalse(); + manager.connectUser(socket1); + assertThat(manager.getUserCount()).isEqualTo(1); + assertThat(manager.containsConnection(socket1)).isTrue(); + + verify(socket1, never()).send(any(String.class)); + } + + @Test + void helloServerMessagesGetAssignedProperly() { + manager.messageReceived( + socket1, + "{\"messageType\":\"HELLO_SERVER\",\"name\":\"SomeAwesomeName\",\"deviceID\":\"YAY\"}"); + + // TODO: test this + } + +// @Test +// void handshakeGetsCompletedProperlyTest() { +// // manager.connectUser(connection, handshake); +// +// manager.getNewUsers().add(connection); +// // assertThat(manager.getUserCount()).isEqualTo(1); +// assertThat(manager.getNewUsers()).containsOnly(connection); +// assertThat(manager.getReadyToConnect()).isEmpty(); +// +// var message = new HelloServerMessage(); +// message.name = "Some Awesome Name"; +// message.deviceID = "Some Interesting ID"; +// +// manager.handshake(connection, message); +// +// assertThat(manager.getUserCount()).isEqualTo(1); +// assertThat(manager.getNewUsers()).isEmpty(); +// assertThat(manager.getReadyToConnect()).containsOnlyKeys(connection); +// } +// +// @Test +// void reconnectMessagesGetAssignedProperly() { +// manager.messageReceived( +// connection, +// "{\"messageType\":\"RECONNECT\",\"reconnect\":\"TRUE\"}"); +// +// verify(manager).reconnectClient( +// eq(connection), +// any(ReconnectMessage.class)); +// } +// +// @Test +// void reconnectWorksProperly() { +// 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); +// } + + + + + + /* UserManager manager; WebSocket connection;