Content of file GameHistoryCollectionImplTest.java
package de.fhdw.gaming.ipspiel21.dilemmaOriginal.domain;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import de.fhdw.gaming.core.domain.GameException;
import de.fhdw.gaming.ipspiel21.dilemmaOriginal.domain.DilemmaGame;
 | No need to import a type that lives in the same package. | |
import de.fhdw.gaming.ipspiel21.dilemmaOriginal.domain.DilemmaGameBuilder;
import de.fhdw.gaming.ipspiel21.dilemmaOriginal.domain.impl.DilemmaGameBuilderImpl;
import de.fhdw.gaming.ipspiel21.dilemmaOriginal.moves.impl.DilemmaDefaultMoveFactory;
import de.fhdw.gaming.ipspiel21.dilemmaOriginal.strategy.DilemmaAlwaysConfessStrategy;
import de.fhdw.gaming.ipspiel21.dilemmaOriginal.strategy.DilemmaStaySilentStrategyFactory;
import de.fhdw.gaming.ipspiel21.evolution.EvolutionPlayer;
import de.fhdw.gaming.ipspiel21.evolution.GameHistory;
import de.fhdw.gaming.ipspiel21.evolution.GameHistoryCollection;
import de.fhdw.gaming.ipspiel21.evolution.GameHistoryCollectionImpl;
import de.fhdw.gaming.ipspiel21.evolution.GameHistoryImpl;
/**
* Tests @GameHistoryCollection.
*/
class GameHistoryCollectionImplTest {
/**
* Game Builder.
*/
private DilemmaGameBuilder gameBuilder;
/**
* Creates a new builder for each test.
*/
@BeforeEach
public void initTest() {
this.gameBuilder = new DilemmaGameBuilderImpl();
}
/**
* Tests numberOfPlayedGames.
*/
@Test
public void numberOfPlayedGamesTest() {
try {
final int gameId = 1;
final String player1 = "Torben";
final String player2 = "Flo";
final DilemmaStaySilentStrategyFactory silentFactory = new DilemmaStaySilentStrategyFactory();
this.gameBuilder.addPlayerBuilder(
this.gameBuilder.createPlayerBuilder().defaultPlayerBuilder().changeName(player1),
new DilemmaAlwaysConfessStrategy(new DilemmaDefaultMoveFactory()));
this.gameBuilder.addPlayerBuilder(
this.gameBuilder.createPlayerBuilder().defaultPlayerBuilder().changeName(player2),
silentFactory.create(new DilemmaDefaultMoveFactory()));
try (DilemmaGame game = this.gameBuilder.build(gameId)) {
final EvolutionPlayer evplayer1 = game.getPlayers().get(player1);
final List<GameHistory> gameHistoryList = new ArrayList<>();
final GameHistoryCollection gameHistoryCollection = new GameHistoryCollectionImpl(gameHistoryList);
evplayer1.setGameHistoryCollection(gameHistoryCollection);
assertThat(evplayer1.getGameHistoryCollection().numberOfPlayedGames(),
is(equalTo(0)));
final GameHistory gameHistory1 = new GameHistoryImpl(1);
gameHistoryCollection.addGameHistory(gameHistory1);
evplayer1.setGameHistoryCollection(gameHistoryCollection);
assertThat(evplayer1.getGameHistoryCollection().numberOfPlayedGames(),
is(equalTo(0)));
evplayer1.getGameHistoryCollection().getGameHistories().get(0).setOutcome(false,
Optional.of(Double.valueOf(15)));
assertThat(evplayer1.getGameHistoryCollection().numberOfPlayedGames(),
is(equalTo(1)));
final GameHistory gameHistory2 = new GameHistoryImpl(2);
gameHistoryCollection.addGameHistory(gameHistory2);
evplayer1.setGameHistoryCollection(gameHistoryCollection);
assertThat(evplayer1.getGameHistoryCollection().numberOfPlayedGames(),
is(equalTo(1)));
evplayer1.getGameHistoryCollection().getGameHistories().get(0).setOutcome(false,
Optional.of(Double.valueOf(10)));
assertThat(evplayer1.getGameHistoryCollection().numberOfPlayedGames(),
is(equalTo(2)));
}
} catch (GameException | InterruptedException e) {
fail(e.getMessage());
}
}
/**
* Tests the getSpecificGameHistory.
*/
@Test
public void specificGameHistoryTest() {
try {
final int gameId = 1;
final String player1 = "Torben";
final String player2 = "Flo";
final DilemmaStaySilentStrategyFactory silentFactory = new DilemmaStaySilentStrategyFactory();
this.gameBuilder.addPlayerBuilder(
this.gameBuilder.createPlayerBuilder().defaultPlayerBuilder().changeName(player1),
new DilemmaAlwaysConfessStrategy(new DilemmaDefaultMoveFactory()));
this.gameBuilder.addPlayerBuilder(
this.gameBuilder.createPlayerBuilder().defaultPlayerBuilder().changeName(player2),
silentFactory.create(new DilemmaDefaultMoveFactory()));
try (DilemmaGame game = this.gameBuilder.build(gameId)) {
final EvolutionPlayer evplayer1 = game.getPlayers().get(player1);
final List<GameHistory> gameHistoryList = new ArrayList<>();
final GameHistoryCollection gameHistoryCollection = new GameHistoryCollectionImpl(gameHistoryList);
final GameHistory gameHistory1 = new GameHistoryImpl(1);
gameHistoryCollection.addGameHistory(gameHistory1);
final GameHistory gameHistory2 = new GameHistoryImpl(2);
gameHistoryCollection.addGameHistory(gameHistory2);
final GameHistory gameHistory3 = new GameHistoryImpl(3);
gameHistoryCollection.addGameHistory(gameHistory3);
evplayer1.setGameHistoryCollection(gameHistoryCollection);
evplayer1.getGameHistoryCollection().getGameHistories().get(1).setOutcome(false,
Optional.of(Double.valueOf(15)));
assertThat(evplayer1.getGameHistoryCollection().getSpecificGameHistory(0).getGameId(),
is(equalTo(2)));
evplayer1.getGameHistoryCollection().getGameHistories().get(0).setOutcome(false,
Optional.of(Double.valueOf(10)));
assertThat(evplayer1.getGameHistoryCollection().getSpecificGameHistory(0).getGameId(),
is(equalTo(3)));
}
} catch (GameException | InterruptedException e) {
fail(e.getMessage());
}
}
/**
* Tests the addGameHistory.
*/
@Test
public void addGameHistoryTest() {
try {
final int gameId = 1;
final String player1 = "Torben";
final String player2 = "Flo";
final DilemmaStaySilentStrategyFactory silentFactory = new DilemmaStaySilentStrategyFactory();
this.gameBuilder.addPlayerBuilder(
this.gameBuilder.createPlayerBuilder().defaultPlayerBuilder().changeName(player1),
new DilemmaAlwaysConfessStrategy(new DilemmaDefaultMoveFactory()));
this.gameBuilder.addPlayerBuilder(
this.gameBuilder.createPlayerBuilder().defaultPlayerBuilder().changeName(player2),
silentFactory.create(new DilemmaDefaultMoveFactory()));
try (DilemmaGame game = this.gameBuilder.build(gameId)) {
final EvolutionPlayer evplayer1 = game.getPlayers().get(player1);
final List<GameHistory> gameHistoryList = new ArrayList<>();
final GameHistoryCollection gameHistoryCollection = new GameHistoryCollectionImpl(gameHistoryList);
final GameHistory gameHistory1 = new GameHistoryImpl(1);
gameHistoryCollection.addGameHistory(gameHistory1);
final GameHistory gameHistory2 = new GameHistoryImpl(2);
gameHistoryCollection.addGameHistory(gameHistory2);
final GameHistory gameHistory3 = new GameHistoryImpl(3);
gameHistoryCollection.addGameHistory(gameHistory3);
evplayer1.setGameHistoryCollection(gameHistoryCollection);
assertThat(evplayer1.getGameHistoryCollection().getGameHistories().get(0).getGameId(),
is(equalTo(3)));
assertThat(evplayer1.getGameHistoryCollection().getGameHistories().get(1).getGameId(),
is(equalTo(2)));
assertThat(evplayer1.getGameHistoryCollection().getGameHistories().get(2).getGameId(),
is(equalTo(1)));
}
} catch (GameException | InterruptedException e) {
fail(e.getMessage());
}
}
}