コード例 #1
0
        public async Task <ActionResult <ShakespeareResult> > Get([FromRoute] string pokemonName, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(pokemonName))
            {
                return(BadRequest("Poke name required."));
            }

            ShakespeareResult shakespeareResult = null;

            _logger.LogInformation("Pokemon API invoke started");
            var pokemonResult = await _pokemonApiService.GetByName(pokemonName, cancellationToken).ConfigureAwait(false);

            _logger.LogInformation("Pokemon API invoke finished");

            if (pokemonResult != null && pokemonResult.Text.Length > 0)
            {
                _logger.LogInformation("Shakespeare API invoke started");
                var shakespeareApiResult = await _shakespeareApiService.Translate(System.Web.HttpUtility.HtmlEncode(pokemonResult.Text));

                _logger.LogInformation("Shakespeare API invoke finished");

                if (shakespeareApiResult != null)
                {
                    shakespeareResult = new ShakespeareResult
                    {
                        Name        = pokemonName,
                        Description = shakespeareApiResult.Contents.Translated
                    };
                }
            }

            return(shakespeareResult is null ? (ActionResult <ShakespeareResult>)NotFound() : Ok(shakespeareResult));
        }
コード例 #2
0
        public async Task Test_WhenPokemonNameIsValid_ExpectTranslatedPokemon(
            Mock <IPokemonApiService> pokemonApiServiceMock,
            Mock <IShakespeareApiService> shakespeareApiServiceMock,
            Mock <ILogger <PokemonController> > logMock,
            string pokemonName,
            ShakespeareApiResult expectedApiResult,
            ShakespeareResult expectedResult)
        {
            // Arrange
            BuildPokemonApiServiceMock(shakespeareApiServiceMock, expectedApiResult);
            var pokemonController = new PokemonController(pokemonApiServiceMock.Object, shakespeareApiServiceMock.Object, logMock.Object);

            // Act
            var actionResult = await pokemonController.Get(pokemonName, CancellationToken.None);

            // Assert
            if (actionResult.Value != null)
            {
                var objectResult = Assert.IsType <OkObjectResult>(actionResult.Result);
                var translated   = Assert.IsType <ShakespeareResult>(objectResult.Value);
                Assert.Equal(expectedResult, translated, new TranslateResultComparer());
            }
        }