Esempio n. 1
0
        public void WhenApplicationWithErrorsThenExceptionsListReportsFaultyRules()
        {
            var testContext = new TestContext();

            var pipeline = new FaultTolerantPromotionPipeline();
            var ruleA    = new PromotionRuleA();
            var ruleB    = new FaultyPromotionRule();
            var ruleC    = new PromotionRuleA();
            var ruleD    = new FaultyPromotionRule();

            pipeline.AddRule(ruleA);
            pipeline.AddRule(ruleB);
            pipeline.AddRule(ruleC);
            pipeline.AddRule(ruleD);

            ICart cart = pipeline.Apply(testContext.CartFactory.Create());

            Assert.NotNull(cart);
            Assert.NotEmpty(pipeline.LastApplyExceptions);

            Assert.Collection(pipeline.LastApplyExceptions,
                              errorEntry =>
            {
                Assert.True((object)errorEntry.Item1 == (object)ruleB);
                Assert.IsType <TestException>(errorEntry.Item2);
            },
                              errorEntry =>
            {
                Assert.True((object)errorEntry.Item1 == (object)ruleD);
                Assert.IsType <TestException>(errorEntry.Item2);
            });
        }
Esempio n. 2
0
        public void PairOfDifferentSkusForRule_Two()
        {
            var testContext = new TestContext();

            var skuA = testContext.CreateNewSku("A", 100);
            var skuB = testContext.CreateNewSku("B", 200);
            var skuC = testContext.CreateNewSku("C", 300);
            var skuD = testContext.CreateNewSku("D", 400);

            var cart = testContext.CartFactory.Create();

            cart.Add(skuA, 2);
            cart.Add(skuB, 3);
            cart.Add(skuC, 2);
            cart.Add(skuD, 3);
            cart.TestTotal(2 * 100 + 3 * 200 + 2 * 300 + 3 * 400);

            var pipeline = new FaultTolerantPromotionPipeline();

            pipeline.AddRule(new PairOfDifferentSkusForRule(testContext.CartFactory, skuA, skuB, 100));
            pipeline.AddRule(new PairOfDifferentSkusForRule(testContext.CartFactory, skuC, skuD, 200));
            var newCart = pipeline.Apply(cart);

            newCart.TestTotal(2 * 100 + 1 * 200 + 2 * 200 + 1 * 400);
        }
Esempio n. 3
0
        public void WhenNoRulesThenExceptionsListIsEmpty()
        {
            var testContext = new TestContext();

            var pipeline = new FaultTolerantPromotionPipeline();

            ICart cart = pipeline.Apply(testContext.CartFactory.Create());

            Assert.NotNull(cart);
            Assert.Empty(pipeline.LastApplyExceptions);
        }
Esempio n. 4
0
        public void WhenApplicationWithErrorsThenExceptionsListIsNotEmpty()
        {
            var testContext = new TestContext();

            var pipeline = new FaultTolerantPromotionPipeline();

            pipeline.AddRule(new PromotionRuleA());
            pipeline.AddRule(new FaultyPromotionRule());

            ICart cart = pipeline.Apply(testContext.CartFactory.Create());

            Assert.NotNull(cart);
            Assert.NotEmpty(pipeline.LastApplyExceptions);
        }
Esempio n. 5
0
        CreateTestFullContext(TestContext testContext)
        {
            var skuA = testContext.CreateNewSku("A", skuAUnitPrice);
            var skuB = testContext.CreateNewSku("B", skuBUnitPrice);
            var skuC = testContext.CreateNewSku("C", skuCUnitPrice);
            var skuD = testContext.CreateNewSku("D", skuDUnitPrice);

            var pipeline = new FaultTolerantPromotionPipeline();

            pipeline.AddRule(new CollectionOfSameSkuForRule(testContext.CartFactory, skuA, 3, rule1BatchPrice));
            pipeline.AddRule(new CollectionOfSameSkuForRule(testContext.CartFactory, skuB, 2, rule2BatchPrice));
            pipeline.AddRule(new PairOfDifferentSkusForRule(testContext.CartFactory, skuC, skuD, rule3BatchPrice));

            return(pipeline, skuA, skuB, skuC, skuD);
        }
Esempio n. 6
0
        /// <inheritdoc/>
        public IPromotionPipeline Create()
        {
            var pipeline = new FaultTolerantPromotionPipeline();

            foreach (var promotionRuleOption in this.promotionRuleOptions)
            {
                var promotionRule = this.CreateRule(promotionRuleOption);
                if (promotionRule == null)
                {
                    throw new InvalidOperationException($"No promotion rule corresponding to '{promotionRuleOption.Id}'");
                }

                pipeline.AddRule(promotionRule);
            }

            return(pipeline);
        }
Esempio n. 7
0
        public void WhenAllRulesFailThenReturnedCartIsTheSameAsOriginal()
        {
            var testContext = new TestContext();

            var pipeline = new FaultTolerantPromotionPipeline();
            var ruleA    = new FaultyPromotionRule();
            var ruleB    = new FaultyPromotionRule();

            pipeline.AddRule(ruleA);
            pipeline.AddRule(ruleB);

            ICart originalCart = testContext.CartFactory.Create();
            ICart newCart      = pipeline.Apply(originalCart);

            Assert.NotNull(newCart);
            Assert.True((object)newCart == (object)originalCart);
        }
Esempio n. 8
0
        public void CollectionOfSameSkuForRule_Two()
        {
            var testContext = new TestContext();

            var skuA = testContext.CreateNewSku("A", 100);
            var skuB = testContext.CreateNewSku("B", 200);

            var cart = testContext.CartFactory.Create();

            cart.Add(skuA, 2);
            cart.Add(skuB, 3);
            cart.TestTotal(2 * 100 + 3 * 200);

            var pipeline = new FaultTolerantPromotionPipeline();

            pipeline.AddRule(new CollectionOfSameSkuForRule(testContext.CartFactory, skuA, 2, 50));
            pipeline.AddRule(new CollectionOfSameSkuForRule(testContext.CartFactory, skuB, 2, 100));
            var newCart = pipeline.Apply(cart);

            newCart.TestTotal(1 * 50 + 1 * 100 + 200);
        }
Esempio n. 9
0
        public void WhenPreviousRuleFaultsThenNextRulesActionIsPreserved()
        {
            var testContext = new TestContext();

            var pipeline = new FaultTolerantPromotionPipeline();
            var sku      = testContext.CreateNewSku("S", 0);
            var ruleA    = new FaultyPromotionRule();
            var ruleB    = new SideEffectsPromotionRule(sku);

            pipeline.AddRule(ruleA);
            pipeline.AddRule(ruleB);

            ICart cart = pipeline.Apply(testContext.CartFactory.Create());

            Assert.NotNull(cart);
            Assert.NotEmpty(pipeline.LastApplyExceptions);
            Assert.Collection(cart, entry =>
            {
                Assert.True((object)entry.Sku == (object)sku);
            });
        }