public ProductBuilderTests() { this.variantBuilder = Substitute.For <IVariantBuilder <Item> >(); this.pricingManager = Substitute.For <IPricingManager>(); this.inventoryManager = Substitute.For <IInventoryManager>(); this.storefrontContext = Substitute.For <IStorefrontContext>(); this.getProductBulkPricesResult = this.Fixture.Create <GetProductBulkPricesResult>(); this.pricingManager .GetProductBulkPrices(Arg.Any <string>(), Arg.Any <IEnumerable <string> >(), Arg.Any <string[]>()) .Returns(this.getProductBulkPricesResult); this.getProductPricesResult = this.Fixture.Create <GetProductPricesResult>(); this.pricingManager .GetProductPrices(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <bool>(), Arg.Any <string[]>()) .Returns(this.getProductPricesResult); this.Fixture.Customize <StockInformation>( info => info.With(i => i.Product, this.Fixture.Create <CommerceInventoryProduct>())); this.getStockInformationResult = this.Fixture.Create <GetStockInformationResult>(); this.inventoryManager .GetStockInformation( Arg.Any <string>(), Arg.Any <IEnumerable <CommerceInventoryProduct> >(), Arg.Any <StockDetailsLevel>()) .Returns(this.getStockInformationResult); this.productBuilder = new ProductBuilder( this.variantBuilder, this.CatalogContext, this.pricingManager, this.inventoryManager, this.storefrontContext, this.CatalogMapper); }
public void ShouldGetProductPricesInCuurencyForTheSpecifiedProducts() { // Arrange GetProductBulkPricesRequest request = new GetProductBulkPricesRequest(new[] { "ProductId1", "ProductId2" }, "PriceType"); request.CurrencyCode = "CAD"; GetProductBulkPricesResult result = new GetProductBulkPricesResult(); ServicePipelineArgs args = new ServicePipelineArgs(request, result); result.Prices.Add("ProductId1", null); this.client.GetProductPrices(Arg.Is <string[]>(argument => (argument.Length == 2) && (argument[0] == "ProductId1") && (argument[1] == "ProductId2")), "PriceType").Returns(new[] { new ProductPriceModel { ProductId = "ProductId1", Price = 2 }, new ProductPriceModel { ProductId = "ProductId2", Price = 1 } }); // Act this.processor.Process(args); // Assert result.Prices.Should().HaveCount(2); result.Prices["ProductId1"].Amount.Should().Be(2); result.Prices["ProductId1"].CurrencyCode.Should().Be("CAD"); result.Prices["ProductId2"].Amount.Should().Be(1); result.Prices["ProductId2"].CurrencyCode.Should().Be("CAD"); }
public void ShouldNotAddNewRecordsIfProductPriceIsNotFound() { // Arrange GetProductBulkPricesRequest request = new GetProductBulkPricesRequest(new[] { "ProductId1" }, "PriceType"); GetProductBulkPricesResult result = new GetProductBulkPricesResult(); ServicePipelineArgs args = new ServicePipelineArgs(request, result); this.client.GetProductPrices(Arg.Is <string[]>(argument => (argument.Length == 1) && (argument[0] == "ProductId1")), "PriceType").Returns(new[] { new ProductPriceModel { ProductId = "ProductId1", Price = null } }); // Act this.processor.Process(args); // Assert result.Prices.Should().BeEmpty(); }
public ManagerResponse <GetProductBulkPricesResult, IDictionary <string, Price> > GetProductBulkPrices(string catalogName, IEnumerable <string> productIds, params string[] priceTypeIds) { if (priceTypeIds == null) { priceTypeIds = PricingManager.DefaultPriceTypeIds; } var bulkPricesRequest = new Sitecore.Commerce.Engine.Connect.Services.Prices.GetProductBulkPricesRequest(catalogName, productIds, priceTypeIds) { DateTime = DateTime.UtcNow }; GetProductBulkPricesResult productBulkPrices = this.pricingServiceProvider.GetProductBulkPrices(bulkPricesRequest); IDictionary <string, Price> result = productBulkPrices.Prices ?? new Dictionary <string, Price>(); return(new ManagerResponse <GetProductBulkPricesResult, IDictionary <string, Price> >(productBulkPrices, result)); }
/// <summary> /// Gets the product bulk prices. /// </summary> /// <param name="productIds">The product ids.</param> /// <returns>The product bulk prices.</returns> public IDictionary <string, decimal> GetProductBulkPrices(IEnumerable <string> productIds) { var ids = productIds as IList <string> ?? productIds.ToList(); GetProductBulkPricesRequest request = new GetProductBulkPricesRequest(ids); GetProductBulkPricesResult result = this.serviceProvider.GetProductBulkPrices(request); var prices = new Dictionary <string, decimal>(ids.Count); foreach (var id in ids) { var price = result.Prices.ContainsKey(id) ? result.Prices[id].Amount : decimal.Zero; if (prices.ContainsKey(id)) { continue; } prices.Add(id, price); } return(prices); }
/// <summary> /// Processes the specified args. /// </summary> /// <param name="args">The args.</param> public override void Process(ServicePipelineArgs args) { GetProductBulkPricesRequest request = (GetProductBulkPricesRequest)args.Request; GetProductBulkPricesResult result = (GetProductBulkPricesResult)args.Result; ProductPriceModel[] productPriceModels; var currencyCode = request.CurrencyCode; if (string.IsNullOrEmpty(currencyCode)) { currencyCode = "USD"; } using (IPricesServiceChannel client = this.GetClient()) { productPriceModels = client.GetProductPrices(request.ProductIds.ToArray(), request.PriceType); } foreach (ProductPriceModel model in productPriceModels) { if (model.Price != null) { if (result.Prices.ContainsKey(model.ProductId)) { result.Prices[model.ProductId] = new Price(model.Price.Value, currencyCode) { PriceType = request.PriceType }; } else { result.Prices.Add(model.ProductId, new Price(model.Price.Value, currencyCode) { PriceType = request.PriceType }); } } } }
/// <summary> /// Processes the specified arguments. /// </summary> /// <param name="args">The arguments.</param> public override void Process(Commerce.Pipelines.ServicePipelineArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNull(args.Request, "args.request"); Assert.ArgumentCondition(args.Request is GetProductBulkPricesRequest, "args.Request", "args.Request is GetProductBulkPricesRequest"); Assert.ArgumentCondition(args.Result is GetProductBulkPricesResult, "args.Result", "args.Result is GetProductBulkPricesResult"); GetProductBulkPricesRequest request = (GetProductBulkPricesRequest)args.Request; GetProductBulkPricesResult result = (GetProductBulkPricesResult)args.Result; Assert.ArgumentNotNull(request.ProductCatalogName, "request.ProductCatalogName"); Assert.ArgumentNotNull(request.ProductIds, "request.ProductIds"); Assert.ArgumentNotNull(request.PriceType, "request.PriceType"); ICatalogRepository catalogRepository = CommerceTypeLoader.CreateInstance <ICatalogRepository>(); bool isList = Array.FindIndex(request.PriceTypeIds as string[], t => t.IndexOf("List", StringComparison.OrdinalIgnoreCase) >= 0) > -1; bool isAdjusted = Array.FindIndex(request.PriceTypeIds as string[], t => t.IndexOf("Adjusted", StringComparison.OrdinalIgnoreCase) >= 0) > -1; var uniqueIds = request.ProductIds.ToList().Distinct(); foreach (var requestedProductId in uniqueIds) { try { var product = catalogRepository.GetProductReadOnly(request.ProductCatalogName, requestedProductId); Dictionary <string, Price> prices = new Dictionary <string, Price>(); // BasePrice is a List price and ListPrice is Adjusted price if (isList) { if (product.HasProperty("BasePrice") && product["BasePrice"] != null) { prices.Add("List", new Price { PriceType = "List", Amount = (product["BasePrice"] as decimal?).Value }); } else { // No base price is defined, the List price is set to the actual ListPrice define in the catalog prices.Add("List", new Price { PriceType = "List", Amount = product.ListPrice }); } } if (isAdjusted && !product.IsListPriceNull()) { prices.Add("Adjusted", new Price { PriceType = "Adjusted", Amount = product.ListPrice }); } result.Prices.Add(requestedProductId, prices); } catch (CommerceServer.Core.EntityDoesNotExistException e) { result.Success = false; result.SystemMessages.Add(new SystemMessage { Message = e.Message }); continue; } } }