public void TestRegionCacheWithExpectedResult() { // arrange var regionDao = MockRepository.GenerateMock<IRegionDao>(); try { // Create some mock region objects Region region1 = new Region { Name = "UK", Id = 1, SupportEmail = "*****@*****.**", SupportNumber = "+4424778845214" }; Region region2 = new Region { Name = "ZA", Id = 2, SupportEmail = "*****@*****.**", SupportNumber = "+4424778845214" }; List<Region> regionList = new List<Region>(); regionList.Add(region1); regionList.Add(region2); regionDao.Stub(x => x.GetAll()).Return(regionList); regionDao.Stub(x => x.GetByKey(2)).Return(null); // Make sure the cache doesn't cheat and get the value from the db regionDao.Expect(x => x.GetByKey(2)).Repeat.Never(); Cache.Region.RegionDao = regionDao; // act Cache.Region.Invalidate(); Cache.Region.GetValue(2); // assert regionDao.VerifyAllExpectations(); } finally { // Make sure this is reset for future tests Cache.Region.RegionDao = new RegionDao(); } }
/// <summary> /// Converts Region to RegionDto /// </summary> /// <param name="region">Region to convert</param> /// <returns>RegionDto</returns> private static RegionDto ConvertRegionToDto(Region region) { return new RegionDto { Id = region.Id, Description = region.Name, SupportEmail = region.SupportEmail, SupportNumber = region.SupportNumber }; }
public void TestInvalidRequestToRegionCacheThrowsException() { // arrange var regionDao = MockRepository.GenerateStub<IRegionDao>(); try { // Create some mock region objects Region region1 = new Region { Name = "UK", Id = 1, SupportEmail = "*****@*****.**", SupportNumber = "+4424778845214" }; Region region2 = new Region { Name = "ZA", Id = 2, SupportEmail = "*****@*****.**", SupportNumber = "+4424778845214" }; List<Region> regionList = new List<Region>(); regionList.Add(region1); regionList.Add(region2); regionDao.Stub(x => x.GetAll()).Return(regionList); regionDao.Stub(x => x.GetByKey(3)).Return(null); // act Cache.Region.RegionDao = regionDao; Cache.Region.Invalidate(); Cache.Region.GetValue(3); // assert // Exception should be thrown above } finally { // Make sure this is reset for future tests Cache.Region.RegionDao = new RegionDao(); } }
public void TestVolatilityOfTheCacheWithExpectedResult() { // arrange var regionDao = MockRepository.GenerateMock<IRegionDao>(); Cache.Region.RegionDao = regionDao; try { // Create some mock region objects Region region1Old = new Region { Name = "UK", Id = 1, SupportEmail = "*****@*****.**", SupportNumber = "+4424778845214" }; Region region1New = new Region { Name = "UK", Id = 1, SupportEmail = "*****@*****.**", SupportNumber = "+4424778845214" }; Region region2 = new Region { Name = "ZA", Id = 2, SupportEmail = "*****@*****.**", SupportNumber = "+4424778845214" }; // Outdated data List<Region> regionListOld = new List<Region>(); regionListOld.Add(region1Old); regionListOld.Add(region2); // New data List<Region> regionListNew = new List<Region>(); regionListNew.Add(region1New); regionListNew.Add(region2); // Set expectations on the mock regionDao.Stub(x => x.GetAll()).Return(regionListOld); // Make sure the cache didn't cheat and get the value from the database regionDao.Expect(x => x.GetByKey(Arg<int>.Is.Anything)).Repeat.Never(); // Declare some thread helpers bool internalThreadInvalidatedCacheComplete = false; // act // Invalidate cache on seperate thread and then on this one and check values each time new Thread(() => { // Do the invalidate Cache.Region.Invalidate(); internalThreadInvalidatedCacheComplete = true; }).Start(); // Wait for the thread to finish while (!internalThreadInvalidatedCacheComplete) { Thread.Sleep(100); } Cache.Region.TryGetValue(1); // assert regionDao.VerifyAllExpectations(); } finally { // Make sure this is reset for future tests Cache.Region.RegionDao = new RegionDao(); } }
public void TestInvalidationWithExpectedResult() { // arrange var regionDao = MockRepository.GenerateStub<IRegionDao>(); try { // Create some mock region objects Region region1Old = new Region { Name = "UK", Id = 1, SupportEmail = "*****@*****.**", SupportNumber = "+4424778845214" }; Region region1New = new Region { Name = "UK", Id = 1, SupportEmail = "*****@*****.**", SupportNumber = "+4424778845214" }; Region region2 = new Region { Name = "ZA", Id = 2, SupportEmail = "*****@*****.**", SupportNumber = "+4424778845214" }; // Outdated data List<Region> regionListOld = new List<Region>(); regionListOld.Add(region1Old); regionListOld.Add(region2); // New data List<Region> regionListNew = new List<Region>(); regionListNew.Add(region1New); regionListNew.Add(region2); regionDao.Stub(x => x.GetAll()).Return(regionListOld).Repeat.Once(); regionDao.Stub(x => x.GetAll()).Return(regionListNew).Repeat.Once(); Cache.Region.RegionDao = regionDao; // act Cache.Region.Invalidate(); // assert Assert.AreEqual(region1Old.SupportEmail, Cache.Region.GetValue(1).SupportEmail, "Check that the initial cache value was correctly loaded"); // act again Cache.Region.Invalidate(); // assert Assert.AreEqual(region1New.SupportEmail, Cache.Region.GetValue(1).SupportEmail, "Check thay the updated cache value has been updated after invalidation"); } finally { // Make sure this is reset for future tests Cache.Region.RegionDao = new RegionDao(); } }
public void TestTryInvalidRequestToRegionCacheWithExpectedResult() { // arrange var regionDao = MockRepository.GenerateMock<IRegionDao>(); try { // Create some mock region objects Region region1 = new Region { Name = "UK", Id = 1, SupportEmail = "*****@*****.**", SupportNumber = "+4424778845214" }; Region region2 = new Region { Name = "ZA", Id = 2, SupportEmail = "*****@*****.**", SupportNumber = "+4424778845214" }; List<Region> regionList = new List<Region>(); regionList.Add(region1); regionList.Add(region2); regionDao.Stub(x => x.GetAll()).Return(regionList); // Confirm that the cache attempts to resolve the missing entry from the database regionDao.Expect(x => x.GetByKey(3)).Return(null); // act Cache.Region.RegionDao = regionDao; Cache.Region.Invalidate(); // Try to access a value not in the cache Cache.Region.TryGetValue(3); // assert regionDao.VerifyAllExpectations(); } finally { // Make sure this is reset for future tests Cache.Region.RegionDao = new RegionDao(); } }