public void GetGetTop5ProductsSold_Given_Null_Should_ThrowArgumentNullException()
        {
            //Arrange
            CollectionOfMerchantOrderResponse sut = null;

            //Act & Assert
            sut.Invoking(s => s.GetTop5ProductsSold()).Should().Throw <ArgumentNullException>();
        }
        public Task <List <ProductDto> > GetTop5ProductsSoldFromOrders(CollectionOfMerchantOrderResponse collectionOfMerchantOrderResponse)
        {
            if (collectionOfMerchantOrderResponse is null)
            {
                throw new ArgumentNullException(nameof(collectionOfMerchantOrderResponse));
            }

            return(GetTop5ProductsSoldFromOrdersInternal(collectionOfMerchantOrderResponse)); //https://rules.sonarsource.com/csharp/RSPEC-4457
        }
Exemplo n.º 3
0
        public async Task GetTop5ProductsSoldFromOrders_Should_ReturnListOfProductDtos()
        {
            var expected = new ProductDtosBuilder()
                           .WithProductDtoIs("ProductNo1", 200, "Name1", "EAN1", 300)
                           .WithProductDtoIs("ProductNo2", 150, "Name2", "EAN2", 200)
                           .WithProductDtoIs("ProductNo3", 50, "Name3", "EAN3", 50)
                           .Build();

            var lines1 = new MerchantOrderLineResponsesBuilder()
                         .WithMerchantOrderLineResponseIs("ProductNo3", 50)
                         .WithMerchantOrderLineResponseIs("ProductNo2", 100)
                         .WithMerchantOrderLineResponseIs("ProductNo1", 50)
                         .Build();

            var lines2 = new MerchantOrderLineResponsesBuilder()
                         .WithMerchantOrderLineResponseIs("ProductNo1", 50)
                         .WithMerchantOrderLineResponseIs("ProductNo2", 50)
                         .WithMerchantOrderLineResponseIs("ProductNo1", 100)
                         .Build();

            var merchantOrderResponses = new MerchantOrderResponsesBuilder()
                                         .WithmerchantOrderResponseIs(lines1)
                                         .WithmerchantOrderResponseIs(lines2)
                                         .Build();

            var collectionMerchantOrderResponse = new CollectionOfMerchantOrderResponse()
            {
                Content = merchantOrderResponses
            };

            var productRespones = new MerchantProductResponsesBuilder()
                                  .WithMerchantProductResponseIs("ProductNo3", "Name3", "EAN3", 50)
                                  .WithMerchantProductResponseIs("ProductNo2", "Name2", "EAN2", 200)
                                  .WithMerchantProductResponseIs("ProductNo1", "Name1", "EAN1", 300)
                                  .Build();

            var collectionMerchantProductResponse = new CollectionOfMerchantProductResponse()
            {
                Content = productRespones
            };

            var mockedchannelEngineHttpClient = new Mock <IChannelEngineHttpClient>();

            mockedchannelEngineHttpClient.Setup(c => c.GetProducts(It.IsAny <List <string> >())).ReturnsAsync(collectionMerchantProductResponse);

            var sut = new ProductsFromOrdersHandler(mockedchannelEngineHttpClient.Object);

            //Act
            var result = await sut.GetTop5ProductsSoldFromOrders(collectionMerchantOrderResponse);

            //Assert
            result.Should().BeEquivalentTo(expected, options => options.WithStrictOrdering());
        }
Exemplo n.º 4
0
        public static List <ProductDto> GetTop5ProductsSold(this CollectionOfMerchantOrderResponse collectionOfMerchantOrderResponse)
        {
            if (collectionOfMerchantOrderResponse?.Content is null)
            {
                throw new ArgumentNullException(nameof(collectionOfMerchantOrderResponse));
            }

            var lines = collectionOfMerchantOrderResponse.Content.SelectMany(c => c.Lines);

            return(lines
                   .GroupBy(
                       l => l.MerchantProductNo,
                       (key, l) => new ProductDto {
                MerchantProductNo = key,
                TotalQuantity = l.Select(l => l.Quantity).Aggregate((a, b) => a + b)
            })
                   .OrderByDescending(p => p.TotalQuantity)
                   .Take(5)
                   .ToList());
        }
        public void GetGetTop5ProductsSold_Should_ReturnTop5ProductsSold()
        {
            //Arrange
            var expected = new ProductDtosBuilder()
                           .WithProductDtoIs("ProductNo1", 200)
                           .WithProductDtoIs("ProductNo2", 150)
                           .WithProductDtoIs("ProductNo3", 50)

                           .Build();

            var lines1 = new MerchantOrderLineResponsesBuilder()
                         .WithMerchantOrderLineResponseIs("ProductNo3", 50)
                         .WithMerchantOrderLineResponseIs("ProductNo2", 100)
                         .WithMerchantOrderLineResponseIs("ProductNo1", 50)
                         .Build();

            var lines2 = new MerchantOrderLineResponsesBuilder()
                         .WithMerchantOrderLineResponseIs("ProductNo1", 50)
                         .WithMerchantOrderLineResponseIs("ProductNo2", 50)
                         .WithMerchantOrderLineResponseIs("ProductNo1", 100)
                         .Build();

            var merchantOrderResponses = new MerchantOrderResponsesBuilder()
                                         .WithmerchantOrderResponseIs(lines1)
                                         .WithmerchantOrderResponseIs(lines2)
                                         .Build();

            var sut = new CollectionOfMerchantOrderResponse()
            {
                Content = merchantOrderResponses
            };

            //Act
            var result = sut.GetTop5ProductsSold();

            //Assert
            result.Should().BeEquivalentTo(expected, option => option.WithStrictOrdering());
        }
Exemplo n.º 6
0
        public IOrderedEnumerable <(string?MerchantProductNo, string?Gtin, int Quantity)> OrderProductsByQuantity(CollectionOfMerchantOrderResponse orders)
        {
            if (orders is null)
            {
                throw new ArgumentNullException(nameof(orders));
            }

            return(orders.Content
                   .SelectMany(x => x.Lines)
                   .GroupBy(x => x.MerchantProductNo)
                   .Select(x => (MerchantProductNo: x.Key, Gtin: x.Select(x => x.Gtin).First(), Quantity: x.Sum(x => x.Quantity)))
                   .OrderByDescending(x => x.Quantity));
        }
        private async Task <List <ProductDto> > GetTop5ProductsSoldFromOrdersInternal(CollectionOfMerchantOrderResponse collectionOfMerchantOrderResponse)
        {
            var products         = collectionOfMerchantOrderResponse.GetTop5ProductsSold();
            var productsResponse = await channelEngineHttpClient.GetProducts(products.Select(p => p.MerchantProductNo).ToList());

            return(MapProductsResponseToProductDtos(productsResponse, products));
        }