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);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CartsServiceTest"/> class.
        /// </summary>
        public CartsServiceTest()
        {
            this.cart = new Cart();
            this.cartFromAnonymous = new Cart();

            this.result = new CartResult {
                Cart = this.cart
            };
            this.resultFromAnonymous = new CartResult {
                Cart = this.cartFromAnonymous
            };

            this.cartServiceProvider = Substitute.For <CartServiceProvider>();
            this.cartServiceProvider.CreateOrResumeCart(Arg.Is <CreateOrResumeCartRequest>(r => r.UserId == "John Carter")).Returns(this.result);

            var pricesResult = new GetProductPricesResult();

            pricesResult.Prices.Add("List", new Price(0, "USD"));
            this.pricingService = Substitute.For <PricingServiceProvider>();
            this.pricingService.GetProductPrices(Arg.Any <GetProductPricesRequest>()).Returns(pricesResult);

            this.contactId = Guid.NewGuid();
            this.cartServiceProvider.CreateOrResumeCart(Arg.Is <CreateOrResumeCartRequest>(r => r.UserId == ID.Parse(this.contactId).ToString())).Returns(this.resultFromAnonymous);

            this.cartServiceProvider.GetCarts(Arg.Any <GetCartsRequest>()).Returns(new GetCartsResult {
                Carts = Enumerable.Empty <CartBase>()
            });
            this.contactFactory = Substitute.For <ContactFactory>();
            this.contactFactory.GetContact().Returns("John Carter");

            var inventoryResult = new GetStockInformationResult();

            inventoryResult.StockInformation.ToList().Add(new StockInformation {
                Product = new InventoryProduct {
                    ProductId = "1001"
                }, Status = StockStatus.InStock
            });
            this._inventoryService = Substitute.For <InventoryServiceProvider>();
            this._inventoryService.GetStockInformation(Arg.Any <GetStockInformationRequest>()).Returns(inventoryResult);

            this._customerService = Substitute.For <CustomerServiceProvider>();

            this._wishListServiceProvider = Substitute.For <WishListServiceProvider>();

            this.service = new CartService(this.cartServiceProvider, this._wishListServiceProvider, this.pricingService, "autohaus", this.contactFactory, this._inventoryService, this._customerService);
        }
        public ManagerResponse <GetProductPricesResult, IDictionary <string, Price> > GetProductPrices(string catalogName, string productId, bool includeVariants, params string[] priceTypeIds)
        {
            if (priceTypeIds == null)
            {
                priceTypeIds = PricingManager.DefaultPriceTypeIds;
            }

            var pricesRequest =
                new Sitecore.Commerce.Engine.Connect.Services.Prices.GetProductPricesRequest(catalogName, productId, priceTypeIds)
            {
                DateTime             = DateTime.UtcNow,
                IncludeVariantPrices = includeVariants
            };

            GetProductPricesResult serviceProviderResult = this.pricingServiceProvider.GetProductPrices(pricesRequest);

            return(new ManagerResponse <GetProductPricesResult, IDictionary <string, Price> >(serviceProviderResult, serviceProviderResult.Prices ?? new Dictionary <string, Price>()));
        }
        public void ShouldLeaveThePriceIntactIfFailedToRetrievePrices()
        {
            // arrange
            var cartLine = new CartLine {
                Product = new CartProduct {
                    ProductId = "TestProductId"
                }
            };

            this.cart.Lines = new ReadOnlyCollectionAdapter <CartLine> {
                cartLine
            };

            var result = new GetProductPricesResult();

            this.pricingService.GetProductPrices(Arg.Is <GetProductPricesRequest>(r => r.ProductId == "TestProductId")).Returns(result);

            // act
            var cartModel = this.service.GetCart();

            // assert
            cartModel.Lines.Single().Product.Price.Should().BeNull();
        }
        public void ShouldGetPriceForProduct()
        {
            // arrange
            var cartLine = new CartLine {
                Product = new CartProduct {
                    ProductId = "TestProductId"
                }
            };

            this.cart.Lines = new ReadOnlyCollectionAdapter <CartLine> {
                cartLine
            };

            var result = new GetProductPricesResult();

            result.Prices.Add("List", new Price(10.50m, "USD"));
            this.pricingService.GetProductPrices(Arg.Is <GetProductPricesRequest>(r => r.ProductId == "TestProductId")).Returns(result);

            // act
            var cartModel = this.service.GetCart();

            // assert
            cartModel.Lines.Single().Product.Price.Amount.Should().Be(10.50m);
        }
        /// <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 GetProductPricesRequest, "args.Request", "args.Request is GetProductPricesRequest");
            Assert.ArgumentCondition(args.Result is GetProductPricesResult, "args.Result", "args.Result is GetProductPricesResult");

            GetProductPricesRequest request = (GetProductPricesRequest)args.Request;
            GetProductPricesResult  result  = (GetProductPricesResult)args.Result;

            Assert.ArgumentNotNull(request.ProductCatalogName, "request.ProductCatalogName");
            Assert.ArgumentNotNull(request.ProductId, "request.ProductId");
            Assert.ArgumentNotNull(request.PriceTypeIds, "request.PriceTypeIds");

            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;

            try
            {
                var product = catalogRepository.GetProductReadOnly(request.ProductCatalogName, request.ProductId);
                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(request.ProductId, prices);
                if (request.IncludeVariantPrices && product is ProductFamily)
                {
                    foreach (Variant variant in ((ProductFamily)product).Variants)
                    {
                        Dictionary <string, Price> variantPrices = new Dictionary <string, Price>();
                        if (isList && product.HasProperty("BasePrice") && variant["BasePriceVariant"] != null)
                        {
                            variantPrices.Add("List", new Price {
                                PriceType = "List", Amount = (variant["BasePriceVariant"] as decimal?).Value
                            });
                        }

                        if (isAdjusted && !variant.IsListPriceNull())
                        {
                            variantPrices.Add("Adjusted", new Price {
                                PriceType = "Adjusted", Amount = variant.ListPrice
                            });
                        }

                        result.Prices.Add(variant.VariantId, variantPrices);
                    }
                }
            }
            catch (CommerceServer.Core.EntityDoesNotExistException e)
            {
                result.Success = false;
                result.SystemMessages.Add(new SystemMessage {
                    Message = e.Message
                });
            }
        }