test: partially TDDed UserManager

This commit is contained in:
Yannik Bretschneider 2021-06-05 19:38:28 +02:00
parent 15eb18e79a
commit 7ce16fd81f

View File

@ -2,28 +2,21 @@ package uulm.teamname.marvelous.server.netconnector;
import org.java_websocket.WebSocket; import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.ClientHandshakeBuilder;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; 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.json.JSON;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.messages.client.*; import uulm.teamname.marvelous.gamelibrary.messages.client.*;
import uulm.teamname.marvelous.server.lobbymanager.Participant; import uulm.teamname.marvelous.server.lobbymanager.Participant;
import java.util.HashMap;
import java.util.Iterator;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
class UserManagerTest { class UserManagerTest {
UserManager manager; UserManager manager;
WebSocket connection; WebSocket connection;
SUID clientID;
Participant participant; Participant participant;
ClientHandshake handshake; ClientHandshake handshake;
@ -36,14 +29,16 @@ class UserManagerTest {
handshake = mock(ClientHandshake.class); handshake = mock(ClientHandshake.class);
when(handshake.getResourceDescriptor()).thenReturn("/someDescriptor"); 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 @Test
void userIsConnectedTest() { void userIsConnectedTest() {
assertThat(manager.getUserCount()).isEqualTo(0); assertThat(manager.getUserCount()).isEqualTo(0);
assertThat(manager.getNewUsers()).doesNotContain(connection); assertThat(manager.getNewUsers()).doesNotContain(connection);
manager.connectUser(connection, handshake); manager.connectUser(connection);
assertThat(manager.getUserCount()).isEqualTo(1); assertThat(manager.getUserCount()).isEqualTo(1);
assertThat(manager.isUserConnected(connection)).isTrue(); assertThat(manager.isUserConnected(connection)).isTrue();
assertThat(manager.getNewUsers()).containsOnly(connection); assertThat(manager.getNewUsers()).containsOnly(connection);
@ -62,7 +57,9 @@ class UserManagerTest {
@Test @Test
void handshakeGetsCompletedProperlyTest() { void handshakeGetsCompletedProperlyTest() {
manager.connectUser(connection, handshake); // manager.connectUser(connection, handshake);
manager.getNewUsers().add(connection);
assertThat(manager.getUserCount()).isEqualTo(1); assertThat(manager.getUserCount()).isEqualTo(1);
assertThat(manager.getNewUsers()).containsOnly(connection); assertThat(manager.getNewUsers()).containsOnly(connection);
assertThat(manager.getReadyToConnect()).isEmpty(); assertThat(manager.getReadyToConnect()).isEmpty();
@ -76,9 +73,9 @@ class UserManagerTest {
assertThat(manager.getUserCount()).isEqualTo(1); assertThat(manager.getUserCount()).isEqualTo(1);
assertThat(manager.getNewUsers()).isEmpty(); assertThat(manager.getNewUsers()).isEmpty();
assertThat(manager.getReadyToConnect()).containsOnlyKeys(connection); assertThat(manager.getReadyToConnect()).containsOnlyKeys(connection);
} }
@Test
void reconnectMessagesGetAssignedProperly() { void reconnectMessagesGetAssignedProperly() {
manager.messageReceived( manager.messageReceived(
connection, connection,
@ -90,15 +87,50 @@ class UserManagerTest {
} }
@Test @Test
@Disabled
void reconnectWorksProperly() { 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() { void playerReadyMessagesGetAssignedProperly() {
manager.messageReceived( manager.messageReceived(
connection, connection,
"{\"messageType\":\"PLAYER_READY\",\"startGame\":\"false\",\"role\":\"Player\"}"); "{\"messageType\":\"PLAYER_READY\",\"startGame\":\"false\",\"role\":\"PLAYER\"}");
verify(manager).assignLobby( verify(manager).assignLobby(
eq(connection), eq(connection),
@ -106,6 +138,7 @@ class UserManagerTest {
} }
@Test
void characterSelectionMessageGetsAssignedProperly() { void characterSelectionMessageGetsAssignedProperly() {
manager.messageReceived( manager.messageReceived(
connection, connection,
@ -117,6 +150,7 @@ class UserManagerTest {
any(CharacterSelectionMessage.class)); any(CharacterSelectionMessage.class));
} }
@Test
void requestMessagesGetRelayedProperly() { void requestMessagesGetRelayedProperly() {
manager.messageReceived( manager.messageReceived(
connection, connection,