Commit 2307ed0e authored by Corentin Guillevic's avatar Corentin Guillevic
Browse files

Add tests for Room

parent eb048573
......@@ -14,7 +14,6 @@ import fr.univnantes.alma.server.game.item.planet.Place;
import fr.univnantes.alma.server.game.item.planet.Planet;
import fr.univnantes.alma.server.game.item.player.Creature;
import fr.univnantes.alma.server.game.item.player.Player;
import fr.univnantes.alma.server.game.item.player.PlayerTeam;
import fr.univnantes.alma.server.game.item.player.Traque;
import fr.univnantes.alma.server.game.item.power.Power;
import fr.univnantes.alma.server.game.item.power.PowerType;
......@@ -353,7 +352,7 @@ public class Game implements GameInterface {
}
}
else {
throw new IllegalArgumentException("Phase current : " + this.state);
return new Response(false, "The current phase is " + state);
}
if(response.isState()) {
playerFinishedPhase(player, phase);
......@@ -651,12 +650,10 @@ public class Game implements GameInterface {
@Override
public void sendGameIsFinished() {
TPlayerTeam winner = new TPlayerTeam(board.winner().toString());
for(Integer id : playersMap.keySet()) {
try {
room.sendGameIsFinished(id, winner);
} catch (TException e) {
e.printStackTrace();
}
try {
room.sendGameIsFinished(winner);
} catch (TException e) {
e.printStackTrace();
}
}
......
......@@ -3,12 +3,9 @@ package fr.univnantes.alma.server.game;
import fr.univnantes.alma.server.game.item.Phase;
import fr.univnantes.alma.server.game.item.action.Action;
import fr.univnantes.alma.server.game.item.board.Board;
import fr.univnantes.alma.server.game.item.board.Score;
import fr.univnantes.alma.server.game.item.card.Card;
import fr.univnantes.alma.server.game.item.jeton.JetonSymbol;
import fr.univnantes.alma.server.game.item.jeton.PlacedJeton;
import fr.univnantes.alma.server.game.item.planet.Planet;
import fr.univnantes.alma.server.game.utilitary.Conversion;
import fr.univnantes.alma.server.game.utilitary.Pair;
import fr.univnantes.alma.server.game.utilitary.request.*;
import fr.univnantes.alma.server.user.User;
......@@ -16,7 +13,6 @@ import fr.univnantes.alma.thrift.*;
import org.apache.thrift.TException;
import org.atlanmod.commons.log.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -36,7 +32,7 @@ public class Room {
/**
* Counter used to increment player identifications.
*/
private final AtomicInteger numberPlayers;
private final AtomicInteger playersCounter;
/**
* Representes the maximum number of players
......@@ -90,7 +86,7 @@ public class Room {
public Room(String roomId, User creator) {
this.requests = new ArrayBlockingQueue<>(MAX_PLAYERS);
this.numberPlayers = new AtomicInteger(1);
this.playersCounter = new AtomicInteger(1);
this.creator = creator;
this.roomId = roomId;
......@@ -108,8 +104,16 @@ public class Room {
return roomId;
}
public AtomicInteger getNumberPlayers() {
return numberPlayers;
public int getNumberPlayers() {
return users.size();
}
/**
* Set the game, usefull for tests
* @param game The game
*/
public void setGame(Game game) {
this.game = game;
}
public boolean isCreator(User user){
......@@ -117,7 +121,7 @@ public class Room {
}
public Response join(User user) {
int currentNumber = numberPlayers.getAndIncrement();
int currentNumber = playersCounter.getAndIncrement();
Response response;
if(isStart){
response = new Response(false, "Game has started");
......@@ -139,12 +143,14 @@ public class Room {
public Response start(User user, int creatureId, Board board, Planet planet) {
Response response;
if(!isCreator(user)) {
response = new Response(false, "Only the game creator can start the game");
Log.info("1");
response = new Response(false, "Only the room creator can start the game");
}
else if(players.size() < MIN_PLAYERS) {
response = new Response(false, "Need " + MIN_PLAYERS + " players");
}
else if(!users.containsKey(creatureId)) {
response = new Response(false, creatureId + " is not a valid id");
}
else{
isStart = true;
this.game = new Game(playersToIdNamePairList(), creatureId, planet, board, this);
......@@ -158,8 +164,12 @@ public class Room {
}
public Response addRequest(GameRequest request) {
requests.offer(request);
return new Response(true, "Request sent");
if(requests.offer(request)) {
return new Response(true, "Request sent");
}
else {
return new Response(false, "Unable to offer this request");
}
}
private void requestManagement(){
......@@ -298,8 +308,10 @@ public class Room {
users.get(idPlayer).sendGameDescription(description);
}
public void sendGameIsFinished(int idPlayer, TPlayerTeam winner) throws TException {
users.get(idPlayer).sendGameIsFinished(winner);
public void sendGameIsFinished(TPlayerTeam winner) throws TException {
for(User user : users.values()) {
user.sendGameIsFinished(winner);
}
}
/****************************
......
......@@ -234,7 +234,7 @@ public class Conversion {
for(JetonSymbol symbol : card.getJetons()) {
attributes.add(new TPair("SYMBOL", symbol.toString()));
}
return new TCard("SURVIVAL", card.getCardName().toString(), attributes);
return new TCard("TRACKING", card.getCardName().toString(), attributes);
}
public static TCard toTCard(Card card) {
......
......@@ -215,9 +215,7 @@ public class GameTest {
assertTrue(game.playerPlayCard(2, Collections.singletonList(plage)).state);
assertTrue(game.playerHasFinished(2, Phase.PHASE_1).state);
//verification erreur
assertThrows(IllegalArgumentException.class,() -> game.playerHasFinished(1,Phase.POSTPHASE_2));
assertFalse(game.playerHasFinished(2, Phase.POSTPHASE_2).state);
assertTrue(game.playerHasFinished(1, Phase.POSTPHASE_1).state);
assertTrue(game.playerHasFinished(2, Phase.POSTPHASE_1).state);
......
package fr.univnantes.alma.server.game;
import fr.univnantes.alma.common.NotAloneDatabase;
import fr.univnantes.alma.data.DatabaseFactory;
import fr.univnantes.alma.server.game.item.Phase;
import fr.univnantes.alma.server.game.item.action.Action;
import fr.univnantes.alma.server.game.item.action.ActionTargetPlayer;
import fr.univnantes.alma.server.game.item.board.Board;
import fr.univnantes.alma.server.game.item.board.BoardColor;
import fr.univnantes.alma.server.game.item.board.BoardDistribution;
import fr.univnantes.alma.server.game.item.card.PlaceCard;
import fr.univnantes.alma.server.game.item.jeton.JetonSymbol;
import fr.univnantes.alma.server.game.item.jeton.PlacedJeton;
import fr.univnantes.alma.server.game.item.planet.Place;
import fr.univnantes.alma.server.game.item.planet.Planet;
import fr.univnantes.alma.server.game.item.player.Creature;
import fr.univnantes.alma.server.game.item.player.Traque;
import fr.univnantes.alma.server.game.utilitary.Conversion;
import fr.univnantes.alma.server.game.utilitary.request.*;
import fr.univnantes.alma.server.user.User;
import fr.univnantes.alma.thrift.*;
import org.apache.thrift.TException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static fr.univnantes.alma.server.game.item.card.CardName.*;
import static fr.univnantes.alma.server.game.item.player.PlayerTeam.CREATURE;
import static fr.univnantes.alma.server.game.item.player.PlayerTeam.TRAQUE;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
class RoomTest {
NotAloneDatabase database = DatabaseFactory.getDatabase();
final PlaceCard antre = database.findPlaceCard(ANTRE.toString());
final PlaceCard jungle = database.findPlaceCard(JUNGLE.toString());
final PlaceCard riviere = database.findPlaceCard(RIVIERE.toString());
final PlaceCard plage = database.findPlaceCard(PLAGE.toString());
final PlaceCard rover = database.findPlaceCard(ROVER.toString());
final PlaceCard marais = database.findPlaceCard(MARAIS.toString());
final PlaceCard abri = database.findPlaceCard(ABRI.toString());
final PlaceCard epave = database.findPlaceCard(EPAVE.toString());
final PlaceCard source = database.findPlaceCard(SOURCE.toString());
final PlaceCard artefact = database.findPlaceCard(ARTEFACT.toString());
User user1;
User user2;
User user3;
User user4;
User user5;
User user6;
User user7;
User user8;
Planet planet;
Board board;
List<PlaceCard> placeCards;
Room genericRoom;
Game genericGame;
@BeforeEach
void setup() throws InterruptedException {
user1 = mock(User.class);
when(user1.getId()).thenReturn("1");
user2 = mock(User.class);
when(user1.getId()).thenReturn("2");
user3 = mock(User.class);
when(user1.getId()).thenReturn("3");
user4 = mock(User.class);
when(user1.getId()).thenReturn("4");
user5 = mock(User.class);
when(user1.getId()).thenReturn("5");
user6 = mock(User.class);
when(user1.getId()).thenReturn("6");
user7 = mock(User.class);
when(user1.getId()).thenReturn("7");
user8 = mock(User.class);
when(user1.getId()).thenReturn("8");
placeCards = new ArrayList();
placeCards.add(antre);
placeCards.add(jungle);
placeCards.add(riviere);
placeCards.add(plage);
placeCards.add(rover);
placeCards.add(marais);
placeCards.add(abri);
placeCards.add(epave);
placeCards.add(source);
placeCards.add(artefact);
planet = new Planet(placeCards);
board = new Board(BoardDistribution.FRONT, BoardColor.BLUE);
String roomId = "1001";
genericRoom = new Room(roomId, user1);
genericRoom.join(user2);
Thread.sleep(20);
genericRoom.start(user1, 1, board, planet);
genericGame = new Game(Arrays.asList(1,2), 1, planet, board, genericRoom);
genericRoom.setGame(genericGame);
}
@Test
void testCreateGame() {
String roomId = "1001";
Room room = new Room(roomId, user1);
assertSame(1, room.getNumberPlayers());
assertEquals(roomId, room.getRoomId());
assertTrue(room.isCreator(user1));
}
@Test
void testJoinGame_sevenPlayersJoin() throws InterruptedException {
String roomId = "1001";
Room room = new Room(roomId, user1);
assertTrue(room.join(user2).state);
assertTrue(room.join(user3).state);
assertTrue(room.join(user4).state);
assertTrue(room.join(user5).state);
assertTrue(room.join(user6).state);
assertTrue(room.join(user7).state);
assertTrue(room.isCreator(user1));
assertFalse(room.isCreator(user2));
Thread.sleep(50);
assertSame(7, room.getNumberPlayers());
}
@Test
void testJoinGame_eightPlayersJoin() throws InterruptedException {
String roomId = "1001";
Room room = new Room(roomId, user1);
assertTrue(room.join(user2).state);
assertTrue(room.join(user3).state);
assertTrue(room.join(user4).state);
assertTrue(room.join(user5).state);
assertTrue(room.join(user6).state);
assertTrue(room.join(user7).state);
Response response = room.join(user8);
assertFalse(response.state);
assertEquals("Room is full", response.getMessage());
Thread.sleep(50);
assertSame(7, room.getNumberPlayers());
}
@Test
void testJoin_gameIsStarted() throws InterruptedException {
String roomId = "1001";
Room room = new Room(roomId, user1);
assertTrue(room.join(user2).state);
Thread.sleep(10);
assertTrue(room.start(user1, 1, board, planet).state);
Response response = room.join(user3);
assertFalse(response.state);
assertEquals("Game has started", response.getMessage());
assertSame(2, room.getNumberPlayers());
}
@Test
void testStart_enoughPlayer() throws InterruptedException {
String roomId = "1001";
Room room = new Room(roomId, user1);
assertTrue(room.join(user2).state);
Thread.sleep(10);
Response response = room.start(user1, 1, board, planet);
assertTrue(response.state);
assertEquals("Game start", response.getMessage());
}
@Test
void testStart_noEnoughPlayer() {
String roomId = "1001";
Room room = new Room(roomId, user1);
Response response = room.start(user1, 1, board, planet);
assertFalse(response.state);
assertEquals("Need 2 players", response.getMessage());
}
@Test
void testStart_isNotCreatorWhoStart() throws InterruptedException {
String roomId = "1001";
Room room = new Room(roomId, user1);
assertTrue(room.join(user2).state);
Thread.sleep(10);
Response response = room.start(user2, 1, board, planet);
assertFalse(response.state);
assertEquals("Only the room creator can start the game", response.getMessage());
}
@Test
void testStart_invalidCreatureId() throws InterruptedException {
String roomId = "1001";
Room room = new Room(roomId, user1);
assertTrue(room.join(user2).state);
Thread.sleep(10);
Response response = room.start(user1, 5, board, planet);
assertFalse(response.state);
assertEquals("5 is not a valid id", response.getMessage());
}
@Test
void testGetGameDescription_creature() {
TDescription tDescription = genericRoom.getGameDescription(user1);
TPlayerTeam tPlayerTeam = tDescription.getTeam();
assertEquals(CREATURE.toString(), tPlayerTeam.getType());
Creature creature = genericGame.getCreature();
//THand
THand tHand = tDescription.getHand();
THand tHandExpected = Conversion.toTHand(creature);
assertEquals(tHandExpected, tHand);
//TScore
TScore tScore = tDescription.getScore();
TScore tScoreExpect = Conversion.toTScore(genericGame.getBoard());
assertEquals(tScoreExpect, tScore);
//TPlanet
TPlanet tPlanet = tDescription.getPlanet();
TPlanet tPlanetExpect = Conversion.toTPlanet(genericGame.getPlanet());
assertEquals(tPlanetExpect, tPlanet);
//Reserve
List<TCardReserve> reserves = tDescription.getReserve();
List<TCardReserve> reservesExpect = Conversion.toTCardReserveList(genericGame.getReserve());
assertEquals(reservesExpect, reserves);
//Variables
List<TGameVariable> variables = tDescription.getVariables();
List<TGameVariable> variablesExpect = Conversion.toTGameVariable(genericGame.getGameRoundVariables());
assertEquals(variablesExpect, variables);
//Traques information
List<TTraque> traques = tDescription.getTraquesInformation();
List<TTraque> traquesExpect = genericGame.getTraques().stream().map(Conversion::toTTraque).collect(Collectors.toList());
assertEquals(traquesExpect, traques);
//Creature information
TCreature tCreature = tDescription.getCreatureInformation();
TCreature tCreatureExpect = Conversion.toTCreature(genericGame.getCreature());
assertEquals(tCreatureExpect, tCreature);
}
@Test
void testGetGameDescription_traque() {
TDescription tDescription = genericRoom.getGameDescription(user2);
TPlayerTeam tPlayerTeam = tDescription.getTeam();
assertEquals(TRAQUE.toString(), tPlayerTeam.getType());
Traque traque = genericGame.getTraques().get(0);
//THand
THand tHand = tDescription.getHand();
THand tHandExpected = Conversion.toTHand(traque);
assertEquals(tHandExpected, tHand);
//TScore
TScore tScore = tDescription.getScore();
TScore tScoreExpect = Conversion.toTScore(genericGame.getBoard());
assertEquals(tScoreExpect, tScore);
//TPlanet
TPlanet tPlanet = tDescription.getPlanet();
TPlanet tPlanetExpect = Conversion.toTPlanet(genericGame.getPlanet());
assertEquals(tPlanetExpect, tPlanet);
//Reserve
List<TCardReserve> reserves = tDescription.getReserve();
List<TCardReserve> reservesExpect = Conversion.toTCardReserveList(genericGame.getReserve());
assertEquals(reservesExpect, reserves);
//Variables
List<TGameVariable> variables = tDescription.getVariables();
List<TGameVariable> variablesExpect = Conversion.toTGameVariable(genericGame.getGameRoundVariables());
assertEquals(variablesExpect, variables);
//Traques information
List<TTraque> traques = tDescription.getTraquesInformation();
List<TTraque> traquesExpect = genericGame.getTraques().stream().map(Conversion::toTTraque).collect(Collectors.toList());
assertEquals(traquesExpect, traques);
//Creature information
TCreature tCreature = tDescription.getCreatureInformation();
TCreature tCreatureExpect = Conversion.toTCreature(genericGame.getCreature());
assertEquals(tCreatureExpect, tCreature);
}
@Test
void testSendFinishPhase_requestAccepted() throws TException, InterruptedException {
Response response = genericRoom.addRequest(new GameFinishPhaseRequest(user1, Phase.PREPHASE_1));
assertTrue(response.state);
assertEquals("Request sent", response.getMessage());
Thread.sleep(10);
Response expected = new Response(true, "");
verify(user1).sendResponse(expected);
}
@Test
void testSendFinishPhase_badRequest() throws TException, InterruptedException {
Response response = genericRoom.addRequest(new GameFinishPhaseRequest(user1, Phase.PHASE_2));
assertTrue(response.state);
assertEquals("Request sent", response.getMessage());
Thread.sleep(10);
Response expected = new Response(false, "The current phase is PREPHASE_1");
verify(user1).sendResponse(expected);
}
@Test
void testSendPlayCards_traque() throws TException, InterruptedException {
//Start phase 1
genericGame.startNextPhase();
Response response = genericRoom.addRequest(new GamePlayCardsRequest(user2, Collections.singletonList(antre)));
assertTrue(response.state);
assertEquals("Request sent", response.getMessage());
Thread.sleep(10);
Response expected = new Response(true, "");
verify(user2).sendResponse(expected);
}
@Test
void testSendPlaceJeton_creature() throws TException, InterruptedException {
//Start phase 1
genericGame.startNextPhase();
//Start postphase 1
genericGame.startNextPhase();
//Start prephase 2
genericGame.startNextPhase();
//Start phase 2
genericGame.startNextPhase();
Response response = genericRoom.addRequest(new GamePlaceJetonsRequest(user1, Collections.singletonList(new PlacedJeton(JetonSymbol.CREATURE, Place.PLACE_ONE))));
assertTrue(response.state);
assertEquals("Request sent", response.getMessage());
Thread.sleep(10);
Response expected = new Response(true, "");
verify(user1).sendResponse(expected);
}
@Test
void testSendResist_traque() throws TException, InterruptedException {
//Start phase 1
genericGame.startNextPhase();
Response response = genericRoom.addRequest(new GameResistRequest(user2, 1));
assertTrue(response.state);
assertEquals("Request sent", response.getMessage());
Thread.sleep(10);
Response expected = new Response(true, "");
verify(user2).sendResponse(expected);
}
@Test
void testSendGiveUp_traque() throws TException, InterruptedException {
//Start phase 1
genericGame.startNextPhase();
Response response = genericRoom.addRequest(new GameGiveUpRequest(user2));
assertTrue(response.state);
assertEquals("Request sent", response.getMessage());
Thread.sleep(10);
Response expected = new Response(true, "");
verify(user2).sendResponse(expected);
}
@Test
void testAskAction() throws TException {
Action action = new ActionTargetPlayer(2);
TAskAction tAskAction = new TAskAction("TARGET_PLAYER", "", new ArrayList<>());
TAction tAction = new TAction("TARGET_PLAYER", Collections.singletonList(new TPair("PLAYER", "2")));
when(user1.askAction(any(TAskAction.class))).thenReturn(tAction);
assertEquals(action, genericRoom.askAction(1, tAskAction));
verify(user1).askAction(tAskAction);
}
@Test
void testSendAction() throws TException {
TAskAction tAskAction = new TAskAction("SHOW_CARD", "", new ArrayList<>());
genericRoom.sendAction(1, tAskAction);
verify(user1).sendAction(tAskAction);
}
@Test
void testSendFirstRoundStart() throws TException {
verify(user1, atLeastOnce()).sendFirstRoundStart();
verify(user2, atLeastOnce()).sendFirstRoundStart();
}
@Test
void testSendStartPhase() throws TException {
genericGame.startNextPhase();
verify(user1, atLeastOnce()).sendStartPhase(eq(new TPhase(Phase.PHASE_1.toString())), any(TDescription.class));
verify(user2, atLeastOnce()).sendStartPhase(eq(new TPhase(Phase.PHASE_1.toString())), any(TDescription.class));
}
@Test
void testSendDescription() throws TException {
TDescription tDescription = new TDescription(new TPlayerTeam("TEST"), null, null, null, null, null, null, null);
genericRoom.sendDescription