Exemplo n.º 1
0
        public async Task Get_ServiceReturnsNull_ReturnsNotFound()
        {
            _mockPokemonService.Setup(ps => ps.GetPokemon(It.IsAny <string>())).ReturnsAsync((Pokemon)null);

            var getResult = await _controller.Get("name");

            Assert.IsInstanceOfType(getResult.Result, typeof(NotFoundResult));
        }
        public void Get_ReturnsOkObjectResult()
        {
            // Act
            var actionResult   = pokemonController.Get();
            var okObjectResult = (OkObjectResult)actionResult;
            var apiResponse    = (string)okObjectResult.Value;

            // Assert
            Assert.AreEqual(okObjectResult.StatusCode, 200);
            Assert.AreEqual(apiResponse, ControllerMessage.ALL_OK);
        }
        public void Get_Single_Id_Valid()
        {
            int id = 1;

            var pokemon = new Pokemon
            {
                Id    = 1,
                Name  = "Bulbasaur",
                Types = new List <PokemonType> {
                    PokemonType.Grass,
                    PokemonType.Poison,
                },
                Weaknesses = new List <PokemonType> {
                    PokemonType.Fire,
                    PokemonType.Psychic,
                    PokemonType.Flying,
                    PokemonType.Ice,
                },
                Description = "There is a plant seed on its back right from the day this Pokémon is born. The seed slowly grows larger.",
            };

            var logic = new Mock <IPokemonLogic>(MockBehavior.Strict);

            logic.Setup(x => x.Get(id))
            .Returns(pokemon);

            var controller = new PokemonController(logic.Object);
            var result     = controller.Get(id);

            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
            logic.VerifyAll();
        }
        public async Task Get_ListPokemon_ReturnList()
        {
            var pokemonRepositoryMock = new Mock <IPokemonRepository>();
            var pokemonList           = new List <Pokemon> {
                new Pokemon {
                    Id       = 1,
                    Name     = "Bulbasaur",
                    TypeOne  = "Grass",
                    TypeTwo  = "Poison",
                    UrlImage = "http://bulbasaur.png"
                },
                new Pokemon {
                    Id       = 2,
                    Name     = "Venusaur",
                    TypeOne  = "Grass",
                    TypeTwo  = "Poison",
                    UrlImage = "http://venusaur.png"
                }
            };


            pokemonRepositoryMock.Setup(x => x.GetPokemonListAsync()).Returns(Task.FromResult(pokemonList));

            var pokemonController = new PokemonController(pokemonRepositoryMock.Object);
            var response          = await pokemonController.Get();

            Assert.IsType <OkObjectResult>(response.Result);
        }
        public void Test_GetAllPokemon()
        {
            var sut    = new PokemonController();
            var actual = sut.Get();

            Assert.True(actual.Count() > 0);
        }
        public void Get_PlaceholderImplementation_ReturnsMessage() // this is an interim test to get have a test project & test in place with the initial check-in
        {
            var controller = new PokemonController();
            var result     = controller.Get();

            Assert.Equal("Success", result.Content);
        }
Exemplo n.º 7
0
        public void Test_GetPokemon(int id)
        {
            var sut    = new PokemonController(new PokemonDbContext());
            var actual = sut.Get(id);

            Assert.False(string.IsNullOrWhiteSpace(actual.Name));
        }
        public void Get_List_Ids_Valid()
        {
            var ids = new List <int>
            {
                1,
                2,
            };

            var pokemon1 = new Pokemon
            {
                Id    = 1,
                Name  = "Bulbasaur",
                Types = new List <PokemonType> {
                    PokemonType.Grass,
                    PokemonType.Poison,
                },
                Weaknesses = new List <PokemonType> {
                    PokemonType.Fire,
                    PokemonType.Psychic,
                    PokemonType.Flying,
                    PokemonType.Ice,
                },
                Description = "There is a plant seed on its back right from the day this Pokémon is born. The seed slowly grows larger.",
            };

            var pokemon2 = new Pokemon
            {
                Id    = 2,
                Name  = "Ivysaur",
                Types = new List <PokemonType> {
                    PokemonType.Grass,
                    PokemonType.Poison,
                },
                Weaknesses = new List <PokemonType> {
                    PokemonType.Fire,
                    PokemonType.Psychic,
                    PokemonType.Flying,
                    PokemonType.Ice,
                },
                Description = "When the bulb on its back grows large, it appears to lose the ability to stand on its hind legs.",
            };

            var pokemonList = new List <Pokemon>
            {
                pokemon1,
                pokemon2,
            };

            var logic = new Mock <IPokemonLogic>(MockBehavior.Strict);

            logic.Setup(x => x.Get(ids))
            .Returns(pokemonList);

            var controller = new PokemonController(logic.Object);
            var result     = controller.Get(ids);

            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
            logic.VerifyAll();
        }
Exemplo n.º 9
0
        public void Test_GetAllPokemon()
        {
            //httpclient - use this to make a call to our service
            var sut    = new PokemonController(new PokemonDbContext());
            var actual = sut.Get();       //actual is an IActionResult

            Assert.True(actual.> 0);      //we want a list
        }
        public async Task Get_InvalidPokemonName_ShouldReturnBadRequest(string pokemonName)
        {
            var pokemonService = new Mock <IPokemonTranslationService>();
            var controller     = new PokemonController(NullLogger <PokemonController> .Instance, pokemonService.Object);

            var actionResult = await controller.Get(pokemonName);

            actionResult.Result.Should().BeOfType <BadRequestObjectResult>();
        }
Exemplo n.º 11
0
        public async void Get_WhenServicesReturn200_ReturnsOkResult()
        {
            var logger     = Mock.Of <ILogger <PokemonController> >();
            var controller = new PokemonController(logger, new Mock200PokemonService(), new Mock200ShakespeareTranslationService());

            var result = await controller.Get("dummy");

            Assert.IsType <OkObjectResult>(result.Result);
        }
Exemplo n.º 12
0
        public async void Get_WhenTranslationServiceReturns404_ReturnsOkResult()
        {
            var logger     = Mock.Of <ILogger <PokemonController> >();
            var controller = new PokemonController(logger, ServiceTestsUtilities.GetMocked404PokemonService(), new Mock200ShakespeareTranslationService());

            var result = await controller.Get("dummy");

            var statusCodeResult = result.Result as StatusCodeResult;

            Assert.Equal(StatusCodes.Status404NotFound, statusCodeResult.StatusCode);
        }
        public async Task Get_ValidNotExistingPokemonName_ShouldReturnNotFound(string pokemonName)
        {
            var pokemonService = new Mock <IPokemonTranslationService>();

            pokemonService.Setup(service => service.GetPokemon(pokemonName)).ReturnsAsync((Pokemon)null);
            var controller = new PokemonController(NullLogger <PokemonController> .Instance, pokemonService.Object);

            var actionResult = await controller.Get(pokemonName);

            actionResult.Result.Should().BeOfType <NotFoundObjectResult>();
        }
        public async Task Get_ListPokemon_ReturnNull()
        {
            var pokemonRepositoryMock = new Mock <IPokemonRepository>();

            pokemonRepositoryMock.Setup(x => x.GetPokemonListAsync()).Returns(Task.FromResult <List <Pokemon> >(null));

            var pokemonController = new PokemonController(pokemonRepositoryMock.Object);
            var response          = await pokemonController.Get();

            Assert.IsType <NotFoundResult>(response.Result);
        }
Exemplo n.º 15
0
        public async void Test_GetAllPokemon()
        {
            //serialization
            //httpclient
            //casting
            var sut = new PokemonController(new PokemonDbContext());

            var actual = await sut.Get() as List <Pokemon>;

            Assert.NotNull(actual);
            Assert.True(actual.Count() > 0);
        }
Exemplo n.º 16
0
        public void Get_Single_Id_Negative_Returns_BadRequestObjectResult()
        {
            int id = -1;

            var logic = new Mock <IPokemonLogic>(MockBehavior.Strict);

            var controller = new PokemonController(logic.Object);
            var result     = controller.Get(id);

            Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));
            logic.VerifyAll();
        }
Exemplo n.º 17
0
        public async void Test_GetAllPokemon()
        {
            // serialization
            // httpclient
            // casting
            var      sut    = new PokemonController(new PokemonDbContext());
            OkResult actual = await sut.Get() as OkResult;

            // actual.
            // var result =

            // Assert.True(actual.Count() > 0);
        }
Exemplo n.º 18
0
        public async void Should_return_NOT_FOUND_for_non_ASCII_invalid_name(string name)
        {
            var logger            = PokemonControllerTests.GetMockLogger();
            var config            = Options.Create(new BaseConfiguration());
            var transformProvider = new ShakespeareTextTransformProvider();
            var pokemonProvider   = new PokemonProvider();
            var pokemonController = new PokemonController(logger, config, pokemonProvider, transformProvider);

            ContentResult response = (await pokemonController.Get(name)) as ContentResult;

            Assert.NotNull(response);
            Assert.Equal(response.StatusCode, (int)HttpStatusCode.NotFound);
        }
Exemplo n.º 19
0
        public void Get_Single_Id_Null_Pokemon_Returns_NotFoundObjectResult()
        {
            int id = 1;

            var logic = new Mock <IPokemonLogic>(MockBehavior.Strict);

            logic.Setup(x => x.Get(id))
            .Returns((Pokemon)null);

            var controller = new PokemonController(logic.Object);
            var result     = controller.Get(id);

            Assert.IsInstanceOfType(result, typeof(NotFoundObjectResult));
            logic.VerifyAll();
        }
Exemplo n.º 20
0
        public async void Should_handle_Shakespeare_endpoint_not_available(string name, string shakespeareEndpoint)
        {
            var logger = PokemonControllerTests.GetMockLogger();
            var config = Options.Create(new BaseConfiguration());

            config.Value.TextTransformConfiguration.EndpointBaseUrl = shakespeareEndpoint;
            var transformProvider = new ShakespeareTextTransformProvider();
            var pokemonProvider   = new PokemonProvider();
            var pokemonController = new PokemonController(logger, config, pokemonProvider, transformProvider);

            ContentResult response = (await pokemonController.Get(name)) as ContentResult;

            Assert.NotNull(response);
            Assert.Equal(response.StatusCode, (int)HttpStatusCode.RequestTimeout);
        }
Exemplo n.º 21
0
        public async void Should_return_a_description_and_the_correct_name(string name)
        {
            var logger            = PokemonControllerTests.GetMockLogger();
            var config            = Options.Create(new BaseConfiguration());
            var transformProvider = new ShakespeareTextTransformProvider();
            var pokemonProvider   = new PokemonProvider();
            var pokemonController = new PokemonController(logger, config, pokemonProvider, transformProvider);
            var response          = (await pokemonController.Get(name)) as OkObjectResult;

            Assert.NotNull(response);
            Assert.Equal(response.StatusCode, (int)HttpStatusCode.OK);

            PokemonResponseDTO content = response.Value as PokemonResponseDTO;

            Assert.Equal(name, content.Name);
        }
Exemplo n.º 22
0
        public async Task GetReturnsError(HttpStatusCode statusCode, string message, int expectedCode, string expectedMessage)
        {
            var pokemonService = new Mock <IPokemonService>();
            var errorResult    = new ErrorResultContent(statusCode, message);

            pokemonService.Setup(c => c.GetPokemon(_knownName))
            .Returns(Task.FromResult(new Result <ShakespearePokemon>(errorResult)));
            var controller = new PokemonController(pokemonService.Object);

            var result = await controller.Get(_knownName);

            var contentResult = result.Result as ContentResult;

            Assert.AreEqual(expectedCode, contentResult.StatusCode);
            Assert.AreEqual(expectedMessage, contentResult.Content);
        }
Exemplo n.º 23
0
        public void Get_List_Ids_Negative_Returns_BadRequestObjectResult()
        {
            var ids = new List <int>
            {
                1,
                -1,
            };

            var logic = new Mock <IPokemonLogic>(MockBehavior.Strict);

            var controller = new PokemonController(logic.Object);
            var result     = controller.Get(ids);

            Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));
            logic.VerifyAll();
        }
        public async Task TestGet_WhenPokemonNameIsInvalid_ExpectBadRequest(
            string pokemonName)
        {
            // Arrange
            const int expectedBadRequest            = StatusCodes.Status400BadRequest;
            var       pokemonTranslationServiceMock = new Mock <IPokemonTranslationService>();
            var       sutController = new PokemonController(pokemonTranslationServiceMock.Object);

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

            var statusCodeResult = actionResult.Result as IStatusCodeActionResult;

            // Assert
            Assert.NotNull(statusCodeResult);
            Assert.Equal(expectedBadRequest, statusCodeResult.StatusCode);
        }
Exemplo n.º 25
0
        public async Task TestPokemonTranslation_WhenSuccessful_ExpectTranslatedModel(
            IPokemonModel expectedPokemonModel)
        {
            // Arrange
            var pokemonTranslationService = DIHelper.GetServices()
                                            .GetConfiguredService <IPokemonTranslationService>();

            var sutController = new PokemonController(pokemonTranslationService);

            // Act
            var actionResult = await sutController.Get(expectedPokemonModel.Name, CancellationToken.None);

            // Assert
            var objectResult      = Assert.IsType <OkObjectResult>(actionResult.Result);
            var translatedPokemon = Assert.IsType <PokemonModel>(objectResult.Value);

            Assert.Equal(expectedPokemonModel, translatedPokemon, new PokemonModelComparer());
        }
        public async Task TestGet_WhenPokemonNameIsValid_ExpectTranslatedPokemon(
            Mock <IPokemonTranslationService> pokemonTranslationServiceMock,
            string pokemonName,
            PokemonModel expectedPokemonModel)
        {
            // Arrange
            SetupPokemonTranslationServiceMock(pokemonTranslationServiceMock, expectedPokemonModel);
            var sutController = new PokemonController(pokemonTranslationServiceMock.Object);

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

            // Assert
            var objectResult      = Assert.IsType <OkObjectResult>(actionResult.Result);
            var translatedPokemon = Assert.IsType <PokemonModel>(objectResult.Value);

            Assert.Equal(expectedPokemonModel, translatedPokemon, new PokemonModelComparer());
        }
        public async Task TestGet_WhenPokemonNotFound_ExpectNotFound(
            Mock <IPokemonTranslationService> pokemonTranslationServiceMock,
            string pokemonName)
        {
            // Arrange
            const int expectedNotFound = StatusCodes.Status404NotFound;

            SetupPokemonTranslationServiceMock(pokemonTranslationServiceMock, default);
            var sutController = new PokemonController(pokemonTranslationServiceMock.Object);

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

            var statusCodeResult = actionResult.Result as IStatusCodeActionResult;

            // Assert
            Assert.NotNull(statusCodeResult);
            Assert.Equal(expectedNotFound, statusCodeResult.StatusCode);
        }
Exemplo n.º 28
0
        public void Get_List_Ids_Empty_Pokemon_List_Returns_NotFoundObjectResult()
        {
            var ids = new List <int>
            {
                1,
                2,
            };

            var logic = new Mock <IPokemonLogic>(MockBehavior.Strict);

            logic.Setup(x => x.Get(ids))
            .Returns(new List <Pokemon>());

            var controller = new PokemonController(logic.Object);
            var result     = controller.Get(ids);

            Assert.IsInstanceOfType(result, typeof(NotFoundObjectResult));
            logic.VerifyAll();
        }
Exemplo n.º 29
0
        public async Task GetReturnsPokemon()
        {
            var pokemonService = new Mock <IPokemonService>();

            pokemonService.Setup(c => c.GetPokemon(_knownName))
            .Returns(Task.FromResult(
                         new Result <ShakespearePokemon>(
                             new ShakespearePokemon(
                                 new Pokemon(new PokemonDto {
                Name = _knownName
            }),
                                 _knownDescription))));
            var controller = new PokemonController(pokemonService.Object);

            var response = await controller.Get(_knownName);

            Assert.AreEqual(_knownName, response.Value.Name);
            Assert.AreEqual(_knownDescription, response.Value.Description);
        }
Exemplo n.º 30
0
        public async Task Test_WhenPokemonNameIsInvalid_ExpectBadRequest(string pokemonName)
        {
            // Arrange
            const int expectedBadRequest        = StatusCodes.Status400BadRequest;
            var       pokemonApiDataMock        = new Mock <IPokemonApiData>();
            var       pokemonApiServiceMock     = new PokemonApiService(pokemonApiDataMock.Object);
            var       shakespeareApiDataMock    = new Mock <IShakespeareApiData>();
            var       shakespeareApiServiceMock = new ShakespeareApiService(shakespeareApiDataMock.Object);
            var       logMock           = new Mock <ILogger <PokemonController> >();
            var       pokemonController = new PokemonController(pokemonApiServiceMock, shakespeareApiServiceMock, logMock.Object);

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

            var statusCodeResult = actionResult.Result as IStatusCodeActionResult;

            // Assert
            Assert.NotNull(statusCodeResult);
            Assert.Equal(expectedBadRequest, statusCodeResult.StatusCode);
        }