From 5589c8ebe6a14f195ad3397fc92e49b816cf3d01 Mon Sep 17 00:00:00 2001 From: djmil Date: Tue, 3 Oct 2023 10:01:58 +0200 Subject: [PATCH] tests for gameStateList() and gameStateGet() --- .../cordaclient/GameProposalTests.java | 4 +- .../cordaclient/GameStateTests.java | 41 +++++++++++++++++-- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/backend/src/test/java/djmil/cordacheckers/cordaclient/GameProposalTests.java b/backend/src/test/java/djmil/cordacheckers/cordaclient/GameProposalTests.java index d363802..c2b5fc4 100644 --- a/backend/src/test/java/djmil/cordacheckers/cordaclient/GameProposalTests.java +++ b/backend/src/test/java/djmil/cordacheckers/cordaclient/GameProposalTests.java @@ -41,7 +41,7 @@ public class GameProposalTests { assertThat(issuerGameView.opponentName()).isEqualToIgnoringCase(acquier); assertThat(issuerGameView.opponentColor()).isEqualByComparingTo(acquierColor); assertThat(issuerGameView.board()).containsAllEntriesOf(GameState.defaultGameBoard); - assertThat(issuerGameView.previousMove()).isEmpty(); + assertThat(issuerGameView.previousMove()).isNull(); assertThat(issuerGameView.moveNumber() == 0); assertThat(issuerGameView.message()).isEqualToIgnoringCase(message); assertThat(issuerGameView.uuid()).isNotNull(); @@ -52,7 +52,7 @@ public class GameProposalTests { GameState acquierGameView = assertDoesNotThrow( () -> { return cordaClient.gameStateList(hiAcquier) .stream() - .filter(gameView -> gameView.uuid().equals(issuerGameView.uuid())) + .filter(gameView -> gameView.uuid().compareTo(issuerGameView.uuid()) == 0) .reduce((a, b) -> {throw new IllegalStateException("Duplicate");}) .get(); }); diff --git a/backend/src/test/java/djmil/cordacheckers/cordaclient/GameStateTests.java b/backend/src/test/java/djmil/cordacheckers/cordaclient/GameStateTests.java index a9151a6..258ff60 100644 --- a/backend/src/test/java/djmil/cordacheckers/cordaclient/GameStateTests.java +++ b/backend/src/test/java/djmil/cordacheckers/cordaclient/GameStateTests.java @@ -1,5 +1,8 @@ package djmil.cordacheckers.cordaclient; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + import java.util.List; import java.util.UUID; @@ -8,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import djmil.cordacheckers.cordaclient.dao.GameState; +import djmil.cordacheckers.cordaclient.dao.Stone.Color; import djmil.cordacheckers.user.HoldingIdentityResolver; @SpringBootTest @@ -24,21 +28,50 @@ public class GameStateTests { @Test void testList() { final var hiPlayer1 = holdingIdentityResolver.getByUsername(player1); + final var hiPlayer2 = holdingIdentityResolver.getByUsername(player2); - List gsList = cordaClient.gameStateList(hiPlayer1); + final GameState p1GameView = cordaClient.gameProposalCreate( + hiPlayer1, hiPlayer2, Color.BLACK, "GameState List test"); - System.out.println(gsList); + final UUID gameUuid = p1GameView.uuid(); + + /* + * Both players shall be able to find newly created GameProposal aka GameList + */ + final List p1GameList = cordaClient.gameStateList(hiPlayer1); + final List p2GameList = cordaClient.gameStateList(hiPlayer2); + + assertThat(findGameStateById(p1GameList, gameUuid)).isNotNull(); + assertThat(findGameStateById(p2GameList, gameUuid)).isNotNull(); } @Test void testGet() { + final var hiPlayer1 = holdingIdentityResolver.getByUsername(player1); final var hiPlayer2 = holdingIdentityResolver.getByUsername(player2); - GameState gameStateView = cordaClient.gameStateGet(hiPlayer2, + assertThatThrownBy(() -> { // trying to get GameState with nonexisting UUID + cordaClient.gameStateGet(hiPlayer2, UUID.fromString("cf357d0a-8f64-4599-b9b5-d263163812d4") ); + }); - System.out.println(gameStateView); + final GameState p1GameView = cordaClient.gameProposalCreate( + hiPlayer1, hiPlayer2, Color.WHITE, "GameState Get test"); + + final UUID gameUuid = p1GameView.uuid(); + + final GameState p2GameView = cordaClient.gameStateGet(hiPlayer2, gameUuid); + + assertThat(p1GameView.message()).isEqualTo(p2GameView.message()); + } + + GameState findGameStateById(List gameStateList, UUID gameUuid) { + return gameStateList + .stream() + .filter(gameView -> gameView.uuid().compareTo(gameUuid) == 0) + .reduce((a, b) -> {throw new IllegalStateException("Duplicate");}) + .get(); } }