コード例 #1
0
ファイル: GamesControllerTest.cs プロジェクト: Liam44/Cygni
        public void Move_ModelNotNull_NameInvalid_MoveValid_IdValid_GameExists_Player1Exists_Player2Exists()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");
            JoinFromBody    joinFromBody = new JoinFromBody("Jack");
            MoveFromBody    moveFromBody = new MoveFromBody("Jill", "Rock");

            // Act
            HttpResponseMessage postResponse = controller.Post(postFromBody);

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

            HttpResponseMessage joinResponse = controller.Join(player1EnteredGame.GameId, joinFromBody);

            joinResponse.TryGetContentValue(out PlayerEnteredGame player2EnteredGame);

            HttpResponseMessage result = controller.Move(player1EnteredGame.GameId, moveFromBody);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out string description));
            Assert.AreEqual($"Player {moveFromBody.Name} is undefined for game {player1EnteredGame.GameId}.", description);
        }
コード例 #2
0
ファイル: GamesControllerTest.cs プロジェクト: Liam44/Cygni
        public void Move_ModelNotNull_NameValid_MoveInvalid_IdValid_GameExists_Player1Exists_Player2Exists()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");
            JoinFromBody    joinFromBody = new JoinFromBody("Jack");
            MoveFromBody    moveFromBody = new MoveFromBody(joinFromBody.Name, "Move");

            // Act
            HttpResponseMessage postResponse = controller.Post(postFromBody);

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

            HttpResponseMessage joinResponse = controller.Join(player1EnteredGame.GameId, joinFromBody);

            joinResponse.TryGetContentValue(out PlayerEnteredGame player2EnteredGame);

            HttpResponseMessage result = controller.Move(player1EnteredGame.GameId, moveFromBody);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out string description));
            Assert.AreEqual($"Move {moveFromBody.Move} isn't defined." +
                            Environment.NewLine +
                            "Authorized moves are: Rock, Paper or Scissors.", description);
        }
コード例 #3
0
ファイル: GamesControllerTest.cs プロジェクト: Liam44/Cygni
        public void Move_ModelNotNull_IdValid_GameDoesntExist()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            MoveFromBody    moveFromBody = new MoveFromBody(null, null);
            string          id           = "abcd";

            // Act
            HttpResponseMessage result = controller.Move(id, moveFromBody);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out string description));
            Assert.AreEqual($"Game {id} doesn't exist.", description);
        }
コード例 #4
0
ファイル: GamesControllerTest.cs プロジェクト: Liam44/Cygni
        public void Move_ModelNotNull_IdEmpty()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            MoveFromBody    moveFromBody = new MoveFromBody(null, null);
            string          id           = string.Empty;

            // Act
            HttpResponseMessage result = controller.Move(id, moveFromBody);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out string description));
            Assert.AreEqual("You must provide a game id.", description);
        }
コード例 #5
0
ファイル: GamesControllerTest.cs プロジェクト: Liam44/Cygni
        public void Move_ModelNull()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            MoveFromBody    moveFromBody = null;
            string          id           = null;

            // Act
            HttpResponseMessage result = controller.Move(id, moveFromBody);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out string description));
            Assert.AreEqual("You must provide a player name and a move.", description);
        }
コード例 #6
0
ファイル: GamesControllerTest.cs プロジェクト: Liam44/Cygni
        public void Move_ModelNotNull_NameValid_MoveValid_IdValid_GameExists_Player1Exists_Player2Exists_Player1HasPlayed_Player2HasntPlayed_DrawnGame()
        {
            // Arrange
            GamesController controller    = NewGamesController();
            PostFromBody    postFromBody  = new PostFromBody("John");
            JoinFromBody    joinFromBody  = new JoinFromBody("Jack");
            MoveFromBody    moveFromBody1 = new MoveFromBody(postFromBody.Name, "Scissors");
            MoveFromBody    moveFromBody2 = new MoveFromBody(joinFromBody.Name, moveFromBody1.Move);

            // Act
            HttpResponseMessage postResponse = controller.Post(postFromBody);

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

            controller.Move(player1EnteredGame.GameId, moveFromBody1);

            HttpResponseMessage joinResponse = controller.Join(player1EnteredGame.GameId, joinFromBody);

            joinResponse.TryGetContentValue(out PlayerEnteredGame player2EnteredGame);

            HttpResponseMessage getResultBefore = controller.Get(player1EnteredGame.GameId);

            getResultBefore.TryGetContentValue(out FilteredGame filteredGameBefore);

            HttpResponseMessage result = controller.Move(player1EnteredGame.GameId, moveFromBody2);

            HttpResponseMessage getResultAfter = controller.Get(player1EnteredGame.GameId);

            getResultAfter.TryGetContentValue(out FilteredGame filteredGameAfter);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.IsNull(result.Content);

            Assert.AreEqual($"Waiting for player 2 {moveFromBody2.Name} to play.", filteredGameBefore.Information);
            Assert.AreEqual(moveFromBody1.Name, filteredGameBefore.Player1.Name);
            Assert.AreEqual($"{moveFromBody1.Name} has already played.", filteredGameBefore.Player1.Move);
            Assert.AreEqual(moveFromBody2.Name, filteredGameBefore.Player2.Name);
            Assert.AreEqual(string.Empty, filteredGameBefore.Player2.Move);

            Assert.AreEqual("The players played a drawn game.", filteredGameAfter.Information);
            Assert.AreEqual(moveFromBody1.Name, filteredGameAfter.Player1.Name);
            Assert.AreEqual(moveFromBody1.Move, filteredGameAfter.Player1.Move);
            Assert.AreEqual(moveFromBody2.Name, filteredGameAfter.Player2.Name);
            Assert.AreEqual(moveFromBody2.Move, filteredGameAfter.Player2.Move);
        }
コード例 #7
0
ファイル: GamesControllerTest.cs プロジェクト: Liam44/Cygni
        public void Move_ModelNotNull_NameValid_MoveValid_IdValid_GameExists_Player1Exists_Player2Exists_Player1HasntPlayed_Player2HasAlreadyPlayed()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");
            JoinFromBody    joinFromBody = new JoinFromBody("Jack");
            MoveFromBody    moveFromBody = new MoveFromBody(joinFromBody.Name, "Rock");

            // Act
            HttpResponseMessage postResponse = controller.Post(postFromBody);

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

            HttpResponseMessage joinResponse = controller.Join(player1EnteredGame.GameId, joinFromBody);

            joinResponse.TryGetContentValue(out PlayerEnteredGame player2EnteredGame);

            controller.Move(player1EnteredGame.GameId, moveFromBody);

            HttpResponseMessage getResultBefore = controller.Get(player1EnteredGame.GameId);

            getResultBefore.TryGetContentValue(out FilteredGame filteredGameBefore);

            HttpResponseMessage result = controller.Move(player1EnteredGame.GameId, moveFromBody);

            HttpResponseMessage getResultAfter = controller.Get(player1EnteredGame.GameId);

            getResultAfter.TryGetContentValue(out FilteredGame filteredGameAfter);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out string description));
            Assert.AreEqual($"Player 2 {joinFromBody.Name} has already played.", description);

            Assert.AreEqual($"Waiting for player 1 {postFromBody.Name} to play.", filteredGameBefore.Information);
            Assert.AreEqual(postFromBody.Name, filteredGameBefore.Player1.Name);
            Assert.AreEqual(string.Empty, filteredGameBefore.Player1.Move);
            Assert.AreEqual(joinFromBody.Name, filteredGameBefore.Player2.Name);
            Assert.AreEqual($"{joinFromBody.Name} has already played.", filteredGameBefore.Player2.Move);

            Assert.AreEqual($"Waiting for player 1 {postFromBody.Name} to play.", filteredGameAfter.Information);
            Assert.AreEqual(postFromBody.Name, filteredGameAfter.Player1.Name);
            Assert.AreEqual(string.Empty, filteredGameAfter.Player1.Move);
            Assert.AreEqual(joinFromBody.Name, filteredGameAfter.Player2.Name);
            Assert.AreEqual($"{joinFromBody.Name} has already played.", filteredGameAfter.Player2.Move);
        }
コード例 #8
0
ファイル: GamesControllerTest.cs プロジェクト: Liam44/Cygni
        public void Move_ModelNotNull_NameValid_MoveEmpty_IdValid_GameExists_Player1Exists_Player2DoesntExist()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");
            MoveFromBody    moveFromBody = new MoveFromBody(postFromBody.Name, string.Empty);

            // Act
            HttpResponseMessage postResponse = controller.Post(postFromBody);

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

            HttpResponseMessage result = controller.Move(player1EnteredGame.GameId, moveFromBody);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out string description));
            Assert.AreEqual("You must provide a valid move.", description);
        }
コード例 #9
0
ファイル: GamesControllerTest.cs プロジェクト: Liam44/Cygni
        public void Move_ModelNotNull_NameValid_MoveValid_IdValid_GameExists_Player1Exists_Player2DoesntExist_Player1HasntPlayed()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");
            MoveFromBody    moveFromBody = new MoveFromBody(postFromBody.Name, "Rock");

            // Act
            HttpResponseMessage postResponse = controller.Post(postFromBody);

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

            HttpResponseMessage getResultBefore = controller.Get(player1EnteredGame.GameId);

            getResultBefore.TryGetContentValue(out FilteredGame filteredGameBefore);

            HttpResponseMessage result = controller.Move(player1EnteredGame.GameId, moveFromBody);

            HttpResponseMessage getResultAfter = controller.Get(player1EnteredGame.GameId);

            getResultAfter.TryGetContentValue(out FilteredGame filteredGameAfter);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.IsNull(result.Content);

            Assert.AreEqual($"Waiting for player 1 {postFromBody.Name} to play." +
                            Environment.NewLine +
                            "Waiting for Player 2 to join the game.", filteredGameBefore.Information);
            Assert.AreEqual(postFromBody.Name, filteredGameBefore.Player1.Name);
            Assert.AreEqual(string.Empty, filteredGameBefore.Player1.Move);
            Assert.AreEqual(string.Empty, filteredGameBefore.Player2.Name);
            Assert.AreEqual(string.Empty, filteredGameBefore.Player2.Move);

            Assert.AreEqual("Waiting for Player 2 to join the game.", filteredGameAfter.Information);
            Assert.AreEqual(postFromBody.Name, filteredGameAfter.Player1.Name);
            Assert.AreEqual($"{moveFromBody.Name} has already played.", filteredGameAfter.Player1.Move);
            Assert.AreEqual(string.Empty, filteredGameAfter.Player2.Name);
            Assert.AreEqual(string.Empty, filteredGameAfter.Player2.Move);
        }
コード例 #10
0
        public HttpResponseMessage Move(string id, [FromBody] MoveFromBody moveFromBody)
        {
            try
            {
                if (moveFromBody == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotAcceptable,
                                                  "You must provide a player name and a move."));
                }

                GameId gameId = new GameId(id);

                // Checks if the game actually exists
                if (gameId.Game == null)
                {
                    return(Request.CreateResponse(gameId.StatusCode, gameId.Message));
                }

                Game game = gameId.Game;

                if (string.IsNullOrEmpty(moveFromBody.Name))
                {
                    return(Request.CreateResponse(HttpStatusCode.NotAcceptable,
                                                  "You must provide a player name."));
                }

                if (string.IsNullOrEmpty(moveFromBody.Move))
                {
                    return(Request.CreateResponse(HttpStatusCode.NotAcceptable,
                                                  "You must provide a valid move."));
                }

                // Does the name match the player 1?
                if (string.Compare(game.Player1.Name, moveFromBody.Name, true) == 0)
                {
                    // Sets player 1's move
                    if (game.Player1.HasAlreadyPlayed)
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotAcceptable,
                                                      $"Player 1 {moveFromBody.Name} has already played."));
                    }
                    else
                    {
                        HttpResponseMessage responseMessage = SetPlayerMove(game.Player1, moveFromBody.Move);

                        if (responseMessage != null)
                        {
                            return(responseMessage);
                        }
                    }
                }
                else if (game.Player2 == null || string.Compare(game.Player2.Name, moveFromBody.Name, true) != 0)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound,
                                                  $"Player {moveFromBody.Name} is undefined for game {id}."));
                }
                else
                {
                    // Sets player 2's move
                    if (game.Player2.HasAlreadyPlayed)
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotAcceptable,
                                                      $"Player 2 {moveFromBody.Name} has already played."));
                    }
                    else
                    {
                        HttpResponseMessage responseMessage = SetPlayerMove(game.Player2, moveFromBody.Move);

                        if (responseMessage != null)
                        {
                            return(responseMessage);
                        }
                    }
                }

                return(Request.CreateResponse());
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }