public async Task InvalidShipIntegrityPlacingTest()
        {
            object expected  = new GameStateResponse(1234); //status = placing
            var    mockCache = MockMemoryCacheService.GetMemoryCache(expected);

            var mockConfig = new Moq.Mock <IOptions <BattleshipConfiguration> >();

            mockConfig.Setup(config => config.Value).Returns(() => prepareConfig());
            var mockEngine = new Moq.Mock <GameEngine>(mockConfig.Object);
            var controller = new BattleShipController(mockCache, mockConfig.Object, mockEngine.Object, null);

            List <ShipPlacement> incorrectShips = prepareValidShipsWithoutOne2Fielded();

            incorrectShips.Add(new ShipPlacement {
                Coordinates = new List <Coordinate> {
                    new Coordinate(8, 8), new Coordinate(10, 8)
                }
            });                                                                                                                           //desintegrated ship

            ShipPlacementRequest request = new ShipPlacementRequest()
            {
                GameId = 1234, PlayerId = 1, Ships = incorrectShips
            };

            var ex = Assert.Throws <InvalidShipIntegrityException>(() => controller.PlaceShips(request));

            Assert.Equal("Each ship needs to have connected coordinates", ex.Message);
        }
        public async Task ProperPlacementWaitingForSecondPlayerPlacingTest()
        {
            object expected  = new GameStateResponse(1234); //status = placing
            var    mockCache = MockMemoryCacheServiceWithGetAndSet.GetMemoryCache(expected);

            var mockConfig = new Moq.Mock <IOptions <BattleshipConfiguration> >();

            mockConfig.Setup(config => config.Value).Returns(() => prepareConfig());
            var mockEngine = new Moq.Mock <GameEngine>(mockConfig.Object);
            var controller = new BattleShipController(mockCache, mockConfig.Object, mockEngine.Object, null);

            List <ShipPlacement> correctShips = prepareValidShipsWithoutOne2Fielded();

            correctShips.Add(new ShipPlacement {
                Coordinates = new List <Coordinate> {
                    new Coordinate(8, 8), new Coordinate(9, 8)
                }
            });

            ShipPlacementRequest request = new ShipPlacementRequest()
            {
                GameId = 1234, PlayerId = 1, Ships = correctShips
            };

            var result     = controller.PlaceShips(request);
            var viewResult = Assert.IsType <ActionResult <GameStateResponse> >(result);
            GameStateResponse expectedNewGame = new GameStateResponse(1234);

            Assert.Equal(result.Value.NextPlayer, 0);                    //still waiting for second player to place ships
            Assert.Equal(result.Value.GameStatus, GameStatuses.PLACING); //still waiting for second player to place ships
            Assert.True(result.Value.Player1Ships.Count > 0);            //ships placed by first player
        }
        public async Task IncorrectCoordinatesShipsPlacingTest()
        {
            object expected  = new GameStateResponse(1234); //status = placing
            var    mockCache = MockMemoryCacheService.GetMemoryCache(expected);

            var mockConfig = new Moq.Mock <IOptions <BattleshipConfiguration> >();

            mockConfig.Setup(config => config.Value).Returns(() => prepareConfig());
            var mockEngine = new Moq.Mock <GameEngine>(mockConfig.Object);
            var controller = new BattleShipController(mockCache, mockConfig.Object, mockEngine.Object, null);

            List <ShipPlacement> incorrectShips = new List <ShipPlacement> {  // INCORRECT COORDINATE 1,12
                new ShipPlacement {
                    Coordinates = new List <Coordinate> {
                        new Coordinate(1, 12), new Coordinate(1, 2)
                    }
                },
                new ShipPlacement {
                    Coordinates = new List <Coordinate> {
                        new Coordinate(3, 2), new Coordinate(2, 2)
                    }
                }
            };
            ShipPlacementRequest request = new ShipPlacementRequest()
            {
                GameId = 1234, PlayerId = 1, Ships = incorrectShips
            };

            var ex = Assert.Throws <InvalidCoordinatesException>(() => controller.PlaceShips(request));

            Assert.Equal("Invalid coordinates supplied, possible values: columns 1-10, rows 1-10", ex.Message);
        }
        public async Task InvalidShipAmountPlacingTest()
        {
            object expected  = new GameStateResponse(1234); //status = placing
            var    mockCache = MockMemoryCacheService.GetMemoryCache(expected);

            var mockConfig = new Moq.Mock <IOptions <BattleshipConfiguration> >();

            mockConfig.Setup(config => config.Value).Returns(() => prepareConfig());
            var mockEngine = new Moq.Mock <GameEngine>(mockConfig.Object);
            var controller = new BattleShipController(mockCache, mockConfig.Object, mockEngine.Object, null);

            List <ShipPlacement> incorrectShips = new List <ShipPlacement> { //ONLY 2 SHIPS
                new ShipPlacement {
                    Coordinates = new List <Coordinate> {
                        new Coordinate(1, 1), new Coordinate(1, 2)
                    }
                },
                new ShipPlacement {
                    Coordinates = new List <Coordinate> {
                        new Coordinate(3, 2), new Coordinate(2, 2)
                    }
                }
            };
            ShipPlacementRequest request = new ShipPlacementRequest()
            {
                GameId = 1234, PlayerId = 1, Ships = incorrectShips
            };

            var ex = Assert.Throws <InvalidShipAmountException>(() => controller.PlaceShips(request));

            Assert.Equal("Provided ships do not conform to the configured amounts, which is: [{\"Size\":5,\"Amount\":1},{\"Size\":4,\"Amount\":2},{\"Size\":3,\"Amount\":3},{\"Size\":2,\"Amount\":4}]", ex.Message);
        }
        public async Task CollidingShipsPlacingTest()
        {
            object expected  = new GameStateResponse(1234); //status = placing
            var    mockCache = MockMemoryCacheService.GetMemoryCache(expected);

            var mockConfig = new Moq.Mock <IOptions <BattleshipConfiguration> >();

            mockConfig.Setup(config => config.Value).Returns(() => prepareConfig());
            var mockEngine = new Moq.Mock <GameEngine>(mockConfig.Object);
            var controller = new BattleShipController(mockCache, mockConfig.Object, mockEngine.Object, null);

            List <ShipPlacement> incorrectShips = new List <ShipPlacement> {  // COLLISION!
                new ShipPlacement {
                    Coordinates = new List <Coordinate> {
                        new Coordinate(1, 1), new Coordinate(1, 2)
                    }
                },
                new ShipPlacement {
                    Coordinates = new List <Coordinate> {
                        new Coordinate(1, 2), new Coordinate(2, 2)
                    }
                }
            };
            ShipPlacementRequest request = new ShipPlacementRequest()
            {
                GameId = 1234, PlayerId = 1, Ships = incorrectShips
            };

            var ex = Assert.Throws <CollidingShipsLogError>(() => controller.PlaceShips(request));

            Assert.Equal("Provided ships are colliding as they share coordinates", ex.Message);
        }
        public async Task InvalidPlayerIdPlacingTest()
        {
            object expected  = new GameStateResponse(1234); //status = placing
            var    mockCache = MockMemoryCacheService.GetMemoryCache(expected);

            var mockConfig = new Moq.Mock <IOptions <BattleshipConfiguration> >();
            var mockEngine = new Moq.Mock <GameEngine>(mockConfig.Object);
            var controller = new BattleShipController(mockCache, mockConfig.Object, mockEngine.Object, null);

            ShipPlacementRequest request = new ShipPlacementRequest()
            {
                GameId = 1234, PlayerId = 15
            };

            var ex = Assert.Throws <InvalidPlayerIdException>(() => controller.PlaceShips(request));

            Assert.Equal("Invalid Player Id. Only Players with Id 1 and 2 are permited", ex.Message);
        }
        public async Task InvalidGamePlacingTest()
        {
            object expected  = null; //game not found
            var    mockCache = MockMemoryCacheService.GetMemoryCache(expected);

            var mockConfig = new Moq.Mock <IOptions <BattleshipConfiguration> >();
            var mockEngine = new Moq.Mock <GameEngine>(mockConfig.Object);
            var controller = new BattleShipController(mockCache, mockConfig.Object, mockEngine.Object, null);

            ShipPlacementRequest request = new ShipPlacementRequest()
            {
                GameId = 1234, PlayerId = 1
            };

            var ex = Assert.Throws <InvalidGameIdException>(() => controller.PlaceShips(request));

            Assert.Equal("Invalid game Id 1234, no valid game with the given id exists.", ex.Message);
        }
        public async Task NoShipsPlacingTest()
        {
            object expected  = new GameStateResponse(1234); //status = placing
            var    mockCache = MockMemoryCacheService.GetMemoryCache(expected);

            var mockConfig = new Moq.Mock <IOptions <BattleshipConfiguration> >();

            mockConfig.Setup(config => config.Value).Returns(() => prepareConfig());
            var mockEngine = new Moq.Mock <GameEngine>(mockConfig.Object);
            var controller = new BattleShipController(mockCache, mockConfig.Object, mockEngine.Object, null);

            ShipPlacementRequest request = new ShipPlacementRequest()
            {
                GameId = 1234, PlayerId = 1
            };

            var ex = Assert.Throws <NoShipsProvidedException>(() => controller.PlaceShips(request));

            Assert.Equal("No ships provided in placement", ex.Message);
        }
        public async Task FinishedGamePlacingTest()
        {
            object expected = new GameStateResponse(1234)
            {
                GameStatus = GameStatuses.FINISHED
            };
            var mockCache = MockMemoryCacheService.GetMemoryCache(expected);

            var mockConfig = new Moq.Mock <IOptions <BattleshipConfiguration> >();
            var mockEngine = new Moq.Mock <GameEngine>(mockConfig.Object);
            var controller = new BattleShipController(mockCache, mockConfig.Object, mockEngine.Object, null);

            ShipPlacementRequest request = new ShipPlacementRequest()
            {
                GameId = 1234, PlayerId = 1
            };

            var ex = Assert.Throws <InvalidGameStatusPlacingException>(() => controller.PlaceShips(request));

            Assert.Equal("Game already started thus it's no longer possible to place ships, current mode: FINISHED", ex.Message);
        }