public async Task Should_Return_201_Status_Code_If_Pokemon_Created()
        {
            // Arrange
            var client = factory.CreateClient();
            await factory.AuthenticateAsync(client);

            var data = BogusViewModel.PokemonViewModelFaker();

            // Act
            var response = await PerformRequest(client, data);

            var pokemonViewModel = JsonConvert.DeserializeObject <PokemonViewModel>(
                await response.Content.ReadAsStringAsync()
                );

            // Assert
            Assert.Equal(HttpStatusCode.Created, response.StatusCode);

            File.Delete(
                Path.Combine(
                    Directory.GetCurrentDirectory(),
                    "..",
                    "..",
                    "..",
                    "..",
                    "PokeAPI",
                    "wwwroot",
                    "images",
                    pokemonViewModel.PhotoUrl
                    )
                );
        }
        public async Task Should_Not_Create_Pokemon_If_Any_Abilities_Not_Exist(
            bool abilityPresent, bool hiddenAbilityPresent
            )
        {
            // Arrange
            var pokemon = BogusDomain.PokemonFaker();

            var stats = BogusDomain.StatsFaker();

            var abilities = BogusDomain.AbilitiesFaker();

            var data = BogusViewModel.PokemonViewModelFaker(pokemon, stats, abilities);

            mapper.Setup(x => x.Map <Pokemon>(data)).Returns(pokemon);
            mapper.Setup(x => x.Map <Stats>(data.Stats)).Returns(stats);
            pokemonRepository.Setup(x => x.ExistsById(pokemon.Id)).ReturnsAsync(false);
            pokemonRepository.Setup(x => x.ExistsByName(pokemon.Name))
            .ReturnsAsync(false);
            pokemonRepository.Setup(x => x.CreatePokemon(pokemon))
            .ReturnsAsync(pokemon);
            statsRepository.Setup(x => x.CreateStats(stats))
            .ReturnsAsync(stats);
            abilitiesRepository.Setup(x => x.ExistsById(abilities[0].Id))
            .ReturnsAsync(abilityPresent);
            abilitiesRepository.Setup(x => x.ExistsById(abilities[1].Id))
            .ReturnsAsync(hiddenAbilityPresent);

            // Act

            // Assert
            await Assert.ThrowsAsync <AbilityException>(
                () => pokemonServices.CreatePokemon(data)
                );
        }
Ejemplo n.º 3
0
        public async Task AddPreEvolution_Should_Return_200_Status_Code_With_Valid_ViewModel()
        {
            // Arrange
            var rnd = new Random();
            var id  = rnd.Next(1, 151);

            var data = new EvolutionViewModel()
            {
                pokemonId          = id,
                pokemonEvolutionId = id + 1
            };

            var result = BogusViewModel.PokemonViewModelFaker();

            result.Abilities = null;
            result.Stats     = null;

            pokemonServices.Setup(x => x.AddPokemonPreEvolution(data)).ReturnsAsync(
                result
                );

            var pokemonController = new PokemonController(pokemonServices.Object);

            // Act
            var response = await pokemonController.AddPreEvolution(data);

            // Assert
            Assert.NotNull(response);
            Assert.IsType <OkObjectResult>(response.Result);
        }
        public async Task Should_Return_400_If_Pokemon_Stats_Invalid(
            int stats,
            int total
            )
        {
            // Arrange
            var client = factory.CreateClient();
            await factory.AuthenticateAsync(client);

            var data = BogusViewModel.PokemonViewModelFaker();

            data.Stats.HitPoints      = stats;
            data.Stats.Attack         = stats;
            data.Stats.Defense        = stats;
            data.Stats.SpecialAttack  = stats;
            data.Stats.SpecialDefense = stats;
            data.Stats.Speed          = stats;
            data.Stats.Total          = total;

            // Act
            var response = await PerformRequest(client, data);

            var errors = JsonConvert.DeserializeObject <IList <dynamic> >(
                await response.Content.ReadAsStringAsync()
                );

            // Assert
            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
            Assert.Equal(7, errors.Count);
        }
        public async Task Should_Not_Be_Able_To_Add_Pre_Evolution_If_Base_Pokemon_Already_Has_Pre_Evolution()
        {
            // Arrange
            var rnd           = new Random();
            var data          = BogusViewModel.EvolutionViewModelFaker();
            var mappedPokemon = BogusViewModel.PokemonViewModelFaker();

            mappedPokemon.Abilities = null;
            mappedPokemon.Stats     = null;

            var pokemon             = BogusDomain.PokemonFaker();
            var preEvolutionPokemon = BogusDomain.PokemonFaker();

            pokemon.Id             = data.pokemonId;
            pokemon.PreEvolutionId = rnd.Next(1, 152);
            preEvolutionPokemon.Id = data.pokemonEvolutionId - 2;

            pokemonRepository.Setup(x => x.ExistsById(data.pokemonId)).ReturnsAsync(true);
            pokemonRepository.Setup(x => x.ExistsById(data.pokemonEvolutionId)).ReturnsAsync(true);
            pokemonRepository.Setup(x => x.FindById(data.pokemonId)).ReturnsAsync(pokemon);
            pokemonRepository.Setup(x => x.FindById(data.pokemonEvolutionId)).ReturnsAsync(
                preEvolutionPokemon
                );

            // Act

            // Assert
            await Assert.ThrowsAsync <PokemonException>(
                () => pokemonServices.AddPokemonPreEvolution(data)
                );
        }
        public async Task Should_Return_401_Status_Code_If_Admin_Not_Authenticated()
        {
            // Arrange
            var client = factory.CreateClient();

            var data = BogusViewModel.PokemonViewModelFaker();

            // Act
            var response = await PerformRequest(client, data);

            // Assert
            Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
        }
Ejemplo n.º 7
0
        public async Task Should_Return_400_Status_Code_If_Photo_Not_Uploaded()
        {
            // Arrange
            var data = BogusViewModel.PokemonViewModelFaker();

            var pokemonController = new PokemonController(pokemonServices.Object);

            // Act
            var response = await pokemonController.Create(data, null);

            // Assert
            Assert.NotNull(response);
            Assert.IsType <BadRequestObjectResult>(response.Result);
        }
        public async Task Should_Return_400_If_Pokemo_ID_Invalid()
        {
            // Arrange
            var client = factory.CreateClient();
            await factory.AuthenticateAsync(client);

            var data = BogusViewModel.PokemonViewModelFaker();

            data.Id = 0;

            // Act
            var response = await PerformRequest(client, data);

            // Assert
            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
        }
        public async Task Should_Not_Create_Pokemon_If_Number_Not_Unique()
        {
            // Arrange
            var pokemon = BogusDomain.PokemonFaker();

            var stats = BogusDomain.StatsFaker();

            var abilities = BogusDomain.AbilitiesFaker();

            var data = BogusViewModel.PokemonViewModelFaker(pokemon, stats, abilities);

            mapper.Setup(x => x.Map <Pokemon>(data)).Returns(pokemon);
            pokemonRepository.Setup(x => x.ExistsById(pokemon.Id)).ReturnsAsync(true);

            // Act

            // Assert
            await Assert.ThrowsAsync <PokemonException>(
                () => pokemonServices.CreatePokemon(data)
                );
        }
Ejemplo n.º 10
0
        public async Task Should_Return_201_Status_Code_With_Valid_ViewModel()
        {
            // Arrange
            var data = BogusViewModel.PokemonViewModelFaker();

            var responseData = data;

            responseData.Abilities = BogusViewModel.AbilityViewModelFaker(2);

            pokemonServices.Setup(x => x.CreatePokemon(data)).ReturnsAsync(
                responseData
                );

            var pokemonController = new PokemonController(pokemonServices.Object);

            // Act
            var response = await pokemonController.Create(data, photo);

            // Assert
            Assert.NotNull(response);
            Assert.IsType <CreatedResult>(response.Result);
        }
        public async Task Should_Be_Able_To_Add_Pre_Evolution_Pokemon_To_Pokemon()
        {
            // Arrange
            var data          = BogusViewModel.EvolutionViewModelFaker();
            var mappedPokemon = BogusViewModel.PokemonViewModelFaker();

            mappedPokemon.Abilities = null;
            mappedPokemon.Stats     = null;

            var pokemon             = BogusDomain.PokemonFaker();
            var preEvolutionPokemon = BogusDomain.PokemonFaker();

            pokemon.Id             = data.pokemonId;
            preEvolutionPokemon.Id = data.pokemonEvolutionId - 2;

            pokemonRepository.Setup(x => x.ExistsById(data.pokemonId)).ReturnsAsync(true);
            pokemonRepository.Setup(x => x.ExistsById(data.pokemonEvolutionId)).ReturnsAsync(true);
            pokemonRepository.Setup(x => x.FindById(data.pokemonId)).ReturnsAsync(pokemon);
            pokemonRepository.Setup(x => x.FindById(data.pokemonEvolutionId)).ReturnsAsync(
                preEvolutionPokemon
                );
            pokemonRepository.Setup(x => x.SaveChangesToDatabase());

            mapper.Setup(x => x.Map <PokemonViewModel>(pokemon)).Returns(() => {
                mappedPokemon.Id             = data.pokemonId;
                mappedPokemon.PreEvolutionId = data.pokemonEvolutionId;

                return(mappedPokemon);
            });

            // Act
            var response = await pokemonServices.AddPokemonPreEvolution(data);

            // Assert
            Assert.Equal(data.pokemonId, response.Id);
            Assert.Equal(data.pokemonEvolutionId, response.PreEvolutionId);
        }
        public async Task Should_Be_Able_To_Create_New_Pokemon()
        {
            // Arrange
            var pokemon = BogusDomain.PokemonFaker();

            var stats = BogusDomain.StatsFaker();

            var abilities = BogusDomain.AbilitiesFaker();

            var data  = BogusViewModel.PokemonViewModelFaker(pokemon, stats, abilities);
            var photo = LoadPokemonPhoto();

            data.PokemonPhoto = photo;

            var response = new PokemonViewModel()
            {
                Id             = pokemon.Id,
                Name           = pokemon.Name,
                EvolutionLevel = pokemon.EvolutionLevel,
                LevelingRate   = pokemon.LevelingRate,
                CatchRate      = pokemon.CatchRate,
                HatchTime      = pokemon.HatchTime,
                CreatedAt      = pokemon.CreatedAt,
            };

            var statsViewModel = data.Stats;

            var abilityViewModel = new AbilityViewModel()
            {
                Id     = abilities[0].Id,
                Name   = abilities[0].Name,
                Effect = abilities[0].Effect,
            };

            var hiddenAbilityViewModel = new AbilityViewModel()
            {
                Id     = abilities[1].Id,
                Name   = abilities[1].Name,
                Effect = abilities[1].Effect,
            };

            mapper.Setup(x => x.Map <Pokemon>(data)).Returns(pokemon);
            mapper.Setup(x => x.Map <Stats>(data.Stats)).Returns(stats);
            pokemonRepository.Setup(x => x.ExistsById(pokemon.Id)).ReturnsAsync(false);
            pokemonRepository.Setup(x => x.ExistsByName(pokemon.Name))
            .ReturnsAsync(false);
            abilitiesRepository.Setup(x => x.ExistsById(
                                          abilities[0].Id
                                          )).ReturnsAsync(true);
            abilitiesRepository.Setup(x => x.ExistsById(
                                          abilities[1].Id
                                          )).ReturnsAsync(true);
            storageProvider.Setup(x => x.SaveFile(photo)).Returns(Guid.NewGuid().ToString());
            pokemonRepository.Setup(x => x.CreatePokemon(pokemon))
            .ReturnsAsync(pokemon);
            statsRepository.Setup(x => x.CreateStats(stats))
            .ReturnsAsync(stats);
            pokemonRepository.Setup(x => x.CreatePokemonAbility(
                                        data.Id, data.Abilities[0].Id
                                        )).ReturnsAsync(new PokemonAbility()
            {
                PokemonId = data.Id,
                AbilityId = data.Abilities[0].Id
            });
            pokemonRepository.Setup(x => x.CreatePokemonAbility(
                                        data.Id, data.Abilities[1].Id
                                        )).ReturnsAsync(new PokemonAbility()
            {
                PokemonId = data.Id,
                AbilityId = data.Abilities[1].Id
            });
            abilitiesRepository.Setup(x => x.FindByPokemonId(data.Id))
            .ReturnsAsync(new List <Ability>()
            {
                abilities[0],
                abilities[1]
            });
            mapper.Setup(x => x.Map <PokemonViewModel>(pokemon)).Returns(
                response
                );
            mapper.Setup(x => x.Map <StatsViewModel>(stats)).Returns(
                statsViewModel
                );
            mapper.Setup(x => x.Map <List <AbilityViewModel> >(abilities)).Returns(
                new List <AbilityViewModel>()
            {
                abilityViewModel,
                hiddenAbilityViewModel
            }
                );

            // Act
            var result = await pokemonServices.CreatePokemon(data);

            // Assert
            Assert.IsType <PokemonViewModel>(result);
            Assert.IsType <StatsViewModel>(result.Stats);
            Assert.IsType <List <AbilityViewModel> >(result.Abilities);
            Assert.Equal(pokemon.Id, result.Id);
            Assert.Equal(stats.Total, result.Stats.Total);
            Assert.Equal(2, result.Abilities.Count);
            Assert.Equal(
                DateTime.Today.ToLocalTime().Date,
                result.CreatedAt.ToLocalTime().Date
                );
        }