Exemplo n.º 1
0
        public async Task It_Is_Possible_To_Invoke_The_Controller_GetAll()
        {
            var serviceMock = new Mock <ICityService>();

            serviceMock.Setup(s => s.GetAll()).ReturnsAsync(
                new List <CityDto>
            {
                new CityDto
                {
                    Id   = Guid.NewGuid(),
                    Name = Faker.Address.City()
                },
                new CityDto
                {
                    Id   = Guid.NewGuid(),
                    Name = Faker.Address.City()
                }
            });

            _controller = new CitiesController(serviceMock.Object);

            var result = await _controller.GetAll();

            Assert.True(result is OkObjectResult);

            var resultValue = ((OkObjectResult)result).Value as IEnumerable <CityDto>;

            Assert.True(resultValue.Count() == 2);
        }
Exemplo n.º 2
0
        public async Task Create_AddCity_AddedCityReturned()
        {
            // Arrange
            var newCity = new CityDto()
            {
                Name = Common.CityName
            };
            var resultCity = new CityDto()
            {
                Id = 1, Name = Common.CityName
            };

            var mock = new Mock <IRestaurantManagementService>();

            mock.Setup(rmService => rmService.AddCityAsync(It.IsAny <CityDto>()))
            .Returns(Task.FromResult(resultCity));

            var loggerMock = new Mock <ILogger <CitiesController> >();

            var controller = new CitiesController(mock.Object, loggerMock.Object);

            // Act
            var result = await controller.Create(newCity);

            // Assert
            var actionResult = Assert.IsType <CreatedAtRouteResult>(result);

            Assert.Equal(resultCity, actionResult?.Value);
        }
Exemplo n.º 3
0
        public async Task Given_A_City_Request_Verify_Service_Method_Are_Called_Once()
        {
            //Arrange
            var cityController = new CitiesController(_serviceCore.Object);

            _serviceCore.Setup(x => x.GetCityByNameAsync(It.IsAny <string>()));
            _serviceCore.Setup(x => x.AddCityAsync(It.IsAny <CityAddTransferModel>()));
            _serviceCore.Setup(x => x.UpdateCityAsync(It.IsAny <int>(), It.IsAny <CityUpdateTransferModel>()));
            _serviceCore.Setup(x => x.DeleteCityAsync(It.IsAny <int>()));
            _serviceCore.Setup(x => x.CityExistsAsync(It.IsAny <int>())).ReturnsAsync(true);

            //Act
            await cityController.GetCity("test");

            await cityController.CreateCity(new CityAddTransferModel());

            await cityController.UpdateCity(1, new CityUpdateTransferModel());

            await cityController.DeleteCity(1);

            //Assert
            _serviceCore.Verify(x => x.GetCityByNameAsync(It.IsAny <string>()), Times.Once);
            _serviceCore.Verify(x => x.GetCityByNameAsync(It.IsAny <string>()), Times.Once);
            _serviceCore.Verify(x => x.AddCityAsync(It.IsAny <CityAddTransferModel>()), Times.Once);
            _serviceCore.Verify(x => x.UpdateCityAsync(It.IsAny <int>(), It.IsAny <CityUpdateTransferModel>()), Times.Once);
            _serviceCore.Verify(x => x.DeleteCityAsync(It.IsAny <int>()), Times.Once);
        }
        public void GetAsync_Return_Empty_List()
        {
            //Arrange
            CityDomainModel cityDomainModel = new CityDomainModel
            {
                Id          = 123,
                CinemasList = new List <CinemaDomainModel>(),
                Name        = "Zrenjanin"
            };

            int expectedStatusCode = 200;

            IEnumerable <CityDomainModel> cityDomainModelsIEn = null;

            Task <IEnumerable <CityDomainModel> > responseTask = Task.FromResult(cityDomainModelsIEn);

            _cityService = new Mock <ICityService>();
            _cityService.Setup(x => x.GetAllAsync()).Returns(responseTask);
            CitiesController citiesController = new CitiesController(_cityService.Object);

            //Act
            var result       = citiesController.GetAsync().ConfigureAwait(false).GetAwaiter().GetResult().Result;
            var objectResult = ((OkObjectResult)result).Value;
            List <CityDomainModel> cityDomainModelResultList = (List <CityDomainModel>)objectResult;

            //Assert
            Assert.IsNotNull(objectResult);
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
            Assert.AreEqual(expectedStatusCode, ((OkObjectResult)result).StatusCode);
        }
        public void GetCity_ById_Async_Return_City_OkObjectResult()
        {
            //Arrange
            CityDomainModel cityDomainModel = new CityDomainModel
            {
                Id          = 123,
                CinemasList = new List <CinemaDomainModel>(),
                Name        = "Zrenjanin"
            };

            int expectedStatusCode = 200;
            Task <CityDomainModel> responseTask = Task.FromResult(cityDomainModel);

            _cityService = new Mock <ICityService>();
            _cityService.Setup(x => x.GetByIdAsync(It.IsAny <int>())).Returns(responseTask);
            CitiesController citiesController = new CitiesController(_cityService.Object);

            //Act
            var             result           = citiesController.GetById(cityDomainModel.Id).ConfigureAwait(false).GetAwaiter().GetResult().Result;
            var             objectResult     = ((OkObjectResult)result).Value;
            CityDomainModel cityDomainResult = (CityDomainModel)objectResult;

            //Assert
            Assert.IsNotNull(objectResult);
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
            Assert.AreEqual(expectedStatusCode, ((OkObjectResult)result).StatusCode);
            Assert.AreEqual(cityDomainModel.Id, cityDomainResult.Id);
        }
Exemplo n.º 6
0
        public async Task It_Is_Possible_To_Invoke_The_Controller_Update()
        {
            var serviceMock = new Mock <ICityService>();

            serviceMock.Setup(s => s.Put(It.IsAny <CityDtoUpdate>())).ReturnsAsync(
                new CityDtoUpdateResult
            {
                Id        = Guid.NewGuid(),
                Name      = "Passo Fundo",
                UpdatedAt = DateTime.Now
            });

            _controller = new CitiesController(serviceMock.Object);

            var cityDtoUpdate = new CityDtoUpdate
            {
                Name = "Passo Fundo"
            };

            var result = await _controller.Put(cityDtoUpdate);

            Assert.True(result is OkObjectResult);

            var resultValue = ((OkObjectResult)result).Value as CityDtoUpdateResult;

            Assert.NotNull(resultValue);
            Assert.Equal(cityDtoUpdate.Name, resultValue.Name);
        }
Exemplo n.º 7
0
        public async Task It_Is_Possible_To_Invoke_The_Controller_Update()
        {
            var serviceMock = new Mock <ICityService>();

            serviceMock.Setup(s => s.Put(It.IsAny <CityDtoUpdate>())).ReturnsAsync(
                new CityDtoUpdateResult
            {
                Id        = Guid.NewGuid(),
                Name      = Faker.Address.City(),
                UpdatedAt = DateTime.Now
            });

            _controller = new CitiesController(serviceMock.Object);
            _controller.ModelState.AddModelError("Name", "Nome é obrigatório.");

            var cityDtoUpdate = new CityDtoUpdate
            {
                Name     = Faker.Address.City(),
                IBGECode = 1
            };

            var result = await _controller.Put(cityDtoUpdate);

            Assert.True(result is BadRequestObjectResult);
        }
Exemplo n.º 8
0
        public void GetGreetings()
        {
            // Arrange
            CitiesController controller = new CitiesController();

            // Act
            IHttpActionResult result = controller.GetGreetings(_CITY_GUID, _USER_GUID);
            var contentResult        = result as OkNegotiatedContentResult <getUserGreetingsResponse>;

            // Assert
            Assert.IsNotNull(result);
            Assert.IsNotNull(contentResult.Content);
            Assert.IsNotNull(contentResult.Content.greetings);
            Assert.IsInstanceOfType(contentResult.Content, typeof(getUserGreetingsResponse));
            Assert.AreNotEqual(contentResult.Content.greetings.Count, 0);

            //***Test Invalid CityGuid Request***
            // Act
            IHttpActionResult resultCityInvalidGuid = controller.GetGreetings("invalid", _USER_GUID);

            // Assert
            Assert.IsNotNull(resultCityInvalidGuid);
            Assert.IsInstanceOfType(resultCityInvalidGuid, typeof(BadRequestErrorMessageResult));
            //*************************

            //***Test Invalid UserGuid Request***
            // Act
            IHttpActionResult resultInvalidUserGuid = controller.GetGreetings(_CITY_GUID, "invalid");

            // Assert
            Assert.IsNotNull(resultInvalidUserGuid);
            Assert.IsInstanceOfType(resultInvalidUserGuid, typeof(BadRequestErrorMessageResult));
            //*************************
        }
Exemplo n.º 9
0
 public CityControllerTest()
 {
     cityMock           = new Mock <IMasterData <City, int> >();
     iMappingMock       = new Mock <IMapping <Core.Model.Mappings.City> >();
     loggerMock         = new Mock <ILogger>();
     mockCityController = new CitiesController(cityMock.Object, loggerMock.Object, iMappingMock.Object);
 }
Exemplo n.º 10
0
        public void GetCitiesControllerInstallNull()
        {
            //Arrange

            var mockCityService = new Mock <ICityService>();
            var cities          = new List <City>();

            cities.Add(new City()
            {
                name = "City of Sydney", id = 6619279, country = "AU"
            });
            mockCityService.Setup(x => x.GetCities("AU")).Returns(cities);
            try
            {
                var controller = new CitiesController(null);

                //Act
                var result = controller.CityDetails("AU");
            }
            catch (Exception ex)
            {
                //Assert
                Assert.AreEqual("System.NullReferenceException", ex.GetType().ToString());
            }
        }
Exemplo n.º 11
0
        public void SetUp()
        {
            logger         = new Mock <ILogger <CitiesController> >();
            citiesServices = new Mock <ICityService>();

            subject = new CitiesController(citiesServices.Object, logger.Object);
        }
Exemplo n.º 12
0
        public Context()
        {
            repository = Substitute.For <ICityInfoRepository>();
            mapper     = Substitute.For <IMapper>();

            controller = new CitiesController(repository, mapper);
        }
Exemplo n.º 13
0
        public async Task GetCities_Should_Return_All_CitiesAsync()
        {
            var cities = new[]
            {
                new CityDto()
                {
                    CityId = 1,
                    Name   = "Stockholm",
                    Sights = new List <SightDto>()
                },
                new CityDto()
                {
                    CityId = 1,
                    Name   = "Lund",
                    Sights = new List <SightDto>()
                }
            };

            var mockedService = new Mock <ICitiesService>();

            mockedService.Setup(x => x.GetAllCitiesAsync())
            .Returns(Task.FromResult((IEnumerable <CityDto>)cities));

            var mockedLogger = new Mock <ILogger <CitiesController> >();
            var controller   = new CitiesController(mockedLogger.Object, mockedService.Object);

            var result = await controller.GetCitiesAsync() as OkObjectResult;

            var data = result.Value as IEnumerable <CityDto>;

            Assert.Collection(data, x => Assert.Contains("Stockholm", x.Name),
                              x => Assert.Contains("Lund", x.Name));
        }
Exemplo n.º 14
0
        public async Task GetCities_CallsServicesForEachCity()
        {
            ICollection <City>    cityList    = new List <City>();
            ICollection <Country> countryList = new List <Country>();

            cityList.Add(new City {
                CountryName = "United Kingdom", Name = "Cardiff"
            });
            _cityRepository.Setup(cr => cr.GetCitiesAsync(It.IsAny <string>())).ReturnsAsync(cityList);
            _mapper.Setup(m => m.Map <CityInformation>(It.IsAny <City>())).Returns(new CityInformation {
                CountryName = "United Kingdom", Name = "Cardiff"
            });
            _countryApiClient.Setup(countries => countries.GetCountriesAsync(It.IsAny <string>())).ReturnsAsync(countryList);
            _weatherApiClient.Setup(weather => weather.GetWeatherForCityAsync(It.IsAny <string>(), It.IsAny <string>()))
            .ReturnsAsync(new Envelope {
                Weather = new Weather[] { new Weather {
                                              Description = "not too shabby"
                                          } }
            });
            _appSettings.SetupGet(appSettings => appSettings.CurrentValue)
            .Returns(new AppSettingsOptions {
                OpenweatherAppId = "12345"
            });

            var c = new CitiesController(_appSettings.Object, _mapper.Object, _countryApiClient.Object, _weatherApiClient.Object, _cityRepository.Object);

            var response = await c.GetCities("mytown");

            Assert.IsNotNull(response);
            Assert.IsInstanceOfType(response, typeof(OkObjectResult));
            Assert.AreEqual("not too shabby", (((OkObjectResult)response).Value as List <CityInformation>).First().WeatherDescription);
            _weatherApiClient.Verify();
            _countryApiClient.Verify();
        }
Exemplo n.º 15
0
        public void GetCities_NameDoesNotExists_ReturnsEmptyList()
        {
            var controller = new CitiesController(new FakeCitiesService());
            var result     = controller.GetCities("non-existing");

            Assert.Empty(result.Value);
        }
Exemplo n.º 16
0
        public async Task It_Is_Possible_To_Invoke_The_Controller_Create()
        {
            var serviceMock = new Mock <ICityService>();

            serviceMock.Setup(s => s.Post(It.IsAny <CityDtoCreate>())).ReturnsAsync(
                new CityDtoCreateResult
            {
                Id        = Guid.NewGuid(),
                Name      = "Passo Fundo",
                CreatedAt = DateTime.Now
            });

            _controller = new CitiesController(serviceMock.Object);
            _controller.ModelState.AddModelError("Name", "Nome é obrigatório.");

            Mock <IUrlHelper> url = new Mock <IUrlHelper>();

            url.Setup(s => s.Link(It.IsAny <string>(), It.IsAny <object>())).Returns("http://localhost:5000");
            _controller.Url = url.Object;

            var cityDtoCreate = new CityDtoCreate
            {
                Name     = "Passo Fundo",
                IBGECode = 1
            };

            var result = await _controller.Post(cityDtoCreate);

            Assert.True(result is BadRequestObjectResult);
        }
Exemplo n.º 17
0
        public async Task It_Is_Possible_To_Invoke_The_Controller_Create()
        {
            var serviceMock = new Mock <ICityService>();

            serviceMock.Setup(s => s.Post(It.IsAny <CityDtoCreate>())).ReturnsAsync(
                new CityDtoCreateResult
            {
                Id        = Guid.NewGuid(),
                Name      = "Passo Fundo",
                CreatedAt = DateTime.Now
            });

            _controller = new CitiesController(serviceMock.Object);
            Mock <IUrlHelper> url = new Mock <IUrlHelper>();

            url.Setup(s => s.Link(It.IsAny <string>(), It.IsAny <object>())).Returns("http://localhost:5000");
            _controller.Url = url.Object;

            var cityDtoCreate = new CityDtoCreate
            {
                Name = "Passo Fundo"
            };

            var result = await _controller.Post(cityDtoCreate);

            Assert.True(result is CreatedResult);

            var resultValue = ((CreatedResult)result).Value as CityDtoCreateResult;

            Assert.NotNull(resultValue);
            Assert.Equal(cityDtoCreate.Name, resultValue.Name);
        }
Exemplo n.º 18
0
        public void GetCities_NameExists_ReturnsOneCity()
        {
            var controller = new CitiesController(new FakeCitiesService());
            var result     = controller.GetCities("existing");

            Assert.Single(result.Value);
        }
Exemplo n.º 19
0
        public async Task GetCity_Should_Return_NotFoundAsync(int id,
                                                              string name, int requestId, string expectedMessage)
        {
            var city = new CityDto()
            {
                CityId = id,
                Name   = name,
                Sights = new List <SightDto>()
            };

            var mockedService = new Mock <ICitiesService>();

            mockedService.Setup(x => x.GetCityByIdAsync(id))
            .Returns(Task.FromResult(city));

            var mockedLogger = new Mock <ILogger <CitiesController> >();
            var controller   = new CitiesController(mockedLogger.Object, mockedService.Object);

            var result = await controller.GetCityAsync(requestId);

            Assert.IsType <NotFoundObjectResult>(result);
            var data = (result as NotFoundObjectResult).Value;

            Assert.Equal(expectedMessage, data);
        }
Exemplo n.º 20
0
 public BaseCityController()
 {
     MockCityRepository = Substitute.For <ICityRepository>();       // Création du mock à partir d'un interafce
     CityController     = new CitiesController(MockCityRepository); // Injection du mock dans le controleur
     CityFaker          = new CityGenerator();
     ToDTO = new EntitiesToDTO();
 }
Exemplo n.º 21
0
        public async Task GetCities_Valid_Test(int page, int pageSize, string cityName)
        {
            // Arrange
            CitiesController citycon = CreateCityController;
            var httpContext          = new Mock <HttpContext>();

            httpContext
            .Setup(m => m.User.IsInRole(Roles.Admin))
            .Returns(true);
            var context = new ControllerContext(
                new ActionContext(
                    httpContext.Object, new RouteData(),
                    new ControllerActionDescriptor()));

            citycon.ControllerContext = context;
            _cityService
            .Setup(c => c.GetAllDTOAsync(It.IsAny <string>()))
            .ReturnsAsync(GetCitiesBySearch());

            // Act
            var result = await citycon.GetCities(page, pageSize, cityName);

            // Assert
            Assert.NotNull(result);
            Assert.IsInstanceOf <OkObjectResult>(result);
            Assert.IsNotNull(((result as ObjectResult).Value as CitiesViewModel)
                             .Cities.Where(c => c.Name.Equals("Львів")));
        }
Exemplo n.º 22
0
        public void GetCity_NoEF_ReturnsNotFound()
        {
            var controller = new CitiesController(_mockCityInfoRepository.Object, _mockMongoRepository.Object, _mockLogger.Object, _mockMailService.Object);
            var result     = controller.GetCity(1);

            Assert.IsInstanceOfType(result, typeof(NotFoundResult));
        }
Exemplo n.º 23
0
        public void GetRating()
        {
            // Arrange
            CitiesController controller = new CitiesController();

            // Act
            IHttpActionResult result = controller.GetRating(_CITY_GUID, _USER_GUID);
            var contentResult        = result as OkNegotiatedContentResult <getRatingForCity>;

            // Assert
            Assert.IsNotNull(result);
            Assert.IsNotNull(contentResult.Content);
            Assert.IsNotNull(contentResult.Content.rating);
            Assert.IsInstanceOfType(contentResult.Content, typeof(getRatingForCity));

            //***Test Invalid CityGuid Request***
            // Act
            IHttpActionResult resultCityInvalidGuid = controller.GetRating("invalid", _USER_GUID);

            // Assert
            Assert.IsNotNull(resultCityInvalidGuid);
            Assert.IsInstanceOfType(resultCityInvalidGuid, typeof(BadRequestErrorMessageResult));
            //*************************

            //***Test Invalid UserGuid Request***
            // Act
            IHttpActionResult resultInvalidUserGuid = controller.GetRating(_CITY_GUID, "invalid");

            // Assert
            Assert.IsNotNull(resultInvalidUserGuid);
            Assert.IsInstanceOfType(resultInvalidUserGuid, typeof(BadRequestErrorMessageResult));
            //*************************
        }
Exemplo n.º 24
0
        static void Main(string[] args)
        {
            ValidatorService validatorService = new ValidatorService();
            FiltrService     filtrService     = new FiltrService();
            CitiesController citiesController = new CitiesController(validatorService, filtrService);
            FiltrData        data             = new FiltrData
            {
                Cities = new City[]
                {
                    new City {
                        Budget = 1000, Name = "Moscow", Point = new Point {
                            X = 1, Y = 1
                        }
                    },
                    new City {
                        Budget = 700, Name = "Piter", Point = new Point {
                            X = 1, Y = 2
                        }
                    },
                    new City {
                        Budget = 300, Name = "Tver", Point = new Point {
                            X = 2, Y = 1
                        }
                    }
                },
                Options = new FiltrOptions
                {
                    Iterations = 12,
                    PercentBet = 0.06
                }
            };

            citiesController.FiltrData(data);
        }
Exemplo n.º 25
0
        public async Task It_Is_Possible_To_Invoke_The_Controller_GetAll()
        {
            var serviceMock = new Mock <ICityService>();

            serviceMock.Setup(s => s.GetAll()).ReturnsAsync(
                new List <CityDto>
            {
                new CityDto
                {
                    Id   = Guid.NewGuid(),
                    Name = Faker.Address.City()
                },
                new CityDto
                {
                    Id   = Guid.NewGuid(),
                    Name = Faker.Address.City()
                }
            });

            _controller = new CitiesController(serviceMock.Object);
            _controller.ModelState.AddModelError("Id", "FormatoInvalido");

            var result = await _controller.GetAll();

            Assert.True(result is BadRequestObjectResult);
        }
Exemplo n.º 26
0
        public void GetCityReturnNotFound()
        {
            var sut = new CitiesController();

            ActionResult response = sut.GetCity(4);

            Assert.IsType <Microsoft.AspNetCore.Mvc.NotFoundResult>(response);
        }
Exemplo n.º 27
0
        public void GetCityReturnOkObjectResult()
        {
            var sut = new CitiesController();

            ActionResult response = sut.GetCity(1);

            Assert.IsType <OkObjectResult>(response);
        }
Exemplo n.º 28
0
        public async Task GetCity_ReturnsOk()
        {
            var controller = new CitiesController();

            var result = await controller.GetCity(1);

            Assert.IsType <OkObjectResult>(result);
        }
Exemplo n.º 29
0
        public void GetAllCities_RedirectsToGetCitiesWithWildcard()
        {
            var c = new CitiesController(_appSettings.Object, _mapper.Object, _countryApiClient.Object, _weatherApiClient.Object, _cityRepository.Object);

            var result = c.GetAllCities();

            Assert.IsInstanceOfType(result, typeof(RedirectToActionResult));
            Assert.AreEqual("*", ((RedirectToActionResult)result).RouteValues["cityName"]);
        }
Exemplo n.º 30
0
        public void PutCityTest()
        {
            var controller = new CitiesController();
            var city1      = new City {
                ID = 4088, Name = "Kittland", District = "Washington", CountryCode = "USA", Population = 888908
            };

            Assert.IsType <OkResult>(controller.Put(4088, city1));
        }
        public void GetAllShouldReturnZeroCitiesWhenEmpty()
        {
            var data = new FakeTouristSitesData();
            var controller = new CitiesController(data);

            var result = controller.Get();

            var okResult = result as OkNegotiatedContentResult<List<CityResponseModel>>;

            Assert.IsNotNull(okResult);
            Assert.AreEqual(0, okResult.Content.Count);
        }
        public void GetShouldReturnOneCityWhenThereIsOne()
        {
            var data = new FakeTouristSitesData();
            data.Cities.Add(new City());
            var controller = new CitiesController(data);
            var result = controller.Get();

            var okResult = result as OkNegotiatedContentResult<List<CityResponseModel>>;

            Assert.IsNotNull(okResult);
            Assert.AreEqual(1, okResult.Content.Count);
        }