From 4e39d01ac64cf5749569135539e9b5982cb725a1 Mon Sep 17 00:00:00 2001 From: Richard Reiber Date: Sun, 6 Jun 2021 19:59:35 +0200 Subject: [PATCH] test: added tests for add and remove Participants as well as first message Tests (broadcast needs to be fixed) --- .../lobbymanager/LobbyConnectionTest.java | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) 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 index ed4a2c6..1016a6d 100644 --- a/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnectionTest.java +++ b/Server/src/test/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnectionTest.java @@ -2,10 +2,14 @@ package uulm.teamname.marvelous.server.lobbymanager; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +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.function.BiConsumer; @@ -60,4 +64,135 @@ class LobbyConnectionTest { 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 webSock = mock(WebSocket.class); + + Participant playerOne = new Participant(webSock, ParticipantType.PlayerOne, "playerOne"); + Participant playerTwo = new Participant(webSock, ParticipantType.PlayerTwo, "playerTwo"); + Participant spectatorOne = new Participant(webSock, ParticipantType.Spectator, "spectatorOne"); + Participant spectatorTwo = new Participant(webSock, 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; + + //TODO: fix them + 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); + } + }