public async void ShouldNotFailIfBadCustomerId()
        {
            //Get Cart: http://localhost:7940/api/shoppingcart/{customerId}
            CartWithCustomerInfo cartWithCustomerInfo = await ShoppingCartTestHelpers.GetCart(100, ServiceAddress, RootAddress);

            Assert.Empty(cartWithCustomerInfo.CartRecords);
        }
        public async void ShouldUpdateCartRecordOnAddIfAlreadyExists()
        {
            // Add to Cart: http://localhost:55882/api/shoppingcartrecord/{customerId} HTTPPost
            // http://localhost:55882/api/shoppingcartrecord/1 {"ProductId":22,"Quantity":2,"CustomerId":1}
            // Content - Type:application / json
            using (var client = new HttpClient())
            {
                var productId = _productId;
                var quantity  = 3;
                var targetUrl = $"{ServiceAddress}{RootAddress}/{_customerId}";
                var response  = await client.PostAsync(targetUrl,
                                                       new StringContent("{" + $"\"ProductId\":{productId},\"Quantity\":{quantity},\"CustomerId\":{_customerId}" + "}",
                                                                         Encoding.UTF8, "application/json"));

                Assert.True(response.IsSuccessStatusCode, response.ReasonPhrase);
                Assert.Equal($"{ServiceAddress}api/shoppingcart/{_customerId}".ToUpper(),
                             response.Headers.Location.AbsoluteUri.ToUpper());
            }
            // validate the cart was added
            CartWithCustomerInfo cartWithCustomerInfo = await ShoppingCartTestHelpers.GetCart(_customerId, ServiceAddress, "api/shoppingcart");

            Assert.Single(cartWithCustomerInfo.CartRecords);
            var cartRecord = cartWithCustomerInfo.CartRecords[cartWithCustomerInfo.CartRecords.Count - 1];

            Assert.Equal(_productId, cartRecord.ProductId);
            Assert.Equal("Travel", cartRecord.CategoryName);
            Assert.Equal(4, cartRecord.Quantity);
        }
        public async void ShouldReturnCustomersCart()
        {
            //Get Cart: http://localhost:7940/api/shoppingcart/{customerId}
            CartWithCustomerInfo cartWithCustomerInfo = await ShoppingCartTestHelpers.GetCart(_customerId, ServiceAddress, RootAddress);

            Assert.Single(cartWithCustomerInfo.CartRecords);
            Assert.Equal(_productId, cartWithCustomerInfo.CartRecords[0].ProductId);
            Assert.Equal("Travel", cartWithCustomerInfo.CartRecords[0].CategoryName);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Index()
        {
            ViewBag.Title  = "Cart";
            ViewBag.Header = "Cart";
            CartWithCustomerInfo cartWithCustomerInfo = await _serviceWrapper.GetCartAsync(ViewBag.CustomerId);

            var mapper    = _config.CreateMapper();
            var viewModel = new CartViewModel
            {
                Customer    = cartWithCustomerInfo.Customer,
                CartRecords = mapper.Map <IList <CartRecordViewModel> >(cartWithCustomerInfo.CartRecords)
            };

            return(View(viewModel));
        }
Ejemplo n.º 5
0
        public async void ShouldDeleteRecordInTheCart()
        {
            // Remove Cart Item: http://localhost:7940/api/shoppingcart/{id} HTTPDelete
            // http://localhost:7940/api/shoppingcart/1
            var cartRecord = await ShoppingCartTestHelpers.GetCartItem(_cartRecordId, ServiceAddress, RootAddress);

            //throw new Exception($"{cartRecord.Quantity}");
            using (var client = new HttpClient())
            {
                var targetUrl = $"{ServiceAddress}{RootAddress}/{cartRecord.Id}";
                //throw new Exception(targetUrl);

                var response = await client.DeleteAsJsonAsync(targetUrl, cartRecord);

                Assert.True(response.IsSuccessStatusCode, response.ReasonPhrase);
            }
            // validate the cart item was updated
            CartWithCustomerInfo cartWithCustomerInfo = await ShoppingCartTestHelpers.GetCart(_customerId, ServiceAddress, "api/shoppingcart");

            Assert.Empty(cartWithCustomerInfo.CartRecords);
        }
        public async void ShouldRemoveRecordOnAddIfQuantityBecomesZero()
        {
            // Add to Cart: http://localhost:7940/api/shoppingcartrecord/{customerId} HTTPPost
            // http://localhost:7940/api/shoppingcartrecord/1 {"ProductId":22,"Quantity":2,"CustomerId":1}
            // Content - Type:application / json
            using (var client = new HttpClient())
            {
                var productId = _productId;
                var quantity  = -1;
                var targetUrl = $"{ServiceAddress}{RootAddress}/{_customerId}";
                var response  = await client.PostAsync(targetUrl,
                                                       new StringContent("{" + $"\"ProductId\":{productId},\"Quantity\":{quantity},\"CustomerId\":{_customerId}" + "}",
                                                                         Encoding.UTF8, "application/json"));

                Assert.True(response.IsSuccessStatusCode, response.ReasonPhrase);
                Assert.Equal($"{ServiceAddress}api/shoppingcart/{_customerId}".ToUpper(),
                             response.Headers.Location.AbsoluteUri.ToUpper());
            }
            // validate the cart was added
            CartWithCustomerInfo cartWithCustomerInfo = await ShoppingCartTestHelpers.GetCart(_customerId, ServiceAddress, "api/shoppingcart");

            Assert.Empty(cartWithCustomerInfo.CartRecords);
        }
        public async void ShouldAddRecordToTheCart()
        {
            // Add to Cart: http://localhost:55882/api/shoppingcartrecord/{customerId} HTTPPost
            // Note: ProductId and Quantity in the body
            // http://localhost:55882/api/shoppingcartrecord/1 {"ProductId":22,"Quantity":2,"CustomerId":1}
            // Content - Type:application / json
            using (var client = new HttpClient())
            {
                var productId = 2;
                var quantity  = 1;
                //var request = new HttpRequestMessage(
                //    HttpMethod.Post,
                //    $"{_serviceAddress}{_rootAddress}{_customerId}")
                //{
                //    Content = new StringContent("{" + $"\"ProductId\":{productId},\"Quantity\":{quantity}" + "}", Encoding.UTF8, "application/json")
                //};
                //var response = await client.SendAsync(request);
                var targetUrl = $"{ServiceAddress}{RootAddress}/{_customerId}";
                var response  = await client.PostAsync(targetUrl,
                                                       new StringContent("{" + $"\"ProductId\":{productId},\"Quantity\":{quantity},\"CustomerId\":{_customerId}" + "}",
                                                                         Encoding.UTF8, "application/json"));

                Assert.True(response.IsSuccessStatusCode, response.ReasonPhrase);
                Assert.Equal($"{ServiceAddress}api/shoppingcart/{_customerId}".ToUpper(),
                             response.Headers.Location.AbsoluteUri.ToUpper());
            }
            // validate the cart was added
            CartWithCustomerInfo cartWithCustomerInfo = await ShoppingCartTestHelpers.GetCart(_customerId, ServiceAddress, "api/shoppingcart");

            Assert.Equal(2, cartWithCustomerInfo.CartRecords.Count);
            var cartRecord = cartWithCustomerInfo.CartRecords[cartWithCustomerInfo.CartRecords.Count - 1];

            Assert.Equal(2, cartRecord.Id);
            Assert.Equal("Munitions", cartRecord.CategoryName);
            Assert.Equal(1, cartRecord.Quantity);
        }