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.

60 lines
2.0 KiB

3 years ago
3 years ago
  1. package de.fd.fh.server.game;
  2. import de.fd.fh.server.user.UserId;
  3. import dev.morphia.Key;
  4. import org.junit.jupiter.api.Test;
  5. import org.mockito.ArgumentCaptor;
  6. import static org.junit.jupiter.api.Assertions.*;
  7. import static org.mockito.ArgumentMatchers.any;
  8. import static org.mockito.BDDMockito.then;
  9. import static org.mockito.Mockito.*;
  10. class GameServiceTest
  11. {
  12. @Test
  13. void testCreateGame()
  14. {
  15. final GameRepository repository = when(mock(GameRepository.class).save(any()))
  16. .thenReturn(new Key<>(Game.class, "testCollection", GameId.of("98765")))
  17. .getMock();
  18. final Game game = new Game();
  19. final GameId result = new GameService(repository).createGame(game);
  20. assertNotNull(result);
  21. assertEquals("98765", result.getIdentifier());
  22. }
  23. @Test
  24. void given_twoUserIds_when_initGame_should_initGame()
  25. {
  26. final UserId blackPlayer = UserId.of("12345");
  27. final UserId whitePlayer = UserId.of("98765");
  28. final GameRepository repository = when(mock(GameRepository.class).save(any()))
  29. .thenReturn(new Key<>(Game.class, "testCollection", GameId.of("4242")))
  30. .getMock();
  31. final ArgumentCaptor<Game> captor = ArgumentCaptor.forClass(Game.class);
  32. final GameId result = new GameService(repository).createGame(blackPlayer, whitePlayer);
  33. verify(repository).save(captor.capture());
  34. assertEquals("4242", result.getIdentifier());
  35. assertNotNull(captor.getValue());
  36. final Game savedGame = captor.getValue();
  37. assertNotNull(savedGame.getId());
  38. assertEquals("12345", savedGame.getBlackPlayer().getIdentifier());
  39. assertEquals("98765", savedGame.getWhitePlayer().getIdentifier());
  40. assertEquals("98765", savedGame.getCurrentPlayer().getIdentifier());
  41. assertEquals(64, savedGame.getFields().size());
  42. then(repository).should().save(any(Game.class));
  43. then(repository).shouldHaveNoMoreInteractions();
  44. }
  45. }