Exemple #1
0
        public void CartsCollectionEntryUsesTheIdInTheInfoHeader()
        {
            var testContext = new TestContext();

            var cartInfo  = new CartInfo();
            var cartEntry = new CartsCollection.CartsCollectionEntry(cartInfo,
                                                                     testContext.CartFactory.Create());

            Assert.Equal(cartEntry.Id, cartInfo.Id);
        }
Exemple #2
0
        public void AddCartEntryToCollection()
        {
            var testContext = new TestContext();

            var cartsCollection = new TestCartsCollection();
            var cartEntry       = new CartsCollection.CartsCollectionEntry(new CartInfo(),
                                                                           testContext.CartFactory.Create());

            cartsCollection.Add(cartEntry);

            Assert.Equal(1, cartsCollection.Count);
        }
Exemple #3
0
        public void RetrieveCartEntryFromCollection()
        {
            var testContext = new TestContext();

            var cartsCollection = new TestCartsCollection();
            var cartEntry       = new CartsCollection.CartsCollectionEntry(new CartInfo(),
                                                                           testContext.CartFactory.Create());

            cartsCollection.Add(cartEntry);
            var retrievedCartEntry = cartsCollection.Retrieve(cartEntry.Id);

            Assert.True((object)cartEntry == (object)retrievedCartEntry);
        }
Exemple #4
0
        public void RetrieveAll()
        {
            var testContext = new TestContext();

            var cartsCollection = new TestCartsCollection();
            var cartEntry1      = new CartsCollection.CartsCollectionEntry(new CartInfo(),
                                                                           testContext.CartFactory.Create());
            var cartEntry2 = new CartsCollection.CartsCollectionEntry(new CartInfo(),
                                                                      testContext.CartFactory.Create());

            cartsCollection.Add(cartEntry1);
            cartsCollection.Add(cartEntry2);
            var retrievedCartEntries = cartsCollection.Retrieve();

            Assert.Equal(2, retrievedCartEntries.Count());
        }
Exemple #5
0
        public void WhenAddingAnExistingCartThenExceptionIsThrown()
        {
            var testContext = new TestContext();

            var cartsCollection = new TestCartsCollection();
            var id         = "001";
            var cartInfo1  = new TestCartInfo(id);
            var cartInfo2  = new TestCartInfo(id);
            var cartEntry1 = new CartsCollection.CartsCollectionEntry(cartInfo1,
                                                                      testContext.CartFactory.Create());
            var cartEntry2 = new CartsCollection.CartsCollectionEntry(cartInfo2,
                                                                      testContext.CartFactory.Create());

            cartsCollection.Add(cartEntry1);

            Assert.Throws <ArgumentException>(() =>
            {
                cartsCollection.Add(cartEntry2);
            });
        }
Exemple #6
0
        private void ApplyPipeline(CartsCollection.CartsCollectionEntry cartEntry)
        {
            Task.Run(() =>
            {
                decimal oldTotal = cartEntry.Cart.Total;
                cartEntry.Cart   = this.promotionPipeline.Apply(cartEntry.Cart);
                decimal newTotal = cartEntry.Cart.Total;

                this.logger?.LogInformation($"Cart {cartEntry.Id} updated through pipeline: {oldTotal} => {newTotal}");

                var faultTolerantPipeline = this.promotionPipeline as FaultTolerantPromotionPipeline;
                if (faultTolerantPipeline != null && faultTolerantPipeline.LastApplyExceptions.Any())
                {
                    foreach (var errorReport in faultTolerantPipeline.LastApplyExceptions)
                    {
                        this.logger?.LogError($"Cart {cartEntry.Id} updated with errors: rule '{errorReport.Item1}' faulted with '{errorReport.Item2.Message}'");
                    }
                }
            });
        }
Exemple #7
0
        public void CannotDeleteCheckedOutCart()
        {
            var testContext = new TestContext();

            var inMemoryCollection = new CartsCollection();
            var cartEntry          = new CartsCollection.CartsCollectionEntry(
                new CartInfo(), testContext.CartFactory.Create());

            cartEntry.Info.CheckedOut = true;
            inMemoryCollection.Add(cartEntry);
            var controller = new CartController(null, inMemoryCollection,
                                                testContext.CartFactory, testContext.PriceList, new NeutralPipeline());

            var response = controller.Delete(cartEntry.Id);

            Assert.NotNull(response);
            Assert.Equal(CartOperationStatus.Error, response.Status);
            Assert.NotNull(response.Exception);
            Assert.Null(response.Body);
        }
Exemple #8
0
        public void WhenCheckoutEmptyCartThenExceptionIsReturnedInMessage()
        {
            var testContext = new TestContext();

            var inMemoryCollection = new CartsCollection();
            var cartEntry          = new CartsCollection.CartsCollectionEntry(
                new CartInfo(), testContext.CartFactory.Create());

            inMemoryCollection.Add(cartEntry);

            var controller = new CartController(null, inMemoryCollection,
                                                testContext.CartFactory, testContext.PriceList, new NeutralPipeline());

            var response = controller.Checkout(cartEntry.Id);

            Assert.NotNull(response);
            Assert.Equal(CartOperationStatus.Error, response.Status);
            Assert.NotNull(response.Exception);
            Assert.Null(response.Body);
        }
Exemple #9
0
        public void WhenDeletingCartThenCollectionShrinks()
        {
            var testContext = new TestContext();

            var inMemoryCollection = new TestCartsCollection();
            var cartEntry          = new CartsCollection.CartsCollectionEntry(
                new CartInfo(), testContext.CartFactory.Create());

            inMemoryCollection.Add(cartEntry);
            Assert.NotEqual(0, inMemoryCollection.Count);

            var controller = new CartController(null, inMemoryCollection,
                                                testContext.CartFactory, testContext.PriceList, new NeutralPipeline());

            var response = controller.Delete(cartEntry.Id);

            Assert.NotNull(response);
            Assert.Equal(CartOperationStatus.Successful, response.Status);
            Assert.NotNull(response.Body);
            Assert.Equal(0, inMemoryCollection.Count);
        }
Exemple #10
0
        public void WhenCheckoutCartThenCartMarkedInCollectionAndNotRemoved()
        {
            var testContext = new TestContext();

            var inMemoryCollection = new CartsCollection();
            var cartEntry          = new CartsCollection.CartsCollectionEntry(
                new CartInfo(), testContext.CartFactory.Create());

            cartEntry.Cart.Add(testContext.CreateNewSku("A", 100));
            inMemoryCollection.Add(cartEntry);

            var controller = new CartController(null, inMemoryCollection,
                                                testContext.CartFactory, testContext.PriceList, new NeutralPipeline());

            var response = controller.Checkout(cartEntry.Id);

            Assert.NotNull(response);
            Assert.Equal(CartOperationStatus.Successful, response.Status);
            Assert.NotNull(response.Body);
            Assert.True(cartEntry.Info.CheckedOut);
        }
Exemple #11
0
        public CartOperationInfo <CartInfo> Create()
        {
            var cartInfo  = new CartInfo();
            var cartEntry = new CartsCollection.CartsCollectionEntry(cartInfo, this.cartFactory.Create());

            try
            {
                this.dataSource.Add(cartEntry);
            }
            catch (Exception e)
            {
                this.logger?.LogError($"Could not create new cart: {cartInfo.Id}");
                return(new CartOperationInfo <CartInfo>(CartOperationType.Create, CartOperationStatus.Error, e));
            }

            this.logger?.LogInformation($"Created new cart: {cartInfo.Id}");

            return(new CartOperationInfo <CartInfo>(CartOperationType.Create, CartOperationStatus.Successful)
            {
                Body = cartInfo
            });
        }
Exemple #12
0
        public void WhenCartExistsThenGetCartReturnsCartContent()
        {
            var testContext = new TestContext();

            var inMemoryCollection = new CartsCollection();
            var cartEntry          = new CartsCollection.CartsCollectionEntry(
                new CartInfo(), testContext.CartFactory.Create());

            inMemoryCollection.Add(cartEntry);

            var controller = new CartController(null, inMemoryCollection,
                                                testContext.CartFactory, testContext.PriceList, new NeutralPipeline());

            var response = controller.GetCart(cartEntry.Id);

            Assert.NotNull(response);
            Assert.Equal(CartOperationStatus.Successful, response.Status);
            Assert.Null(response.Exception);
            Assert.NotNull(response.Body);
            Assert.NotNull(response.Body.Info);
            Assert.NotNull(response.Body.CartEntries);
        }
Exemple #13
0
        public void WhenAddingToCartThenCartIsUpdatedInCollection()
        {
            var testContext = new TestContext();

            var sku = testContext.CreateNewSku("A", 100);

            var inMemoryCollection = new CartsCollection();
            var cartEntry          = new CartsCollection.CartsCollectionEntry(
                new CartInfo(), testContext.CartFactory.Create());

            inMemoryCollection.Add(cartEntry);
            Assert.Empty(cartEntry.Cart);

            var controller = new CartController(null, inMemoryCollection,
                                                testContext.CartFactory, testContext.PriceList, new NeutralPipeline());

            var response = controller.Add(cartEntry.Id, sku.Id, null);

            Assert.NotNull(response);
            Assert.Equal(CartOperationStatus.Successful, response.Status);
            Assert.NotNull(response.Body);
            Assert.NotEmpty(cartEntry.Cart);
        }
Exemple #14
0
        public void WhenAddingToCheckedOutCartThenExceptionIsReturnedInMessage()
        {
            var testContext = new TestContext();

            var sku = testContext.CreateNewSku("A", 100);

            var inMemoryCollection = new CartsCollection();
            var cartEntry          = new CartsCollection.CartsCollectionEntry(
                new CartInfo(), testContext.CartFactory.Create());

            cartEntry.Info.CheckedOut = true;
            inMemoryCollection.Add(cartEntry);

            var controller = new CartController(null, inMemoryCollection,
                                                testContext.CartFactory, testContext.PriceList, new NeutralPipeline());

            var response = controller.Add(cartEntry.Id, sku.Id, null);

            Assert.NotNull(response);
            Assert.Equal(CartOperationStatus.Error, response.Status);
            Assert.NotNull(response.Exception);
            Assert.Null(response.Body);
        }