gruppe06-hufflepuff-projekt.../test/ch/zhaw/catan/SiedlerGameTest.java

293 lines
12 KiB
Java
Raw Normal View History

2021-11-19 08:32:32 +01:00
package ch.zhaw.catan;
2021-12-10 20:15:48 +01:00
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;
2021-11-19 08:32:32 +01:00
2021-12-10 20:15:48 +01:00
import java.awt.Point;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
2021-12-10 20:15:48 +01:00
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertThrows;
2021-12-10 11:45:57 +01:00
/**
2021-12-10 22:26:00 +01:00
* SiedlerGameTest Class
*
* contains all the test cases for SiedlerGame class.
2021-12-10 11:45:57 +01:00
* The Test cases are categorized into
2021-12-10 20:15:48 +01:00
* - Positive TestCases
* Tests the methods of SiedlerGame with the intended values
2021-12-10 22:26:00 +01:00
*
2021-12-10 20:15:48 +01:00
* - Negative TestCases
* Tests the methods of SiedlerGame with values, that results to errors and failure
2021-12-10 22:26:00 +01:00
*
2021-12-10 20:15:48 +01:00
* - 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
2021-12-10 22:26:00 +01:00
* @author Michael Ziegler
2021-12-10 11:45:57 +01:00
*/
2021-11-19 08:32:32 +01:00
public class SiedlerGameTest {
private final static int DEFAULT_WINPOINTS = 5;
2021-12-10 22:26:00 +01:00
private final static int DEFAULT_PLAYER_AMOUNT = 4;
2021-11-19 08:32:32 +01:00
/**
* Property START_SETTLEMENT_POSITIONS
2021-12-10 22:26:00 +01:00
*
* 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<Config.Faction, ArrayList<Point>> START_SETTLEMENT_POSITIONS = Map.of(
2021-12-10 20:15:48 +01:00
Config.Faction.BLUE, new ArrayList<>(List.of(new Point(4, 4), new Point(5, 7))),
Config.Faction.RED, new ArrayList<>(List.of(new Point(10, 4), new Point(9, 7))),
Config.Faction.GREEN, new ArrayList<>(List.of(new Point(4, 18), new Point(5, 15))),
Config.Faction.YELLOW, new ArrayList<>(List.of(new Point(10, 18), new Point(9, 15)))
);
/**
2021-12-10 11:45:57 +01:00
* Property START_ROADS_POSITION
2021-12-10 22:26:00 +01:00
*
* 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<Config.Faction, ArrayList<Tuple<Point, Point>>> START_ROADS_POSITIONS = Map.of(
Config.Faction.BLUE, new ArrayList<>(List.of(
new Tuple<>(new Point(4, 4), new Point(4, 6)), new Tuple<>(new Point(4, 6), new Point(5, 7)))),
Config.Faction.RED, new ArrayList<>(List.of(
new Tuple<>(new Point(10, 4), new Point(10, 6)), new Tuple<>(new Point(10, 6), new Point(9, 7)))),
Config.Faction.GREEN, new ArrayList<>(List.of(
new Tuple<>(new Point(4, 18), new Point(4, 16)), new Tuple<>(new Point(4, 16), new Point(5, 15)))),
Config.Faction.YELLOW, new ArrayList<>(List.of(
new Tuple<>(new Point(10, 18), new Point(10, 16)), new Tuple<>(new Point(10, 16), new Point(9, 15))))
);
/**
* This test class contains all positive test cases.
*/
@Nested
@DisplayName("Positive test cases")
class PositiveTestcases {
/**
* This test will check, if the game initializes with all 4 factions.
* All factions should have no resources
*/
@ParameterizedTest(name = "Test with {arguments} players")
@ValueSource(ints = {2, 3, 4})
@DisplayName("Game initializing with different amount of players within the domain (Players have no resources), expected ok")
2021-12-10 22:26:00 +01:00
public void TestGameInitializationWithAllPlayerCounts(int playerAmount) {
SiedlerGame game = new SiedlerGame(DEFAULT_WINPOINTS, playerAmount);
2021-12-10 09:12:16 +01:00
2021-12-10 22:39:12 +01:00
for (int i = 0 ; i < game.getPlayerFactions().size(); i++) {
HashMap<Config.Resource, Integer> resources = game.getCurrentPlayerResource();
2021-12-10 09:12:16 +01:00
Assertions.assertEquals(0, resources.get(Config.Resource.BRICK));
Assertions.assertEquals(0, resources.get(Config.Resource.GRAIN));
Assertions.assertEquals(0, resources.get(Config.Resource.LUMBER));
Assertions.assertEquals(0, resources.get(Config.Resource.ORE));
Assertions.assertEquals(0, resources.get(Config.Resource.WOOL));
2021-12-10 09:12:16 +01:00
game.switchToNextPlayer();
2021-12-10 09:12:16 +01:00
}
}
/**
* Tests if the method throwDice halves the resources of the player
* 7 will be passed to the throwDice Method when called
2021-12-10 22:26:00 +01:00
*
* 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
2021-12-10 22:26:00 +01:00
*
* 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));
2021-12-10 22:37:24 +01:00
throwDiceSeveralTimes(game, 8, 3);
Assertions.assertEquals(8, game.getCurrentPlayerResource().get(Config.Resource.WOOL));
throwDiceSeveralTimes(game, 7, 1);
Assertions.assertEquals(4, game.getCurrentPlayerResource().get(Config.Resource.WOOL));
}
}
/**
2021-12-10 22:26:00 +01:00
* NegativeTestcases
*
2021-12-10 11:45:57 +01:00
* contains all negative test cases
*/
@Nested
@DisplayName("Negative test cases")
class NegativeTestcases {
/**
2021-12-10 22:26:00 +01:00
* Tests if siedler game 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")
2021-12-10 22:26:00 +01:00
public void startSiedlerGameWithOnePlayErrorMoreThanMaximum(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, () -> {
2021-12-10 22:39:12 +01:00
new SiedlerGame(1, 4);
});
Assertions.assertEquals(IllegalArgumentException.class, exc.getClass());
}
/**
2021-12-10 22:26:00 +01:00
* This testcase will test, if the methods placeInitialRoad and placeInitialSettlement are overwriting
* already occupied positions if the same faction or any other faction are calling the method with the same positions
2021-12-10 22:26:00 +01:00
*
* 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<Point, Point> roadEndpoints = START_ROADS_POSITIONS.get(Config.Faction.BLUE).get(1);
Assertions.assertTrue(game.placeInitialRoad(roadEndpoints.first, roadEndpoints.second));
Assertions.assertFalse(game.placeInitialRoad(roadEndpoints.first, roadEndpoints.second));
// Switch to other player to see if the occupied fields can be overwritten, when switched to the next player/faction
game.switchToNextPlayer();
Assertions.assertFalse(game.placeInitialRoad(roadEndpoints.first, roadEndpoints.second));
Assertions.assertFalse(game.placeInitialSettlement(settlementPoint, false));
}
}
/**
2021-12-10 22:26:00 +01:00
* SystemTestCases
*
* This class simulates a running game and tests multiple sequences of this game.
*/
@Nested
@DisplayName("System test cases")
class SystemTestcases {
/**
2021-12-10 22:26:00 +01:00
* 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<Point, Point> road1 = START_ROADS_POSITIONS.get(game.getCurrentPlayerFaction()).get(0);
Assertions.assertTrue(game.placeInitialRoad(road1.first, road1.second));
game.switchToNextPlayer();
Assertions.assertTrue(game.placeInitialSettlement(START_SETTLEMENT_POSITIONS.get(game.getCurrentPlayerFaction()).get(1), false));
Tuple<Point, Point> road2 = START_ROADS_POSITIONS.get(game.getCurrentPlayerFaction()).get(1);
Assertions.assertTrue(game.placeInitialRoad(road2.first, road2.second));
}
/**
2021-12-10 22:26:00 +01:00
* Tests, if the players can do all the actions in the building phase.
*/
@Test
public void TestGameAfterSetupPhase() {
SiedlerGame game = gameAfterSetupPhase();
2021-12-10 19:30:44 +01:00
throwDiceSeveralTimes(game, 8, 1);
Assertions.assertEquals(2, game.getCurrentPlayerResourceStock(Config.Resource.WOOL));
2021-12-10 11:45:57 +01:00
}
2021-12-10 19:30:44 +01:00
}
/**
* Initializes a game with the default values of winning points and amount of player
*
2021-12-10 22:26:00 +01:00
* @return SiedlerGame that was created with default winpoints and default player amount.
*/
private static SiedlerGame startGame() {
2021-12-10 22:26:00 +01:00
return new SiedlerGame(DEFAULT_WINPOINTS, DEFAULT_PLAYER_AMOUNT);
}
/**
* This method will set the game to the timeline after the setup.
* Players have already set each 2 Settlements and 2 Roads.
*
* @return SiedlerGame The game after the setup phase
*/
private static SiedlerGame gameAfterSetupPhase() {
SiedlerGame game = startGame();
for (int i = 0; i < game.getPlayerFactions().size(); i++) {
Config.Faction f = game.getCurrentPlayerFaction();
game.placeInitialSettlement(START_SETTLEMENT_POSITIONS.get(f).get(0), false);
Tuple<Point, Point> firstRoad = START_ROADS_POSITIONS.get(f).get(0);
game.placeInitialRoad(firstRoad.first, firstRoad.second);
game.switchToNextPlayer();
}
for (int i = 0; i < game.getPlayerFactions().size(); i++) {
game.switchToPreviousPlayer();
Config.Faction f = game.getCurrentPlayerFaction();
game.placeInitialSettlement(START_SETTLEMENT_POSITIONS.get(f).get(1), false);
Tuple<Point, Point> secondRoad = START_ROADS_POSITIONS.get(f).get(1);
game.placeInitialRoad(secondRoad.first, secondRoad.second);
}
return game;
}
/**
2021-12-10 22:26:00 +01:00
* This method will call the method "throwDice" multiple times and passes the set value of the dice.
*
2021-12-10 20:15:48 +01:00
* @param game Type SiedlerGame, the game itself in a running state
* @param dice Type int, Set Value of dice
* @param amountDiceThrows Type int, The amount of dice throws
*/
private static void throwDiceSeveralTimes(SiedlerGame game, int dice, int amountDiceThrows) {
for (int i = 0; i < amountDiceThrows; i++) {
game.throwDice(dice);
}
}
2021-11-19 08:32:32 +01:00
}