Beispiel #1
0
        public void TestAddShip_BoardNotExist_ErrorResponse()
        {
            // Arrange
            var boardsManager        = new GameBoardsManager();
            var battleShipController = new BattleShipController(boardsManager);

            // Act
            var addShipResult = battleShipController.AddShip(new AddShipRequest()
            {
                BoardId      = "fake id",
                HeadPosition =
                    new BattleShipStateTracker.Request.Coordinate()
                {
                    X = 5, Y = 6
                },
                TailPosition =
                    new BattleShipStateTracker.Request.Coordinate()
                {
                    X = 6, Y = 6
                }
            }) as JsonResult;

            // Assert
            Assert.IsNotNull(addShipResult);
            Assert.AreEqual(StatusCodes.Status400BadRequest, addShipResult.StatusCode);
            var value = addShipResult.Value as ErrorResponse;

            Assert.IsNotNull(value);
        }
Beispiel #2
0
        public void TestAddShip_Success()
        {
            // Arrange
            var boardsManager        = new GameBoardsManager();
            var battleShipController = new BattleShipController(boardsManager);
            var result  = battleShipController.AddBoard() as JsonResult;
            var boardId = (result.Value as BoardResponse).BoardId;

            // Act
            var addShipResult = battleShipController.AddShip(new AddShipRequest()
            {
                BoardId = boardId, HeadPosition =
                    new BattleShipStateTracker.Request.Coordinate()
                {
                    X = 5, Y = 6
                },
                TailPosition =
                    new BattleShipStateTracker.Request.Coordinate()
                {
                    X = 6, Y = 6
                }
            }) as JsonResult;

            // Assert
            Assert.IsNotNull(addShipResult);
            Assert.AreEqual(StatusCodes.Status201Created, addShipResult.StatusCode);
            var value = addShipResult.Value as SuccessResponse;

            Assert.IsNotNull(value);
        }
Beispiel #3
0
        public void TestAddShip_NotVerticalOrHorizontal_ErrorResponse()
        {
            // Arrange
            var boardsManager        = new GameBoardsManager();
            var battleShipController = new BattleShipController(boardsManager);
            var result  = battleShipController.AddBoard() as JsonResult;
            var boardId = (result.Value as BoardResponse).BoardId;

            // Act
            var addShipResult = battleShipController.AddShip(new AddShipRequest()
            {
                BoardId      = boardId,
                HeadPosition =
                    new BattleShipStateTracker.Request.Coordinate()
                {
                    X = 5, Y = 6
                },
                TailPosition =
                    new BattleShipStateTracker.Request.Coordinate()
                {
                    X = 7, Y = 8
                }
            }) as JsonResult;

            // Assert
            Assert.IsNotNull(addShipResult);
            Assert.AreEqual(StatusCodes.Status400BadRequest, addShipResult.StatusCode);
            var value = addShipResult.Value as ErrorResponse;

            Assert.IsNotNull(value);
        }
Beispiel #4
0
        public void TestAttack_WasAttacked_ErrorResponse()
        {
            // Arrange
            var boardsManager        = new GameBoardsManager();
            var battleShipController = new BattleShipController(boardsManager);
            var result  = battleShipController.AddBoard() as JsonResult;
            var boardId = (result.Value as BoardResponse).BoardId;

            battleShipController.AddShip(new AddShipRequest()
            {
                BoardId      = boardId,
                HeadPosition =
                    new BattleShipStateTracker.Request.Coordinate()
                {
                    X = 5, Y = 6
                },
                TailPosition =
                    new BattleShipStateTracker.Request.Coordinate()
                {
                    X = 6, Y = 6
                }
            });

            battleShipController.Attack(new AttackRequest()
            {
                BoardId  = boardId,
                Position = new BattleShipStateTracker.Request.Coordinate()
                {
                    X = 5, Y = 6
                }
            });

            // Act
            var attackResult = battleShipController.Attack(new AttackRequest()
            {
                BoardId  = boardId,
                Position = new BattleShipStateTracker.Request.Coordinate()
                {
                    X = 5, Y = 6
                }
            }) as JsonResult;

            // Assert
            Assert.IsNotNull(attackResult);
            Assert.AreEqual(StatusCodes.Status400BadRequest, attackResult.StatusCode);
            var value = attackResult.Value as ErrorResponse;

            Assert.IsNotNull(value);
            Assert.AreEqual("The position (5,6) has been attacked before.", value.ErrorMessages[0]);
        }
Beispiel #5
0
        public void TestAttack_HitResponse()
        {
            // Arrange
            var boardsManager        = new GameBoardsManager();
            var battleShipController = new BattleShipController(boardsManager);
            var result  = battleShipController.AddBoard() as JsonResult;
            var boardId = (result.Value as BoardResponse).BoardId;

            battleShipController.AddShip(new AddShipRequest()
            {
                BoardId      = boardId,
                HeadPosition =
                    new BattleShipStateTracker.Request.Coordinate()
                {
                    X = 5, Y = 6
                },
                TailPosition =
                    new BattleShipStateTracker.Request.Coordinate()
                {
                    X = 6, Y = 6
                }
            });

            // Act
            var attackResult = battleShipController.Attack(new AttackRequest()
            {
                BoardId  = boardId,
                Position = new BattleShipStateTracker.Request.Coordinate()
                {
                    X = 5, Y = 6
                }
            }) as JsonResult;

            // Assert
            Assert.IsNotNull(attackResult);
            Assert.AreEqual(StatusCodes.Status200OK, attackResult.StatusCode);
            var value = attackResult.Value as HitResponse;

            Assert.IsNotNull(value);
            Assert.IsTrue(value.Hit);
        }