test: added DisconnectSegmentTest

This commit is contained in:
Richard Reiber 2021-06-05 20:30:48 +02:00
parent f8428fea3e
commit 552bab8d93

View File

@ -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();
}
}