public ActionResult <BattleshipResult> AttackOpponent([FromBody] BattleshipOptions battleshipOptions) { var playerBoard = _memoryCache.Get <Cell[][]>(battleshipOptions.PlayerId); if (playerBoard == null) { return(NotFound(nameof(playerBoard))); } var opponentBoard = _memoryCache.Get <Cell[][]>(battleshipOptions.OpponentId); if (opponentBoard == null) { return(NotFound(nameof(opponentBoard))); } var battleshipAttacked = _battleshipUtility.Attack(opponentBoard, battleshipOptions.Row.GetValueOrDefault(), battleshipOptions.Column.GetValueOrDefault()); return(new BattleshipResult { PlayerBoard = DisplayBoard(playerBoard), OpponentBoard = DisplayBoard(opponentBoard), Results = new[] { battleshipAttacked.Message }, PlayerId = battleshipOptions.PlayerId.GetValueOrDefault(), OpponentId = battleshipOptions.OpponentId.GetValueOrDefault() }); }
public void ReturnsBadRequestWhenModelStateIsInvalid() { // Arrange var battleshipOptions = new BattleshipOptions { Alignment = BattleShip.Horizontal, Column = 1, Row = 0, ShipSize = 4, OpponentId = 2 }; _mockBattleshipUtility.Setup(m => m.AddBattleship(It.IsAny <Cell[][]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns((BattleshipUtilityResult)null); Cell[][] playerBoard = null; var memoryCache = MockMemoryCacheService.GetMockedMemoryCache(); memoryCache.Set(0, playerBoard); var battleshipController = new BattleshipController(memoryCache, _mockBattleshipUtility.Object); // Act battleshipController.ModelState.AddModelError("Bad", "Request"); var subject = battleshipController.Add(battleshipOptions); // Assert var badRequestObjectResult = subject.Result as Microsoft.AspNetCore.Mvc.BadRequestObjectResult; Assert.IsNull(subject.Value); Assert.AreEqual(badRequestObjectResult.StatusCode.Value, StatusCodes.Status400BadRequest); }
public void WhenPropertiesAreSet() { var model = new BattleshipOptions { PlayerId = 1, Column = 1, Row = 1, OpponentId = 2 }; var result = validator.TestValidate(model); result.ShouldNotHaveAnyValidationErrors(); }
public void WhenPropertyValuesAreInvalid() { var model = new BattleshipOptions { PlayerId = 0, Column = 0, Row = 0, OpponentId = 0 }; var result = validator.TestValidate(model); result.ShouldHaveAnyValidationError(); }
public void WhenPlayerIdIsNull() { var model = new BattleshipOptions { PlayerId = null }; var result = validator.TestValidate(model); result.ShouldHaveValidationErrorFor(bs => bs.PlayerId); }
public ActionResult <BattleshipResult> Add([FromBody] BattleshipOptions battleshipOptions) { // TODO: Get specific BadRequest message if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var playerBoard = _memoryCache.Get <Cell[][]>(battleshipOptions.PlayerId); if (playerBoard == null) { return(NotFound(nameof(playerBoard))); } var result = _battleshipUtility.AddBattleship(playerBoard, battleshipOptions.Row.GetValueOrDefault(), battleshipOptions.Column.GetValueOrDefault(), battleshipOptions.ShipSize.GetValueOrDefault(), battleshipOptions.Alignment); // TODO: Test NotFound result message if (result == null) { return(NotFound(nameof(result))); } // TODO: Try with an error message var opponentBoard = _memoryCache.Get <Cell[][]>(battleshipOptions.OpponentId); if (opponentBoard == null) { return(NotFound()); } return(new BattleshipResult { PlayerBoard = DisplayBoard(playerBoard), OpponentBoard = DisplayBoard(opponentBoard), Results = new[] { result.Message }, PlayerId = battleshipOptions.PlayerId.GetValueOrDefault(), OpponentId = battleshipOptions.OpponentId.GetValueOrDefault(), ResultType = result.ResultType }); }
public void ReturnsValidBattleship() { // Arrange var battleshipOptions = new BattleshipOptions { Alignment = BattleShip.Horizontal, Column = 1, PlayerId = 1, Row = 1, ShipSize = 4, OpponentId = 2 }; _mockBattleshipUtility.Setup( m => m.AddBattleship( It.IsAny <Cell[][]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())) .Returns(new BattleshipUtilityResult("Battleship added", BattleshipResultType.Added)); var memoryCache = MockMemoryCacheService.GetMemoryCache(BattleshipUtilityTestHelpers.CreateDefaultBoard()); var battleshipController = new BattleshipController(memoryCache, _mockBattleshipUtility.Object); // Act var subject = battleshipController.Add(battleshipOptions); // Assert Assert.IsNotNull(subject); Assert.AreEqual(subject.Value.ResultType, BattleshipResultType.Added); }
public void ReturnsNotFoundWhenPlayerboardIsNull() { // Arrange var battleshipOptions = new BattleshipOptions { Alignment = BattleShip.Horizontal, Column = 1, PlayerId = 1, Row = 1, ShipSize = 4, OpponentId = 2 }; _mockBattleshipUtility.Setup(m => m.AddBattleship(It.IsAny <Cell[][]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns((BattleshipUtilityResult)null); Cell[][] playerBoard = null; var memoryCache = MockMemoryCacheService.GetMockedMemoryCache(); memoryCache.Set(0, playerBoard); var battleshipController = new BattleshipController(memoryCache, _mockBattleshipUtility.Object); // Act var subject = battleshipController.Add(battleshipOptions); // Assert var notFoundObjectResult = subject.Result as Microsoft.AspNetCore.Mvc.NotFoundObjectResult; Assert.IsNull(subject.Value); Assert.AreEqual(notFoundObjectResult.StatusCode.Value, StatusCodes.Status404NotFound); }
public string Reset([FromBody] BattleshipOptions battleshipOptions) { _memoryCache.Remove(battleshipOptions.PlayerId); _memoryCache.Remove(battleshipOptions.OpponentId); return($"Player board: {battleshipOptions.PlayerId} and opponent board: {battleshipOptions.OpponentId} have been reset"); }