Esempio n. 1
0
        public void Create_HappyPath_ReturnsViewResult()
        {
            var       gameController = CreateGameController();
            GameModel gameModel      = new GameModel();
            UserShipsLocationViewModel userShipsLocationVM = new UserShipsLocationViewModel();

            var result = gameController.Create(gameModel, userShipsLocationVM);

            Assert.IsAssignableFrom <ViewResult>(result);
        }
Esempio n. 2
0
        public void CheckShips_ServiceProviderThrowsException_ExceptionDoesNotPropagate()
        {
            mockServiceProvider.Setup(x => x.GetService(It.IsAny <Type>())).Throws <Exception>();
            var gameController = CreateGameController();
            UserShipsLocationViewModel userShipsLocationVM = null;

            Action action = () => gameController.CheckShips(userShipsLocationVM);

            Assert.DoesNotThrow(action.Invoke);
        }
Esempio n. 3
0
        public void Create_GameServiceThrowsException_ReturnsRedirectToActionResult()
        {
            mockServiceProvider.Setup(x => x.GetService(It.IsAny <Type>())).Throws <Exception>();
            var       gameController = CreateGameController();
            GameModel gameModel      = null;
            UserShipsLocationViewModel userShipsLocationVM = null;

            var result = gameController.Create(gameModel, userShipsLocationVM);

            Assert.IsAssignableFrom <RedirectToActionResult>(result);
        }
Esempio n. 4
0
        public void CheckShips_HappyPath_ReturnsRedirectToActionResult()
        {
            var gameController = CreateGameController();

            mockGameService.Setup(x => x.GetShipsPositions(It.IsAny <IList <ShipLayout> >())).Returns(It.IsAny <bool[]>());
            UserShipsLocationViewModel userShipsLocationVM = null;

            var result = gameController.CheckShips(userShipsLocationVM);

            Assert.IsAssignableFrom <RedirectToActionResult>(result);
        }
Esempio n. 5
0
        public void Create_GameServiceThrowsException_GameServiceRemovesGame()
        {
            var gameController = CreateGameController();

            mockGameService.Setup(x => x.InitializeGame(It.IsAny <IList <ShipLayout> >(), It.IsAny <IDifficultyLevel>())).Throws <Exception>();
            GameModel gameModel = new GameModel();
            UserShipsLocationViewModel userShipsLocationVM = new UserShipsLocationViewModel();

            var result = gameController.Create(gameModel, userShipsLocationVM);

            mockGameService.Verify(x => x.RemoveGame(gameModel.GameGuid), Times.Once);
        }
Esempio n. 6
0
        public void Create_ModelIsNotValid_ReturnsViewResult()
        {
            var gameController = CreateGameController();

            gameController.ModelState.AddModelError("errorKey", "errorMessage");
            GameModel gameModel = null;
            UserShipsLocationViewModel userShipsLocationVM = null;

            var result = gameController.Create(gameModel, userShipsLocationVM);

            Assert.IsAssignableFrom <ViewResult>(result);
        }
Esempio n. 7
0
        public void Create_GameServiceThrowsException_MessageToUserIsSavedToTempData()
        {
            var gameController = CreateGameController();

            mockGameService.Setup(x => x.InitializeGame(It.IsAny <IList <ShipLayout> >(), It.IsAny <IDifficultyLevel>())).Throws <Exception>();
            GameModel gameModel = new GameModel();
            UserShipsLocationViewModel userShipsLocationVM = new UserShipsLocationViewModel();

            var result = gameController.Create(gameModel, userShipsLocationVM);

            var messageToUser = gameController.TempData[nameof(UserCommunicationViewModel.MessageToUser)] as string;

            Assert.IsNotNull(messageToUser, "Message to user should be saved in TempData");
        }
Esempio n. 8
0
        public void CheckShips_GameServiceThrowsException_MessageToUserIsSavedToTempData()
        {
            var gameController = CreateGameController();
            var exception      = new Exception("TestException");

            mockGameService.Setup(x => x.GetShipsPositions(It.IsAny <IList <ShipLayout> >())).Throws(exception);
            UserShipsLocationViewModel userShipsLocationVM = new UserShipsLocationViewModel();

            gameController.CheckShips(userShipsLocationVM);

            var messageToUser = gameController.TempData[nameof(UserCommunicationViewModel.MessageToUser)] as string;

            Assert.IsNotNull(messageToUser, "Message to user should be saved in TempData");
            Assert.That(messageToUser.Contains(exception.Message), "Message to user should contain exception message");
        }
Esempio n. 9
0
        public void Create_ModelIsNotValid_AddsMessageToTempData()
        {
            var gameController = CreateGameController();

            gameController.ModelState.AddModelError("errorKey", "errorMessage");
            GameModel gameModel = null;
            UserShipsLocationViewModel userShipsLocationVM = null;

            var result = gameController.Create(gameModel, userShipsLocationVM);

            Assert.IsAssignableFrom <ViewResult>(result);

            var messageToUser = gameController.TempData[nameof(UserCommunicationViewModel.MessageToUser)] as string;

            Assert.IsNotNull(messageToUser, "Message to user should be saved in TempData");
        }
Esempio n. 10
0
        public void Index_TempDataContainsShipsFields_AddShipFieldsToModel()
        {
            var       gameController = CreateGameController();
            GameModel gameModel      = new GameModel();

            bool[] shipsFields         = new[] { false, true };
            var    userShipsLocationVM = new UserShipsLocationViewModel()
            {
                ShipsFields = shipsFields
            };
            var serializedVm = JsonConvert.SerializeObject(userShipsLocationVM);

            gameController.TempData[nameof(UserShipsLocationViewModel)] = serializedVm;

            gameController.Index(gameModel);

            Assert.AreEqual(shipsFields, gameModel.ShipsFields);
        }
Esempio n. 11
0
        public IActionResult CheckShips(UserShipsLocationViewModel userShipsLocationVM)
        {
            try
            {
                var shipLayouts = userShipsLocationVM.ShipsLayouts;
                var gameService = _serviceProvider.GetService <IGameService>();
                var fields      = gameService.GetShipsPositions(shipLayouts);
                userShipsLocationVM.ShipsFields = fields;
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"General error in {nameof(CheckShips)} method");
                TempData[nameof(UserCommunicationViewModel.MessageToUser)] = "Error: " + e.Message;
            }

            TempData.AddSerializedObject(userShipsLocationVM, nameof(UserShipsLocationViewModel));

            return(RedirectToAction(nameof(Index)));
        }
Esempio n. 12
0
        public void CheckShips_HappyPath_UserShipLocationIsSavedToTempData()
        {
            var gameController = CreateGameController();
            var shipFields     = new[] { false, true };

            mockGameService.Setup(x => x.GetShipsPositions(It.IsAny <IList <ShipLayout> >())).Returns(shipFields);
            UserShipsLocationViewModel userShipsLocationVM = new UserShipsLocationViewModel();

            gameController.CheckShips(userShipsLocationVM);


            var serializedShipsLocation = gameController.TempData[nameof(UserShipsLocationViewModel)] as string;

            Assert.IsNotNull(serializedShipsLocation, "Player ships location should be saved in TempData");

            var deserializedUserShipsLocationVM = JsonConvert.DeserializeObject <UserShipsLocationViewModel>(serializedShipsLocation);

            Assert.AreEqual(shipFields, deserializedUserShipsLocationVM.ShipsFields, "Ships locations should be the same");
        }
Esempio n. 13
0
        public void Create_GameServiceThrowsException_ShipsLocationIsSavedToTempData()
        {
            var gameController = CreateGameController();
            var shipFields     = new[] { false, true };

            mockGameService.Setup(x => x.InitializeGame(It.IsAny <IList <ShipLayout> >(), It.IsAny <IDifficultyLevel>())).Throws <Exception>();
            GameModel gameModel = new GameModel();
            UserShipsLocationViewModel userShipsLocationVM = new UserShipsLocationViewModel()
            {
                ShipsFields = shipFields
            };

            var result = gameController.Create(gameModel, userShipsLocationVM);

            var serializedShipsLocation = gameController.TempData[nameof(UserShipsLocationViewModel)] as string;

            Assert.IsNotNull(serializedShipsLocation, "Player ships location should be saved in TempData");

            var deserializedUserShipsLocationVM = JsonConvert.DeserializeObject <UserShipsLocationViewModel>(serializedShipsLocation);

            Assert.AreEqual(shipFields, deserializedUserShipsLocationVM.ShipsFields, "Ships locations should be the same");
        }
Esempio n. 14
0
        public IActionResult Create(GameModel gameModel, UserShipsLocationViewModel userShipsLocationVM)
        {
            // Guid has not been created yet
            ModelState.ClearValidationState(nameof(GameModel.GameGuid));
            ModelState.MarkFieldSkipped(nameof(GameModel.GameGuid));
            if (!ModelState.IsValid)
            {
                _logger.LogError($"Model was not validated in {nameof(Create)} method");
                TempData[nameof(UserCommunicationViewModel.MessageToUser)] = WarningGameRestarted;
                return(View(nameof(Index), gameModel));
            }

            IGameService gameService = null;

            try
            {
                gameService        = _serviceProvider.GetService <IGameService>();
                gameModel.Game     = gameService.InitializeGame(userShipsLocationVM.ShipsLayouts, new DifficultyLevelEasy());
                gameModel.GameGuid = gameService.CurrentGameGuid;

                return(View("GameProgress", gameModel));
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"General error in {nameof(Create)} method");

                if (gameService != null)
                {
                    gameService.RemoveGame(gameModel.GameGuid);
                }

                TempData[nameof(UserCommunicationViewModel.MessageToUser)] = "Error: " + e.Message;
                TempData.AddSerializedObject(userShipsLocationVM, nameof(UserShipsLocationViewModel));

                return(RedirectToAction(nameof(Index)));
            }
        }