Commit ee1a6040 authored by Corentin Guillevic's avatar Corentin Guillevic
Browse files

Add method equals to GameXXXRequest ; Add test for RoomServiceController

parent 2307ed0e
......@@ -164,7 +164,10 @@ public class Room {
}
public Response addRequest(GameRequest request) {
if(requests.offer(request)) {
if(!userIsInRoom(request.getUser())) {
return new Response(false, "You must be in the room");
}
else if(requests.offer(request)) {
return new Response(true, "Request sent");
}
else {
......@@ -342,4 +345,8 @@ public class Room {
return new Pair<>(inGameId, name);
}
private boolean userIsInRoom(User user) {
return players.containsKey(user.getId());
}
}
......@@ -25,6 +25,25 @@ public class RoomServiceController implements RoomService {
private final Map<String, Room> rooms = new HashMap<>();
private final Map<User, String> players = new HashMap<>();
public Map<String, Room> getRooms() {
return rooms;
}
public Map<User, String> getPlayers() {
return players;
}
public boolean addRoom(Room room) {
String roomId = room.getRoomId();
if(rooms.containsKey(roomId)) {
return false;
}
else{
rooms.put(roomId, room);
return true;
}
}
@Override
public String createRoom(User user) throws InvalidOperationException {
if(players.containsKey(user)){
......
......@@ -4,18 +4,13 @@ import fr.univnantes.alma.server.game.item.Phase;
import fr.univnantes.alma.server.user.User;
public class GameFinishPhaseRequest extends GameRequest{
private User user;
private Phase phase;
public GameFinishPhaseRequest(User user, Phase phase) {
this.user = user;
super(user);
this.phase = phase;
}
public User getUser() {
return user;
}
public Phase getPhase() {
return phase;
}
......@@ -24,4 +19,13 @@ public class GameFinishPhaseRequest extends GameRequest{
public GameRequestType getType() {
return GameRequestType.FINISH_PHASE;
}
@Override
public boolean equals(Object obj) {
if(!super.equals(obj)) {
return false;
}
GameFinishPhaseRequest gameFinishPhaseRequest = (GameFinishPhaseRequest) obj;
return phase.equals(gameFinishPhaseRequest.getPhase());
}
}
......@@ -3,14 +3,9 @@ package fr.univnantes.alma.server.game.utilitary.request;
import fr.univnantes.alma.server.user.User;
public class GameGiveUpRequest extends GameRequest {
private User user;
public GameGiveUpRequest(User user) {
this.user = user;
}
public User getUser() {
return user;
super(user);
}
@Override
......
......@@ -4,18 +4,13 @@ import fr.univnantes.alma.server.user.User;
public class GameJoinRequest extends GameRequest {
private User user;
private int inGameId;
public GameJoinRequest(User user, int id){
this.user = user;
super(user);
this.inGameId = id;
}
public User getUser() {
return user;
}
public int getInGameId() {
return inGameId;
}
......@@ -24,4 +19,13 @@ public class GameJoinRequest extends GameRequest {
public GameRequestType getType() {
return GameRequestType.JOIN;
}
@Override
public boolean equals(Object obj) {
if(!super.equals(obj)) {
return false;
}
GameJoinRequest gameJoinRequest = (GameJoinRequest) obj;
return inGameId == gameJoinRequest.getInGameId();
}
}
......@@ -6,18 +6,13 @@ import fr.univnantes.alma.server.user.User;
import java.util.List;
public class GamePlaceJetonsRequest extends GameRequest{
private User user;
private List<PlacedJeton> jetonList;
public GamePlaceJetonsRequest(User user, List<PlacedJeton> jetonList) {
this.user = user;
super(user);
this.jetonList = jetonList;
}
public User getUser() {
return user;
}
public List<PlacedJeton> getJetonList() {
return jetonList;
}
......@@ -26,4 +21,13 @@ public class GamePlaceJetonsRequest extends GameRequest{
public GameRequestType getType() {
return GameRequestType.PLACE_JETONS;
}
@Override
public boolean equals(Object obj) {
if(!super.equals(obj)) {
return false;
}
GamePlaceJetonsRequest gamePlaceJetonsRequest = (GamePlaceJetonsRequest) obj;
return jetonList.equals(gamePlaceJetonsRequest.getJetonList());
}
}
......@@ -7,18 +7,13 @@ import fr.univnantes.alma.server.user.User;
import java.util.List;
public class GamePlayCardsRequest extends GameRequest {
private User user;
private List<Card> cards;
public GamePlayCardsRequest(User user, List<Card> cards) {
this.user = user;
super(user);
this.cards = cards;
}
public User getUser() {
return user;
}
public List<Card> getCards() {
return cards;
}
......@@ -27,4 +22,13 @@ public class GamePlayCardsRequest extends GameRequest {
public GameRequest.GameRequestType getType() {
return GameRequest.GameRequestType.PLAY_CARDS;
}
@Override
public boolean equals(Object obj) {
if(!super.equals(obj)) {
return false;
}
GamePlayCardsRequest gamePlayCardsRequest = (GamePlayCardsRequest) obj;
return cards.equals(gamePlayCardsRequest.getCards());
}
}
package fr.univnantes.alma.server.game.utilitary.request;
public abstract class GameRequest {
import fr.univnantes.alma.server.game.item.card.Card;
import fr.univnantes.alma.server.user.User;
public abstract class GameRequest {
public enum GameRequestType{JOIN, FINISH_PHASE, PLAY_CARDS, PLACE_JETONS, RESIST, GIVE_UP}
protected User user;
protected GameRequest(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public abstract GameRequestType getType();
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
GameRequest gameRequest = (GameRequest) obj;
return gameRequest.getUser().equals(this.user) && getType().equals(gameRequest.getType());
}
}
......@@ -3,18 +3,13 @@ package fr.univnantes.alma.server.game.utilitary.request;
import fr.univnantes.alma.server.user.User;
public class GameResistRequest extends GameRequest {
private User user;
private int number;
public GameResistRequest(User user, int number) {
this.user = user;
super(user);
this.number = number;
}
public User getUser() {
return user;
}
public int getNumber() {
return number;
}
......@@ -23,4 +18,13 @@ public class GameResistRequest extends GameRequest {
public GameRequestType getType() {
return GameRequestType.RESIST;
}
@Override
public boolean equals(Object obj) {
if(!super.equals(obj)) {
return false;
}
GameResistRequest gameResistRequest = (GameResistRequest) obj;
return number == gameResistRequest.getNumber();
}
}
package fr.univnantes.alma.server.game;
import fr.univnantes.alma.server.game.item.Phase;
import fr.univnantes.alma.server.game.item.board.Board;
import fr.univnantes.alma.server.game.utilitary.request.*;
import fr.univnantes.alma.server.user.User;
import fr.univnantes.alma.thrift.GameNotFound;
import fr.univnantes.alma.thrift.InvalidOperationException;
import fr.univnantes.alma.thrift.Response;
import org.checkerframework.checker.units.qual.A;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class RoomServiceControllerTest {
Room room;
RoomServiceController roomServiceController;
User user1;
User user2;
User user3;
@BeforeEach
void setup() {
user1 = mock(User.class);
when(user1.getId()).thenReturn("1");
user2 = mock(User.class);
when(user2.getId()).thenReturn("2");
user3 = mock(User.class);
room = mock(Room.class);
when(room.getRoomId()).thenReturn("900");
when(room.join(user1)).thenReturn(new Response(true, ""));
roomServiceController = new RoomServiceController();
roomServiceController.addRoom(room);
}
@Test
void testAddRoom_alreadyExist() throws InvalidOperationException {
Room room = mock(Room.class);
when(room.getRoomId()).thenReturn("900");
assertFalse(roomServiceController.addRoom(room));
}
@Test
void testCreateRoom() throws InvalidOperationException {
String roomId = roomServiceController.createRoom(user1);
Map<String, Room> stringRoomMap = roomServiceController.getRooms();
Map<User, String> userStringMap = roomServiceController.getPlayers();
assertTrue(stringRoomMap.containsKey(roomId));
assertTrue(userStringMap.containsKey(user1));
Room room = stringRoomMap.get(roomId);
assertTrue(room.isCreator(user1));
}
@Test
void testCreateRoom_userAlreadyInAGame() throws InvalidOperationException {
roomServiceController.createRoom(user1);
assertThrows(InvalidOperationException.class, () -> roomServiceController.createRoom(user1));
}
@Test
void testJoinRoom() throws GameNotFound {
roomServiceController.joinRoom(user1, "900");
Map<User, String> userStringMap = roomServiceController.getPlayers();
verify(room).join(user1);
assertTrue(userStringMap.containsKey(user1));
}
@Test
void testJoinRoom_playerAlreadyInARoom() throws InvalidOperationException, GameNotFound {
roomServiceController.createRoom(user1);
Response response = new Response(false, "Player already in a room");
assertEquals(response, roomServiceController.joinRoom(user1, "900"));
}
@Test
void testJoinRoom_inexistantGame() {
assertThrows(GameNotFound.class, () -> roomServiceController.joinRoom(user1, "800"));
}
@Test
void testStartGame() throws GameNotFound {
roomServiceController.joinRoom(user1, "900");
roomServiceController.startGame(user1, 1, null, null);
verify(room).start(user1, 1, null, null);
}
@Test
void testStartGame_playerNotInARoom() throws GameNotFound {
Response response = roomServiceController.startGame(user1, 1, null, null);
assertEquals(new Response(false, "Player is not in a room"), response);
}
@Test
void testGetGameDescription() throws GameNotFound, InvalidOperationException {
roomServiceController.joinRoom(user1, "900");
roomServiceController.getGameDescription(user1);
verify(room).getGameDescription(user1);
}
@Test
void testGetGameDescription_playerNotInARoom() {
assertThrows(InvalidOperationException.class, () -> roomServiceController.getGameDescription(user1));
}
@Test
void testSendFinishPhase() throws GameNotFound, InvalidOperationException {
roomServiceController.joinRoom(user1, "900");
roomServiceController.sendFinishPhase(user1, Phase.PREPHASE_1);
verify(room).addRequest(new GameFinishPhaseRequest(user1, Phase.PREPHASE_1));
}
@Test
void testSendFinishPhase_playerNotInARoom() {
assertThrows(InvalidOperationException.class, () -> roomServiceController.sendFinishPhase(user1, Phase.PREPHASE_1));
}
@Test
void testSendPlayCards() throws GameNotFound, InvalidOperationException {
roomServiceController.joinRoom(user1, "900");
roomServiceController.sendPlayCards(user1, new ArrayList<>());
verify(room).addRequest(new GamePlayCardsRequest(user1, new ArrayList<>()));
}
@Test
void testSendPlayCards_playerNotInARoom() {
assertThrows(InvalidOperationException.class, () -> roomServiceController.sendPlayCards(user1, new ArrayList<>()));
}
@Test
void testSendPlaceJetons() throws GameNotFound, InvalidOperationException {
roomServiceController.joinRoom(user1, "900");
roomServiceController.sendPlaceJetons(user1, new ArrayList<>());
verify(room).addRequest(new GamePlaceJetonsRequest(user1, new ArrayList<>()));
}
@Test
void testSendPlaceJetons_playerNotInARoom() {
assertThrows(InvalidOperationException.class, () -> roomServiceController.sendPlaceJetons(user1, new ArrayList<>()));
}
@Test
void testSendResist() throws GameNotFound, InvalidOperationException {
roomServiceController.joinRoom(user1, "900");
roomServiceController.sendResist(user1, 1);
verify(room).addRequest(new GameResistRequest(user1, 1));
}
@Test
void testSendResist_playerNotInARoom() {
assertThrows(InvalidOperationException.class, () -> roomServiceController.sendResist(user1, 1));
}
@Test
void testSendGiveUp() throws GameNotFound, InvalidOperationException {
roomServiceController.joinRoom(user1, "900");
roomServiceController.sendGiveUp(user1);
verify(room).addRequest(new GameGiveUpRequest(user1));
}
@Test
void testSendGiveUp_playerNotInARoom() {
assertThrows(InvalidOperationException.class, () -> roomServiceController.sendGiveUp(user1));
}
}
\ No newline at end of file
......@@ -72,19 +72,19 @@ class RoomTest {
user1 = mock(User.class);
when(user1.getId()).thenReturn("1");
user2 = mock(User.class);
when(user1.getId()).thenReturn("2");
when(user2.getId()).thenReturn("2");
user3 = mock(User.class);
when(user1.getId()).thenReturn("3");
when(user3.getId()).thenReturn("3");
user4 = mock(User.class);
when(user1.getId()).thenReturn("4");
when(user4.getId()).thenReturn("4");
user5 = mock(User.class);
when(user1.getId()).thenReturn("5");
when(user5.getId()).thenReturn("5");
user6 = mock(User.class);
when(user1.getId()).thenReturn("6");
when(user6.getId()).thenReturn("6");
user7 = mock(User.class);
when(user1.getId()).thenReturn("7");
when(user7.getId()).thenReturn("7");
user8 = mock(User.class);
when(user1.getId()).thenReturn("8");
when(user8.getId()).thenReturn("8");
placeCards = new ArrayList();
placeCards.add(antre);
......@@ -291,6 +291,13 @@ class RoomTest {
assertEquals(tCreatureExpect, tCreature);
}
@Test
void testAddRequest_userNotInTheRoom() {
Response response = genericRoom.addRequest(new GameFinishPhaseRequest(user3, Phase.PREPHASE_1));
assertFalse(response.state);
assertEquals("You must be in the room", response.getMessage());
}
@Test
void testSendFinishPhase_requestAccepted() throws TException, InterruptedException {
Response response = genericRoom.addRequest(new GameFinishPhaseRequest(user1, Phase.PREPHASE_1));
......
......@@ -3,25 +3,5 @@ package fr.univnantes.alma.server.game.item.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
class PlaceCardTest {
/*
@Test
void testGetNumber() {
PlaceCard card = new PlaceCard("ID", "Name", "Description", PowerType.ANTRE, 1, "url", "rgba(255,255,255)");
assertEquals(1, card.getNumber());
}
@Test
void testEquals() {
PlaceCard card1 = new PlaceCard("ID", "Name", "Description", PowerType.ANTRE, 1, "url", "rgba(255,255,255)");
PlaceCard card2 = new PlaceCard("ID", "Name", "Description", PowerType.ANTRE, 1, "url", "rgba(255,255,255)");
assertEquals(card1, card2);
}
@Test
void getType() {
PlaceCard card1 = new PlaceCard("ID", "Name", "Description", PowerType.ANTRE, 1, "url", "rgba(255,255,255)");
assertEquals(Card.CardType.PLACE, card1.getType());
}
*/
}
\ No newline at end of file
package fr.univnantes.alma.server.game.item.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
class PlayerCardTest {
/*
@Test
void getPhase() {
PlayerCard card = new SurvivalCard("ID", "Name", "Description", PowerType.ACHARNEMENT, Phase.PHASE_1);
assertEquals(Phase.PHASE_1, card.getPhase());
}
*/
}
\ No newline at end of file
......@@ -3,12 +3,5 @@ package fr.univnantes.alma.server.game.item.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SurvivalCardTest {
/*
@Test
void getType() {
PlayerCard card = new SurvivalCard("ID", "Name", "Description", PowerType.ESQUIVE, Phase.PHASE_1);
assertEquals(Card.CardType.SURVIVAL, card.getType());
}
*/
}
\ No newline at end of file
......@@ -3,12 +3,5 @@ package fr.univnantes.alma.server.game.item.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
class TrackingCardTest {
/*
@Test
void getType() {
PlayerCard card = new TrackingCard("ID", "Name", "Description", PowerType.DOMINATION, Phase.PHASE_1);
assertEquals(Card.CardType.TRACKING, card.getType());
}
*/
}
\ No newline at end of file