public override List <Product> Sort(List <Product> products)
        {
            var productNames = products.Select(p => p.Name).ToHashSet();

            var shopperHistory      = _apiResourceProxy.GetShopperHistory().GetAwaiter().GetResult();
            var popularProducts     = ProductsByPopularity(shopperHistory);
            var popularProductNames = popularProducts.Select(p => p.Name).ToHashSet();

            var sortedProducts = new List <Product>();

            // populate sortedProducts based on the popular products
            foreach (var popularProduct in popularProducts)
            {
                if (!productNames.Contains(popularProduct.Name))
                {
                    continue;
                }

                popularProduct.Quantity = 0;
                sortedProducts.Add(popularProduct);
            }

            // populate sortedProducts based on the products that are not in popular products list
            var productsNotInPopularProductSet = products.Where(p => !popularProductNames.Contains(p.Name)).ToList();

            foreach (var notPopularProduct in productsNotInPopularProductSet)
            {
                sortedProducts.Add(notPopularProduct);
            }

            return(sortedProducts);
        }
        public void apiresourceproxy_should_throw_exception_if_get_shopper_history_error_response(HttpStatusCode errorStatusCode)
        {
            // Assert
            _sut = GetClient <IApiResourceProxy>("http://apihost.com/api/resource/", errorStatusCode);

            // Act
            Func <Task> action = async() => await _sut.GetShopperHistory();

            // Assert
            action.Should().Throw <HttpRequestException>();

            if (errorStatusCode != HttpStatusCode.Unauthorized && errorStatusCode != HttpStatusCode.BadRequest)
            {
                action = async() => await _sut.GetShopperHistory();

                action.Should().Throw <BrokenCircuitException>();
            }
        }
        public async void get_shopper_history_should_call_the_correct_endpoint()
        {
            // Act
            await _sut.GetShopperHistory();

            // Assert
            MockHttpMessageHandler.Request.RequestUri.PathAndQuery.Should()
            .Be($"/api/resource/shopperhistory?token={Token}");
        }
        public void sort_product_should_return_products_sorted_by_product_popularity()
        {
            // Arrange
            _apiResourceProxy.GetShopperHistory().Returns(Mocks.MockData.GetShopperHistory());

            // Act
            var result = _sut.Sort(Mocks.MockData.GetProducts());

            // Assert
            result.Should().BeEquivalentTo(Mocks.MockData.GetProductSorted(SortOption.Recommended));
        }
Example #5
0
        public async Task product_service_sort_should_return_correct_products(SortOption sortOption)
        {
            // Arrange
            _apiResourceProxy.GetProducts().Returns(Mocks.MockData.GetProducts());
            _apiResourceProxy.GetShopperHistory().Returns(Mocks.MockData.GetShopperHistory());

            // Act
            var result = await _sut.Sort(sortOption);

            // Assert
            result.Should().BeEquivalentTo(Mocks.MockData.GetProductSorted(sortOption));

            if (sortOption != SortOption.Recommended)
            {
                // Shopper history only called for recommended
                _apiResourceProxy.Received(0).GetShopperHistory();
            }
            else
            {
                _apiResourceProxy.Received(1).GetShopperHistory();
            }
        }