public async Task GetGame_WhenExceptionIsCaught_ShouldReturnInternalServerError()
        {
            // Arrange
            var  gameRepository = A.Fake <IGameRepository>();
            Game?game           = new Game();

            A.CallTo(() => gameRepository.GetGameAsync(A <int> .Ignored)).Throws <Exception>();

            var sharedRepository = A.Fake <ISharedRepository>();

            var       mapper    = A.Fake <IMapper>();
            GameModel?gameModel = new GameModel();

            A.CallTo(() => mapper.Map <GameModel>(A <Game> .Ignored)).Returns(gameModel);

            var linkGenerator = A.Fake <LinkGenerator>();
            var gameService   = A.Fake <IGameService>();

            var testController =
                new GamesController(gameRepository, sharedRepository, mapper, linkGenerator, gameService);

            int id = 1;

            // Act
            var result = await testController.GetGame(id);

            // Assert
            result.Result.ShouldBeOfType <ObjectResult>();
            ((ObjectResult)result.Result).StatusCode.ShouldBe(StatusCodes.Status500InternalServerError);
            ((ObjectResult)result.Result).Value.ShouldBe("Database failure");
        }
        private void TestGameLeave(GameModel game)
        {
            // remove everyone from game, player[0] is the host

            GamesController controller = new GamesController();
            // Add Authorization
            var context = new HttpControllerContext();
            var request = new HttpRequestMessage();

            controller.ControllerContext = context;
            controller.Request           = request;

            var players = TestHelpers.TestData.getAuthorizedPlayers();

            // verify other players can leave game, but cannot leave a second time
            for (int i = players.Count - 1; i >= 0; i--)
            {
                var testPlayer = players[i];
                request.Headers.Remove("Authorization");
                request.Headers.Add("Authorization", testPlayer.AuthCode);

                // can leave
                var result = controller.LeaveGame(game.Id);
                Assert.IsInstanceOfType(result, typeof(OkResult));

                // cannot leave again
                result = controller.LeaveGame(game.Id);
                Assert.IsInstanceOfType(result, typeof(NotFoundResult));
            }

            // verify game no longer exists
            var getResult = controller.GetGame(game.Id);

            Assert.IsInstanceOfType(getResult, typeof(NotFoundResult));
        }
        public async Task GetGame_WhenGameIsNotNull_ShouldReturnGameModelOfDesiredGame()
        {
            // Arrange
            var  gameRepository = A.Fake <IGameRepository>();
            Game?game           = new Game();

            A.CallTo(() => gameRepository.GetGameAsync(A <int> .Ignored)).Returns(game);

            var sharedRepository = A.Fake <ISharedRepository>();

            var       mapper    = A.Fake <IMapper>();
            GameModel?gameModel = new GameModel();

            A.CallTo(() => mapper.Map <GameModel>(A <Game> .Ignored)).Returns(gameModel);

            var linkGenerator = A.Fake <LinkGenerator>();
            var gameService   = A.Fake <IGameService>();

            var testController =
                new GamesController(gameRepository, sharedRepository, mapper, linkGenerator, gameService);

            int id = 1;

            // Act
            var result = await testController.GetGame(id);

            // Assert
            A.CallTo(() => gameRepository.GetGameAsync(id)).MustHaveHappenedOnceExactly();
            A.CallTo(() => mapper.Map <GameModel>(game)).MustHaveHappenedOnceExactly();
            result.Value.ShouldBeOfType <GameModel>();
        }
Beispiel #4
0
        public void GetGamByKeyReturnNotFoundWhenWronkKey()
        {
            // Arrange
            var mockRepository = new Mock <IUnitOfWork>();
            var controller     = new GamesController(mockRepository.Object);

            // Act
            var actionResult = controller.GetGame("wrongKey");

            // Assert
            Assert.IsInstanceOfType(actionResult, typeof(NotFoundResult));
        }
        public void GetGame_WhenModelStateIsInvalid_ReturnsBadRequest()
        {
            // Arrange
            var gameService = new Mock <IGameService>();
            var contoller   = new GamesController(gameService.Object);

            contoller.ModelState.AddModelError("test", "test");

            // Act
            var result = contoller.GetGame(1).Result;

            // Assert
            Assert.IsType <BadRequestObjectResult>(result);
        }
Beispiel #6
0
        public void IssueErrorAndMessageShouldGetGameFail()
        {
            // Arrange
            var gameId = 1;

            // Act
            var result     = sutFailure.GetGame(gameId, baseRequest);
            var message    = ((GameResult)((NotFoundObjectResult)result.Result.Result).Value).Message;
            var statusCode = ((NotFoundObjectResult)result.Result.Result).StatusCode;

            // Assert
            Assert.That(result.Result, Is.InstanceOf <ActionResult <Game> >());
            Assert.That(message, Is.EqualTo("Status Code 404: Game not Found"));
            Assert.That(statusCode, Is.EqualTo(404));
        }
Beispiel #7
0
        public async Task GetGame_NonexistentZeroId_NotFoundResult()
        {
            // Arrange
            const int gameId = 0;

            InitializeInmemoryDatabase(out DbContextOptions <GameReviewsContext> options, GetFakeList());

            // Act
            using (var context = new GameReviewsContext(options))
            {
                GamesController gamesController = new GamesController(context);
                var             result          = (await gamesController.GetGame(gameId)).Result;

                // Assert
                Assert.IsType <NotFoundResult>(result);
            }
        }
Beispiel #8
0
        public void SuccessfullyGetGame()
        {
            // Arrange
            var gameId = 1;

            // Act
            var result     = sutSuccess.GetGame(gameId, baseRequest);
            var message    = ((GameResult)((OkObjectResult)result.Result.Result).Value).Message;
            var statusCode = ((OkObjectResult)result.Result.Result).StatusCode;
            var game       = ((GameResult)((OkObjectResult)result.Result.Result).Value).Game;

            // Assert
            Assert.That(result.Result, Is.InstanceOf <ActionResult <Game> >());
            Assert.That(message, Is.EqualTo("Status Code 200: Game Found"));
            Assert.That(statusCode, Is.EqualTo(200));
            Assert.That(game, Is.InstanceOf <Game>());
        }
Beispiel #9
0
        public void Get_should_return_game_by_id()
        {
            //Arrange
            int id = It.IsAny <int>();

            _gameService.Setup(x => x.Get(It.IsAny <int>())).Returns(new GameDTO()
            {
                Id = id
            });

            //Act
            var actionResult = _gamesController.GetGame(It.IsAny <int>());

            //Assert
            actionResult.Should().BeOfType <GameModel>();
            Assert.AreEqual(id, actionResult.Id);
        }
        public void GetGamByKeyReturnNotFoundWhenWronkKey()
        {
            // Arrange
            string wrongKey    = "wrongKey";
            var    mockService = new Mock <IGameService>();

            mockService.Setup(s => s.GetGameByKey(wrongKey)).Returns(It.IsAny <GameDto>());


            var controller = new GamesController(mockService.Object);

            // Act
            var actionResult = controller.GetGame(wrongKey);

            // Assert
            Assert.IsInstanceOfType(actionResult, typeof(NotFoundResult));
        }
Beispiel #11
0
        public void Get_Game_By_ID_Should_Return_Game()
        {
            TestHelpers.SetupDbSet(this.mockSet, this.data);

            GamesController controller = new GamesController(this.mockContext.Object);

            controller.Request       = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();

            IHttpActionResult response = controller.GetGame(data.First().id);

            var result       = response as OkNegotiatedContentResult <game>;
            var coutryResult = result.Content;

            Assert.IsNotNull(response);
            Assert.IsNotNull(coutryResult);
            Assert.AreEqual(data.First(), coutryResult);
        }
Beispiel #12
0
    public void GetGameByInvalidGameIdTest()
    {
        var player = new Player()
        {
            Name = "TestPlayer", Id = Guid.NewGuid()
        };
        var gameId = Guid.NewGuid();

        this.moqGamesRepository.Setup(m => m.GetGame(It.IsAny <Guid>())).Returns(() => null);
        GamesController gameController = new GamesController(this.moqGamesRepository.Object, this.autoMapper);

        var result = gameController.GetGame(gameId);

        Assert.IsNotNull(result);
        Assert.That(result.Result, Is.InstanceOf(typeof(NotFoundResult)));
        NotFoundResult notFoundResult = result.Result as NotFoundResult;

        Assert.AreEqual(notFoundResult.StatusCode, 404);
    }
        public void GetGamByKey_Ok()
        {
            // Arrange
            string correctKey   = "correctKey";
            var    expextedGame = new Mock <GameDto>().Object;
            var    mockService  = new Mock <IGameService>();

            mockService.Setup(s => s.GetGameByKey(correctKey)).Returns(expextedGame);


            var controller = new GamesController(mockService.Object);

            // Act
            var actionResult  = controller.GetGame(correctKey);
            var createdResult = actionResult as OkNegotiatedContentResult <GameDto>;

            // Assert
            Assert.IsInstanceOfType(actionResult, typeof(OkNegotiatedContentResult <GameDto>));
        }
        public async Task GetGame_WhenModelStateIsValidAndGameDoesNotExist_ReturnNotFound()
        {
            // Arrange
            var game = new Game
            {
                ID        = 1,
                StartTime = new DateTime(2018, 01, 01, 00, 00, 00)
            };

            var gameService = new Mock <IGameService>();

            gameService.Setup(s => s.GetGame(1)).Returns(Task.FromResult <Game>(null));
            var controller = new GamesController(gameService.Object);

            // Act
            var result = await controller.GetGame(1);

            // Assert
            var apiResult = Assert.IsType <NotFoundResult>(result);
        }
Beispiel #15
0
    public void GetGameByGuidTest()
    {
        var player = new Player()
        {
            Name = "TestPlayer", Id = Guid.NewGuid()
        };
        var gameId = Guid.NewGuid();
        var game   = RepositoryTestService.GetGame(gameId, new Player[] { player });

        this.moqGamesRepository.Setup(m => m.GetGame(It.IsAny <Guid>())).Returns(game);
        GamesController gameController = new GamesController(this.moqGamesRepository.Object, this.autoMapper);

        var result = gameController.GetGame(gameId);

        Assert.IsNotNull(result);
        Assert.That(result.Result, Is.InstanceOf(typeof(OkObjectResult)));
        OkObjectResult okObjectResult = result.Result as OkObjectResult;

        Assert.AreEqual(okObjectResult.StatusCode, 200);
        Assert.That(okObjectResult.Value, Is.InstanceOf(typeof(GameDTO)));
        Assert.AreEqual((okObjectResult.Value as GameDTO).Id, gameId);
    }
        public async Task GetGame_WhenModelStateIsValidAndGameExists_ReturnGame()
        {
            // Arrange
            var game = new Game
            {
                ID        = 1,
                StartTime = new DateTime(2018, 01, 01, 00, 00, 00)
            };

            var gameService = new Mock <IGameService>();

            gameService.Setup(s => s.GetGame(1)).ReturnsAsync(game);
            var controller = new GamesController(gameService.Object);

            // Act
            var result = await controller.GetGame(1);

            // Assert
            var apiResult    = Assert.IsType <OkObjectResult>(result);
            var returnedGame = (Game)apiResult.Value;

            Assert.Equal(1, returnedGame.ID);
        }
Beispiel #17
0
        public async Task GetGame_ExistingId_TaskActionResultContainsGame()
        {
            // Arrange
            const int gameId = 3;

            Game expectedGame = GetFakeList().Where(d => d.ID == gameId).FirstOrDefault();

            InitializeInmemoryDatabase(out DbContextOptions <GameReviewsContext> options, GetFakeList());

            // Act
            using (var context = new GameReviewsContext(options))
            {
                GamesController gamesController = new GamesController(context);
                var             actionResult    = await gamesController.GetGame(gameId);

                Game         game   = actionResult.Value;
                ActionResult result = actionResult.Result;

                // Assert
                Assert.True(AreEqual(expectedGame, game));
                Assert.IsNotType <NotFoundResult>(result);
            }
        }
        private GameModel TestGameCreate()
        {
            GamesController controller = new GamesController();

            var result = controller.GetGames();

            Assert.IsInstanceOfType(result, typeof(GetGameListType));

            var gameList = ((GetGameListType)result).Content;

            long nextExpectedGameId = 1;

            if (gameList.Count != 0)
            {
                nextExpectedGameId = gameList[gameList.Count - 1].Id + 1;
            }

            // test that nextExpectedGameId does not yet exist
            result = controller.GetGame(nextExpectedGameId);
            Assert.IsInstanceOfType(result, typeof(NotFoundResult));

            // verify that all others in gamelist can be found
            foreach (GameModel game in gameList)
            {
                result = controller.GetGame(game.Id);
                Assert.IsInstanceOfType(result, typeof(GetGameType));
                Assert.AreEqual(game, ((GetGameType)result).Content);
            }

            var player = TestHelpers.TestData.getAuthorizedPlayers()[0];

            // Test Game Create and verify you can get it
            result = controller.CreateGame();

            // Need to provide authorization to create a game
            Assert.IsInstanceOfType(result, typeof(UnauthorizedResult));

            // Add Authorization
            var context = new HttpControllerContext();
            var request = new HttpRequestMessage();

            request.Headers.Add("Authorization", player.AuthCode);

            controller.ControllerContext = context;
            controller.Request           = request;

            result = controller.CreateGame();
            Assert.IsInstanceOfType(result, typeof(PostGameType));

            GameModel newGame = ((PostGameType)result).Content;

            Assert.AreEqual(player.PlayerName, newGame.Hostname);
            Assert.AreEqual(nextExpectedGameId, newGame.Id);

            // Test that game can now be found
            // test that nextExpectedGameId does not yet exist
            result = controller.GetGame(newGame.Id);
            Assert.IsInstanceOfType(result, typeof(GetGameType));

            return(newGame);
        }