コード例 #1
0
        public bool AddDeveloperToShoppingCart(ShoppingCartDeveloper newDeveloper)
        {
            try
            {
                if (newDeveloper == default(ShoppingCartDeveloper) ||
                    String.IsNullOrEmpty(newDeveloper.Username) ||
                    newDeveloper.Price ==default(double))
                {
                    throw new ArgumentNullException();
                }

                ShoppingCart shoppingCart = (ShoppingCart)Context.Cache[CacheKey];

                if (shoppingCart != null)
                {
                    if (shoppingCart.ShoppingCartDevelopers.FirstOrDefault(x => x.Username.Equals(newDeveloper.Username))
                                                                == default(ShoppingCartDeveloper))
                    {
                        shoppingCart.ShoppingCartDevelopers.Add(newDeveloper);
                    }
                    else
                    {
                        throw new DuplicateWaitObjectException();
                    }
                }
                else
                {
                    shoppingCart = new ShoppingCart()
                    {
                        ShoppingCartDevelopers = new List<ShoppingCartDeveloper>()
                    };

                    shoppingCart.ShoppingCartDevelopers.Add(newDeveloper);
                }

                Context.Cache[CacheKey] = shoppingCart;
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #2
0
        public void CleanShoppingCartWithSuccess()
        {
            var shopppingCart = new ShoppingCart() { ShoppingCartDevelopers = new List<ShoppingCartDeveloper>() };

            shopppingCart.ShoppingCartDevelopers.Add(new ShoppingCartDeveloper()
            {
                Username = "******",
                Price = 100
            });
            shopppingCart.ShoppingCartDevelopers.Add(new ShoppingCartDeveloper()
            {
                Username = "******",
                Price = 300
            });

            HttpContext.Current = new HttpContext(new HttpRequest(null, "http://localhost", null), new HttpResponse(null));

            HttpContext.Current.Cache[CacheKey] = shopppingCart;

            ShoppingCartRepository repository = new ShoppingCartRepository();

            bool cartCleaned= repository.CleanShoppingCart();

            var contextModified = (ShoppingCart)HttpContext.Current.Cache[CacheKey];

            Assert.IsTrue(cartCleaned);
            Assert.AreEqual(0, contextModified.ShoppingCartDevelopers.Count);
        }
コード例 #3
0
        public void RemoveDeveloperFromShoppingCartErrorByRemovingNonExistentDeveloper()
        {
            //ARRANGE
            var developerUsernameToRemove = "wanessa";

            var fakeDeveloper = new ShoppingCartDeveloper()
            {
                Username = "******",
                Price = 200
            };

            var shopppingCart = new ShoppingCart() { ShoppingCartDevelopers = new List<ShoppingCartDeveloper>() };
            shopppingCart.ShoppingCartDevelopers.Add(fakeDeveloper);

            HttpContext.Current = new HttpContext(new HttpRequest(null, "http://localhost", null), new HttpResponse(null));

            HttpContext.Current.Cache[CacheKey] = shopppingCart;

            ShoppingCartRepository repository = new ShoppingCartRepository();

            //ACT
            bool developerRemoved = repository.RemoveDeveloperFromShoppingCart(developerUsernameToRemove);

            //ASSERT
            Assert.IsFalse(developerRemoved);
            var contextModified = (ShoppingCart)HttpContext.Current.Cache[CacheKey];
            Assert.AreEqual(1, contextModified.ShoppingCartDevelopers.Count);
        }
コード例 #4
0
        public void CleanEmptyShoppingCartWithoutError()
        {
            var shopppingCart = new ShoppingCart() { ShoppingCartDevelopers = new List<ShoppingCartDeveloper>() };

            HttpContext.Current = new HttpContext(new HttpRequest(null, "http://localhost", null), new HttpResponse(null));

            HttpContext.Current.Cache[CacheKey] = shopppingCart;

            ShoppingCartRepository repository = new ShoppingCartRepository();

            bool cartCleaned = repository.CleanShoppingCart();

            var contextModified = (ShoppingCart)HttpContext.Current.Cache[CacheKey];

            Assert.IsTrue(cartCleaned);
            Assert.AreEqual(0, contextModified.ShoppingCartDevelopers.Count);
        }