示例#1
0
        public void ConsultaPaginada()
        {
            //arrange
            PreparaBaseDeDados();

            //action
            DtoGameRequest dto = new DtoGameRequest();

            dto.PageNumber = 1;
            dto.PageSize   = 2;
            var result = _controller.Get(dto) as OkObjectResult;
            Dictionary <string, Game> game = (Dictionary <string, Game>)result.Value;

            //assert
            Assert.IsNotNull(result);
            Assert.AreEqual(200, result.StatusCode);
            Assert.IsTrue(game["game_1"].TotalKills == 6);
            Assert.IsTrue(game["game_1"].Players.Length == 3);
            Assert.IsTrue(game["game_1"].Players[0] == "Zeh");
            Assert.IsTrue(game["game_1"].Players[1] == "Isgalamido");
            Assert.IsTrue(game["game_1"].Players[2] == "Dono da Bola");
            Assert.IsTrue(game["game_1"].Kills.Count == 3);
            Assert.IsTrue(game["game_1"].Kills["Zeh"] == 2);
            Assert.IsTrue(game["game_1"].Kills["Isgalamido"] == 1);
            Assert.IsTrue(game["game_1"].Kills["Dono da Bola"] == -5);

            Assert.IsTrue(game["game_2"].TotalKills == 2);
            Assert.IsTrue(game["game_2"].Players.Length == 2);
            Assert.IsTrue(game["game_2"].Players[0] == "Zeh");
            Assert.IsTrue(game["game_2"].Players[1] == "Dono da Bola");
            Assert.IsTrue(game["game_2"].Kills.Count == 2);
            Assert.IsTrue(game["game_2"].Kills["Zeh"] == 1);
            Assert.IsTrue(game["game_2"].Kills["Dono da Bola"] == -2);
        }
示例#2
0
        public async void CrudFriend()
        {
            //CREATE
            var gamePost = new Game {
                Title       = "Game Test",
                Description = "Description Test",
                Genre       = "Genre Test"
            };

            var postResult = await gamesController.Post(gamePost);

            var okPostResult = (OkObjectResult)postResult.Result;

            var game = okPostResult.Value as Game;

            Assert.NotEmpty(game.Id);

            //UPDATE
            gamePost.Title = "Test2";

            var putResult = await gamesController.Put(game.Id, gamePost);

            var okPutResult = (OkObjectResult)putResult.Result;

            game = okPutResult.Value as Game;

            Assert.Equal("Test2", game.Title);

            //LIST
            var getLResult = await gamesController.Get();

            var okGetLResult = (ObjectResult)getLResult.Result;

            var lgame = okGetLResult.Value as IEnumerable <Game>;

            Assert.NotEmpty(lgame.Where(a => a.Title == "Test2"));

            //GET
            var getResult = await gamesController.Get(game.Id);

            var okGetResult = (ObjectResult)getResult.Result;

            game = okGetResult.Value as Game;

            Assert.Equal("Test2", game.Title);


            //REMOVE
            var deleteResult = await gamesController.Delete(game.Id);

            Assert.IsType <OkResult>(deleteResult);
        }
示例#3
0
        public void GetAll_WhenScoresInDb_ShouldReturnScores()
        {
            var repo = Mock.Create <IRepository <Game> >();

            Mock.Arrange(() => repo.All())
            .Returns(() => new List <Game>().AsQueryable());

            var data = Mock.Create <IBullsAndCowsData>();

            Mock.Arrange(() => data.Games)
            .Returns(() => repo);

            var controller = new GamesController(data, null, null, null);

            this.SetupController(controller);

            var actionResult = controller.Get();

            var response = actionResult.ExecuteAsync(CancellationToken.None).Result;

            var actual = response.Content.ReadAsStringAsync().Result;

            var expected = "[]"; //empty array

            Assert.AreEqual(actual, expected);
        }
示例#4
0
        public async Task Quando_Requisitar_Get()
        {
            // Arrange
            var serviceMock = new Mock <IGameService>();
            var id          = Guid.NewGuid();

            serviceMock.Setup(a => a.Get(It.IsAny <Guid>())).ReturnsAsync(new GameEntity
            {
                GameId   = 5,
                PlayerId = 4,
                Win      = 2,
                Id       = id,
            });

            _controller = new GamesController(serviceMock.Object);

            // Act
            var result = await _controller.Get(id);

            // Assert
            Assert.True(result is OkObjectResult);
            var resultValue = ((OkObjectResult)result).Value as GameEntity;

            Assert.NotNull(resultValue);
            Assert.Equal(4, resultValue.PlayerId);
            Assert.Equal(5, resultValue.GameId);
        }
        public void ShouldReturnGamesByFilter()
        {
            var model = new GamesFilterModel
            {
                GameName = "GameName",
                MaxPrice = 10,
                MinPrice = 5,
                PageInfo = new PageInfo
                {
                    PageNumber = 1,
                    PageSize   = PageSize.TenItems,
                    TotalItems = 20
                },

                PublishingDatePeriod     = PublishingDatePeriod.AllTime,
                SelectedGenresIds        = new int[] { },
                SelectedPublishersIds    = new int[] { },
                SelectedPlatformTypesIds = new int[] { },
                SortingObject            = SortingObject.Default
            };

            _gameService
            .Setup(m => m.GetGames(It.IsAny <GamesFilterAttributes>()))
            .Returns(new List <GameDto>());

            _gamesController.Get(model);

            _gameService.Verify(m => m.GetGames(It.IsAny <GamesFilterAttributes>()), Times.AtLeastOnce);
        }
示例#6
0
        public void Get_IdValid_GameExists_Player1HasntPlayed_Player2DoesntExist()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");

            // Act
            HttpResponseMessage postResponse = controller.Post(postFromBody);

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

            HttpResponseMessage result = controller.Get(player1EnteredGame.GameId);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out FilteredGame content));
            Assert.AreEqual(content.ID, player1EnteredGame.GameId);
            Assert.AreEqual($"Waiting for player 1 {postFromBody.Name} to play." +
                            Environment.NewLine +
                            "Waiting for Player 2 to join the game.", content.Information);
            Assert.IsNotNull(content.Player1);
            Assert.AreEqual(postFromBody.Name, content.Player1.Name);
            Assert.AreEqual(string.Empty, content.Player1.Move);
            Assert.IsNotNull(content.Player2);
            Assert.AreEqual(string.Empty, content.Player2.Name);
            Assert.AreEqual(string.Empty, content.Player2.Move);
        }
示例#7
0
        public void Move_ModelNotNull_NameValid_MoveValid_IdValid_GameExists_Player1Exists_Player2Exists_Player1HasPlayed_Player2HasntPlayed_DrawnGame()
        {
            // Arrange
            GamesController controller    = NewGamesController();
            PostFromBody    postFromBody  = new PostFromBody("John");
            JoinFromBody    joinFromBody  = new JoinFromBody("Jack");
            MoveFromBody    moveFromBody1 = new MoveFromBody(postFromBody.Name, "Scissors");
            MoveFromBody    moveFromBody2 = new MoveFromBody(joinFromBody.Name, moveFromBody1.Move);

            // Act
            HttpResponseMessage postResponse = controller.Post(postFromBody);

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

            controller.Move(player1EnteredGame.GameId, moveFromBody1);

            HttpResponseMessage joinResponse = controller.Join(player1EnteredGame.GameId, joinFromBody);

            joinResponse.TryGetContentValue(out PlayerEnteredGame player2EnteredGame);

            HttpResponseMessage getResultBefore = controller.Get(player1EnteredGame.GameId);

            getResultBefore.TryGetContentValue(out FilteredGame filteredGameBefore);

            HttpResponseMessage result = controller.Move(player1EnteredGame.GameId, moveFromBody2);

            HttpResponseMessage getResultAfter = controller.Get(player1EnteredGame.GameId);

            getResultAfter.TryGetContentValue(out FilteredGame filteredGameAfter);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.IsNull(result.Content);

            Assert.AreEqual($"Waiting for player 2 {moveFromBody2.Name} to play.", filteredGameBefore.Information);
            Assert.AreEqual(moveFromBody1.Name, filteredGameBefore.Player1.Name);
            Assert.AreEqual($"{moveFromBody1.Name} has already played.", filteredGameBefore.Player1.Move);
            Assert.AreEqual(moveFromBody2.Name, filteredGameBefore.Player2.Name);
            Assert.AreEqual(string.Empty, filteredGameBefore.Player2.Move);

            Assert.AreEqual("The players played a drawn game.", filteredGameAfter.Information);
            Assert.AreEqual(moveFromBody1.Name, filteredGameAfter.Player1.Name);
            Assert.AreEqual(moveFromBody1.Move, filteredGameAfter.Player1.Move);
            Assert.AreEqual(moveFromBody2.Name, filteredGameAfter.Player2.Name);
            Assert.AreEqual(moveFromBody2.Move, filteredGameAfter.Player2.Move);
        }
示例#8
0
        public void Move_ModelNotNull_NameValid_MoveValid_IdValid_GameExists_Player1Exists_Player2Exists_Player1HasntPlayed_Player2HasAlreadyPlayed()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");
            JoinFromBody    joinFromBody = new JoinFromBody("Jack");
            MoveFromBody    moveFromBody = new MoveFromBody(joinFromBody.Name, "Rock");

            // Act
            HttpResponseMessage postResponse = controller.Post(postFromBody);

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

            HttpResponseMessage joinResponse = controller.Join(player1EnteredGame.GameId, joinFromBody);

            joinResponse.TryGetContentValue(out PlayerEnteredGame player2EnteredGame);

            controller.Move(player1EnteredGame.GameId, moveFromBody);

            HttpResponseMessage getResultBefore = controller.Get(player1EnteredGame.GameId);

            getResultBefore.TryGetContentValue(out FilteredGame filteredGameBefore);

            HttpResponseMessage result = controller.Move(player1EnteredGame.GameId, moveFromBody);

            HttpResponseMessage getResultAfter = controller.Get(player1EnteredGame.GameId);

            getResultAfter.TryGetContentValue(out FilteredGame filteredGameAfter);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out string description));
            Assert.AreEqual($"Player 2 {joinFromBody.Name} has already played.", description);

            Assert.AreEqual($"Waiting for player 1 {postFromBody.Name} to play.", filteredGameBefore.Information);
            Assert.AreEqual(postFromBody.Name, filteredGameBefore.Player1.Name);
            Assert.AreEqual(string.Empty, filteredGameBefore.Player1.Move);
            Assert.AreEqual(joinFromBody.Name, filteredGameBefore.Player2.Name);
            Assert.AreEqual($"{joinFromBody.Name} has already played.", filteredGameBefore.Player2.Move);

            Assert.AreEqual($"Waiting for player 1 {postFromBody.Name} to play.", filteredGameAfter.Information);
            Assert.AreEqual(postFromBody.Name, filteredGameAfter.Player1.Name);
            Assert.AreEqual(string.Empty, filteredGameAfter.Player1.Move);
            Assert.AreEqual(joinFromBody.Name, filteredGameAfter.Player2.Name);
            Assert.AreEqual($"{joinFromBody.Name} has already played.", filteredGameAfter.Player2.Move);
        }
示例#9
0
        public void Move_ModelNotNull_NameValid_MoveValid_IdValid_GameExists_Player1Exists_Player2DoesntExist_Player1HasntPlayed()
        {
            // Arrange
            GamesController controller   = NewGamesController();
            PostFromBody    postFromBody = new PostFromBody("John");
            MoveFromBody    moveFromBody = new MoveFromBody(postFromBody.Name, "Rock");

            // Act
            HttpResponseMessage postResponse = controller.Post(postFromBody);

            postResponse.TryGetContentValue(out PlayerEnteredGame player1EnteredGame);

            HttpResponseMessage getResultBefore = controller.Get(player1EnteredGame.GameId);

            getResultBefore.TryGetContentValue(out FilteredGame filteredGameBefore);

            HttpResponseMessage result = controller.Move(player1EnteredGame.GameId, moveFromBody);

            HttpResponseMessage getResultAfter = controller.Get(player1EnteredGame.GameId);

            getResultAfter.TryGetContentValue(out FilteredGame filteredGameAfter);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.IsNull(result.Content);

            Assert.AreEqual($"Waiting for player 1 {postFromBody.Name} to play." +
                            Environment.NewLine +
                            "Waiting for Player 2 to join the game.", filteredGameBefore.Information);
            Assert.AreEqual(postFromBody.Name, filteredGameBefore.Player1.Name);
            Assert.AreEqual(string.Empty, filteredGameBefore.Player1.Move);
            Assert.AreEqual(string.Empty, filteredGameBefore.Player2.Name);
            Assert.AreEqual(string.Empty, filteredGameBefore.Player2.Move);

            Assert.AreEqual("Waiting for Player 2 to join the game.", filteredGameAfter.Information);
            Assert.AreEqual(postFromBody.Name, filteredGameAfter.Player1.Name);
            Assert.AreEqual($"{moveFromBody.Name} has already played.", filteredGameAfter.Player1.Move);
            Assert.AreEqual(string.Empty, filteredGameAfter.Player2.Name);
            Assert.AreEqual(string.Empty, filteredGameAfter.Player2.Move);
        }
        public void GetGames_BLLReturnsNothing_ReturnsEmptyJson()
        {
            // Arrange
            var mock = new Mock <IStoreService>();

            mock.Setup(a => a.GetGames()).Returns(new List <GameDTO>());
            var sut = new GamesController(mock.Object, _loggerMock.Object);

            // Act
            var res = sut.Get();

            // Assert
            Assert.That(res, Is.TypeOf(typeof(JsonResult)));
            Assert.That(res.Data, Is.Empty);
        }
示例#11
0
        public void Get_IdValid_GameDoesntExist()
        {
            // Arrange
            GamesController controller = NewGamesController();
            string          id         = "abcd";

            // Act
            HttpResponseMessage result = controller.Get(id);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out string description));
            Assert.AreEqual($"Game {id} doesn't exist.", description);
        }
示例#12
0
        public void Get_IdEmpty()
        {
            // Arrange
            GamesController controller = NewGamesController();
            string          id         = string.Empty;

            // Act
            HttpResponseMessage result = controller.Get(id);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode);
            Assert.IsTrue(result.TryGetContentValue(out string description));
            Assert.AreEqual("You must provide a game id.", description);
        }
示例#13
0
    public async void GetNonexistentGameTest(long id)
    {
        // Arrange
        var mockRepo = new Mock <ICasinoRepository>();

        mockRepo.Setup(repo => repo.GetGameAsync(id))
        .ReturnsAsync(GetTestGame());
        var controller = new GamesController(mockRepo.Object);

        // Act
        var result = await controller.Get(4444);

        // Assert
        var notFoundResult = Assert.IsType <ObjectResult>(result);

        Assert.Equal(StatusCodes.Status204NoContent, notFoundResult.StatusCode);
    }
示例#14
0
    public async void GetSpecificGameTest(long id)
    {
        // Arrange
        var mockRepo = new Mock <ICasinoRepository>();

        mockRepo.Setup(repo => repo.GetGameAsync(id))
        .ReturnsAsync(GetTestGame());
        var controller = new GamesController(mockRepo.Object);

        // Act
        var result = await controller.Get(id);

        // Assert
        var okResult    = Assert.IsType <OkObjectResult>(result);
        var returnValue = Assert.IsType <Game>(okResult.Value);

        Assert.Equal(GetTestGame().Name, returnValue.Name);
    }
        public void GetGames_BLLReturnsSomeData_ReturnsGamesJson()
        {
            // Arrange
            var mock = new Mock <IStoreService>();

            mock.Setup(a => a.GetGames()).Returns(new List <GameDTO> {
                new GameDTO(), new GameDTO()
            });
            var sut = new GamesController(mock.Object, _loggerMock.Object);

            // Act
            var res = sut.Get();

            // Assert
            Assert.That(res, Is.TypeOf(typeof(JsonResult)));
            Assert.That(res.Data, Is.TypeOf(typeof(List <GameViewModel>)));
            Assert.That(res.Data as List <GameViewModel>, Has.Count.EqualTo(2));
        }
示例#16
0
    public async void GetAllGamesTest()
    {
        // Arrange
        var mockRepo = new Mock <ICasinoRepository>();

        mockRepo.Setup(repo => repo.GetAllGamesAsync())
        .ReturnsAsync(GetTestGames());
        var controller = new GamesController(mockRepo.Object);

        // Act
        var result = await controller.Get();

        // Assert
        var okResult    = Assert.IsType <OkObjectResult>(result);
        var returnValue = Assert.IsType <List <Game> >(okResult.Value);

        Assert.Equal(GetTestGame().Name, returnValue.FirstOrDefault().Name);
    }
        //
        // GET: /Game/
        public ActionResult Index()
        {
            var jsonObject = apiController.Get();

            if (jsonObject.Data == null)
            {
                // Do we have errors?
                if (jsonObject.Errors.Any())
                {
                    ViewBag.Flash = string.Join(",", jsonObject.Errors);
                }
                return(View(new List <Game>()));
            }
            else
            {
                return(View(jsonObject.Data));
            }
        }
示例#18
0
        public void GetByIdApiTest()
        {
            int pageId = 1;

            List <Game> games = JsonConvert.DeserializeObject <List <Game> >(GamesApi.Resource.GamesArrayJson);

            DbHelper.FormatCommentDatesForUi(games);

            var searchService = new Mock <IGamesApiService>();

            searchService.Setup(s => s.RetrieveGameById(1)).
            Returns(games[0]);

            var controller = new GamesController(searchService.Object);

            var result = controller.Get(pageId);

            Assert.True(result.Value.Equals(GamesApi.Resource.ExampleJson));
        }
示例#19
0
    public async void GetAllGamesEmptyTest()
    {
        // Arrange
        var mockRepo = new Mock <ICasinoRepository>();

        mockRepo.Setup(repo => repo.GetAllGamesAsync())
        .ReturnsAsync(new List <Game>());
        var controller = new GamesController(mockRepo.Object);

        // Act
        var result = await controller.Get();

        // Assert
        var okResult    = Assert.IsType <OkObjectResult>(result);
        var returnValue = Assert.IsType <List <Game> >(okResult.Value);

        // Empty repository returns empty list
        Assert.Empty(returnValue);
    }
示例#20
0
        public async Task Quando_Requisitar_Get_BadRequest()
        {
            // Arrange
            var serviceMock = new Mock <IGameService>();

            serviceMock.Setup(a => a.Get(It.IsAny <Guid>())).ReturnsAsync(new GameEntity()
            {
                GameId   = 5,
                PlayerId = 4,
                Win      = 2,
                Id       = Guid.NewGuid(),
            });

            _controller = new GamesController(serviceMock.Object);
            _controller.ModelState.AddModelError("Id", "Formato Invalido");

            // Act
            var result = await _controller.Get(Guid.NewGuid());

            // Assert
            Assert.True(result is BadRequestObjectResult);
            Assert.False(_controller.ModelState.IsValid);
        }
示例#21
0
        public void PlayGame()
        {
            var controller = new GamesController();

            var result = controller.Get();
            var list   = result.Select(JsonConvert.DeserializeObject <HangmanGame>).ToArray();

            Assert.IsTrue(!list.Any());

            controller.Post();

            result = controller.Get();
            list   = result.Select(JsonConvert.DeserializeObject <HangmanGame>).ToArray();
            Assert.AreEqual(list.Count(), 1);

            var id = list.First().GameID;

            var firstGame = JsonConvert.DeserializeObject <HangmanGame>(controller.Get(id));

            Assert.IsNull(firstGame.GetWord()); //dont hint the word to the user.

            Assert.AreEqual(firstGame.Status, GameStatus.Busy);
            Assert.AreEqual(firstGame.TriesLeft, 11);
            Assert.IsFalse(firstGame.Guess.Any(x => x != '.'));

            firstGame = JsonConvert.DeserializeObject <HangmanGame>(controller.Get(id));

            var nextChar  = 'a';
            var triesLeft = 11;
            var guess     = firstGame.Guess;

            do
            {
                controller.Post(id, nextChar);
                firstGame = JsonConvert.DeserializeObject <HangmanGame>(controller.Get(id));

                if (firstGame.Status == GameStatus.Success)
                {
                    Assert.IsFalse(firstGame.Guess.Any(x => x == '.'));
                }

                if (firstGame.TriesLeft == triesLeft)
                {
                    Assert.AreNotEqual(guess, firstGame.Guess);
                    Assert.IsTrue(firstGame.Guess.Contains(nextChar));
                }
                else
                {
                    Assert.AreEqual(guess, firstGame.Guess);
                    --triesLeft;
                }

                if (firstGame.TriesLeft == 0)
                {
                    Assert.AreEqual(firstGame.Status, GameStatus.Fail);
                }
                guess = firstGame.Guess;
                ++nextChar;
            } while (firstGame.TriesLeft > 0 && firstGame.Status == GameStatus.Busy);


            controller.Post();

            result = controller.Get();
            list   = result.Select(JsonConvert.DeserializeObject <HangmanGame>).ToArray();
            Assert.AreEqual(list.Count(), 2);
            id = list.Last().GameID;

            var secondGame = JsonConvert.DeserializeObject <HangmanGame>(controller.Get(id));
            var clrGame    = HangmanFactory.Current.GetGame(id);

            var answer = clrGame.GetWord();

            Assert.AreEqual(answer.Length, secondGame.Guess.Length);

            foreach (var a in answer)
            {
                if (secondGame.Guess.Contains(a))
                {
                    continue;
                }

                controller.Post(id, a);
                secondGame = JsonConvert.DeserializeObject <HangmanGame>(controller.Get(id));
                Assert.AreEqual(secondGame.TriesLeft, 11);

                if (secondGame.Status == GameStatus.Success)
                {
                    break;
                }
            }

            Assert.AreEqual(secondGame.Status, GameStatus.Success);
            Assert.AreEqual(secondGame.TriesLeft, 11);
            Assert.AreEqual(secondGame.Guess, answer);
        }
示例#22
0
 dynamic GetGame(int id)
 {
     return((controller.Get(id) as DynamicJsonResult).Data);
 }