public void Execute_12_NoFulfillmentFee( IRuleValue <string> fulfillmentOptionName, IRuleValue <decimal> amountOff, Cart cart, CommerceContext commerceContext, IRuleExecutionContext context, string fulfillmentOptionNameValue, FulfillmentComponent fulfillmentComponent) { /********************************************** * Arrange **********************************************/ cart.Adjustments.Clear(); cart.Lines.ForEach(l => l.Adjustments.Clear()); cart.SetComponent(fulfillmentComponent); context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext); commerceContext.AddObject(cart); fulfillmentOptionName.Yield(context).ReturnsForAnyArgs(fulfillmentOptionNameValue); amountOff.Yield(context).ReturnsForAnyArgs(MINIMUM_AMOUNT_OFF); var commander = Substitute.For <CommerceCommander>(Substitute.For <IServiceProvider>()); var command = Substitute.For <GetFulfillmentMethodsCommand>( Substitute.For <IFindEntityPipeline>(), Substitute.For <IGetCartFulfillmentMethodsPipeline>(), Substitute.For <IGetCartLineFulfillmentMethodsPipeline>(), Substitute.For <IGetFulfillmentMethodsPipeline>(), Substitute.For <IServiceProvider>()); var fulfillmentMethod = new FulfillmentMethod() { FulfillmentType = fulfillmentOptionNameValue }; command.Process(commerceContext).ReturnsForAnyArgs(new List <FulfillmentMethod>() { fulfillmentMethod }.AsEnumerable()); commander.When(x => x.Command <GetFulfillmentMethodsCommand>()).DoNotCallBase(); commander.Command <GetFulfillmentMethodsCommand>().Returns(command); var action = new CartShippingOptionAmountOffAction(commander) { FulfillmentOptionName = fulfillmentOptionName, AmountOff = amountOff }; /********************************************** * Act **********************************************/ Action executeAction = () => action.Execute(context); /********************************************** * Assert **********************************************/ executeAction.Should().NotThrow <Exception>(); command.Received().Process(commerceContext); cart.Lines.SelectMany(l => l.Adjustments).Should().BeEmpty(); cart.Adjustments.Should().BeEmpty(); }
public void Evaluate_05_NoFulfillmentOptionName( CommerceContext commerceContext, Cart cart, IRuleExecutionContext context, FulfillmentComponent fulfillmentComponent) { /********************************************** * Arrange **********************************************/ cart.SetComponent(fulfillmentComponent); context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext); commerceContext.AddObject(cart); var commander = Substitute.For <CommerceCommander>(Substitute.For <IServiceProvider>()); var condition = new CartHasFulfillmentOptionCondition(commander); condition.FulfillmentOptionName = null; /********************************************** * Act **********************************************/ Action evaluateCondition = () => condition.Evaluate(context); /********************************************** * Assert **********************************************/ evaluateCondition.Should().Throw <Exception>(); }
public void Evaluate_09_EmptyFulfillmentMethodName( IRuleValue <string> fulfillmentOptionName, CommerceContext commerceContext, Cart cart, IRuleExecutionContext context, SplitFulfillmentComponent splitFulfillmentComponent, FulfillmentComponent fulfillmentComponent, string fulfillmentOptionNameValue) { /********************************************** * Arrange **********************************************/ fulfillmentComponent.FulfillmentMethod.Name = null; cart.SetComponent(splitFulfillmentComponent); while (cart.Lines.Count > 1) { cart.Lines.RemoveAt(0); } cart.Lines[0].SetComponent(fulfillmentComponent); context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext); commerceContext.AddObject(cart); fulfillmentOptionName.Yield(context).ReturnsForAnyArgs(fulfillmentOptionNameValue); var command = Substitute.For <GetFulfillmentMethodsCommand>( Substitute.For <IFindEntityPipeline>(), Substitute.For <IGetCartFulfillmentMethodsPipeline>(), Substitute.For <IGetCartLineFulfillmentMethodsPipeline>(), Substitute.For <IGetFulfillmentMethodsPipeline>(), Substitute.For <IServiceProvider>()); var commander = Substitute.For <CommerceCommander>(Substitute.For <IServiceProvider>()); var fulfillmentMethod = new FulfillmentMethod() { FulfillmentType = fulfillmentOptionNameValue }; command.Process(commerceContext).ReturnsForAnyArgs(new List <FulfillmentMethod>() { fulfillmentMethod }.AsEnumerable()); commander.When(x => x.Command <GetFulfillmentMethodsCommand>()).DoNotCallBase(); commander.Command <GetFulfillmentMethodsCommand>().Returns(command); var condition = new CartLineHasFulfillmentOptionCondition(commander) { FulfillmentOptionName = fulfillmentOptionName }; /********************************************** * Act **********************************************/ var result = condition.Evaluate(context); /********************************************** * Assert **********************************************/ result.Should().BeFalse(); }
public void Evaluate_HasMatchingFulfillmentMethod_True( IRuleValue <string> fulfillmentOptionName, CommerceContext commerceContext, Cart cart, IRuleExecutionContext context, FulfillmentComponent fulfillmentComponent, string fulfillmentOptionNameValue) { /********************************************** * Arrange **********************************************/ cart.SetComponent(fulfillmentComponent); context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext); commerceContext.AddObject(cart); fulfillmentOptionName.Yield(context).ReturnsForAnyArgs(fulfillmentOptionNameValue); var command = Substitute.For <GetFulfillmentMethodsCommand>( Substitute.For <IFindEntityPipeline>(), Substitute.For <IGetCartFulfillmentMethodsPipeline>(), Substitute.For <IGetCartLineFulfillmentMethodsPipeline>(), Substitute.For <IGetFulfillmentMethodsPipeline>(), Substitute.For <IServiceProvider>()); var commander = Substitute.For <CommerceCommander>(Substitute.For <IServiceProvider>()); var fulfillmentMethod = new FulfillmentMethod { FulfillmentType = fulfillmentOptionNameValue, Id = fulfillmentComponent.FulfillmentMethod.EntityTarget, Name = fulfillmentComponent.FulfillmentMethod.Name }; command.Process(commerceContext).ReturnsForAnyArgs(new List <FulfillmentMethod>() { fulfillmentMethod }.AsEnumerable()); commander.When(x => x.Command <GetFulfillmentMethodsCommand>()).DoNotCallBase(); commander.Command <GetFulfillmentMethodsCommand>().Returns(command); var condition = new CartHasFulfillmentOptionCondition(commander) { FulfillmentOptionName = fulfillmentOptionName }; /********************************************** * Act **********************************************/ var result = condition.Evaluate(context); /********************************************** * Assert **********************************************/ result.Should().BeTrue(); }
public void Execute_WithProperties( IRuleValue <string> fulfillmentOptionName, IRuleValue <decimal> amountOff, Cart cart, CommerceContext commerceContext, IRuleExecutionContext context, string fulfillmentOptionNameValue, FulfillmentComponent fulfillmentComponent) { /********************************************** * Arrange **********************************************/ const decimal FULFILLMENT_FEE = MINIMUM_AMOUNT_OFF + 10m; cart.Adjustments.Clear(); var awardedAdjustment = new AwardedAdjustment() { Name = "FulfillmentFee" }; awardedAdjustment.Adjustment = new Money(FULFILLMENT_FEE); cart.Adjustments.Add(awardedAdjustment); cart.Lines.ForEach(l => l.Adjustments.Clear()); cart.SetComponent(fulfillmentComponent); context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext); commerceContext.AddObject(cart); var globalPricingPolicy = commerceContext.GetPolicy <GlobalPricingPolicy>(); globalPricingPolicy.ShouldRoundPriceCalc = false; fulfillmentOptionName.Yield(context).ReturnsForAnyArgs(fulfillmentOptionNameValue); amountOff.Yield(context).ReturnsForAnyArgs(MINIMUM_AMOUNT_OFF); var propertiesModel = new PropertiesModel(); propertiesModel.Properties.Add("PromotionId", "id"); propertiesModel.Properties.Add("PromotionCartText", "carttext"); propertiesModel.Properties.Add("PromotionText", "text"); commerceContext.AddObject(propertiesModel); var commander = Substitute.For <CommerceCommander>(Substitute.For <IServiceProvider>()); var command = Substitute.For <GetFulfillmentMethodsCommand>( Substitute.For <IFindEntityPipeline>(), Substitute.For <IGetCartFulfillmentMethodsPipeline>(), Substitute.For <IGetCartLineFulfillmentMethodsPipeline>(), Substitute.For <IGetFulfillmentMethodsPipeline>(), Substitute.For <IServiceProvider>()); var fulfillmentMethod = new FulfillmentMethod() { FulfillmentType = fulfillmentOptionNameValue }; command.Process(commerceContext).ReturnsForAnyArgs(new List <FulfillmentMethod>() { fulfillmentMethod }.AsEnumerable()); commander.When(x => x.Command <GetFulfillmentMethodsCommand>()).DoNotCallBase(); commander.Command <GetFulfillmentMethodsCommand>().Returns(command); var action = new CartShippingOptionAmountOffAction(commander) { FulfillmentOptionName = fulfillmentOptionName, AmountOff = amountOff }; /********************************************** * Act **********************************************/ Action executeAction = () => action.Execute(context); /********************************************** * Assert **********************************************/ executeAction.Should().NotThrow <Exception>(); cart.Lines.SelectMany(l => l.Adjustments).Should().HaveCount(0); cart.Adjustments.Should().NotBeEmpty(); cart.Adjustments.LastOrDefault().Should().NotBeNull(); cart.Adjustments.LastOrDefault().Should().BeOfType <CartLevelAwardedAdjustment>(); cart.Adjustments.LastOrDefault().Name.Should().Be("text"); cart.Adjustments.LastOrDefault().DisplayName.Should().Be("carttext"); cart.Adjustments.LastOrDefault().AdjustmentType.Should().Be(commerceContext.GetPolicy <KnownCartAdjustmentTypesPolicy>().Discount); cart.Adjustments.LastOrDefault().AwardingBlock.Should().Contain(nameof(CartShippingOptionAmountOffAction)); cart.Adjustments.LastOrDefault().IsTaxable.Should().BeFalse(); cart.Adjustments.LastOrDefault().Adjustment.CurrencyCode.Should().Be(commerceContext.CurrentCurrency()); cart.Adjustments.LastOrDefault().Adjustment.Amount.Should().Be(-MINIMUM_AMOUNT_OFF); cart.HasComponent <MessagesComponent>().Should().BeTrue(); }
public void Execute_AmountOffLessThanFulfillmentFee_False( IRuleValue <string> fulfillmentOptionName, IRuleValue <decimal> amountOff, Cart cart, CommerceContext commerceContext, IRuleExecutionContext context, string fulfillmentOptionNameValue, FulfillmentComponent fulfillmentComponent) { /********************************************** * Arrange **********************************************/ const decimal FULFILLMENT_FEE = 10m; const decimal AMOUNT_OFF = FULFILLMENT_FEE + 1m; cart.Adjustments.Clear(); var awardedAdjustment = new AwardedAdjustment() { Name = "FulfillmentFee" }; awardedAdjustment.Adjustment = new Money(FULFILLMENT_FEE); cart.Adjustments.Add(awardedAdjustment); cart.Lines.ForEach(l => l.Adjustments.Clear()); cart.SetComponent(fulfillmentComponent); context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext); commerceContext.AddObject(cart); fulfillmentOptionName.Yield(context).ReturnsForAnyArgs(fulfillmentOptionNameValue); amountOff.Yield(context).ReturnsForAnyArgs(AMOUNT_OFF); var commander = Substitute.For <CommerceCommander>(Substitute.For <IServiceProvider>()); var command = Substitute.For <GetFulfillmentMethodsCommand>( Substitute.For <IFindEntityPipeline>(), Substitute.For <IGetCartFulfillmentMethodsPipeline>(), Substitute.For <IGetCartLineFulfillmentMethodsPipeline>(), Substitute.For <IGetFulfillmentMethodsPipeline>(), Substitute.For <IServiceProvider>()); var fulfillmentMethod = new FulfillmentMethod() { FulfillmentType = fulfillmentOptionNameValue }; command.Process(commerceContext).ReturnsForAnyArgs(new List <FulfillmentMethod>() { fulfillmentMethod }.AsEnumerable()); commander.When(x => x.Command <GetFulfillmentMethodsCommand>()).DoNotCallBase(); commander.Command <GetFulfillmentMethodsCommand>().Returns(command); var action = new CartShippingOptionAmountOffAction(commander) { FulfillmentOptionName = fulfillmentOptionName, AmountOff = amountOff }; /********************************************** * Act **********************************************/ Action executeAction = () => action.Execute(context); /********************************************** * Assert **********************************************/ executeAction.Should().NotThrow <Exception>(); command.Received().Process(commerceContext); cart.Lines.SelectMany(l => l.Adjustments).Should().BeEmpty(); cart.Adjustments.Should().HaveCount(2, "The cart adjustments should only contain the fulfillment fee and shipping discount adjustments."); var adjustment = cart.Adjustments.LastOrDefault(); adjustment.Adjustment.Amount.Should().Be(-FULFILLMENT_FEE, "Adjustment amount should match the AmountOff rule."); }
public async Task <IActionResult> Put([FromBody] ODataActionParameters value) { if (!this.ModelState.IsValid || value == null) { return((IActionResult) new BadRequestObjectResult((object)value)); } string id = value["customerId"].ToString(); if (!this.ModelState.IsValid || string.IsNullOrEmpty(id)) { return((IActionResult)this.NotFound()); } GetCustomerCommand command0 = this.Command <GetCustomerCommand>(); Customer customer = await command0.Process(this.CurrentContext, id); var y = customer.GetComponent <AddressComponent>(); //refactor string randoms = Guid.NewGuid().ToString().Replace("-", string.Empty).Replace("+", string.Empty); string cartId = string.Format("{0}{1}", "CUSTOM", randoms); string str = value["itemId"].ToString(); Decimal result; string q = "1"; if (!Decimal.TryParse(q, out result)) { return((IActionResult) new BadRequestObjectResult((object)q)); } AddCartLineCommand command = this.Command <AddCartLineCommand>(); CartLineComponent line = new CartLineComponent() { ItemId = str, Quantity = result }; Cart cart = await command.Process(this.CurrentContext, cartId, line); //FulfillmentComponent FulfillmentComponent fulfillment = (PhysicalFulfillmentComponent) new PhysicalFulfillmentComponent() { ShippingParty = new Party() { Address1 = y.Party.Address1, City = y.Party.City, ZipPostalCode = y.Party.ZipPostalCode, State = y.Party.State, StateCode = y.Party.StateCode, CountryCode = y.Party.CountryCode, AddressName = y.Party.AddressName, Name = y.Party.Name }, FulfillmentMethod = new EntityReference() { Name = "Ground", EntityTarget = "B146622D-DC86-48A3-B72A-05EE8FFD187A" } }; SetCartFulfillmentCommand _CartFulfillmentCommand = this.Command <SetCartFulfillmentCommand>(); Cart cart1 = await _CartFulfillmentCommand.Process(CurrentContext, cartId, fulfillment); //FederatedPaymentComponent decimal gt; Decimal.TryParse(cart1.Totals.GrandTotal.Amount.ToString(), out gt); FederatedPaymentComponent paymentComponent = new FederatedPaymentComponent(new Money() { Amount = gt }); paymentComponent.PaymentMethod = new EntityReference() { EntityTarget = "0CFFAB11-2674-4A18-AB04-228B1F8A1DEC", Name = "Federated" }; paymentComponent.PaymentMethodNonce = "fake-valid-nonce"; paymentComponent.BillingParty = new Party() { Address1 = y.Party.Address1, City = y.Party.City, ZipPostalCode = y.Party.ZipPostalCode, State = y.Party.State, StateCode = y.Party.StateCode, CountryCode = y.Party.CountryCode, AddressName = y.Party.AddressName, }; AddPaymentsCommand _PaymentsCommand = this.Command <AddPaymentsCommand>(); Cart cart2 = await _PaymentsCommand.Process(this.CurrentContext, cartId, (IEnumerable <PaymentComponent>) new List <PaymentComponent>() { (PaymentComponent)paymentComponent }); //CreateOrderCommand string email = customer.Email; CreateOrderCommand _CreateOrderCommand = this.Command <CreateOrderCommand>(); Order order = await _CreateOrderCommand.Process(this.CurrentContext, cartId, email); return((IActionResult) new ObjectResult((object)_CreateOrderCommand)); }