public void VMListensToShoppingCartUpdatedEvent_ThenCalculatesTotalNumberOfItemsInCart()
        {
            var shoppingCart = new ShoppingCart(new List<ShoppingCartItem>());
            var shoppingCartRepository = new MockShoppingCartRepository();

            shoppingCartRepository.GetShoppingCartAsyncDelegate = () => Task.FromResult(shoppingCart);
            var shoppingCartUpdatedEvent = new ShoppingCartUpdatedEvent();
            var eventAggregator = new MockEventAggregator();
            eventAggregator.GetEventDelegate = type =>
            {
                if (type == typeof(ShoppingCartUpdatedEvent)) return shoppingCartUpdatedEvent;
                if (type == typeof(ShoppingCartItemUpdatedEvent)) return new ShoppingCartItemUpdatedEvent();
                return null;
            };
            var accountService = new MockAccountService();
            accountService.VerifyUserAuthenticationAsyncDelegate = () => Task.FromResult((UserInfo)null);
            var target = new ShoppingCartTabUserControlViewModel(shoppingCartRepository, eventAggregator, null, null, accountService);

            shoppingCart = new ShoppingCart(new List<ShoppingCartItem>()
                                               {
                                                   new ShoppingCartItem {Quantity = 1},
                                                   new ShoppingCartItem {Quantity = 2}
                                               });

            Assert.AreEqual(0, target.ItemCount);

            shoppingCartUpdatedEvent.Publish(null);

            Assert.AreEqual(3, target.ItemCount);

        }
        public void ShoppingCartUpdated_WithNullCart_SetsItemCountZero()
        {
            var shoppingCartRepository = new MockShoppingCartRepository();
            var shoppingCart = new ShoppingCart(new List<ShoppingCartItem> {new ShoppingCartItem {Quantity = 99}});
            shoppingCartRepository.GetShoppingCartAsyncDelegate = () => Task.FromResult(shoppingCart);
            var eventAggregator = new MockEventAggregator();
            var shoppingCartUpdatedEvent = new ShoppingCartUpdatedEvent();
            eventAggregator.GetEventDelegate = type =>
            {
                if (type == typeof(ShoppingCartUpdatedEvent)) return shoppingCartUpdatedEvent;
                if (type == typeof(ShoppingCartItemUpdatedEvent)) return new ShoppingCartItemUpdatedEvent();
                return null;
            };
            var accountService = new MockAccountService();
            accountService.VerifyUserAuthenticationAsyncDelegate = () => Task.FromResult((UserInfo)null);
            var target = new ShoppingCartTabUserControlViewModel(shoppingCartRepository, eventAggregator, null, null, accountService);

            shoppingCartUpdatedEvent.Publish(null);

            Assert.AreEqual(99, target.ItemCount);

            shoppingCartRepository.GetShoppingCartAsyncDelegate = () => Task.FromResult<ShoppingCart>(null);
            shoppingCartUpdatedEvent.Publish(null);

            Assert.AreEqual(0, target.ItemCount);
        }
        public void OnNavigatedTo_Fill_Properties_With_Shopping_Cart_Items()
        {
            var navigationService = new MockNavigationService();
            var shoppingCartRepository = new MockShoppingCartRepository();
            shoppingCartRepository.GetShoppingCartAsyncDelegate = () =>
            {
                ShoppingCart shoppingCart = null;
                var shoppingCartItems = new ObservableCollection<ShoppingCartItem>
                                            {
                                                new ShoppingCartItem() {Product = new Product { ListPrice = 100, DiscountPercentage = 50, ProductNumber = "p1", ImageUri = new Uri("http://image")}, Currency = "USD", Quantity = 1}, 
                                                new ShoppingCartItem() {Product = new Product { ListPrice = 100, DiscountPercentage = 50, ProductNumber = "p2", ImageUri = new Uri("http://image")}, Currency = "USD", Quantity = 1}
                                            };
                shoppingCart = new ShoppingCart(shoppingCartItems) { Currency = "USD"};

                return Task.FromResult(shoppingCart);
            };

            bool signInUserControlOnNavigatedToCalled = false;
            var signInUserControlViewModel = new MockSignInUserControlViewModel()
                {
                    OnNavigatedToDelegate = (a, b, c) => signInUserControlOnNavigatedToCalled = true
                };

            var eventAggregator = new MockEventAggregator();
            eventAggregator.GetEventDelegate = type => new MockShoppingCartUpdatedEvent();
            var target = new ShoppingCartPageViewModel(shoppingCartRepository, navigationService, new MockAccountService(), signInUserControlViewModel, null, null, null, null, eventAggregator);
            target.OnNavigatedTo(new NavigatedToEventArgs { Parameter = null, NavigationMode = NavigationMode.New }, null);

            Assert.IsTrue(signInUserControlOnNavigatedToCalled);
            Assert.AreEqual("$200.00", target.FullPrice);
            Assert.AreEqual("$100.00", target.TotalDiscount);
            Assert.AreEqual(2, target.ShoppingCartItemViewModels.Count);
        }
        public async Task<Order> CreateOrderAsync(string userId, ShoppingCart shoppingCart, Address shippingAddress, Address billingAddress, PaymentMethod paymentMethod, ShippingMethod shippingMethod)
        {
            Order order = new Order
                {
                    UserId = userId,
                    ShoppingCart = shoppingCart,
                    ShippingAddress = shippingAddress,
                    BillingAddress = billingAddress,
                    PaymentMethod = paymentMethod,
                    ShippingMethod = shippingMethod
                };

            order.Id = await _orderService.CreateOrderAsync(order);

            return order;
        }
 public async Task CreateBasicOrderAsync(string userId, ShoppingCart shoppingCart, Address shippingAddress, Address billingAddress, PaymentMethod paymentMethod)
 {
     var basicShippingMethod = await _shippingMethodService.GetBasicShippingMethodAsync();
     _currentOrder = await CreateOrderAsync(userId, shoppingCart, shippingAddress, billingAddress, paymentMethod, basicShippingMethod);
     _sessionStateService.SessionState[OrderKey] = _currentOrder;
 }
 public Task<Order> CreateOrderAsync(string userId, ShoppingCart shoppingCart, Address shippingAddress, Address billingAddress, PaymentMethod paymentMethod, ShippingMethod shippingMethod)
 {
     return CreateOrderAsyncDelegate(userId, shoppingCart, shippingAddress, billingAddress, paymentMethod, shippingMethod);
 }
        public async Task GetShoppingCartAsync_CachesCart()
        {

            var shoppingCart = new ShoppingCart(new Collection<ShoppingCartItem>());
            var shoppingCartService = new MockShoppingCartService()
                {
                    GetShoppingCartAsyncDelegate = s => Task.FromResult(shoppingCart)
                };

            var target = new ShoppingCartRepository(shoppingCartService, null, null, new MockSessionStateService());
            var firstCartReturned = await target.GetShoppingCartAsync();
            shoppingCartService.GetShoppingCartAsyncDelegate = s =>
            {
                Assert.Fail("Should not have called proxy second time.");
                return Task.FromResult((ShoppingCart)null);
            };

            var secondCartReturned = await target.GetShoppingCartAsync();
            Assert.AreSame(shoppingCart, firstCartReturned);
            Assert.AreSame(shoppingCart, secondCartReturned);
        }
        public void ShoppingCartUpdated_WhenShoppingCartChanged()
        {
            var navigationService = new MockNavigationService();
            var accountService = new MockAccountService();
            var shoppingCartRepository = new MockShoppingCartRepository();
            shoppingCartRepository.GetShoppingCartAsyncDelegate = () =>
            {
                ShoppingCart shoppingCart = new ShoppingCart(new ObservableCollection<ShoppingCartItem>()) { Currency = "USD"};
                return Task.FromResult(shoppingCart);
            };
            bool signInUserControlOnNavigatedToCalled = false;
            var signInUserControlViewModel = new MockSignInUserControlViewModel()
            {
                OnNavigatedToDelegate = (a, b, c) => signInUserControlOnNavigatedToCalled = true
            };
            var eventAggregator = new MockEventAggregator();
            var shoppingCartUpdatedEvent = new ShoppingCartUpdatedEvent();
            eventAggregator.GetEventDelegate = type => shoppingCartUpdatedEvent;
            var target = new ShoppingCartPageViewModel(shoppingCartRepository, navigationService, accountService, signInUserControlViewModel, null, null, null, null, eventAggregator);
            target.OnNavigatedTo(new NavigatedToEventArgs { Parameter = null, NavigationMode = NavigationMode.New }, null);  

            Assert.AreEqual("$0.00", target.FullPrice);

            shoppingCartRepository.GetShoppingCartAsyncDelegate = () =>
            {
                var shoppingCartItems = new ObservableCollection<ShoppingCartItem>
                                            {
                                                new ShoppingCartItem() { Product = new Product { ListPrice = 100, ProductNumber = "p1", ImageUri = new Uri("http://image")}, Currency = "USD", Quantity = 2}, 
                                            };
                ShoppingCart shoppingCart = new ShoppingCart(new ObservableCollection<ShoppingCartItem>(shoppingCartItems)) { Currency = "USD" };
                return Task.FromResult(shoppingCart);
            };

            shoppingCartUpdatedEvent.Publish(null);

            Assert.IsTrue(signInUserControlOnNavigatedToCalled);
            Assert.AreEqual("$200.00", target.FullPrice);
        }
        private async Task UpdateShoppingCartInfoAsync()
        {
            string errorMessage = string.Empty;

            try
            {
                _shoppingCart = await _shoppingCartRepository.GetShoppingCartAsync();

                if (_shoppingCart != null && _shoppingCart.ShoppingCartItems != null)
                {
                    ShoppingCartItemViewModels = new ObservableCollection<ShoppingCartItemViewModel>();
                    foreach (var item in _shoppingCart.ShoppingCartItems)
                    {
                        var shoppingCartItemViewModel = new ShoppingCartItemViewModel(item, _resourceLoader);
                        shoppingCartItemViewModel.PropertyChanged += ShoppingCartItemViewModel_PropertyChanged;
                        ShoppingCartItemViewModels.Add(shoppingCartItemViewModel);
                    }

                    CheckoutCommand.RaiseCanExecuteChanged();
                    OnPropertyChanged("FullPrice");
                    OnPropertyChanged("TotalDiscount");
                    OnPropertyChanged("TotalPrice");
                }
            }
            catch (Exception ex)
            {
                errorMessage = string.Format(CultureInfo.CurrentCulture, _resourceLoader.GetString("GeneralServiceErrorMessage"), Environment.NewLine, ex.Message);
            }

            if (!string.IsNullOrWhiteSpace(errorMessage))
            {
                await _alertMessageService.ShowAsync(errorMessage, _resourceLoader.GetString("ErrorServiceUnreachable"));
            }
        }