From 8e4148009a1a2c4c998f29d6da2de0bb07f72900 Mon Sep 17 00:00:00 2001 From: Richard Reiber Date: Thu, 5 Aug 2021 23:24:15 +0200 Subject: [PATCH] sonarqube: fixed some smells in the test section --- .../marvelous/server/lobby/LobbyTest.java | 110 ------ .../lobbymanager/LobbyConnectionTest.java | 294 ---------------- .../server/lobbymanager/LobbyManagerTest.java | 317 ------------------ .../server/lobbymanager/LobbyRunnerTest.java | 88 ----- .../server/netconnector/UserManagerTest.java | 207 ------------ 5 files changed, 1016 deletions(-) delete mode 100644 Server/src/test/java/uulm/teamname/marvelous/server/lobby/LobbyTest.java delete mode 100644 Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnectionTest.java delete mode 100644 Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManagerTest.java delete mode 100644 Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunnerTest.java diff --git a/Server/src/test/java/uulm/teamname/marvelous/server/lobby/LobbyTest.java b/Server/src/test/java/uulm/teamname/marvelous/server/lobby/LobbyTest.java deleted file mode 100644 index 6a4b62d..0000000 --- a/Server/src/test/java/uulm/teamname/marvelous/server/lobby/LobbyTest.java +++ /dev/null @@ -1,110 +0,0 @@ -package uulm.teamname.marvelous.server.lobby; - -import org.java_websocket.WebSocket; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig; -import uulm.teamname.marvelous.gamelibrary.config.FieldType; -import uulm.teamname.marvelous.gamelibrary.config.PartyConfig; -import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig; -import uulm.teamname.marvelous.gamelibrary.events.EventBuilder; -import uulm.teamname.marvelous.gamelibrary.events.EventType; -import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; -import uulm.teamname.marvelous.gamelibrary.requests.Request; -import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder; -import uulm.teamname.marvelous.gamelibrary.requests.RequestType; -import uulm.teamname.marvelous.server.lobbymanager.LobbyConnection; -import uulm.teamname.marvelous.server.lobbymanager.Participant; -import uulm.teamname.marvelous.server.netconnector.Client; - -import java.util.List; - -import static org.mockito.Mockito.*; - -class LobbyTest { - - @Mock - - Lobby lobby; - LobbyConnection connection; - - @BeforeEach - void beforeEach(){ - var gameID = "GameID"; - connection = mock(LobbyConnection.class); - var partyConfig = mock(PartyConfig.class); - var scenarioConfig = new ScenarioConfig(); - scenarioConfig.scenario = new FieldType[][] { - {FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS}, - {FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS} - }; - var characterConfig = mock(CharacterConfig.class); - - lobby = spy(new Lobby( - gameID, - connection, - partyConfig, - characterConfig, - scenarioConfig, - List.of(1, 2, 3, 4, 5, 6), - List.of(7, 8, 9, 10, 11, 12) - )); - } - - @Test - @Disabled - void receiveRequestsTest(){ - var requests = new Request[] { - new RequestBuilder(RequestType.Req).buildGameRequest(), - new RequestBuilder(RequestType.MoveRequest).buildGameRequest() - }; - var playerConnection = mock(WebSocket.class); - doNothing().when(lobby).updateTurnTimer(); - - Participant playerOne = new Participant( - new Client(playerConnection), "LobbyOne", ParticipantType.PlayerOne, false); - lobby.receiveRequests(requests, playerOne); - } - - @Test - @Disabled - void soonTimeoutTest(){ - var participant = mock(Participant.class); - lobby.soonTimeout(participant, 15); - verify(connection).sendEvents(participant, new EventBuilder(EventType.TimeoutWarningEvent).buildGameStateEvent()); - } - - @Test - @Disabled - void generateWinPlayer1Test(){ - var webSoc = mock(WebSocket.class); - Participant winner = new Participant(new Client(webSoc), "someLobby", ParticipantType.PlayerOne, false); - lobby.triggerWin(winner); - verify(connection).broadcastEvents( - new EventBuilder(EventType.WinEvent) - .withPlayerWon(1) - .buildGameStateEvent(), - new EventBuilder(EventType.DisconnectEvent) - .buildGameStateEvent()); - verify(connection).terminate(); - } - - @Test - @Disabled - void generateWinPlayer2Test(){ - var webSoc = mock(WebSocket.class); - Participant winner = new Participant(new Client(webSoc), "someLobby", ParticipantType.PlayerOne, false); - lobby.triggerWin(winner); - verify(connection).broadcastEvents( - new EventBuilder(EventType.WinEvent) - .withPlayerWon(2) - .buildGameStateEvent(), - new EventBuilder(EventType.DisconnectEvent) - .buildGameStateEvent()); - verify(connection).terminate(); - } - - -} 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 deleted file mode 100644 index 0ffad7f..0000000 --- a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnectionTest.java +++ /dev/null @@ -1,294 +0,0 @@ -package uulm.teamname.marvelous.server.lobbymanager; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.MockedStatic; -import uulm.teamname.marvelous.gamelibrary.events.Event; -import uulm.teamname.marvelous.gamelibrary.events.EventBuilder; -import uulm.teamname.marvelous.gamelibrary.events.EventType; -import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage; - -import org.java_websocket.WebSocket; -import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; -import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -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 { - @BeforeEach - void setUp() { - - } - - - 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(); - } - - @Test - void addPlayerOneTest(){ - var websocket = mock(WebSocket.class); - - Participant playerOne = new Participant(websocket, ParticipantType.PlayerOne, "PlayerOne"); - - assertThat(connection.addPlayer1(playerOne)).isTrue(); - assertThat(connection.hasPlayer1()).isTrue(); - assertThat(connection.addPlayer1(playerOne)).isFalse(); - } - - @Test - void addPlayerTwoTest(){ - var websocket = mock(WebSocket.class); - - Participant playerTwo = new Participant(websocket, ParticipantType.PlayerTwo, "PlayerTwo"); - - assertThat(connection.addPlayer2(playerTwo)).isTrue(); - assertThat(connection.hasPlayer2()).isTrue(); - assertThat(connection.addPlayer2(playerTwo)).isFalse(); - } - - @Test - void addPlayerTest(){ - var websocket = mock(WebSocket.class); - - Participant playerOne = new Participant(websocket, ParticipantType.PlayerOne, "PlayerOne"); - Participant playerTwo = new Participant(websocket, ParticipantType.PlayerTwo, "PlayerTwo"); - Participant spectator = new Participant(websocket, ParticipantType.Spectator,"Spectator"); - - assertThat(connection.addPlayer(playerOne)).isTrue(); - assertThat(connection.addPlayer(playerTwo)).isTrue(); - assertThat(connection.addPlayer(spectator)).isFalse(); - assertThat(connection.contains(playerOne)).isTrue(); - assertThat(connection.contains(playerTwo)).isTrue(); - assertThat(connection.contains(spectator)).isFalse(); - } - - @Test - void addSpectatorTest(){ - var websocket = mock(WebSocket.class); - - Participant playerOne = new Participant(websocket, ParticipantType.PlayerOne, "PlayerOne"); - Participant playerTwo = new Participant(websocket, ParticipantType.PlayerTwo, "PlayerTwo"); - Participant spectator = new Participant(websocket, ParticipantType.Spectator,"Spectator"); - - assertThat(connection.addSpectator(playerOne)).isFalse(); - assertThat(connection.addSpectator(playerTwo)).isFalse(); - assertThat(connection.addSpectator(spectator)).isTrue(); - assertThat(connection.contains(playerOne)).isFalse(); - assertThat(connection.contains(playerTwo)).isFalse(); - assertThat(connection.contains(spectator)).isTrue(); - } - - @Test - void addOneSpectatorAndOnePlayer(){ - var websocket = mock(WebSocket.class); - - Participant playerOne = new Participant(websocket, ParticipantType.PlayerOne, "PlayerOne"); - Participant spectator = new Participant(websocket, ParticipantType.Spectator,"Spectator"); - - assertThat(connection.addPlayer(playerOne)).isTrue(); - assertThat(connection.addSpectator(spectator)).isTrue(); - assertThat(connection.contains(playerOne)).isTrue(); - assertThat(connection.contains(spectator)).isTrue(); - } - - @Test - void removeParticipantTest(){ - var websocket = mock(WebSocket.class); - - Participant playerOne = new Participant(websocket, ParticipantType.PlayerOne, "PlayerOne"); - Participant playerTwo = new Participant(websocket, ParticipantType.PlayerTwo, "PlayerTwo"); - Participant spectator = new Participant(websocket, ParticipantType.Spectator,"Spectator"); - - connection.addPlayer(playerOne); - connection.addPlayer(playerTwo); - connection.addSpectator(spectator); - - assertThat(connection.removeParticipant(spectator)).isTrue(); - assertThat(connection.removeParticipant(playerOne)).isTrue(); - assertThat(connection.removeParticipant(playerOne)).isFalse(); - assertThat(connection.removeParticipant(playerTwo)).isTrue(); - } - - @Test - void sendMessageTest(){ - var webSocket = mock(WebSocket.class); - var participant = new Participant(webSocket, ParticipantType.PlayerOne, "playerOne"); - var event = new Event[]{ - new EventBuilder(EventType.PauseStartEvent).buildGameEvent(), - new EventBuilder(EventType.MoveEvent).buildGameEvent() - }; - - var message = new EventMessage(); - message.messages = event; - - connection.sendEvents(participant, event); - verify(sendMessageCallback).accept(participant.getConnection(), message); - } - - @Test - void broadcastEventTest(){ - var player1 = mock(WebSocket.class); - var player2 = mock(WebSocket.class); - var spectator1 = mock(WebSocket.class); - var spectator2 = mock(WebSocket.class); - - Participant playerOne = new Participant(player1, ParticipantType.PlayerOne, "playerOne"); - Participant playerTwo = new Participant(player2, ParticipantType.PlayerTwo, "playerTwo"); - Participant spectatorOne = new Participant(spectator1, ParticipantType.Spectator, "spectatorOne"); - Participant spectatorTwo = new Participant(spectator2, ParticipantType.Spectator, "spectatorTwo"); - - connection.addPlayer(playerOne); - connection.addPlayer(playerTwo); - connection.addSpectator(spectatorOne); - connection.addSpectator(spectatorTwo); - - var events = new Event[]{ - new EventBuilder(EventType.PauseStartEvent).buildGameEvent(), - new EventBuilder(EventType.MoveEvent).buildGameEvent() - }; - - var message = new EventMessage(); - message.messages = events; - - connection.broadcastEvents(events); - - verify(sendMessageCallback).accept(playerOne.getConnection(), message); - verify(sendMessageCallback).accept(playerTwo.getConnection(), message); - verify(sendMessageCallback).accept(spectatorOne.getConnection(), message); - verify(sendMessageCallback).accept(spectatorTwo.getConnection(), message); - } - - @Test - void broadcastEventsListToArrayTest(){ - var player1 = mock(WebSocket.class); - var player2 = mock(WebSocket.class); - var spectatorSocket = mock(WebSocket.class); - - Participant playerOne = new Participant(player1, ParticipantType.PlayerOne, "playerOne"); - Participant playerTwo = new Participant(player2, ParticipantType.PlayerTwo, "playerTwo"); - Participant spectator = new Participant(spectatorSocket, ParticipantType.Spectator, "spectator"); - - connection.addPlayer(playerOne); - connection.addPlayer(playerTwo); - connection.addSpectator(spectator); - - var events = new ArrayList(); - events.add(new EventBuilder(EventType.PauseStartEvent).buildGameEvent()); - events.add(new EventBuilder(EventType.MoveEvent).buildGameEvent()); - - connection.broadcastEvents(events); - - var message = new EventMessage(); - message.messages = events.toArray(new Event[0]); - - verify(sendMessageCallback).accept(playerOne.getConnection(), message); - verify(sendMessageCallback).accept(playerTwo.getConnection(), message); - verify(sendMessageCallback).accept(spectator.getConnection(), message); - } - - @Test - void hashCodeTest() { - var hashSet = new HashSet(); - LobbyConnection connection = new LobbyConnection("yay", null, null); - hashSet.add(connection); - assertThat(hashSet).containsOnly(connection); - connection.addPlayer1(mock(Participant.class)); - assertThat(hashSet).containsOnly(connection); - var spectator = new Participant(null, ParticipantType.Spectator, "SomeSpectator"); - connection.addSpectator(spectator); - assertThat(hashSet).containsOnly(connection); - } - - @Test - void broadcastToAllExceptTest(){ - var player1 = mock(WebSocket.class); - var player2 = mock(WebSocket.class); - var spectator1 = mock(WebSocket.class); - var spectator2 = mock(WebSocket.class); - - Participant playerOne = new Participant(player1, ParticipantType.PlayerOne, "playerOne"); - Participant playerTwo = new Participant(player2, ParticipantType.PlayerTwo, "playerTwo"); - Participant spectatorOne = new Participant(spectator1, ParticipantType.Spectator, "spectatorOne"); - Participant spectatorTwo = new Participant(spectator2, ParticipantType.Spectator, "spectatorTwo"); - - connection.addPlayer(playerOne); - connection.addPlayer(playerTwo); - connection.addSpectator(spectatorOne); - connection.addSpectator(spectatorTwo); - - var events = new Event[]{ - new EventBuilder(EventType.PauseStartEvent).buildGameEvent(), - new EventBuilder(EventType.MoveEvent).buildGameEvent() - }; - - var message = new EventMessage(); - message.messages = events; - - connection.broadcastToAllExcept(playerTwo, events); - - verify(sendMessageCallback).accept(playerOne.getConnection(), message); - //playerTwo is the only one who doesn't receive the message - verify(sendMessageCallback, never()).accept(playerTwo.getConnection(), message); - verify(sendMessageCallback).accept(spectatorOne.getConnection(), message); - verify(sendMessageCallback).accept(spectatorTwo.getConnection(), message); - } - - @Test - void terminateTest(){ - assertThat(connection.isActive()).isFalse(); - connection.terminateConnection(); - assertThat(connection.isActive()).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 deleted file mode 100644 index 0b678c0..0000000 --- a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyManagerTest.java +++ /dev/null @@ -1,317 +0,0 @@ -package uulm.teamname.marvelous.server.lobbymanager; - -import org.java_websocket.WebSocket; -import org.junit.jupiter.api.AfterEach; -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.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.*; -import static org.assertj.core.api.Assertions.*; - -class LobbyManagerTest { - /* - BiConsumer onMessageCallback; - BiConsumer onErrorCallback; - - LobbyManager manager; - WebSocket player1; - WebSocket player2; - WebSocket player3; - WebSocket spectator; - - PlayerReadyMessage playerReady; - PlayerReadyMessage spectatorReady; - PlayerReadyMessage aiReady; - - Participant player1Participant; - Participant player2Participant; - Participant player3Participant; - - LobbyRunner lobbyRunner; - MockedStatic lobbyRunnerMockedStatic; - MockedStatic serverMockedStatic; - - @BeforeEach - void beforeEach() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { - - onMessageCallback = mock(BiConsumer.class); - onErrorCallback = mock(BiConsumer.class); - - manager = new LobbyManager(onMessageCallback, onErrorCallback); - - player1 = mock(WebSocket.class); - player2 = mock(WebSocket.class); - player3 = mock(WebSocket.class); - spectator = mock(WebSocket.class); - - playerReady = new PlayerReadyMessage(); - playerReady.role = RoleEnum.PLAYER; - - spectatorReady = new PlayerReadyMessage(); - spectatorReady.role = RoleEnum.SPECTATOR; - - aiReady = new PlayerReadyMessage(); - aiReady.role = RoleEnum.KI; - - player1Participant = new Participant( - player1, - ParticipantType.PlayerOne, - "AwesomePlayer"); - player2Participant = new Participant( - player2, - ParticipantType.PlayerTwo, - "MoreAwesomePlayer"); - player3Participant = new Participant( - player3, - ParticipantType.PlayerOne, - "AwesomestAwesomePlayer"); - - 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); - } - - @AfterEach - void afterEach() { - lobbyRunnerMockedStatic.close(); - serverMockedStatic.close(); - } - - @Test - void lobbyManagerGetsCreatedEmpty() { - assertThat(manager.getLobbies()).isEmpty(); - } - - @Test - @DisplayName("When a participant is added, a new Lobby is created") - void lobbyCreationTest() { - - - - var message = new PlayerReadyMessage(); - message.role = RoleEnum.PLAYER; - - assertThat(manager.getLobbies()).isEmpty(); - - when(player1.getResourceDescriptor()).thenReturn("/ResourcesFTW"); - - manager.assignLobbyToConnection(player1, "AwesomePlayer", message); - - Participant player1Participant = new Participant(player1, ParticipantType.PlayerOne, "AwesomePlayer"); - - assertThat(manager.getLobbies()).containsOnlyKeys(player1Participant); - assertThat(manager.getLobbies().get(player1Participant)).isNotNull(); - assertThat(manager.getLobbies().get(player1Participant).getPlayer1()).isEqualTo(player1Participant); - - assertThat(manager.getResourceDescriptorToLobby()).containsOnlyKeys("/ResourcesFTW"); - assertThat(manager.getResourceDescriptorToLobby().get("/ResourcesFTW")) - .isEqualTo(manager.getLobbies().get(player1Participant)) - .isNotNull(); - } - - @Test - @DisplayName("when two participants with same ResourceDescriptor connect, they get assigned to the same lobby") - void twoParticipantsSameLobbyTest() { - - - - when(player1.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor"); - when(player2.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor"); - - manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady); - manager.assignLobbyToConnection(player2, "MoreAwesomePlayer", playerReady); - - Participant player1Participant = new Participant( - player1, ParticipantType.PlayerOne, "AwesomePlayer"); - - Participant player2Participant = new Participant( - player2, ParticipantType.PlayerTwo, "MoreAwesomePlayer"); - - assertThat(manager.getLobbies()).containsOnlyKeys(player1Participant, player2Participant); - assertThat(manager.getLobbies().get(player1Participant)) - .isEqualTo(manager.getLobbies().get(player2Participant)) - .isNotNull(); - - assertThat(manager.getResourceDescriptorToLobby()).containsOnlyKeys("/fancyResourceDescriptor"); - assertThat(manager.getResourceDescriptorToLobby().get("/fancyResourceDescriptor")) - .isEqualTo(manager.getLobbies().get(player1Participant)) - .isEqualTo(manager.getLobbies().get(player2Participant)) - .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.assignLobbyToConnection(player1, "AwesomePlayer", playerReady); - - assertThat(manager.getLobbies()).hasSize(1); - assertThat(manager.getLobbies().get(player1Participant)).isNotNull(); - assertThat(manager.getLobbies().get(player1Participant).gameID).isNotNull(); - - assertThat(manager.getResourceDescriptorToLobby()).hasSize(1); - assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID)) - .isNotNull() - .isEqualTo(manager.getLobbies().get(player1Participant)); - } - - @Test - @DisplayName("When two people without ResourceDescriptor join, they get allocated to the same lobby") - void randomLobbyTwoPlayerTest() { - when(player1.getResourceDescriptor()).thenReturn(null); - when(player2.getResourceDescriptor()).thenReturn(null); - manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady); - manager.assignLobbyToConnection(player2, "MoreAwesomePlayer", playerReady); - - assertThat(manager.getLobbies()).hasSize(2); - assertThat(manager.getLobbies().get(player1Participant)) - .isNotNull() - .isEqualTo(manager.getLobbies().get(player2Participant)); - - assertThat(manager.getResourceDescriptorToLobby()).hasSize(1); - assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID)) - .isNotNull() - .isEqualTo(manager.getLobbies().get(player1Participant)) - .isEqualTo(manager.getLobbies().get(player2Participant)); - } - - @Test - @DisplayName("When three players connect, a new lobby gets created") - void randomLobbyThreePlayerTest() { - when(player1.getResourceDescriptor()).thenReturn(null); - when(player2.getResourceDescriptor()).thenReturn(null); - when(player3.getResourceDescriptor()).thenReturn(null); - 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)) - .isNotNull() - .isEqualTo(manager.getLobbies().get(player2Participant)) - .isNotEqualTo(manager.getLobbies().get(player3Participant)); - - assertThat(manager.getLobbies().get(player3Participant)).isNotNull(); - - assertThat(manager.getResourceDescriptorToLobby()).hasSize(2); - 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)); - assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player3Participant).gameID)) - .isNotNull() - .isEqualTo(manager.getLobbies().get(player3Participant)); - } - - @Test - @DisplayName("Spectator joins full lobby") - void spactatorsJoinLobby() { - when(player1.getResourceDescriptor()).thenReturn("/awesomeLobby"); - when(player2.getResourceDescriptor()).thenReturn("/awesomeLobby"); - when(spectator.getResourceDescriptor()).thenReturn("/awesomeLobby"); - - Participant player1Participant = new Participant(player1, ParticipantType.PlayerOne, "playerOne"); - Participant player2Participant = new Participant(player2, ParticipantType.PlayerTwo, "playerTwo"); - 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); - 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); - assertThat(manager.getLobbies()).containsOnlyKeys( - player1Participant, - player2Participant, - spectator1); - - assertThat(manager.getLobbies().get(player1Participant)) - .isNotNull() - .isEqualTo(manager.getLobbies().get(player2Participant)) - .isEqualTo(manager.getLobbies().get(spectator1)); - - assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID)) - .isNotNull() - .isEqualTo(manager.getLobbies().get(player1Participant)) - .isEqualTo(manager.getLobbies().get(player2Participant)) - .isEqualTo(manager.getLobbies().get(spectator1)); - - 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)); - } */ -} 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 deleted file mode 100644 index 4f292b9..0000000 --- a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunnerTest.java +++ /dev/null @@ -1,88 +0,0 @@ -package uulm.teamname.marvelous.server.lobbymanager; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -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; -import java.util.concurrent.*; - -import static org.mockito.Mockito.*; -import static org.junit.jupiter.api.Assertions.*; -import static org.assertj.core.api.Assertions.*; - -class LobbyRunnerTest { - /* - - LobbyConnection connection; - LobbyRunner lobbyRunner; - - MockedStatic serverMockedStatic; - - @BeforeEach - void setUp() - throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { - connection = mock(LobbyConnection.class); - // when(connection.gameID).thenReturn("/SomeGameID"); - - var constructor = LobbyRunner.class.getDeclaredConstructor(); - constructor.setAccessible(true); - - lobbyRunner = constructor.newInstance(); - - serverMockedStatic = mockStatic(Server.class); - serverMockedStatic.when(Server::getMaxLobbies).thenReturn(1); - } - - @AfterEach - void tearDown() { - serverMockedStatic.close(); - } - - - // It seems very impractical indeed to properly to check if another thread executed this, so I won't - @Test - void lobbyGetsStartedTest() throws InterruptedException { - lobbyRunner.startLobby(connection); - Thread.sleep(10); - verify(connection).run(); - } - - @Test - void canAddLobbyTest() throws InterruptedException { - assertThat(lobbyRunner.canAddLobby()).isTrue(); - lobbyRunner.startLobby(connection); - assertThat(lobbyRunner.canAddLobby()).isFalse(); - } - - @Test - void removeLobbyRemovesLobbyAndSetsTerminationFlag() { - lobbyRunner.startLobby(connection); - } - - @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(); - } - */ -} 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 50e8ae1..24e49f3 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 @@ -60,211 +60,4 @@ class UserManagerTest { // 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; - SUID clientID; - Participant participant; - ClientHandshake handshake; - - @BeforeEach - void beforeEach() - throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { - connection = mock(WebSocket.class); - - var constructor = UserManager.class.getDeclaredConstructor(); - constructor.setAccessible(true); - manager = spy(constructor.newInstance()); - - handshake = mock(ClientHandshake.class); - when(handshake.getResourceDescriptor()).thenReturn("/someDescriptor"); - - clientID = new SUID("Awesome Name", "Awesome deviceID"); - - participant = spy(new Participant(connection, ParticipantType.PlayerOne, "Awesome Name")); - } - - @Test - void userIsConnectedTest() { - assertThat(manager.getUserCount()).isEqualTo(0); - assertThat(manager.getNewUsers()).doesNotContain(connection); - manager.connectUser(connection); - assertThat(manager.getUserCount()).isEqualTo(1); - assertThat(manager.isUserConnected(connection)).isTrue(); - assertThat(manager.getNewUsers()).containsOnly(connection); - } - - @Test - void helloServerMessagesGetAssignedProperly() { - manager.messageReceived( - connection, - "{\"messageType\":\"HELLO_SERVER\",\"name\":\"SomeAwesomeName\",\"deviceID\":\"YAY\"}"); - - verify(manager).handshake( - eq(connection), - any(HelloServerMessage.class)); - } - - @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); // TODO: fix this - } - - @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() { - manager.messageReceived( - connection, - "{\"messageType\":\"PLAYER_READY\",\"startGame\":\"false\",\"role\":\"PLAYER\"}"); - - verify(manager).playerReady( - eq(connection), - any(PlayerReadyMessage.class)); - } - - - @Test - void characterSelectionMessageGetsAssignedProperly() { - manager.messageReceived( - connection, - "{\"messageType\":\"CHARACTER_SELECTION\",\"characters\":" + - "[true, true, false, true, false, false, true, true, false, false, false, true]}"); - - verify(manager).charactersSelected( - eq(connection), - any(CharacterSelectionMessage.class)); - } - - @Test - void requestMessagesGetRelayedProperly() { - manager.messageReceived( - connection, - "{\"messageType\":\"REQUESTS\",\"messages\":[{\"requestType\":\"EndRoundRequest\"}]}"); - - verify(manager).relayRequestMessage( - eq(connection), - any(RequestMessage.class)); - } */ }