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

            ShoppingCartRepository repository = new ShoppingCartRepository();

            var newDeveloper = new ShoppingCartDeveloper();

            newDeveloper.Price = default(double);
            newDeveloper.Username = String.Empty;
            newDeveloper.Hours = default(int);

               repository.AddDeveloperToShoppingCart(newDeveloper);
        }
Пример #2
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;
            }
        }
        public ShoppingCartDeveloper GeDeveloperDetails(string username)
        {
            try
            {
                var developerOnGit = GitUserAPI.GetUserDetails(username);

                var shoppingCartDeveloper = new ShoppingCartDeveloper(){
                    Username = developerOnGit.login,
                    Price = DefineDeveloperPrice(developerOnGit)
                };

                return shoppingCartDeveloper;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public void AddDeveloperToShoppingCart()
        {
            HttpContext.Current = new HttpContext(new HttpRequest(null, "http://localhost", null), new HttpResponse(null));

            var newDeveloper = new ShoppingCartDeveloper()
            {
                Username="******",
                Price = 200,
                Hours = 40
            };

            ShoppingCartRepository repository = new ShoppingCartRepository();

            bool devloperAdded = repository.AddDeveloperToShoppingCart(newDeveloper);

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

            Assert.IsTrue(devloperAdded);
            Assert.AreEqual(newDeveloper.Price,contextModified.ShoppingCartDevelopers.Find(x=>x.Username.Equals(newDeveloper.Username)).Price);
        }
        public void AddDeveloperToShoppingCartErrorByDuplicatingDevelopers()
        {
            HttpContext.Current = new HttpContext(new HttpRequest(null, "http://localhost", null), new HttpResponse(null));

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

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

            ShoppingCartRepository repository = new ShoppingCartRepository();

            bool firsDevloperAdded = repository.AddDeveloperToShoppingCart(newDeveloper);
            bool secondDeveloperAdded = repository.AddDeveloperToShoppingCart(duplicatedDeveloper);

            Assert.IsTrue(firsDevloperAdded);
            Assert.IsFalse(secondDeveloperAdded);
        }
Пример #6
0
        public HttpResponseMessage Post(ShoppingCartDeveloper developer)
        {
            try
            {
                ShoppingCartRepository.AddDeveloperToShoppingCart(developer);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(System.Net.HttpStatusCode.InternalServerError, ex.Message);
            }

            return Request.CreateResponse<ShoppingCartDeveloper>(System.Net.HttpStatusCode.Created, developer);
        }
        public void PostWithErrorResponseTest()
        {
            var developer = new ShoppingCartDeveloper();

            var shoppingCartRepository = new Mock<IShoppingCartRepository>();
            shoppingCartRepository.Setup(s => s.AddDeveloperToShoppingCart(developer)).Throws(new DuplicateWaitObjectException());

            ShoppingCartController controller = CreateController(shoppingCartRepository);

            var result = controller.Post(developer);

            Assert.AreEqual(System.Net.HttpStatusCode.InternalServerError, result.StatusCode);
            Assert.IsNotNull(result.Content);
        }
        public void PostWithSuccessResponseTest()
        {
            var developer = new ShoppingCartDeveloper()
            {
                Username= "******",
                Price = 200
            };

            var shoppingCartRepository = new Mock<IShoppingCartRepository>();
            shoppingCartRepository.Setup(s => s.AddDeveloperToShoppingCart(developer)).Returns(true);

            ShoppingCartController controller = CreateController(shoppingCartRepository);

            var result = controller.Post(developer);

            Assert.AreEqual(System.Net.HttpStatusCode.Created, result.StatusCode);
            Assert.IsNotNull(result.Content);
        }
        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);
        }