From 552bab8d937da55cc5a911acd85c077c6bb66c40 Mon Sep 17 00:00:00 2001 From: Richard Reiber Date: Sat, 5 Jun 2021 20:30:48 +0200 Subject: [PATCH] test: added DisconnectSegmentTest --- .../pipelining/DisconnectSegmentTest.java | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 Server/src/test/java/uulm/teamname/marvelous/server/lobby/pipelining/DisconnectSegmentTest.java diff --git a/Server/src/test/java/uulm/teamname/marvelous/server/lobby/pipelining/DisconnectSegmentTest.java b/Server/src/test/java/uulm/teamname/marvelous/server/lobby/pipelining/DisconnectSegmentTest.java new file mode 100644 index 0000000..86e411d --- /dev/null +++ b/Server/src/test/java/uulm/teamname/marvelous/server/lobby/pipelining/DisconnectSegmentTest.java @@ -0,0 +1,103 @@ +package uulm.teamname.marvelous.server.lobby.pipelining; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import uulm.teamname.marvelous.gamelibrary.config.FieldType; +import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig; +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.lobby.Lobby; +import uulm.teamname.marvelous.server.lobbymanager.LobbyConnection; +import uulm.teamname.marvelous.server.lobbymanager.Participant; + +import java.util.ArrayList; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; +import static org.assertj.core.api.Assertions.*; + +class DisconnectSegmentTest { + Participant player1; + Participant player2; + Participant spectator; + DisconnectSegment disconnectSegment; + Lobby lobby; + LobbyConnection connection; + + @BeforeEach + void beforeEach(){ + connection = mock(LobbyConnection.class); + + player1 = mock(Participant.class); + player2 = mock(Participant.class); + spectator = mock(Participant.class); + + lobby = mock(Lobby.class); + disconnectSegment = new DisconnectSegment(lobby); + + when(lobby.getConnection()).thenReturn(connection); + when(connection.getPlayer1()).thenReturn(player1); + when(connection.getPlayer2()).thenReturn(player2); + } + + @Test + void noDisconnectRequestTest(){ + var requests = new Request[] { + new RequestBuilder(RequestType.Req).buildGameRequest(), + new RequestBuilder(RequestType.MoveRequest).buildGameRequest() + }; + Packet packet = new Packet(requests, player1); + AtomicBoolean abort = new AtomicBoolean(false); + disconnectSegment.processRequests(packet, new ArrayList<>(), abort); + assertThat(packet).containsOnly(requests); + } + + @Test + void disconnectRequestBySpectatorTest(){ + var requests = new Request[] { + new RequestBuilder(RequestType.DisconnectRequest).buildGameRequest(), + new RequestBuilder(RequestType.Req).buildGameRequest(), + new RequestBuilder(RequestType.MoveRequest).buildGameRequest() + }; + Packet packet = new Packet(requests, spectator); + AtomicBoolean abort = new AtomicBoolean(false); + disconnectSegment.processRequests(packet, new ArrayList<>(), abort); + assertThat(packet).doesNotContain(requests); + verify(connection).hasPlayer1(); + verify(connection).hasPlayer2(); + } + + @Test + void disconnectRequestByPlayer1Test(){ + var requests = new Request[] { + new RequestBuilder(RequestType.DisconnectRequest).buildGameRequest(), + new RequestBuilder(RequestType.Req).buildGameRequest(), + new RequestBuilder(RequestType.MoveRequest).buildGameRequest() + }; + Packet packet = new Packet(requests, player1); + AtomicBoolean abort = new AtomicBoolean(false); + disconnectSegment.processRequests(packet, new ArrayList<>(), abort); + assertThat(packet).doesNotContain(requests); + verify(connection).removePlayer(player1); + assertThat(verify(connection).hasPlayer1()).isFalse(); + verify(connection).hasPlayer2(); + } + + @Test + void disconnectRequestByPlayer2Test(){ + var requests = new Request[] { + new RequestBuilder(RequestType.DisconnectRequest).buildGameRequest(), + new RequestBuilder(RequestType.Req).buildGameRequest(), + new RequestBuilder(RequestType.MoveRequest).buildGameRequest() + }; + Packet packet = new Packet(requests, player2); + AtomicBoolean abort = new AtomicBoolean(false); + disconnectSegment.processRequests(packet, new ArrayList<>(), abort); + assertThat(packet).doesNotContain(requests); + verify(connection).removePlayer(player2); + verify(connection).hasPlayer1(); + assertThat(verify(connection).hasPlayer2()).isFalse(); + } +} \ No newline at end of file