public ShipPositioningModelBuilder()
        {
            ShipKind kind = ShipKind.All.NextRandomElement();

            _model = new ShipPositioningModel
            {
                ShipCode           = kind.Code,
                SegmentCoordinates = new GridCoordinateModel[kind.Size]
            };

            for (int i = 0; i < kind.Size; i++)
            {
                _model.SegmentCoordinates[i] = new GridCoordinateModelBuilder().Build();
            }
        }
        public async Task <IActionResult> PositionShipOnGrid(Guid id, ShipPositioningModel model)
        {
            var currentUser = await _userManager.GetUserAsync(User);

            try
            {
                GridCoordinate[] segmentCoordinates = model.SegmentCoordinates.Select(cm => new GridCoordinate(cm.Row, cm.Column)).ToArray();
                ShipKind         shipKind           = ShipKind.CreateFromCode(model.ShipCode);
                Result           result             = _gameService.PositionShipOnGrid(id, currentUser.Id, shipKind, segmentCoordinates);
                return(Ok(result));
            }
            catch (DataNotFoundException)
            {
                ModelState.AddModelError("gameNotFound", $"Could not find a game with id {id}");
            }
            catch (ApplicationException applicationException)
            {
                ModelState.AddModelError("applicationException", applicationException.Message);
            }
            return(BadRequest(ModelState));
        }
Exemple #3
0
        private void ActAndAssertPositionShipOnGridShouldReturnBadRequest(Guid gameId, ShipPositioningModel model)
        {
            //Act
            var result = _controller.PositionShipOnGrid(gameId, model).Result as BadRequestObjectResult;

            //Assert
            Assert.That(result, Is.Not.Null, "An instance of 'BadRequestObjectResult' should be returned.");

            _userManagerMock.Verify(manager => manager.GetUserAsync(It.IsAny <ClaimsPrincipal>()), Times.Once,
                                    "The 'GetUserAsync' of the UserManager is not called");
            _gameServiceMock.Verify(service =>
                                    service.PositionShipOnGrid(It.IsAny <Guid>(), It.IsAny <Guid>(), It.IsAny <ShipKind>(), It.IsAny <GridCoordinate[]>()), Times.Once,
                                    "The 'PositionShipOnGrid' of the IGameService is not called.");

            var serializableError = result.Value as SerializableError;

            Assert.IsNotNull(serializableError, "The 'BadRequestObjectResult' should contain a ModelState error collection");
            Assert.That(serializableError.Count, Is.EqualTo(1), "The 'BadRequestObjectResult' should exactly one error");
        }
        private void ActAndAssertPositionShipOnGridShouldReturnBadRequest(Guid gameId, ShipPositioningModel model)
        {
            //Act
            var result = _controller.PositionShipOnGrid(gameId, model).Result as BadRequestObjectResult;

            //Assert
            Assert.That(result, Is.Not.Null, "An instance of 'BadRequestObjectResult' should be returned.");
            _userManagerMock.Verify();
            _gameServiceMock.Verify();
            var serializableError = result.Value as SerializableError;

            Assert.IsNotNull(serializableError, "The 'BadRequestObjectResult' should contain a ModelState error collection");
            Assert.That(serializableError.Count, Is.EqualTo(1), "The 'BadRequestObjectResult' should exactly one error");
        }