Example #1
0
        /// <inheritdoc/>
        public Pokemon Get(int id)
        {
            if (id < 1)
            {
                throw new InvalidOperationException($"Id must be at least 1. Id parameter: {id}.");
            }

            var pokemon = pokemonRepository.Get(id);

            return(pokemon);
        }
Example #2
0
        public void IfRepositoryThrowsArgumentOutOfRangeException_GetPokemon_ShouldReturnHttpNotFound()
        {
            var pokemonName = new Faker().Lorem.Word();

            A.CallTo(() => _pokemonRepository.Get(A <string> ._)).Throws <ArgumentOutOfRangeException>();

            var actionResult = _pokemonService.GetPokemon(pokemonName);

            var result = CastActionResultOrFail <NotFoundResult, Pokemon>(actionResult);

            result.StatusCode.Should().Be((int)HttpStatusCode.NotFound);
        }
        public ActionResult <Pokemon> GetPokemon(string name)
        {
            try
            {
                var sw = AStartedStopwatch();
                _logger.LogInformation($"{nameof(GetPokemon)} invoked, pokemon name: {name}");

                var result = ToPokemon(_pokemonRepository.Get(name));

                _logger.LogInformation($"{nameof(GetPokemon)} completed, elapsed time: {sw.ElapsedMilliseconds}ms");

                return(result);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                _logger.LogError(ex, "No pokemon found with the given name");
                return(NotFound());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "An unexpected error occurred while retrieving pokemon information");
                return(StatusCode(500));
            }
        }
Example #4
0
 public PokemonModel Get(long id)
 {
     return(new PokemonModel(pokemonRepository.Get(id)));
 }
Example #5
0
 public void IfGivenNameIsNull_Get_ShouldThrowException()
 {
     this.Invoking(t => _pokemonRepository.Get(null))
     .Should().ThrowExactly <ArgumentNullException>()
     .And.ParamName.Should().Be("name");
 }