From 3ab8d578291c829ac4a13a57d1f598707217622f Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Sun, 6 Jun 2021 21:35:36 +0200 Subject: [PATCH] test: created tests for LobbyManager and LobbyRunner --- .../server/lobbymanager/LobbyManagerTest.java | 27 ++++++++++++------- .../server/lobbymanager/LobbyRunnerTest.java | 15 +++++++++-- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManagerTest.java b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManagerTest.java index 93c839e..521ea9e 100644 --- a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManagerTest.java +++ b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManagerTest.java @@ -11,8 +11,11 @@ import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage; import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; import uulm.teamname.marvelous.gamelibrary.messages.RoleEnum; import uulm.teamname.marvelous.gamelibrary.messages.client.PlayerReadyMessage; +import uulm.teamname.marvelous.server.Server; import uulm.teamname.marvelous.server.lobby.Lobby; +import uulm.teamname.marvelous.server.netconnector.UserManager; +import java.lang.reflect.InvocationTargetException; import java.util.function.BiConsumer; import static org.mockito.Mockito.*; @@ -39,9 +42,10 @@ class LobbyManagerTest { LobbyRunner lobbyRunner; MockedStatic lobbyRunnerMockedStatic; + MockedStatic serverMockedStatic; @BeforeEach - void beforeEach() { + void beforeEach() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { onMessageCallback = mock(BiConsumer.class); onErrorCallback = mock(BiConsumer.class); @@ -75,9 +79,14 @@ class LobbyManagerTest { ParticipantType.PlayerOne, "AwesomestAwesomePlayer"); - lobbyRunner = spy(LobbyRunner.getInstance()); + var constructor = LobbyRunner.class.getDeclaredConstructor(); + constructor.setAccessible(true); + lobbyRunner = spy(constructor.newInstance()); doNothing().when(lobbyRunner).startLobby(any(LobbyConnection.class)); + serverMockedStatic = Mockito.mockStatic(Server.class); + serverMockedStatic.when(Server::getMaxLobbies).thenReturn(2); + lobbyRunnerMockedStatic = Mockito.mockStatic(LobbyRunner.class); lobbyRunnerMockedStatic.when(LobbyRunner::getInstance).thenReturn(lobbyRunner); } @@ -85,6 +94,7 @@ class LobbyManagerTest { @AfterEach void afterEach() { lobbyRunnerMockedStatic.close(); + serverMockedStatic.close(); } @Test @@ -273,13 +283,14 @@ class LobbyManagerTest { Participant spectator1 = new Participant(spectator, ParticipantType.Spectator, "spectator1"); manager.assignLobbyToConnection(player1, "playerOne", playerReady); + verify(lobbyRunner, never()).startLobby(any(LobbyConnection.class)); + manager.assignLobbyToConnection(player2, "playerTwo", playerReady); - try { - Thread.sleep(100); - } catch (InterruptedException e) { - e.printStackTrace(); - } + verify(lobbyRunner, times(1)).startLobby(any(LobbyConnection.class)); + when(lobbyRunner.isStarted(any(LobbyConnection.class))).thenReturn(true); + manager.assignLobbyToConnection(spectator, "spectator1", spectatorReady); + verify(lobbyRunner, times(1)).startLobby(any(LobbyConnection.class)); // manager.assignLobbyToConnection(spectator, "spectator2", spectatorReady); assertThat(manager.getResourceDescriptorToLobby()).hasSize(1); @@ -302,7 +313,5 @@ class LobbyManagerTest { assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID)) .isEqualTo(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player2Participant).gameID)) .isEqualTo(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(spectator1).gameID)); - - verify(lobbyRunner, times(1)).startLobby(any(LobbyConnection.class)); } } diff --git a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunnerTest.java b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunnerTest.java index 1046ec6..df7bfdd 100644 --- a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunnerTest.java +++ b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunnerTest.java @@ -8,6 +8,7 @@ import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; import uulm.teamname.marvelous.server.Server; import java.lang.reflect.InvocationTargetException; @@ -47,13 +48,14 @@ class LobbyRunnerTest { // It seems very impractical indeed to properly to check if another thread executed this, so I won't @Test - void lobbyGetsStartedTest() { + void lobbyGetsStartedTest() throws InterruptedException { lobbyRunner.startLobby(connection); + Thread.sleep(10); verify(connection).run(); } @Test - void canAddLobbyTest() { + void canAddLobbyTest() throws InterruptedException { assertThat(lobbyRunner.canAddLobby()).isTrue(); lobbyRunner.startLobby(connection); assertThat(lobbyRunner.canAddLobby()).isFalse(); @@ -66,11 +68,20 @@ class LobbyRunnerTest { @Test void lobbyIsStartedTest() { + connection = spy(new LobbyConnection("SomeGameID", null, null)); + doNothing().when(connection).run(); assertThat(lobbyRunner.isStarted(connection)).isFalse(); + lobbyRunner.startLobby(connection); assertThat(lobbyRunner.isStarted(connection)).isTrue(); + connection.addSpectator(new Participant(null, ParticipantType.Spectator, "Hi")); + connection.addSpectator(new Participant(null, ParticipantType.Spectator, "Hi2")); + connection.removeParticipant(new Participant(null, ParticipantType.Spectator, "Hi")); + assertThat(lobbyRunner.isStarted(connection)).isTrue(); + lobbyRunner.removeLobby(connection); + assertThat(lobbyRunner.isStarted(connection)).isFalse(); } }