From 472a1e70ce3211ed58259e4bde928d68990e2427 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Sun, 6 Jun 2021 17:46:25 +0200 Subject: [PATCH] test: created tests for LobbyConnection / Manager / Runner --- .../lobbymanager/LobbyConnectionTest.java | 63 ++++++++++++++ .../server/lobbymanager/LobbyManagerTest.java | 82 +++++++++++++++---- .../server/lobbymanager/LobbyRunnerTest.java | 28 +++++++ 3 files changed, 156 insertions(+), 17 deletions(-) create mode 100644 Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnectionTest.java create mode 100644 Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunnerTest.java 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 new file mode 100644 index 0000000..ed4a2c6 --- /dev/null +++ b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnectionTest.java @@ -0,0 +1,63 @@ +package uulm.teamname.marvelous.server.lobbymanager; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage; + +import org.java_websocket.WebSocket; +import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; + +import java.util.function.BiConsumer; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.*; + +class LobbyConnectionTest { + + BiConsumer sendMessageCallback; + BiConsumer sendErrorCallback; + + WebSocket player1, player2, spectator; + + Participant player1Participant, player2Participant, spectatorParticipant; + + LobbyConnection connection; + + @BeforeEach + void beforeEach() { + 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); + } + + @Test + void lobbyIsFullTest() { + assertThat(connection.isFull()).isFalse(); + connection.addPlayer1(player1Participant); + assertThat(connection.isFull()).isFalse(); + connection.addPlayer2(player2Participant); + assertThat(connection.isFull()).isTrue(); + } + + @Test + void lobbyIsFullTestTwo() { + assertThat(connection.isFull()).isFalse(); + connection.addPlayer2(player2Participant); + assertThat(connection.isFull()).isFalse(); + connection.addSpectator(spectatorParticipant); + assertThat(connection.isFull()).isFalse(); + connection.addPlayer1(player1Participant); + assertThat(connection.isFull()).isTrue(); + } + +} 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 c644b38..8bd8046 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 @@ -4,23 +4,21 @@ import org.java_websocket.WebSocket; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.mockito.MockedStatic; -import org.mockito.Mockito; -import uulm.teamname.marvelous.gamelibrary.config.FieldType; -import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig; +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 java.util.stream.Collectors; +import java.util.function.BiConsumer; import static org.mockito.Mockito.*; -import static org.junit.jupiter.api.Assertions.*; import static org.assertj.core.api.Assertions.*; class LobbyManagerTest { + BiConsumer onMessageCallback; + BiConsumer onErrorCallback; + LobbyManager manager; WebSocket player1; WebSocket player2; @@ -37,7 +35,11 @@ class LobbyManagerTest { @BeforeEach void beforeEach() { - manager = new LobbyManager(); + + onMessageCallback = mock(BiConsumer.class); + onErrorCallback = mock(BiConsumer.class); + + manager = new LobbyManager(onMessageCallback, onErrorCallback); player1 = mock(WebSocket.class); player2 = mock(WebSocket.class); @@ -82,7 +84,7 @@ class LobbyManagerTest { when(player1.getResourceDescriptor()).thenReturn("/ResourcesFTW"); - manager.assignLobbyToParticipant(player1, "AwesomePlayer", message); + manager.assignLobbyToConnection(player1, "AwesomePlayer", message); Participant player1Participant = new Participant(player1, ParticipantType.PlayerOne, "AwesomePlayer"); @@ -105,8 +107,8 @@ class LobbyManagerTest { when(player1.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor"); when(player2.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor"); - manager.assignLobbyToParticipant(player1, "AwesomePlayer", playerReady); - manager.assignLobbyToParticipant(player2, "MoreAwesomePlayer", playerReady); + manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady); + manager.assignLobbyToConnection(player2, "MoreAwesomePlayer", playerReady); Participant player1Participant = new Participant( player1, ParticipantType.PlayerOne, "AwesomePlayer"); @@ -126,12 +128,58 @@ class LobbyManagerTest { .isNotNull(); } + + @Test + @DisplayName("when three participants with same ResourceDescriptor connect, the third gets a new, random lobby") + void threeParticipantsRandomLobbyTest() { + + + + when(player1.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor"); + when(player2.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor"); + when(player3.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor"); + + manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady); + manager.assignLobbyToConnection(player2, "MoreAwesomePlayer", playerReady); + manager.assignLobbyToConnection(player3, "AwesomestAwesomePlayer", playerReady); + + assertThat(manager.getLobbies()).containsOnlyKeys( + player1Participant, + player2Participant, + player3Participant); + assertThat(manager.getLobbies().get(player1Participant)) + .isEqualTo(manager.getLobbies().get(player2Participant)) + .isNotEqualTo(manager.getLobbies().get(player3Participant)) + .isNotNull(); + + assertThat(manager.getResourceDescriptorToLobby()).containsKey("/fancyResourceDescriptor"); + assertThat(manager.getResourceDescriptorToLobby().get("/fancyResourceDescriptor")) + .isEqualTo(manager.getLobbies().get(player1Participant)) + .isEqualTo(manager.getLobbies().get(player2Participant)) + .isNotNull(); + + assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID)) + .isNotNull() + .isEqualTo(manager.getLobbies().get(player1Participant)) + .isEqualTo(manager.getLobbies().get(player2Participant)) + .isNotEqualTo(manager.getLobbies().get(player3Participant)); + + System.out.println(manager.getLobbies().get(player3Participant)); + System.out.println(manager.getResourceDescriptorToLobby()); + + assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player3Participant).gameID)) + .isNotNull() + .isNotEqualTo(manager.getLobbies().get(player1Participant)) + .isNotEqualTo(manager.getLobbies().get(player2Participant)) + .isEqualTo(manager.getLobbies().get(player3Participant)); + } + @Test @DisplayName("On Participant without ResourceDescriptor random lobby gets generated") void randomLobbyTest() { when(player1.getResourceDescriptor()).thenReturn(null); - manager.assignLobbyToParticipant(player1, "AwesomePlayer", playerReady); + manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady); assertThat(manager.getLobbies()).hasSize(1); assertThat(manager.getLobbies().get(player1Participant)).isNotNull(); @@ -148,8 +196,8 @@ class LobbyManagerTest { void randomLobbyTwoPlayerTest() { when(player1.getResourceDescriptor()).thenReturn(null); when(player2.getResourceDescriptor()).thenReturn(null); - manager.assignLobbyToParticipant(player1, "AwesomePlayer", playerReady); - manager.assignLobbyToParticipant(player2, "MoreAwesomePlayer", playerReady); + manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady); + manager.assignLobbyToConnection(player2, "MoreAwesomePlayer", playerReady); assertThat(manager.getLobbies()).hasSize(2); assertThat(manager.getLobbies().get(player1Participant)) @@ -169,9 +217,9 @@ class LobbyManagerTest { when(player1.getResourceDescriptor()).thenReturn(null); when(player2.getResourceDescriptor()).thenReturn(null); when(player3.getResourceDescriptor()).thenReturn(null); - manager.assignLobbyToParticipant(player1, "AwesomePlayer", playerReady); - manager.assignLobbyToParticipant(player2, "MoreAwesomePlayer", playerReady); - manager.assignLobbyToParticipant(player3, "AwesomestAwesomePlayer", playerReady); + manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady); + manager.assignLobbyToConnection(player2, "MoreAwesomePlayer", playerReady); + manager.assignLobbyToConnection(player3, "AwesomestAwesomePlayer", playerReady); assertThat(manager.getLobbies()).hasSize(3); assertThat(manager.getLobbies().get(player1Participant)) 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 new file mode 100644 index 0000000..af51587 --- /dev/null +++ b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunnerTest.java @@ -0,0 +1,28 @@ +package uulm.teamname.marvelous.server.lobbymanager; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.*; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; + +class LobbyRunnerTest { + + LobbyConnection connection; + + @BeforeEach + void setUp() { + connection = mock(LobbyConnection.class); + + } + + @Test + void lobbyGetsStartedTest() { + + + } + +}