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); + } + }