You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.0 KiB

package de.fd.fh.server.game;
import de.fd.fh.server.user.UserId;
import dev.morphia.Key;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.*;
class GameServiceTest
{
@Test
void testCreateGame()
{
final GameRepository repository = when(mock(GameRepository.class).save(any()))
.thenReturn(new Key<>(Game.class, "testCollection", GameId.of("98765")))
.getMock();
final Game game = new Game();
final GameId result = new GameService(repository).createGame(game);
assertNotNull(result);
assertEquals("98765", result.getIdentifier());
}
@Test
void given_twoUserIds_when_initGame_should_initGame()
{
final UserId blackPlayer = UserId.of("12345");
final UserId whitePlayer = UserId.of("98765");
final GameRepository repository = when(mock(GameRepository.class).save(any()))
.thenReturn(new Key<>(Game.class, "testCollection", GameId.of("4242")))
.getMock();
final ArgumentCaptor<Game> captor = ArgumentCaptor.forClass(Game.class);
final GameId result = new GameService(repository).createGame(blackPlayer, whitePlayer);
verify(repository).save(captor.capture());
assertEquals("4242", result.getIdentifier());
assertNotNull(captor.getValue());
final Game savedGame = captor.getValue();
assertNotNull(savedGame.getId());
assertEquals("12345", savedGame.getBlackPlayer().getIdentifier());
assertEquals("98765", savedGame.getWhitePlayer().getIdentifier());
assertEquals("98765", savedGame.getCurrentPlayer().getIdentifier());
assertEquals(64, savedGame.getFields().size());
then(repository).should().save(any(Game.class));
then(repository).shouldHaveNoMoreInteractions();
}
}