Exemplo n.º 1
0
        public async Task <ActionResult <Model.Pokemonsieur> > GetAsync([FromRoute] string pokemonName)
        {
            try
            {
                if (!Regex.IsMatch(pokemonName, @"^[a-zA-Z]+$"))
                {
                    _logger.LogError("Invalid input - {name}", pokemonName);
                    return(StatusCode(StatusCodes.Status400BadRequest, _pokemonsieurService.GetErrorDetails(StatusCodes.Status400BadRequest)));
                }

                Model.Pokemonsieur pokemonsieur = await _pokemonsieurService.GetDetailsAndTranslateAsync(pokemonName);

                if (pokemonsieur.Error is null)
                {
                    return(pokemonsieur);
                }

                return(StatusCode(pokemonsieur.Error.Code, pokemonsieur.Error));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Application error, check exception details.");
                return(StatusCode(StatusCodes.Status500InternalServerError, _pokemonsieurService.GetErrorDetails(StatusCodes.Status500InternalServerError)));
            }
        }
        public async void Valid_PokemonName_ReturnTranslationApiError()
        {
            //Arrange
            _mockPokemonService.Setup(p => p.GetPokemonDetailsAsync(It.IsAny <string>())).Returns(Task.FromResult(new PokemonDetails
            {
                Name        = TestData._mockName,
                Description = TestData._mockDetails
            }));

            _mockTranslationService.Setup(p => p.GetTranslationAsync(It.IsAny <string>())).Returns(Task.FromResult(new Translation
            {
                Error = new Error
                {
                    Code = 400
                }
            }));

            _pokemonsieurService = new PokemonsieurService(_mockLogger.Object, _mockTranslationService.Object, _mockPokemonService.Object);

            //Act
            var output = await _pokemonsieurService.GetDetailsAndTranslateAsync(TestData._mockName);

            //Assert
            Assert.NotNull(output.Error);
            Assert.Equal(400, output.Error.Code);
        }