test: begun to write some tests for the Server components

This commit is contained in:
Yannik Bretschneider 2021-06-08 03:16:43 +02:00
parent 5f30982cfb
commit 8daa7afac3
2 changed files with 118 additions and 10 deletions

View File

@ -23,8 +23,12 @@ import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.*;
class LobbyConnectionTest {
@BeforeEach
void setUp() {
}
/*
BiConsumer<WebSocket, BasicMessage> sendMessageCallback;
BiConsumer<WebSocket, String> 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();

View File

@ -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;