public async Task GetCategories_Calls_Service_When_Cache_Miss() { var cacheService = new MockCacheService(); cacheService.GetDataDelegate = s => { throw new FileNotFoundException(); }; cacheService.SaveDataAsyncDelegate = (s, c) => Task.FromResult(new Uri("http://test.org")); var productCatalogService = new MockProductCatalogService(); var categories = new List <Category> { new Category { Id = 1 }, new Category { Id = 2 } }; productCatalogService.GetSubcategoriesAsyncDelegate = (parentId, maxProducts) => Task.FromResult(new ReadOnlyCollection <Category>(categories)); var target = new ProductCatalogRepository(productCatalogService, cacheService); var returnedCategories = await target.GetRootCategoriesAsync(0); Assert.AreEqual(2, returnedCategories.Count); Assert.AreEqual(1, returnedCategories[0].Id); Assert.AreEqual(2, returnedCategories[1].Id); }
public async Task GetCategories_Saves_Data_To_Cache() { var cacheService = new MockCacheService(); cacheService.GetDataDelegate = s => { throw new FileNotFoundException(); }; cacheService.SaveDataAsyncDelegate = (s, o) => { var collection = (ReadOnlyCollection <Category>)o; Assert.AreEqual("Categories-0-0", s); Assert.AreEqual(2, collection.Count); Assert.AreEqual(1, collection[0].Id); Assert.AreEqual(2, collection[1].Id); return(Task.FromResult(new Uri("http://test.org"))); }; var productCatalogService = new MockProductCatalogService(); var categories = new List <Category> { new Category { Id = 1 }, new Category { Id = 2 } }; productCatalogService.GetSubcategoriesAsyncDelegate = (parentId, maxProducts) => Task.FromResult(new ReadOnlyCollection <Category>(categories)); var target = new ProductCatalogRepository(productCatalogService, cacheService); await target.GetRootCategoriesAsync(0); }
public void GetItemPrice() { var fileReader = new Mock <IProductCatalogFileReader>(); fileReader.Setup(x => x.GetTextReader()).Returns(_catalogTestData.GetValidTestData()); var testRepository = new ProductCatalogRepository(fileReader.Object); var priceService = new PriceService(testRepository); var saleItem = new BasketItem() { Name = "Apple" }; var normalItem = new BasketItem() { Name = "Banana" }; var saleItemResult = priceService.GetItemPrice(saleItem); var normalItemResult = priceService.GetItemPrice(normalItem); Assert.IsTrue(saleItemResult == 0.50M); Assert.IsTrue(normalItemResult == 0.75M); }
public async Task GetSubcategories_Uses_Cache_When_Data_Available() { var cacheService = new MockCacheService(); var categories = new List <Category> { new Category { Id = 10 }, new Category { Id = 11 } }; cacheService.GetDataDelegate = s => { if (s == "Categories-1-10") { return(new ReadOnlyCollection <Category>(categories)); } return(new ReadOnlyCollection <Category>(null)); }; var productCatalogService = new MockProductCatalogService(); productCatalogService.GetSubcategoriesAsyncDelegate = (parentId, maxProducts) => Task.FromResult(new ReadOnlyCollection <Category>(null)); var target = new ProductCatalogRepository(productCatalogService, cacheService); var returnedCategories = await target.GetSubcategoriesAsync(1, 10); Assert.AreEqual(2, returnedCategories.Count); Assert.AreEqual(10, returnedCategories[0].Id); Assert.AreEqual(11, returnedCategories[1].Id); }
public void TestLoadANormalCatalog() { var fileReader = new Mock <IProductCatalogFileReader>(); fileReader.Setup(x => x.GetTextReader()).Returns(_catalogTestData.GetValidTestData()); var testRepository = new ProductCatalogRepository(fileReader.Object); var catalog = testRepository.GetItems().ToList(); Assert.IsTrue(catalog.Count() == 2); }
public CatalogProductsController(StatelessServiceContext context) { var configurationPackage = context.CodePackageActivationContext.GetConfigurationPackageObject("Config"); var vendorDatabaseConfigSection = configurationPackage.Settings.Sections.Contains("VendorDatabase") ? configurationPackage.Settings.Sections["VendorDatabase"] : null; var databaseEndpoint = new Uri(vendorDatabaseConfigSection?.Parameters["Endpoint"]?.Value ?? "https://localhost:8081"); var databaseId = vendorDatabaseConfigSection?.Parameters["DatabaseId"]?.Value ?? "microwish"; var collectionName = vendorDatabaseConfigSection?.Parameters["CollectionName"]?.Value ?? "catalog"; var authKey = vendorDatabaseConfigSection?.Parameters["AccessKey"]?.Value ?? "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; this.repository = new ProductCatalogRepository(databaseEndpoint, databaseId, collectionName, authKey); }
public void IfItemNotFoundExceptionThrown() { var fileReader = new Mock <IProductCatalogFileReader>(); fileReader.Setup(x => x.GetTextReader()).Returns(_catalogTestData.GetValidTestData()); var testRepository = new ProductCatalogRepository(fileReader.Object); var priceService = new PriceService(testRepository); var notFoundItem = new BasketItem() { Name = "Cheese" }; priceService.GetItemPrice(notFoundItem); }
public async Task GetCategories_Uses_Cache_When_Data_Available() { var cacheService = new MockCacheService(); // cacheService.DataExistsAndIsValidAsyncDelegate = s => Task.FromResult(true); var categories = new List <Category> { new Category { Id = 1 }, new Category { Id = 2 } }; cacheService.GetDataDelegate = (string s) => { if (s == "Categories-0-0") { return(new ReadOnlyCollection <Category>(categories)); } return(new ReadOnlyCollection <Category>(null)); }; var productCatalogService = new MockProductCatalogService { GetSubcategoriesAsyncDelegate = (parentId, maxProducts) => Task.FromResult <ICollection <Category> >( new Collection <Category>(null)) }; var target = new ProductCatalogRepository(productCatalogService, cacheService); var returnedCategories = (await target.GetRootCategoriesAsync(0)).ToList(); Assert.AreEqual(2, returnedCategories.Count); Assert.AreEqual(1, returnedCategories[0].Id); Assert.AreEqual(2, returnedCategories[1].Id); }
public void Initialize() { // create repository objRepo = new ProductCatalogRepository(new Data.Infrastructure.DatabaseFactory()); }