예제 #1
0
        public HttpResponseMessage Post([FromBody] PostFromBody postFromBody)
        {
            if (postFromBody == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotAcceptable,
                                              "You must provide a player name."));
            }

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

            Game game = new Game(postFromBody.Name);

            if (Games == null)
            {
                Games = new Dictionary <string, Game>();
            }

            Games.Add(game.ID, game);

            // Checks that everything went fine during the creation of the new game session
            GameId gameId = new GameId(game.ID);

            if (gameId.Game.IsVoid)
            {
                return(Request.CreateResponse(gameId.StatusCode, gameId.Message));
            }

            return(Request.CreateResponse(HttpStatusCode.OK,
                                          new PlayerEnteredGame(game.ID, postFromBody.Name)));
        }
예제 #2
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);
        }
예제 #3
0
        public void Get_IdValid_GameExists_Player1HasntPlayed_Player2DoesntExist()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");

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

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

            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 to join the game.", 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(string.Empty, content.Player2.Name);
            Assert.AreEqual(string.Empty, content.Player2.Move);
        }
예제 #4
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);
        }
예제 #5
0
        public void Post_ModelNotNull_NameEmpty()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody(string.Empty);

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

            // 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.", description);
        }
예제 #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);
        }
예제 #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);
        }
예제 #8
0
        public void Post_ModelNotNull_NameValid()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");

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

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out PlayerEnteredGame player1EnteredGame));
            Assert.IsNotNull(player1EnteredGame.GameId);
            Assert.AreEqual($"Welcome {postFromBody.Name}." + Environment.NewLine +
                            "What will be your move: Rock, Paper or Scissors?", player1EnteredGame.Message);
        }
예제 #9
0
        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);
        }
예제 #10
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);
        }
예제 #11
0
        public void Join_IdValid_GameExists_ModelNotNull_NameNull()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");
            JoinFromBody    joinFromBody = new JoinFromBody(null);

            // 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);
        }
예제 #12
0
        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);
        }