public async void Edit_RedirectToAction()
        {
            // Arrange
            Apartment apartment = new Apartment {
                Id = 1
            };

            Mock <ApartmentRepository> mockApartamentRepository = new Mock <ApartmentRepository>();

            Mock <IUnitOfWork> mockIUnitOfWork = new Mock <IUnitOfWork>();

            mockIUnitOfWork
            .Setup(uw => uw.GetRepository <Apartment, ApartmentRepository>())
            .Returns(mockApartamentRepository.Object);

            Mock <IFileService> mockIFileService = new Mock <IFileService>();

            ApartmentsController controller = new ApartmentsController(mockIUnitOfWork.Object, mockIFileService.Object);

            // Act
            IActionResult result = await controller.Edit(apartment.Id, apartment, null);

            // Assert
            Assert.NotNull(result);
            RedirectToActionResult redirect = Assert.IsType <RedirectToActionResult>(result);

            Assert.Equal(nameof(ApartmentsController.Index), redirect.ActionName);
            Assert.Null(redirect.ControllerName);
        }
        public async void DeleteConfirmed_RedirectToActionResult()
        {
            // Arrange
            Apartment apartment = new Apartment
            {
                Id          = 1,
                Description = "nice",
                Price       = 50
            };

            Mock <ApartmentRepository> mockApartmentRepository = new Mock <ApartmentRepository>();

            mockApartmentRepository
            .Setup(ap => ap.GetAsync(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(Task.FromResult(apartment));

            Mock <IUnitOfWork> mockUnitOfWork = new Mock <IUnitOfWork>();

            mockUnitOfWork
            .Setup(u => u.GetRepository <Apartment, ApartmentRepository>())
            .Returns(mockApartmentRepository.Object);
            Mock <IFileService> mockIIFileService = new Mock <IFileService>();

            ApartmentsController controller = new ApartmentsController(mockUnitOfWork.Object, mockIIFileService.Object);

            // Act
            IActionResult result = await controller.DeleteConfirmed(apartment.Id);

            // Assert
            Assert.NotNull(result);
            RedirectToActionResult redirect = Assert.IsType <RedirectToActionResult>(result);

            Assert.Equal(nameof(ApartmentsController.Index), redirect.ActionName);
            Assert.Null(redirect.ControllerName);
        }
        public async void Delete_ViewResult()
        {
            // Arrange
            Apartment apartment = new Apartment {
                Id = 7, Name = "NewApartament"
            };

            Mock <ApartmentRepository> mockApartamentRepository = new Mock <ApartmentRepository>();

            mockApartamentRepository
            .Setup(ar => ar.GetAsync(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(Task.FromResult(apartment));

            Mock <IUnitOfWork> mockIUnitOfWork = new Mock <IUnitOfWork>();

            mockIUnitOfWork
            .Setup(uw => uw.GetRepository <Apartment, ApartmentRepository>())
            .Returns(mockApartamentRepository.Object);
            Mock <IFileService> mockIFileService = new Mock <IFileService>();

            ApartmentsController controller = new ApartmentsController(mockIUnitOfWork.Object, mockIFileService.Object);

            // Act
            IActionResult result = await controller.Delete(apartment.Id);

            // Assert
            Assert.NotNull(result);
            ViewResult viewResult = Assert.IsType <ViewResult>(result);

            Assert.NotNull(viewResult.Model);
            Apartment apartmentModel = Assert.IsType <Apartment>(viewResult.Model);

            Assert.Same(apartment, apartmentModel);
            Assert.Null(viewResult.ViewName);
        }
        public void GetApartmentImage_OkObjectResult()
        {
            // Arrange
            Apartment apartment = new Apartment {
                Id = 5, Name = "Avalon", Price = 100, MainPhoto = "house.jpg"
            };

            Mock <ApartmentRepository> mockApartmentRepository = new Mock <ApartmentRepository>();

            mockApartmentRepository
            .Setup(ar => ar.GetImageById(It.IsAny <int>()))
            .Returns(apartment.MainPhoto);

            Mock <IUnitOfWork> mockIUnitOfWork = new Mock <IUnitOfWork>();

            mockIUnitOfWork
            .Setup(u => u.GetRepository <Apartment, ApartmentRepository>())
            .Returns(mockApartmentRepository.Object);
            Mock <IFileService> mockIIFileService = new Mock <IFileService>();

            ApartmentsController controller = new ApartmentsController(mockIUnitOfWork.Object, mockIIFileService.Object);

            // Act
            IActionResult result = controller.GetApartmentImage(apartment.Id);

            // Assert
            Assert.NotNull(result);
            OkObjectResult okObjectResult = Assert.IsType <OkObjectResult>(result);

            Assert.NotNull(okObjectResult.Value);
            string mainPhotoModel = Assert.IsType <string>(okObjectResult.Value);

            Assert.Same(apartment.MainPhoto, mainPhotoModel);
        }
        public async void SingleApartmentIsNull_NotFoundResult()
        {
            // Arrange
            int apartmentId = 1;
            Mock <ApartmentRepository> mockApartmentRepository = new Mock <ApartmentRepository>();

            mockApartmentRepository
            .Setup(ar => ar.GetAsync(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(Task.FromResult(null as Apartment));

            Mock <IUnitOfWork> mockUnitOfWork = new Mock <IUnitOfWork>();

            mockUnitOfWork
            .Setup(u => u.GetRepository <Apartment, ApartmentRepository>())
            .Returns(mockApartmentRepository.Object);

            ApartmentsController controller = new ApartmentsController(mockUnitOfWork.Object);

            // Act
            IActionResult result = await controller.Single(apartmentId);

            // Assert
            Assert.NotNull(result);
            Assert.IsType <NotFoundResult>(result);
        }
        public async void EditModelIsNotValid_ViewResult()
        {
            // Arrange
            Apartment apartment = new Apartment {
                Id = 1
            };

            Mock <IUnitOfWork>   mockIUnitOfWork  = new Mock <IUnitOfWork>();
            Mock <IFileService>  mockIFileService = new Mock <IFileService>();
            ApartmentsController controller       = new ApartmentsController(mockIUnitOfWork.Object, mockIFileService.Object);

            controller.ModelState.AddModelError("RentEndDate", "Bad date!");

            // Act
            IActionResult result = await controller.Edit(apartment.Id, apartment, null);

            // Assert
            Assert.NotNull(result);
            ViewResult viewResult = Assert.IsType <ViewResult>(result);

            Assert.NotNull(viewResult.Model);
            Apartment apartmentModel = Assert.IsType <Apartment>(viewResult.Model);

            Assert.Same(apartment, apartmentModel);
            Assert.Null(viewResult.ViewName);
        }
        public async void DetailsApartamentIsNull_NotFoundResult()
        {
            // Arrange
            Apartment apartment = new Apartment {
                Id = 7, Name = "NewApartament"
            };

            Mock <ApartmentRepository> mockApartamentRepository = new Mock <ApartmentRepository>();

            mockApartamentRepository
            .Setup(ar => ar.GetAsync(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(Task.FromResult(null as Apartment));

            Mock <IUnitOfWork> mockIUnitOfWork = new Mock <IUnitOfWork>();

            mockIUnitOfWork
            .Setup(uw => uw.GetRepository <Apartment, ApartmentRepository>())
            .Returns(mockApartamentRepository.Object);

            Mock <IFileService> mockIFileService = new Mock <IFileService>();

            ApartmentsController controller = new ApartmentsController(mockIUnitOfWork.Object, mockIFileService.Object);

            // Act
            IActionResult result = await controller.Details(apartment.Id);

            // Assert
            Assert.NotNull(result);
            Assert.IsType <NotFoundResult>(result);
        }
        public async void Single_ViewResult()
        {
            // Arrange
            int       userId = 1, apartmentId = 1;
            bool      isRenter = true, hasRequest = false;
            Apartment apartment = new Apartment {
                Id = 1
            };

            Mock <ApartmentRepository> mockApartmentRepository = new Mock <ApartmentRepository>();

            mockApartmentRepository
            .Setup(ar => ar.GetAsync(It.IsAny <int>(), It.IsAny <string>()))
            .Returns(Task.FromResult(apartment));
            mockApartmentRepository
            .Setup(ar => ar.IsRenter(It.IsAny <int>(), It.IsAny <int>()))
            .Returns(isRenter);
            mockApartmentRepository
            .Setup(ar => ar.HasRequest(It.IsAny <int>(), It.IsAny <int>()))
            .Returns(hasRequest);

            Mock <IUnitOfWork> mockUnitOfWork = new Mock <IUnitOfWork>();

            mockUnitOfWork
            .Setup(u => u.GetRepository <Apartment, ApartmentRepository>())
            .Returns(mockApartmentRepository.Object);

            ControllerBase controller = new ApartmentsController(mockUnitOfWork.Object);

            Mock <ClaimsPrincipal> userMock = new Mock <ClaimsPrincipal>();

            userMock
            .Setup(p => p.FindFirst(It.IsAny <string>()))
            .Returns(new Claim(nameof(User.Id), userId.ToString()));

            controller.ControllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = userMock.Object
                }
            };

            // Act
            IActionResult result = await(controller as ApartmentsController)?.Single(apartmentId);

            // Assert
            Assert.NotNull(result);
            ViewResult viewResult = Assert.IsType <ViewResult>(result);

            Assert.NotNull(viewResult.Model);
            SingleViewModel singleViewModel = Assert.IsType <SingleViewModel>(viewResult.Model);

            Assert.Equal(userId, singleViewModel.UserId);
            Assert.Same(apartment, singleViewModel.Apartment);
            Assert.Equal(isRenter, singleViewModel.IsRenter);
            Assert.Equal(hasRequest, singleViewModel.HasUserRequest);
            Assert.Null(viewResult.ViewName);
        }
        public void MyRent_ViewResult()
        {
            // Arrange
            int userId = 1;
            IEnumerable <Apartment> apartments = new List <Apartment>
            {
                new Apartment {
                    Id = 1
                },
                new Apartment {
                    Id = 2
                }
            };

            Mock <ApartmentRepository> mockApartmentRepository = new Mock <ApartmentRepository>();

            mockApartmentRepository
            .Setup(ar => ar.Get(It.IsAny <Expression <Func <Apartment, bool> > >(),
                                It.IsAny <Func <IQueryable <Apartment>, IOrderedQueryable <Apartment> > >(),
                                It.IsAny <string>(), It.IsAny <int>(),
                                It.IsAny <int>()))
            .Returns(apartments);

            Mock <IUnitOfWork> mockUnitOfWork = new Mock <IUnitOfWork>();

            mockUnitOfWork
            .Setup(u => u.GetRepository <Apartment, ApartmentRepository>())
            .Returns(mockApartmentRepository.Object);

            ControllerBase controller = new ApartmentsController(mockUnitOfWork.Object);

            Mock <ClaimsPrincipal> userMock = new Mock <ClaimsPrincipal>();

            userMock
            .Setup(p => p.FindFirst(It.IsAny <string>()))
            .Returns(new Claim(nameof(User.Id), userId.ToString()));

            controller.ControllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = userMock.Object
                }
            };

            // Act
            IActionResult result = (controller as ApartmentsController)?.MyRent();

            // Assert
            Assert.NotNull(result);
            ViewResult viewResult = Assert.IsType <ViewResult>(result);

            Assert.NotNull(viewResult.Model);
            ListViewModel listViewModel = Assert.IsType <ListViewModel>(viewResult.Model);

            Assert.Same(apartments, listViewModel.Apartments);
            Assert.Null(viewResult.ViewName);
        }
        public async void SingleApartmentIdIsNull_NotFoundResult()
        {
            // Arrange
            Mock <IUnitOfWork> mockUnitOfWork = new Mock <IUnitOfWork>();

            ApartmentsController controller = new ApartmentsController(mockUnitOfWork.Object);

            // Act
            IActionResult result = await controller.Single(null);

            // Assert
            Assert.NotNull(result);
            Assert.IsType <NotFoundResult>(result);
        }
        public async void DetailsIdIsNull_NotFoundResult()
        {
            // Arrange
            Mock <IUnitOfWork> mockIUnitOfWork = new Mock <IUnitOfWork>();

            Mock <IFileService> mockIFileService = new Mock <IFileService>();

            ApartmentsController controller = new ApartmentsController(mockIUnitOfWork.Object, mockIFileService.Object);

            // Act
            IActionResult result = await controller.Details(null);

            // Assert
            Assert.NotNull(result);
            Assert.IsType <NotFoundResult>(result);
        }
        public void Create_ViewResult()
        {
            // Arrange
            Mock <IUnitOfWork>   mockIUnitOfWork  = new Mock <IUnitOfWork>();
            Mock <IFileService>  mockIFileService = new Mock <IFileService>();
            ApartmentsController controller       = new ApartmentsController(mockIUnitOfWork.Object, mockIFileService.Object);

            // Act
            IActionResult result = controller.Create();

            // Assert
            Assert.NotNull(result);
            ViewResult viewResult = Assert.IsType <ViewResult>(result);

            Assert.Null(viewResult.ViewName);
        }
        public void InitializeTest()
        {
            _apartmentsRepository = new FakeApartmentsRepository();
            _locationsRepository  = new FakeLocationsRepository();
            var apartmentsAutoMapperProfile = new ApartmentProfile();
            var locationsAutoMapperProfile  = new LocationProfile();
            var mapperConfigs = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(apartmentsAutoMapperProfile);
                cfg.AddProfile(locationsAutoMapperProfile);
            });
            var mapper = new Mapper(mapperConfigs);

            //Func<AuthorizationResult> authResult = authSucceeds ?
            //    (Func<AuthorizationResult>)AuthorizationResult.Success : AuthorizationResult.Failed;

            var iLoggerMock = new Mock <ILogger <ApartmentsController> >();

            _controller = new ApartmentsController(iLoggerMock.Object, _apartmentsRepository, _locationsRepository, mapper);
        }
        public void Index_ViewResult()
        {
            // Arrange
            int?daysToFree = 5;
            IEnumerable <Apartment> apartments = new List <Apartment>()
            {
                new Apartment {
                    Name = "NewApartament", Renter = new User {
                        FirstName = "Jeson"
                    }
                }
            };

            Mock <ApartmentRepository> mockApartamentRepository = new Mock <ApartmentRepository>();

            mockApartamentRepository
            .Setup(ar => ar.Get(It.IsAny <Expression <Func <Apartment, bool> > >(), null, It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>()))
            .Returns(apartments);

            Mock <IUnitOfWork> mockUnitOfWork = new Mock <IUnitOfWork>();

            mockUnitOfWork
            .Setup(uw => uw.GetRepository <Apartment, ApartmentRepository>())
            .Returns(mockApartamentRepository.Object);
            Mock <IFileService> mockIFileService = new Mock <IFileService>();

            ApartmentsController controller = new ApartmentsController(mockUnitOfWork.Object, mockIFileService.Object);

            // Act
            IActionResult result = controller.Index(daysToFree, isFree: null);

            // Assert
            Assert.NotNull(result);
            ViewResult viewResult = Assert.IsType <ViewResult>(result);

            Assert.NotNull(viewResult.Model);
            IndexViewModel indexViewModel = Assert.IsType <IndexViewModel>(viewResult.Model);

            Assert.Equal(apartments, indexViewModel.Apartments);
            Assert.Null(viewResult.ViewName);
        }
        public async void EditIdsAreDifferent_NotFoundResult()
        {
            // Arrange
            int       id        = 1;
            Apartment apartment = new Apartment {
                Id = 7, Name = "NewApartament"
            };

            Mock <IUnitOfWork> mockIUnitOfWork = new Mock <IUnitOfWork>();

            Mock <IFileService> mockIFileService = new Mock <IFileService>();

            ApartmentsController controller = new ApartmentsController(mockIUnitOfWork.Object, mockIFileService.Object);

            // Act
            IActionResult result = await controller.Edit(id, apartment, null);

            // Assert
            Assert.NotNull(result);
            Assert.IsType <NotFoundResult>(result);
        }
        public void Index_ViewResult()
        {
            // Arrange
            IEnumerable <Apartment> apartments = new List <Apartment>
            {
                new Apartment {
                    Id = 1
                },
                new Apartment {
                    Id = 2
                }
            };

            Mock <ApartmentRepository> mockApartmentRepository = new Mock <ApartmentRepository>();

            mockApartmentRepository
            .Setup(ar => ar.GetRandom(It.IsAny <int>()))
            .Returns(apartments);

            Mock <IUnitOfWork> mockUnitOfWork = new Mock <IUnitOfWork>();

            mockUnitOfWork
            .Setup(u => u.GetRepository <Apartment, ApartmentRepository>())
            .Returns(mockApartmentRepository.Object);

            ApartmentsController controller = new ApartmentsController(mockUnitOfWork.Object);

            // Act
            IActionResult result = controller.Index();

            // Assert
            Assert.NotNull(result);
            ViewResult viewResult = Assert.IsType <ViewResult>(result);

            Assert.NotNull(viewResult.Model);
            IEnumerable <Apartment> apartmentsModel = Assert.IsAssignableFrom <IEnumerable <Apartment> >(viewResult.Model);

            Assert.Same(apartments, apartmentsModel);
            Assert.Null(viewResult.ViewName);
        }
        public void GetApartmentsList_OkObjectResult()
        {
            // Arrange
            int userId = 1;
            IEnumerable <Apartment> apartments = new List <Apartment>
            {
                new Apartment {
                    Id = 5, Name = "Avalon"
                }
            };

            Mock <ApartmentRepository> mockApartmentRepository = new Mock <ApartmentRepository>();

            mockApartmentRepository
            .Setup(ap => ap.Get(It.IsAny <Expression <Func <Apartment, bool> > >(),
                                null,
                                null, null,
                                null))
            .Returns(apartments);

            Mock <IUnitOfWork> mockIUnitOfWork = new Mock <IUnitOfWork>();

            mockIUnitOfWork
            .Setup(u => u.GetRepository <Apartment, ApartmentRepository>())
            .Returns(mockApartmentRepository.Object);
            Mock <IFileService> mockIIFileService = new Mock <IFileService>();

            ApartmentsController controller = new ApartmentsController(mockIUnitOfWork.Object, mockIIFileService.Object);

            // Act
            IActionResult result = controller.GetApartmentsList(userId);

            // Assert
            Assert.NotNull(result);
            OkObjectResult okObjectResult = Assert.IsType <OkObjectResult>(result);

            Assert.NotNull(okObjectResult.Value);
            Assert.IsAssignableFrom <IEnumerable <Apartment> >(okObjectResult.Value);
        }