Beispiel #1
0
        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);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        public void Get_IdValid_GameExists_Player1HasntPlayed_Player2Exists_Player2HasntPlayed()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");
            JoinFromBody    joinFromBody = new JoinFromBody("Jack");

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

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

            controller.Join(player1EnteredGame.GameId, joinFromBody);

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

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out FilteredGame content));
            Assert.AreEqual(content.ID, player1EnteredGame.GameId);
            Assert.AreEqual($"Waiting for player 1 {postFromBody.Name} to play." +
                            Environment.NewLine +
                            $"Waiting for player 2 {joinFromBody.Name} to play.", content.Information);
            Assert.IsNotNull(content.Player1);
            Assert.AreEqual(postFromBody.Name, content.Player1.Name);
            Assert.AreEqual(string.Empty, content.Player1.Move);
            Assert.IsNotNull(content.Player2);
            Assert.AreEqual(joinFromBody.Name, content.Player2.Name);
            Assert.AreEqual(string.Empty, content.Player2.Move);
        }
Beispiel #4
0
        public void Join_IdNull()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            string          id           = null;
            JoinFromBody    joinFromBody = new JoinFromBody(null);

            // Act
            HttpResponseMessage result = controller.Join(id, joinFromBody);

            // 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);
        }
Beispiel #5
0
        public void Join_IdValid_GameDoesntExist()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            string          id           = "abcd";
            JoinFromBody    joinFromBody = new JoinFromBody(null);

            // Act
            HttpResponseMessage result = controller.Join(id, joinFromBody);

            // 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);
        }
Beispiel #6
0
        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);
        }
Beispiel #7
0
        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);
        }
Beispiel #8
0
        public HttpResponseMessage Join(string id, [FromBody] JoinFromBody joinFromBody)
        {
            try
            {
                if (joinFromBody == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotAcceptable,
                                                  "You must provide a player name."));
                }

                GameId gameId = new GameId(id);

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

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

                if (string.Compare(gameId.Game.Player1.Name, joinFromBody.Name, true) == 0)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotAcceptable,
                                                  "A player with a similar name already entered the game."));
                }

                if (gameId.Game.Player2 != null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotAcceptable,
                                                  "Player 2 has already joined the game."));
                }

                gameId.Game.Player2 = new Player(joinFromBody.Name);

                return(Request.CreateResponse(HttpStatusCode.OK,
                                              new PlayerEnteredGame(id, joinFromBody.Name)));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
Beispiel #9
0
        public void Join_IdValid_GameExists_ModelNotNull_NameValid_Player2HasSameNameAsPlayer1()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");
            JoinFromBody    joinFromBody = new JoinFromBody(postFromBody.Name);

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

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

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

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out string description));
            Assert.AreEqual("A player with a similar name already entered the game.", description);
        }
Beispiel #10
0
        public void Join_IdValid_GameExists_ModelNotNull_NameEmpty()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");
            JoinFromBody    joinFromBody = new JoinFromBody(string.Empty);

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

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

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

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out string description));
            Assert.AreEqual("You must provide a valid name.", description);
        }
Beispiel #11
0
        public void Join_IdValid_GameExists_ModelNotNull_NameValid_Player2DoesntExist()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");
            JoinFromBody    joinFromBody = new JoinFromBody("Jack");

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

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

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

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out PlayerEnteredGame player2EnteredGame));
            Assert.AreEqual(player1EnteredGame.GameId, player2EnteredGame.GameId);
            Assert.AreEqual($"Welcome {joinFromBody.Name}." + Environment.NewLine +
                            "What will be your move: Rock, Paper or Scissors?", player2EnteredGame.Message);
        }