public void CloseAd_FromNonOwner()
        {
            var openAd = mockContainer.AdRepositoryMock.Object.All().FirstOrDefault(ad => ad.Status == AdStatus.Open);

            if (openAd == null)
            {
                Assert.Fail("No ads with open status available");
            }

            var adId = openAd.Id;
            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(r => r.Ads).Returns(this.mockContainer.AdRepositoryMock.Object);

            var mockIdProvider = new Mock<IUserIdProvider>();
            mockIdProvider.Setup(r => r.GetUserId())
                .Returns(this.mockContainer.UserRepositoryMock.Object.All().FirstOrDefault(u => u.Id != openAd.OwnerId).Id);

            var adsController = new AdsController(mockContext.Object, mockIdProvider.Object);
            SetupController(adsController);

            var resposnse = adsController.CloseAd(adId).ExecuteAsync(CancellationToken.None).Result;
            Assert.AreEqual(HttpStatusCode.BadRequest, resposnse.StatusCode);
            mockContext.Verify(c => c.SaveChanges(), Times.Never);
            var stillOpenAd = mockContext.Object.Ads.Find(adId);
            Assert.AreEqual(AdStatus.Open, stillOpenAd.Status);
        }
        public void ClosingAdd_AsOwner_ShouldReturn200Ok()
        {
            // Arrange
            var fakeAds = this.mockContainer.AdRepositoryMock.Object.All();

            // Act
            var openAd = fakeAds.FirstOrDefault(a => a.Status == AdStatus.Open);
            if (openAd == null)
            {
                Assert.Fail("Cannot perform test there is no open ads.");
            }

            var adId = openAd.Id;
            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(c => c.Ads).Returns(this.mockContainer.AdRepositoryMock.Object);
            var mockProvider = new Mock<IUserIdProvider>();
            mockProvider.Setup(ip => ip.GetUserId()).Returns(openAd.OwnerId);

            var adsController = new AdsController(mockContext.Object, mockProvider.Object);
            this.SetupController(adsController);
            var httpResponse = adsController.CloseAd(adId).ExecuteAsync(CancellationToken.None).Result;

            // Assert
            mockContext.Verify(c => c.SaveChanges(), Times.Once);
            Assert.AreEqual(AdStatus.Closed, openAd.Status);
            Assert.IsNotNull(openAd.ClosedOn);
        }
        public void TestGetAdsShouldReturnAllAdsSortedByType()
        {
            var mockedContext = new Mock<IOnlineShopData>();
            mockedContext.Setup(c => c.Ads).Returns(this.mocks.AdRepositoryMock.Object);
            var adsController = new AdsController(mockedContext.Object);

            adsController.Request = new HttpRequestMessage();
            adsController.Configuration = new HttpConfiguration();
            var response = adsController.GetAds().ExecuteAsync(CancellationToken.None).Result;

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            var responseAds = response.Content
                .ReadAsAsync<IEnumerable<AdViewModel>>()
                .Result
                .Select(a => a.Id)
                .ToList();
            var fakeAds = this.mocks.AdRepositoryMock.Object.All()
                .Select(AdViewModel.Create)
                .OrderBy(a => a.Type)
                .ThenBy(a => a.PostedOn)
                .Select(a => a.Id)
                .ToList();
            CollectionAssert.AreEqual(fakeAds, responseAds);
        }
        public void CloseAd_AsNotAdOwner_ShouldReturn400BadRequest()
        {
            // Arrange
            var fakeAds = this.mockContainer.AdRepositoryMock.Object.All();

            // Act
            var openAd = fakeAds.FirstOrDefault(a => a.Status == AdStatus.Open);
            if (openAd == null)
            {
                Assert.Fail("Cannot perform test, because there are no open ads.");
            }

            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(c => c.Ads).Returns(this.mockContainer.AdRepositoryMock.Object);

            var mockProvider = new Mock<IUserIdProvider>();
            var fakeUsers = this.mockContainer.ApplicationUserRepositoryMock.Object.All();
            var nonExistingUser = fakeUsers.FirstOrDefault();
            mockProvider.Setup(ip => ip.GetUserId()).Returns(nonExistingUser.Id);
            var adsController = new AdsController(mockContext.Object, mockProvider.Object);
            this.SetupController(adsController);

            var httpResponse = adsController.CloseAd(openAd.Id).ExecuteAsync(CancellationToken.None).Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResponse.StatusCode);
            Assert.AreEqual(AdStatus.Open, openAd.Status);
            Assert.IsNull(openAd.ClosedOn);
        }
        public void GetAllAds_Should_Return_Total_Ads_Sorted_By_TypeIndex()
        {
            // Arrange
            var fakeAds = this.mocks.AdRepositoryMock.Object.All();

            var mockContext = new Mock<IOnlineShopData>();
            var mockUserIdProvider = new Mock<IUserIdProvider>();
            mockContext.Setup(c => c.Ads.All())
                .Returns(fakeAds);

            var adsController = new AdsController(mockContext.Object, mockUserIdProvider.Object);
            this.SetupController(adsController);

            // Act
            var response = adsController.GetAds()
                .ExecuteAsync(CancellationToken.None).Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            var adsResponse = response.Content
                .ReadAsAsync<IEnumerable<AdViewModel>>()
                .Result.Select(a => a.Id)
                .ToList();

            var orderedFakeAds = fakeAds
                .OrderByDescending(a => a.Type.Index)
                .ThenBy(a => a.PostedOn)
                .Select(a => a.Id)
                .ToList();

            CollectionAssert.AreEqual(orderedFakeAds, adsResponse);
        }
        public void CreateAd_Should_Succesfully_Add_To_Repository()
        {
            var ads = new List<Ad>();

            var fakeUser = this.mocks.UserRepositoryMock.Object.All().FirstOrDefault();
            if (fakeUser == null)
            {
                Assert.Fail("Cannot perform test - no users available!");
            }
            this.mocks.AdRepositoryMock
                .Setup(r => r.Add(It.IsAny<Ad>()))
                .Callback((Ad ad) =>
                {
                    ad.Owner = fakeUser;
                    ads.Add(ad);
                });

            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(m => m.Ads).Returns(this.mocks.AdRepositoryMock.Object);
            mockContext.Setup(m => m.AdTypes).Returns(this.mocks.AdTypeRepositoryMock.Object);
            mockContext.Setup(m => m.Categories).Returns(this.mocks.CategoryRepositoryMock.Object);
            mockContext.Setup(m => m.Users).Returns(this.mocks.UserRepositoryMock.Object);

            var mockIdProvider = new Mock<IUserIdProvider>();
            mockIdProvider.Setup(m => m.GetUserId()).Returns(fakeUser.Id);

            var adsController = new AdsController(mockContext.Object,mockIdProvider.Object);
            this.SetupController(adsController);

            var randomName = Guid.NewGuid().ToString();
            var newAd = new CreateAdBindingModel()
            {
                Name = randomName,
                Categories = new[] {"0", "2"},
                Price = 300,
                Description = "none",
                TypeId = 1.ToString()
            };

            var response = adsController.PostAd(newAd).ExecuteAsync(CancellationToken.None).Result;

            mockContext.Verify(c=>c.SaveChanges(),Times.Once);

            Assert.AreEqual(response.StatusCode,HttpStatusCode.OK);
            Assert.AreEqual(ads.Count,1);
            Assert.AreEqual(newAd.Name,ads[0].Name);

        }
        public void GetAllAds_Should_Return_All_Ads_Sorted_By_TypeIndex()
        {
            var fakeAds = this.mocks.AdRepositoryMock.Object.All();
            var fakeUser = this.mocks.UserRepositoryMock.Object.All().FirstOrDefault();
            if (fakeUser == null)
            {
                Assert.Fail("Cannot perform test - no users available.");
            }

            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(c => c.Ads.All()).Returns(fakeAds);
            var fakeUsers = this.mocks.UserRepositoryMock.Object.All();
            mockContext.Setup(c => c.Users.All()).Returns(fakeUsers);
            
            var mockIdProvider = new Mock<IUserIdProvider>();
            mockIdProvider.Setup(ip => ip.GetUserId()).Returns(fakeUser.Id);
            var adsController = new AdsController(mockContext.Object, mockIdProvider.Object);

            adsController.Request = new HttpRequestMessage();
            adsController.Configuration = new HttpConfiguration();

            var response = adsController.GetAds().ExecuteAsync(CancellationToken.None).Result;

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            var adsResponse = response.Content
                .ReadAsAsync<IEnumerable<AdViewModel>>()
                .Result.Select(a => a.Id)
                .ToList();

            var orderedFakeAds = fakeAds
                .OrderByDescending(a => a.Type.Index)
                .ThenByDescending(a => a.PostedOn)
                .Select(a => a.Id)
                .ToList();

            CollectionAssert.AreEqual(orderedFakeAds, adsResponse);
        }
        public void GetAllAds_Should_Return_Total_Ads_Sorted_By_TypeIndex()
        {
            //Arrange
            var fakeAds = this.mocks.AdRepositoryMock.Object.All();

            var mockContext = new Mock<IOnlineShopData>();
            var mockUserIdProvider = new Mock<IUserIdProvider>();
            mockContext.Setup(c => c.Ads.All())
                .Returns(fakeAds);

            var adsController = new AdsController(mockContext.Object,
                mockUserIdProvider.Object);
            this.SetupController(adsController);

            //Act- invoke the GetAllAds() method from the controller.
            var response = adsController.GetAds()
                .ExecuteAsync(CancellationToken.None).Result;

            //Assert that the response status code is 200 OK
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            //Deserialize the response data with ReadAsAsync<T>(). Select only the IDs.
            var adsResponse = response.Content.ReadAsAsync<IEnumerable<AdViewModel>>()
                .Result.Select(a => a.Id)
                .ToList();

            //Order the fake ads collection just like the controller does the ordering.
            //Select only the IDs.
            var orderedFakeAds = fakeAds
                .OrderBy(a => a.Type.Index)
                .ThenBy(a => a.PostedOn)
                .Select(a => a.Id)
                .ToList();

            //Assert that the two collections have the same elements
            CollectionAssert.AreEqual(orderedFakeAds, adsResponse);
        }
        public void GetAllAds_Should_Return_Total_ads_Sorted_by_Type_Index()
        {

            // Arrange
            var fakeAds = mocks.AdRepositoryMock.Object.All();

            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(m => m.Ads.All()).Returns(fakeAds);

            var mockUserIdProvider = new Mock<IUserIdProvider>();


            var adsController = new AdsController(mockContext.Object,mockUserIdProvider.Object);

            this.SetupController(adsController);

            // Act
            var response = adsController.GetAllAds().ExecuteAsync(CancellationToken.None)
                .Result;

            //Assert
            Assert.AreEqual(response.StatusCode, HttpStatusCode.OK);

        }
 private void SetupController(AdsController adsController)
 {
     adsController.Request = new HttpRequestMessage();
     adsController.Configuration = new HttpConfiguration();
 }
        public void Closing_Ad_As_NonOwner_Should_Return_400BadRequest()
        {
            var openAd = this.mocks.AdRepositoryMock.Object
                .All()
                .FirstOrDefault(a => a.Status == AdStatus.Open);
            if (openAd == null)
            {
                Assert.Fail("Can not perform this test-no open ads available.");
            }
            var adId = openAd.Id;

            var fakeUsers = this.mocks.UserRepositoryMock.Object;
            ApplicationUser foreignUser = fakeUsers.All().FirstOrDefault(f => f.Id != openAd.OwnerId);
            if (foreignUser == null)
            {
                Assert.Fail("Can not perform this test - need user who is not the owner ");
            }

            var mockIdProvider = new Mock<IUserIdProvider>();
            mockIdProvider.Setup(m => m.GetUserId()).Returns(foreignUser.Id);

            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(m => m.Ads).Returns(this.mocks.AdRepositoryMock.Object);
            
            var adsController = new AdsController(mockContext.Object,mockIdProvider.Object);
            SetupController(adsController);

            var response = adsController.CloseAd(adId).ExecuteAsync(CancellationToken.None).Result;

            Assert.AreEqual(HttpStatusCode.BadRequest,response.StatusCode);

        }
        public void Closing_Ad_As_Owner_Should_Return_200OK()
        {
            var fakeAd = this.mocks.AdRepositoryMock.Object.All();

            var openAd = fakeAd.FirstOrDefault(f => f.Status == AdStatus.Open);
            if (openAd == null)
            {
                Assert.Fail("Can not perform test -no open ads available.");
            }
            
            var adId = openAd.Id;
            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(m => m.Ads).Returns(this.mocks.AdRepositoryMock.Object);

            var mockIdProvider = new Mock<IUserIdProvider>();
            
            mockIdProvider.Setup(id => id.GetUserId()).Returns(openAd.OwnerId);

            var adsController = new AdsController(mockContext.Object,mockIdProvider.Object);
            this.SetupController(adsController);
            var response = adsController.CloseAd(adId).ExecuteAsync(CancellationToken.None).Result;

            Assert.AreEqual(response.StatusCode,HttpStatusCode.OK);
            mockContext.Verify(m=>m.SaveChanges(),Times.Once);

            Assert.IsNotNull(openAd.ClosedOn);
            Assert.AreEqual(openAd.Status,AdStatus.Closed);

        }
        public void CreateAd_ShouldAddNewlyCreatedAd()
        {
            var ads = new List<Ad>();

            var fakeUser = this.mockContainer.UserRepositoryMock.Object.All().FirstOrDefault();
            if (fakeUser == null)
            {
                Assert.Fail("Cannot perform test - no users available.");
            }

            this.mockContainer.AdRepositoryMock
                .Setup(r => r.Add(It.IsAny<Ad>()))
                .Callback((Ad ad) =>
                {
                    ad.Owner = fakeUser;
                    ads.Add(ad);
                });

            var fakeCat = mockContainer.CategoryRepositoryMock.Object.All();
            var fakeAdTypes = mockContainer.AdTypeRepositoryMock.Object.All();
            var fakeUsers = mockContainer.UserRepositoryMock.Object.All();

            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(r => r.Ads.All()).Returns(ads.AsQueryable());
            mockContext.Setup(r => r.Ads.Add(It.IsAny<Ad>())).Callback((Ad ad) => ads.Add(ad));

            mockContext.Setup(r => r.Categories.All()).Returns(fakeCat.AsQueryable());
            mockContext.Setup(r => r.AdTypes.All()).Returns(fakeAdTypes.AsQueryable());
            mockContext.Setup(r => r.Users.All()).Returns(fakeUsers.AsQueryable());

            var mockIdProvider = new Mock<IUserIdProvider>();
            mockIdProvider.Setup(r => r.GetUserId()).Returns(fakeUser.Id);

            var adsController = new AdsController(mockContext.Object, mockIdProvider.Object);
            this.SetupController(adsController);

            var randomName = Guid.NewGuid().ToString();
            var newAd = new CreateAdBindingModel()
            {
                Name = randomName,
                Price = 555,
                TypeId = 1,
                Description = "Testing testing",
                Categories = new[] { 1, 2, 3 }
            };

            var response = adsController.CreateAd(newAd).ExecuteAsync(CancellationToken.None).Result;
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            mockContext.Verify(c => c.SaveChanges(), Times.Once);

            Assert.AreEqual(1, ads.Count);
            Assert.AreEqual(randomName, ads.First().Name);
        }
        public void CreateAd_WithCorrectData_ShouldSuccessfullyAddAdInRepository()
        {
            // Arrange
            var ads = new List<Ad>();

            // Act
            var mockContext = new Mock<IOnlineShopData>();
            var fakeAds = this.mockContainer.AdRepositoryMock.Object.All();
            var fakeAdTypes = this.mockContainer.AdTypeRepositoryMock.Object.All();
            var fakeCategories = this.mockContainer.CategoryRepositoryMock.Object.All();
            var fakeUsers = this.mockContainer.ApplicationUserRepositoryMock.Object.All();

            mockContext.Setup(r => r.Ads).Returns(this.mockContainer.AdRepositoryMock.Object);
            mockContext.Setup(r => r.AdTypes).Returns(this.mockContainer.AdTypeRepositoryMock.Object);
            mockContext.Setup(r => r.Categories).Returns(this.mockContainer.CategoryRepositoryMock.Object);
            mockContext.Setup(r => r.ApplicationUsers).Returns(this.mockContainer.ApplicationUserRepositoryMock.Object);
            var fakeUser = this.mockContainer.ApplicationUserRepositoryMock.Object.All().FirstOrDefault();
            var mockIdProvider = new Mock<IUserIdProvider>();
            if (fakeUser == null)
            {
                Assert.Fail("Cannot perform test no users available.");
            }

            mockIdProvider.Setup(r => r.GetUserId()).Returns(fakeUser.Id);

            var adsController = new AdsController(mockContext.Object, mockIdProvider.Object);
            this.SetupController(adsController);
            this.mockContainer.AdRepositoryMock.Setup(r => r.Add(It.IsAny<Ad>()))
                .Callback((Ad ad) =>
                {
                   // ad.Id = 10;   // Uncomment if id is required in the test
                    ad.Owner = fakeUser;
                    ads.Add(ad);
                });
            var randomName = Guid.NewGuid().ToString();
            var newAd = new CreateAdBindingModel()
                        {
                            Name = randomName,
                            Price = 555,
                            TypeId = 1,
                            Description = "Put description here",
                            Categories = new[] { 1, 2 }
                        };

            var httpResponse = adsController.CreateAd(newAd).ExecuteAsync(CancellationToken.None).Result;

            // Assert
            mockContext.Verify(c => c.SaveChanges(), Times.Once);
            Assert.AreEqual(1, ads.Count);
            Assert.AreEqual(ads[0].Name, newAd.Name);
        }
        public void TestGetAllAds_ShouldReturnAllAdsSortedByIndex()
        {
            var fakeAds = mockContainer.AdRepositoryMock.Object.All();

            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(r => r.Ads.All()).Returns(fakeAds.AsQueryable());

            var adsController = new AdsController(mockContext.Object);
            this.SetupController(adsController);

            var result = adsController.GetAds().ExecuteAsync(CancellationToken.None).Result;
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            var adResponse = result.Content.ReadAsAsync<IEnumerable<AdsViewModel>>()
                .Result
                .OrderByDescending(a => a.Type)
                .ThenByDescending(a => a.PostedOn)
                .Select(a => new {a.Id})
                .ToList();
            var fa = fakeAds
                .OrderByDescending(a => a.Type.Index)
                .ThenByDescending(a => a.PostedOn)
                .Select(a => new { a.Id })
                .ToList();
            Assert.AreEqual(2, adResponse.Count);
            Assert.AreEqual(2, fa.Count);
            CollectionAssert.AreEqual(fa, adResponse);
        }
        public void TestCloseAds_ShouldCloseAnAd()
        {
            var fakeAds = mockContainer.AdRepositoryMock.Object.All();
            var openAd = fakeAds.FirstOrDefault(ad => ad.Status == AdStatus.Open);

            if (openAd == null)
            {
                Assert.Fail("No open ads - cannot perform test");
            }

            var adId = openAd.Id;
            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(r => r.Ads).Returns(this.mockContainer.AdRepositoryMock.Object);

            var mockIdProvider = new Mock<IUserIdProvider>();
            mockIdProvider.Setup(i => i.GetUserId()).Returns(openAd.OwnerId);

            var adsController = new AdsController(mockContext.Object, mockIdProvider.Object);
            SetupController(adsController);

            var response = adsController.CloseAd(adId).ExecuteAsync(CancellationToken.None).Result;

            Assert.AreEqual(HttpStatusCode.OK,response.StatusCode);
            mockContext.Verify(c => c.SaveChanges(), Times.Once);
            var closedAd = mockContext.Object.Ads.Find(adId);
            Assert.AreNotEqual(null, closedAd.ClosedOn);
            Assert.AreEqual(AdStatus.Closed, closedAd.Status);
        }
        public void GetAllAds_ShouldReturn_AllAds_SortedByIndex()
        {
            // Arrange
            var fakeAds = this.mockContainer.AdRepositoryMock.Object.All();
            var fakeUser = this.mockContainer.ApplicationUserRepositoryMock.Object.All().FirstOrDefault();
            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(r => r.Ads.All()).Returns(fakeAds.AsQueryable());
            var mockIdProvider = new Mock<IUserIdProvider>();
            if (fakeUser == null)
            {
                Assert.Fail("Cannot perform test no users available.");
            }

            mockIdProvider.Setup(r => r.GetUserId()).Returns(fakeUser.Id);

            // Act
            var adsController = new AdsController(mockContext.Object, mockIdProvider.Object);
            this.SetupController(adsController);
            var httpResponse = adsController.GetAllAds().ExecuteAsync(CancellationToken.None).Result;
            var result = httpResponse.Content.ReadAsAsync<IEnumerable<AllAdsViewModel>>().Result.Select(a => a.Id);
            var orderedFakeAds = fakeAds.OrderBy(a => a.TypeId).Select(a => a.Id).ToList();

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode);
            CollectionAssert.AreEqual(orderedFakeAds, result.ToList());
        }
        public void Closing_Ad_As_NonOwner_Should_Return_400BadRequest()
        {
            var fakeAds = this.mocks.AdRepositoryMock.Object.All();
            var openAd = fakeAds.FirstOrDefault(ad => ad.Status == AdStatus.Open);
            if (openAd == null)
            {
                Assert.Fail("Cannot perform test - no open ads available.");
            }

            var fakeUsers = this.mocks.UserRepositoryMock.Object.All();
            ApplicationUser foreignUser = fakeUsers
                .FirstOrDefault(u => u.Id != openAd.OwnerId);

            if (foreignUser == null)
            {
                Assert.Fail("Cannot perform test - no user who is non owner of the ad.");
            }

            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(c => c.Ads)
                .Returns(this.mocks.AdRepositoryMock.Object);

            var mockUserIdProvider = new Mock<IUserIdProvider>();
            mockUserIdProvider.Setup(uip => uip.GetUserId())
                .Returns(foreignUser.Id);

            var adsController = new AdsController(mockContext.Object, mockUserIdProvider.Object);
            this.SetupController(adsController);

            var response = adsController.CloseAd(openAd.Id)
                .ExecuteAsync(CancellationToken.None).Result;

            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
            mockContext.Verify(c => c.SaveChanges(), Times.Never);
            Assert.IsNull(openAd.ClosedOn);
            Assert.AreEqual(AdStatus.Open, openAd.Status);
        }
        public void ClosingAd_As_Owner_Should_return_200OK()
        {
            var fakeAds = this.mocks.AdRepositoryMock.Object.All();
            var openAd = fakeAds.FirstOrDefault(ad => ad.Status == AdStatus.Open);
            if (openAd==null)
            {
                Assert.Fail("Cannot perform test - no open ads available.");
            }
            var adId = openAd.Id;
            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(c => c.Ads).Returns(this.mocks.AdRepositoryMock.Object);
            var mockIdProvider = new Mock<IUserIdProvider>();
            mockIdProvider.Setup(ip => ip.GetUserId()).Returns(openAd.OwnerId);

            var adsController = new AdsController(mockContext.Object, mockIdProvider.Object);
            adsController.Request = new HttpRequestMessage();
            adsController.Configuration = new HttpConfiguration();

            var response = adsController.CloseAd(adId).ExecuteAsync(CancellationToken.None).Result;
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            mockContext.Verify(c => c.SaveChanges(), Times.Once);
            Assert.AreNotEqual(null, openAd.ClosedOn);
            Assert.AreEqual(AdStatus.Closed, openAd.Status);
        }
        public void CreateAd_Should_Successfully_Add_To_Repository()
        {
            var ads = new List<Ad>();
            var fakeUser = this.mocks.UserRepositoryMock.Object.All().FirstOrDefault();
            if (fakeUser == null)
            {
                Assert.Fail("Cannot perform test - no users available.");
            }
            this.mocks.AdRepositoryMock
                .Setup(r => r.Add(It.IsAny<Ad>()))
                .Callback((Ad ad) =>
                {
                    ad.Owner = fakeUser;
                    ads.Add(ad);
                });
            var mockContext = new Mock<IOnlineShopData>();
            mockContext.Setup(c => c.Ads).Returns(this.mocks.AdRepositoryMock.Object);
            var fakeCategories = this.mocks.CategoryRepositoryMock.Object.All();
            mockContext.Setup(c => c.Categories.All()).Returns(fakeCategories);
            mockContext.Setup(c => c.Categories.Find(It.IsAny<int>()))
                .Returns((int id) => fakeCategories.FirstOrDefault(c => c.Id == id));
            var fakeUsers = this.mocks.UserRepositoryMock.Object.All();
            mockContext.Setup(c => c.Users).Returns(this.mocks.UserRepositoryMock.Object);
            var fakeAdTypes = this.mocks.AdTypeRepositoryMock.Object.All();
            mockContext.Setup(c => c.AdTypes).Returns(this.mocks.AdTypeRepositoryMock.Object);

            var mockIdProvider = new Mock<IUserIdProvider>();
            mockIdProvider.Setup(ip => ip.GetUserId()).Returns(fakeUser.Id);

            var adsController = new AdsController(mockContext.Object, mockIdProvider.Object);
            adsController.Request = new HttpRequestMessage();
            adsController.Configuration = new HttpConfiguration();

            var randomName = Guid.NewGuid().ToString();
            var newAd = new CreateAdBindingModel()
            {
                Name = randomName,
                Price = 999,
                TypeId = 1,
                Description = "Nothing to say",
                Categories = new[] {1, 2} 
            };
            var response = adsController.CreateAd(newAd).ExecuteAsync(CancellationToken.None).Result;
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            mockContext.Verify(c=>c.SaveChanges(), Times.Once);
            Assert.AreEqual(1, ads.Count);
            Assert.AreEqual(newAd.Name, ads[0].Name);
        }