public async Task GetAllEntities_Returns_AllEntities()
        {
            var service = SetupService();
            var encryptionService = new MockEncryptionService();
            encryptionService.DecryptMessageDelegate = buffer => Task.FromResult("1234");
            var target = new CheckoutDataRepository(service, encryptionService);

            var shippingAddresses = target.GetAllShippingAddresses();
            var bilingAddresses = target.GetAllBillingAddresses();
            var paymentMethods = await target.GetAllPaymentMethodsAsync();

            Assert.AreEqual(3, shippingAddresses.Count());
            Assert.AreEqual(2, bilingAddresses.Count());
            Assert.AreEqual(1, paymentMethods.Count());
        }
        public async Task GetEntity_Returns_Entity()
        {
            var service = SetupService();
            var encryptionService = new MockEncryptionService();
            encryptionService.DecryptMessageDelegate = buffer => Task.FromResult("1234");
            var target = new CheckoutDataRepository(service, encryptionService);

            var shippingAddress = target.GetShippingAddress("3");
            var bilingAddress = target.GetBillingAddress("2");
            var paymentMethod = await target.GetPaymentMethodAsync("1");

            Assert.AreEqual(shippingAddress.FirstName, "Anne");
            Assert.AreEqual(bilingAddress.FirstName, "Jane");
            Assert.AreEqual(paymentMethod.CardholderName, "John Doe");
        }
        public async Task GetDefaultValues_Returns_DefaultValues()
        {
            var service = SetupService();
            var encryptionService = new MockEncryptionService();
            encryptionService.DecryptMessageDelegate = buffer => Task.FromResult("1234");
            var target = new CheckoutDataRepository(service, encryptionService);

            var defaultShippingAddress = target.GetDefaultShippingAddress();
            var defaultBilingAddress = target.GetDefaultBillingAddress();
            var defaultPaymentMethod = await target.GetDefaultPaymentMethodAsync();

            Assert.IsNotNull(defaultShippingAddress);
            Assert.AreEqual(defaultShippingAddress.Id, "3");
            Assert.IsNotNull(defaultBilingAddress);
            Assert.AreEqual(defaultBilingAddress.Id, "2");
            Assert.IsNull(defaultPaymentMethod);
        }
        public async Task SaveEntity_SavesEntity()
        {
            var encryptedCardNumber = false;
            var service = SetupService();
            var encryptionService = new MockEncryptionService();
            encryptionService.EncryptMessageDelegate = s =>
                                                           {
                                                               Assert.AreEqual("12345", s);
                                                               encryptedCardNumber = true;
                                                               return Task.FromResult<IBuffer>(null);
                                                           };
            encryptionService.DecryptMessageDelegate = buffer => Task.FromResult("1111");

            var target = new CheckoutDataRepository(service, encryptionService);

            var savedShippingAddress = target.SaveShippingAddress(new Address() { FirstName = "TestFirstName", LastName = "TestLastName" });
            var savedBillingAddress = target.SaveBillingAddress(new Address() { FirstName = "TestFirstName", LastName = "TestLastName" });
            var savedPaymentMethod = await target.SavePaymentMethodAsync(new PaymentMethod() { CardholderName = "TestCardholderName", CardNumber = "12345"});

            Assert.IsNotNull(savedShippingAddress);
            Assert.IsNotNull(savedBillingAddress);
            Assert.IsNotNull(savedPaymentMethod);

            var shippingAddress = target.GetShippingAddress(savedShippingAddress.Id);
            var billingAddress = target.GetBillingAddress(savedBillingAddress.Id);
            var paymentMethod = await target.GetPaymentMethodAsync(savedPaymentMethod.Id);
            
            Assert.AreEqual(savedShippingAddress.Id, shippingAddress.Id);
            Assert.AreEqual(savedBillingAddress.Id, billingAddress.Id);
            Assert.AreEqual(savedPaymentMethod.Id, paymentMethod.Id);

            Assert.IsTrue(encryptedCardNumber);
        }
        public async Task RemoveDefaultEntity_RemovesDefaultEntity()
        {
            var service = SetupService();
            var encryptionService = new MockEncryptionService();
            encryptionService.DecryptMessageDelegate = buffer => Task.FromResult("1234");
            var target = new CheckoutDataRepository(service, encryptionService);
            target.SetDefaultPaymentMethod(await target.GetPaymentMethodAsync("1"));

            var defaultShippingAddress = target.GetDefaultShippingAddress();
            var defaultBillingAddress = target.GetDefaultBillingAddress();
            var defaultPaymentMethod = await target.GetDefaultPaymentMethodAsync();

            Assert.IsNotNull(defaultShippingAddress);
            Assert.IsNotNull(defaultBillingAddress);
            Assert.IsNotNull(defaultPaymentMethod);

            target.RemoveDefaultShippingAddress();
            target.RemoveDefaultBillingAddress();
            target.RemoveDefaultPaymentMethod();

            defaultShippingAddress = target.GetDefaultShippingAddress();
            defaultBillingAddress = target.GetDefaultBillingAddress();
            defaultPaymentMethod = await target.GetDefaultPaymentMethodAsync();

            Assert.IsNull(defaultShippingAddress);
            Assert.IsNull(defaultBillingAddress);
            Assert.IsNull(defaultPaymentMethod);
        }
        private void BootstrapApplication(INavigationService navService)
        {
            // Initialize service and repositories
            var checkoutDataRepository = new CheckoutDataRepository(new SettingsStoreService());
            var orderServiceProxy = new OrderServiceProxy();
            var shippingMethodServiceProxy = new ShippingMethodServiceProxy();

            // Set up the list of known types for the SuspensionManager
            SuspensionManager.KnownTypes.Add(typeof(Address));
            SuspensionManager.KnownTypes.Add(typeof(PaymentMethod));
            SuspensionManager.KnownTypes.Add(typeof(UserInfo));
            SuspensionManager.KnownTypes.Add(typeof(ReadOnlyDictionary<string, ReadOnlyCollection<string>>));
           

            // Create Repositories
            // <snippet510>
            // </snippet510>

            // Update resolver because the ViewModels are located in a separate assembly than the Views. The ViewModel Types are in the Kona.UILogic assembly.
            // <snippet301>
            ViewModelLocator.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
                    {
                        var viewName = viewType.Name;
                        var viewModelName = String.Format(CultureInfo.InvariantCulture, "Kona.UILogic.ViewModels.{0}ViewModel, Kona.UILogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", viewName);
                        return Type.GetType(viewModelName);
                    });
            // </snippet301>

            // <snippet302>
            ViewModelLocator.Register(typeof(HubPage), () => new HubPageViewModel(_productCatalogRepository, navService, new AlertMessageService(), _resourceLoader));
            ViewModelLocator.Register(typeof(GroupDetailPage), () => new GroupDetailPageViewModel(_productCatalogRepository, navService));
            ViewModelLocator.Register(typeof(ItemDetailPage), () => new ItemDetailPageViewModel(_productCatalogRepository, navService, _shoppingCartRepository));
            ViewModelLocator.Register(typeof(SignInFlyout), () => new SignInFlyoutViewModel(_accountService, _credentialStore));
            ViewModelLocator.Register(typeof(SignOutFlyout), () => new SignOutFlyoutViewModel(_accountService, _credentialStore, navService));
            ViewModelLocator.Register(typeof(ShoppingCartPage), () => new ShoppingCartPageViewModel(_shoppingCartRepository, navService, _accountService, _settingsCharmService, _eventAggregator));
            ViewModelLocator.Register(typeof(CheckoutSummaryPage), () => new CheckoutSummaryPageViewModel(navService, orderServiceProxy, shippingMethodServiceProxy, checkoutDataRepository, _shoppingCartRepository, _accountService, CreateSettingsCharmService(), _resourceLoader));
            ViewModelLocator.Register(typeof(CheckoutHubPage), () => new CheckoutHubPageViewModel(navService, _accountService, orderServiceProxy, shippingMethodServiceProxy, _shoppingCartRepository,
                                                                                                              new ShippingAddressUserControlViewModel(checkoutDataRepository, new LocationServiceProxy(), _resourceLoader),
                                                                                                              new BillingAddressUserControlViewModel(checkoutDataRepository, new LocationServiceProxy(), _resourceLoader), 
                                                                                                              new PaymentMethodUserControlViewModel(checkoutDataRepository), _settingsCharmService));
            ViewModelLocator.Register(typeof(ShippingAddressFlyout), () => new ShippingAddressFlyoutViewModel(new ShippingAddressUserControlViewModel(checkoutDataRepository, new LocationServiceProxy(), _resourceLoader), _resourceLoader));
            ViewModelLocator.Register(typeof(BillingAddressFlyout), () => new BillingAddressFlyoutViewModel(new BillingAddressUserControlViewModel(checkoutDataRepository, new LocationServiceProxy(), _resourceLoader), _resourceLoader));
            ViewModelLocator.Register(typeof(PaymentMethodFlyout), () => new PaymentMethodFlyoutViewModel(new PaymentMethodUserControlViewModel(checkoutDataRepository), _resourceLoader));
            ViewModelLocator.Register(typeof(ShoppingCartTabUserControl), () => new ShoppingCartTabUserControlViewModel(_shoppingCartRepository, _eventAggregator, _navigationService, new AlertMessageService(), _resourceLoader, _accountService));
            ViewModelLocator.Register(typeof(TopAppBarUserControl), () => new TopAppBarUserControlViewModel(_navigationService));
            ViewModelLocator.Register(typeof(ChangeDefaultsFlyout), () => new ChangeDefaultsFlyoutViewModel(checkoutDataRepository, _resourceLoader));
            //</snippet302>
        }