Server/Server/src/test/java/uulm/teamname/marvelous/server/lobby/pipelining/FilterEndRoundRequestSegmen...

77 lines
2.7 KiB
Java

package uulm.teamname.marvelous.server.lobby.pipelining;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
import uulm.teamname.marvelous.gamelibrary.entities.EntityType;
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.Participant;
import uulm.teamname.marvelous.server.netconnector.Client;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.mockito.Mockito.*;
import static org.assertj.core.api.Assertions.*;
class FilterEndRoundRequestSegmentTest {
FilterEndRoundRequestSegment segment;
EntityID activeCharacter;
Participant activeParticipant, inactiveParticipant;
Request[] requests;
@BeforeEach
void beforeEach() {
this.segment = new FilterEndRoundRequestSegment(this::getActiveCharacter);
this.activeCharacter = new EntityID(EntityType.P1, 2);
this.activeParticipant = new Participant(mock(Client.class), null, ParticipantType.PlayerOne, false);
this.inactiveParticipant = new Participant(mock(Client.class), null, ParticipantType.PlayerTwo, false);
requests = new Request[] {
new RequestBuilder(RequestType.EndRoundRequest).buildGameRequest()
};
}
private EntityID getActiveCharacter() {
return activeCharacter;
}
@Test
@DisplayName("Request from active participant doesn't get filtered")
void packetFromActiveParticipantTest() {
var packet = new Packet(requests, activeParticipant);
var atomicBoolean = new AtomicBoolean(false);
var processedPacket = (Packet) packet.clone();
segment.processRequests(processedPacket, new ArrayList<>(), atomicBoolean);
assertThat(processedPacket).isEqualTo(packet);
assertThat(atomicBoolean.get()).isFalse();
}
@Test
@DisplayName("Request from non-active participant gets flagged as an error")
void packetFromNonActiveParticipantTest() {
var packet = new Packet(requests, inactiveParticipant);
var atomicBoolean = new AtomicBoolean(false);
var processedPacket = (Packet) packet.clone();
segment.processRequests(processedPacket, new ArrayList<>(), atomicBoolean);
// assertThat(processedPacket).isEqualTo(packet); is not necessary as there's no actual filtering going on
assertThat(atomicBoolean.get()).isTrue();
}
}