package ch.zhaw.catan; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import java.awt.Point; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertThrows; /** * @class SiedlerGameTest *
* contains all of the test cases for SiedlerGame class. * The Test cases are categorized into * - Positive TestCases * Tests the methods of SiedlerGame with the intended values *
* - Negative TestCases * Tests the methods of SiedlerGame with values, that results to errors and failure *
* - SystemTestCases * Tests the coordination of all methods and simulates a game with programmed moves * Checks if the methods are being executed and the values are set correctly */ public class SiedlerGameTest { private final static int DEFAULT_WINPOINTS = 5; private final static int DEFAULT_PLAYERAMOUNT = 4; /** * Property START_SETTLEMENT_POSITIONS *
* Lists all positions of Settlement for every faction in the initialization phase.
* Each faction is assigned to a list of specific settlements, which represents as a list of points
*/
private final static Map
* Lists all endpoints of roads for every faction in the initialization phase.
* Each faction is assigned to a specific Road, which represents as a tuple of 2 points
*/
private final static Map
* Method does not halve a resource, when the amount is below 7.
* Player has gotten 8 wool resources and throws a 7 with the dice
*
* expected: throwDice with a 7 will halve wool resource, the amount should be 4
*/
@Test
@DisplayName("Test Throw Dice with value 7, expected half the resources")
public void TestThrowDiceWith7() {
SiedlerGame game = gameAfterSetupPhase();
throwDiceSeveralTimes(game, 8, 1);
throwDiceSeveralTimes(game, 7, 1);
Assertions.assertEquals(2, game.getCurrentPlayerResource().get(Config.Resource.WOOL));
throwDiceSeveralTimes(game, 8, 1);
throwDiceSeveralTimes(game, 8, 1);
throwDiceSeveralTimes(game, 8, 1);
Assertions.assertEquals(8, game.getCurrentPlayerResource().get(Config.Resource.WOOL));
throwDiceSeveralTimes(game, 7, 1);
Assertions.assertEquals(4, game.getCurrentPlayerResource().get(Config.Resource.WOOL));
}
}
/**
* @Class NegAtiveTestcases
*
* contains all negative test cases
*/
@Nested
@DisplayName("Negative test cases")
class NegativeTestcases {
/**
* Tests if siedlergame will start with one or five players.
* 1 Player is below the minimum
* 5 Players are above the maximum
*/
@ParameterizedTest(name = "Test with {arguments} players")
@ValueSource(ints = {1, 5})
@DisplayName("Starting Siedler game with one player or 5 players, expects fail")
public void startSiedlerGameWithOnePlayerorMoreThanMaximum(int playerAmount) {
Exception exc = assertThrows(IllegalArgumentException.class, () -> {
new SiedlerGame(DEFAULT_WINPOINTS, playerAmount);
});
Assertions.assertEquals(IllegalArgumentException.class, exc.getClass());
}
/**
* Tests if the game crashes when passing an amount of points, that is below the minimum amount
*/
@Test
@DisplayName("Starting Siedler game with one player, expects fail")
public void startSiedlerGameWithLowerThanMinimumWinPoints() {
Exception exc = assertThrows(IllegalArgumentException.class, () -> {
SiedlerGame game = new SiedlerGame(1, 4);
});
Assertions.assertEquals(IllegalArgumentException.class, exc.getClass());
}
/**
* This testcase will test, if the methods placeInitialRoad and placeInitialSettlement are overwritting
* already occupied positions if the same faction or any other faction are calling the method with the same positions
*
* Expected: Method placeInitialRoad placeInitialSettlement should return false, independent of the current faction playing
*/
@Test
@DisplayName("Test placeInitialRoad and placeInitialSettlement with already occupied positions")
public void testPlaceInitialStructuresInAlreadyOccupiedPositions() {
SiedlerGame game = startGame();
Point settlementPoint = START_SETTLEMENT_POSITIONS.get(Config.Faction.BLUE).get(1);
Assertions.assertTrue(game.placeInitialSettlement(settlementPoint, false));
Assertions.assertFalse(game.placeInitialSettlement(settlementPoint, false));
Tuple
* This class simulates a running game and tests multiple sequences of this game.
*/
@Nested
@DisplayName("System test cases")
class SystemTestcases {
/**
* TestS if the players can place initial settlements and roads
*/
@Test
@DisplayName("2 Players initialize a settlement and position")
public void TestGameInSetupPhase() {
SiedlerGame game = startGame();
Assertions.assertTrue(game.placeInitialSettlement(START_SETTLEMENT_POSITIONS.get(game.getCurrentPlayerFaction()).get(0), false));
Tuple