public async Task CalculateEstimate_InvalidWholesaler_ReturnsFailureResultWithNotFoundCode()
        {
            // Arrange
            var order = new Order()
            {
                WholesalerId = Guid.NewGuid(),
                Items        = new List <OrderItem>
                {
                    new OrderItem {
                        BeerId = new Guid("253F30AC-ACF7-4577-AD8F-F7252B0979C8"), Quantity = 5
                    }
                }
            };

            var wholesalers = new List <Wholesaler>
            {
                new Wholesaler {
                    Id = Guid.NewGuid(), Name = "Test Wholesaler"
                }
            };

            var breweryContextMock = new Mock <BreweryContext>();

            breweryContextMock.Setup(x => x.Wholesalers).ReturnsDbSet(wholesalers);

            var wholesalerService = new WholesalerService(breweryContextMock.Object);

            // Act
            var result = await wholesalerService.CalculateEstimate(order);

            // Assert
            result.IsFailure.Should().BeTrue();
            result.ErrorCode.Should().Be(ResultErrorCode.NotFound);
        }
        public void GetQuotation_CheckDuplicateOrder_ThrowException()
        {
            QuotationCommand command = new QuotationCommand()
            {
                WholesalerId = 1,
                TotalPrice   = 100,
                Items        = new List <ItemCommand>
                {
                    new ItemCommand()
                    {
                        BeerId = 7, Quantity = 10
                    },
                    new ItemCommand()
                    {
                        BeerId = 7, Quantity = 3
                    }
                }
            };

            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                Action action = () => wholesalerService.GetQuotation(command);
                action.Should().ThrowExactly <DuplicateItemException>().WithMessage(ExceptionMessage.DUPLICATE_ITEM);
            }
        }
Exemple #3
0
        public void SetUp()
        {
            _uow = Substitute.For <IUnitOfWork>();
            Mapper.Initialize(c => c.AddProfile <MappingProfile>());
            _wholesalerService = new WholesalerService(_uow);

            wholesalerList = new List <Wholesaler>
            {
                new Wholesaler()
                {
                    ByUser       = "******",
                    IsDeleted    = false,
                    WholesalerId = 1,
                    Name         = "Test",
                    Updated      = DateTime.Now
                },
                new Wholesaler()
                {
                    ByUser       = "******",
                    IsDeleted    = true,
                    WholesalerId = 1,
                    Name         = "Test",
                    Updated      = DateTime.Now
                }
            };
        }
        public void GetQuotation_WholesalerNotExist_ThrowException()
        {
            QuotationCommand command = new QuotationCommand()
            {
                WholesalerId = 70,
                TotalPrice   = 100,
                Items        = new List <ItemCommand>
                {
                    new ItemCommand()
                    {
                        BeerId = 1, Quantity = 10
                    },
                    new ItemCommand()
                    {
                        BeerId = 2, Quantity = 5
                    }
                }
            };

            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                Action action = () => wholesalerService.GetQuotation(command);
                action.Should().ThrowExactly <NotFindObjectException>().WithMessage(ExceptionMessage.WHOLESALER_NOT_EXIST);
            }
        }
        public async Task GetQuote_ReturnOkDiscount20()
        {
            using (var context = new BeerContext(ContextOptions))
            {
                var service      = new WholesalerService(context);
                var quoteCommand = new GetQuoteCommand
                {
                    CommandLines = new List <BeerQuantity>()
                    {
                        new BeerQuantity {
                            BeerId = 1, Quantity = 12
                        },
                        new BeerQuantity {
                            BeerId = 2, Quantity = 12
                        }
                    }
                };

                var quote = await service.GetQuote(1, quoteCommand);

                Assert.Equal(12, quote.Discount);
                Assert.Equal(60, quote.Total);
                Assert.Equal(48, quote.Price);
            }
        }
        public async Task GenerateQuote_GivenAQuoteRequestWithLessThan10Items_ShouldReturnARawPriceAndPriceEqualToExpectedPrice()
        {
            //Arrange
            var requestedItemId = Guid.NewGuid();

            var wholesalerStock = new[] {
                new StockItem {
                    ItemId = requestedItemId, Quantity = 5, UnitPrice = 1.0f
                }
            };

            var expectedPrice = 5.0f;

            var quoteRequest = new QuoteRequest {
                WholesalerId = Guid.NewGuid(), RequestItems = new[] { new RequestItem {
                                                                          ItemId = requestedItemId, Quantity = 5
                                                                      } }
            };

            var mockedWholesalerQuery = new Mock <IWholesalerQuery>();

            mockedWholesalerQuery.Setup(query => query.GetWholesalerStock(It.IsAny <Guid>()))
            .ReturnsAsync(wholesalerStock);

            var wholesalerService = new WholesalerService(mockedWholesalerQuery.Object, null);

            //Act
            var result = await wholesalerService.GenerateQuote(quoteRequest);

            //Assert
            Assert.Equal(expectedPrice, result.Price);
            Assert.Equal(expectedPrice, result.RawPrice);
            Assert.Equal(0, result.Discount);
        }
        public async Task CalculateEstimate_DuplicateItems_ReturnsFailureResultWithValidationCode()
        {
            // Arrange
            var order = new Order()
            {
                WholesalerId = Guid.NewGuid(),
                Items        = new List <OrderItem>
                {
                    new OrderItem {
                        BeerId = new Guid("253F30AC-ACF7-4577-AD8F-F7252B0979C8"), Quantity = 5
                    },
                    new OrderItem {
                        BeerId = new Guid("253F30AC-ACF7-4577-AD8F-F7252B0979C8"), Quantity = 10
                    }
                }
            };

            var breweryContextMock = new Mock <BreweryContext>();
            var wholesalerService  = new WholesalerService(breweryContextMock.Object);

            // Act
            var result = await wholesalerService.CalculateEstimate(order);

            // Assert
            result.IsFailure.Should().BeTrue();
            result.ErrorCode.Should().Be(ResultErrorCode.Validation);
        }
        public void AddBeer_CommandIsNull_ThrowException()
        {
            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                Action action = () => wholesalerService.AddNewBeerToWholesaler(null);
                action.Should().ThrowExactly <HttpBodyException>().WithMessage(ExceptionMessage.COMMAND_IS_NULL);
            }
        }
        public async Task GetQuote_NotOk_ListIsNull()
        {
            using (var context = new BeerContext(ContextOptions))
            {
                var service      = new WholesalerService(context);
                var quoteCommand = new GetQuoteCommand
                {
                };

                await Assert.ThrowsAsync <CustomBadRequestException>(() => service.GetQuote(1, quoteCommand));
            }
        }
        public async Task AddBeerWholesaler_BeerNotExist()
        {
            var command = new AddBeerToWholesalerCommand {
                BeerId = 100, WholesalerId = 1, Stock = 20
            };

            using (var context = new BeerContext(ContextOptions))
            {
                var service = new WholesalerService(context);

                await Assert.ThrowsAsync <CustomBadRequestException>(() => service.AddBeer(command));
            }
        }
        public async Task GetQuote_NotOk_EmptyList()
        {
            using (var context = new BeerContext(ContextOptions))
            {
                var service      = new WholesalerService(context);
                var quoteCommand = new GetQuoteCommand
                {
                    CommandLines = new List <BeerQuantity>()
                };

                await Assert.ThrowsAsync <CustomBadRequestException>(() => service.GetQuote(1, quoteCommand));
            }
        }
        public void AddBeer_DuplicateBeer_ThrowException()
        {
            SellBeerCommand command = new SellBeerCommand()
            {
                BeerId = 1, WholesalerId = 1, Stock = 10
            };

            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                Action action = () => wholesalerService.AddNewBeerToWholesaler(command);
                action.Should().ThrowExactly <DuplicateItemException>().WithMessage(ExceptionMessage.ALREADY_SELL);
            }
        }
        public void AddBeer_StockIsNegative_ThrowException()
        {
            SellBeerCommand command = new SellBeerCommand()
            {
                BeerId = 1, WholesalerId = 2, Stock = -10
            };

            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                Action action = () => wholesalerService.AddNewBeerToWholesaler(command);
                action.Should().ThrowExactly <HttpBodyException>().WithMessage(ExceptionMessage.NEGATIVE_STOCK);
            }
        }
        public void AddBeer_WholesalerNotExist_ThrowException()
        {
            SellBeerCommand command = new SellBeerCommand()
            {
                BeerId = 2, WholesalerId = 7, Stock = 10
            };

            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                Action action = () => wholesalerService.AddNewBeerToWholesaler(command);
                action.Should().ThrowExactly <NotFindObjectException>().WithMessage(ExceptionMessage.WHOLESALER_NOT_EXIST);
            }
        }
        public void UpdateStock_Stock_IsOk()
        {
            UpdateStockCommand command = new UpdateStockCommand()
            {
                BeerId = 1, WholesalerId = 1, Stock = 9
            };

            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                var actual = wholesalerService.UpdateWholesalerBeer(command);
                actual.Stock.Should().Be(9);
            }
        }
        public void GetQuotation_CommandItemIsNull_ThrowException()
        {
            QuotationCommand command = new QuotationCommand()
            {
                WholesalerId = 1,
                TotalPrice   = 100,
                Items        = null
            };

            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                Action action = () => wholesalerService.GetQuotation(command);
                action.Should().ThrowExactly <HttpBodyException>().WithMessage(ExceptionMessage.COMMAND_IS_NULL);
            }
        }
        public async Task CalculateEstimate_NotEnoughBeers_ReturnsFailureResultWithBusinessCode()
        {
            // Arrange
            var wholesalerId = new Guid("9D01DFC3-1227-427B-8E02-FCEC6522215B");
            var beerId       = new Guid("253F30AC-ACF7-4577-AD8F-F7252B0979C8");

            var order = new Order()
            {
                WholesalerId = wholesalerId,
                Items        = new List <OrderItem>
                {
                    new OrderItem {
                        BeerId = beerId, Quantity = 5
                    }
                }
            };

            var wholesalers = new List <Wholesaler>
            {
                new Wholesaler
                {
                    Id     = wholesalerId,
                    Name   = "Test Wholesaler",
                    Stocks = new List <Stock>
                    {
                        new Stock {
                            Id = Guid.NewGuid(), BeerId = beerId, WholesalerId = wholesalerId, Quantity = 2
                        }
                    }
                }
            };

            var breweryContextMock = new Mock <BreweryContext>();

            breweryContextMock.Setup(x => x.Wholesalers).ReturnsDbSet(wholesalers);

            var wholesalerService = new WholesalerService(breweryContextMock.Object);

            // Act
            var result = await wholesalerService.CalculateEstimate(order);

            // Assert
            result.IsFailure.Should().BeTrue();
            result.ErrorCode.Should().Be(ResultErrorCode.Business);
        }
        public void AddBeerToWholesaler_OK()
        {
            SellBeerCommand command = new SellBeerCommand()
            {
                BeerId = 5, WholesalerId = 2, Stock = 1000
            };

            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                var actual = wholesalerService.AddNewBeerToWholesaler(command);

                actual.BeerId.Should().Be(command.BeerId);
                actual.WholesalerId.Should().Be(command.WholesalerId);
                actual.Stock.Should().Be(command.Stock);
            }
        }
        public async Task AddBeerWholesaler_ReturnOk()
        {
            var command = new AddBeerToWholesalerCommand {
                BeerId = 5, WholesalerId = 2, Stock = 20
            };

            using (var context = new BeerContext(ContextOptions))
            {
                var service = new WholesalerService(context);

                await service.AddBeer(command);

                var wholesaler = await context.Wholesalers
                                 .Include(w => w.WholesalerBeers)
                                 .SingleOrDefaultAsync(w => w.Id == command.WholesalerId);

                Assert.Contains(wholesaler.WholesalerBeers, wb => wb.BeerId == 5);
            }
        }
        public async Task CalculateEstimate_EmptyItems_ReturnsFailureResultWithValidationCode()
        {
            // Arrange
            var order = new Order()
            {
                WholesalerId = Guid.NewGuid(),
                Items        = new List <OrderItem>()
            };

            var breweryContextMock = new Mock <BreweryContext>();
            var wholesalerService  = new WholesalerService(breweryContextMock.Object);

            // Act
            var result = await wholesalerService.CalculateEstimate(order);

            // Assert
            result.IsFailure.Should().BeTrue();
            result.ErrorCode.Should().Be(ResultErrorCode.Validation);
        }
        public async Task GetQuote_NotOk_WholesalerNotFound()
        {
            using (var context = new BeerContext(ContextOptions))
            {
                var service      = new WholesalerService(context);
                var quoteCommand = new GetQuoteCommand
                {
                    CommandLines = new List <BeerQuantity>()
                    {
                        new BeerQuantity {
                            BeerId = 1, Quantity = 3
                        },
                        new BeerQuantity {
                            BeerId = 2, Quantity = 4
                        }
                    }
                };

                await Assert.ThrowsAsync <CustomBadRequestException>(() => service.GetQuote(5, quoteCommand));
            }
        }
        public void GetQuotation_StockNotEnough_ThrowException()
        {
            QuotationCommand command = new QuotationCommand()
            {
                WholesalerId = 1,
                TotalPrice   = 100,
                Items        = new List <ItemCommand>
                {
                    new ItemCommand()
                    {
                        BeerId   = 1,
                        Quantity = 40
                    }
                }
            };

            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                Action action = () => wholesalerService.GetQuotation(command);
                action.Should().ThrowExactly <NotEnoughQuantityException>().WithMessage(ExceptionMessage.ENOUGH_STOCK);
            }
        }
        public void GetQuotation_Discount20_IsOk()
        {
            var expect = 44.00;
            QuotationCommand command = new QuotationCommand()
            {
                WholesalerId = 1,
                Items        = new List <ItemCommand>
                {
                    new ItemCommand()
                    {
                        BeerId   = 1,
                        Quantity = 25
                    }
                }
            };

            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                var actual = wholesalerService.GetQuotation(command);
                actual.Should().Be(expect);
            }
        }
        public async Task CalculateEstimate_Buy48Beers_ReturnsSuccessResultWith20PercentDiscount()
        {
            // Arrange
            var wholesalerId      = new Guid("9D01DFC3-1227-427B-8E02-FCEC6522215B");
            var leffeBruneBeerId  = new Guid("253F30AC-ACF7-4577-AD8F-F7252B0979C8");
            var leffeTripleBeerId = new Guid("F781DCCB-E4D9-4CD6-9684-0FDFB7E136CA");

            var order = new Order()
            {
                WholesalerId = wholesalerId,
                Items        = new List <OrderItem>
                {
                    new OrderItem {
                        BeerId = leffeBruneBeerId, Quantity = 24
                    },
                    new OrderItem {
                        BeerId = leffeTripleBeerId, Quantity = 24
                    }
                }
            };

            var leffeBruneBeer = new Beer()
            {
                Id              = leffeBruneBeerId,
                Name            = "Leffe Brune",
                PriceWithoutVat = 3.5
            };

            var leffeTripleBeer = new Beer()
            {
                Id              = leffeBruneBeerId,
                Name            = "Leffe Triple",
                PriceWithoutVat = 5
            };

            var wholesalers = new List <Wholesaler>
            {
                new Wholesaler
                {
                    Id     = wholesalerId,
                    Name   = "Test Wholesaler",
                    Stocks = new List <Stock>
                    {
                        new Stock {
                            Id = Guid.NewGuid(), BeerId = leffeBruneBeerId, WholesalerId = wholesalerId, Quantity = 50, Beer = leffeBruneBeer
                        },
                        new Stock {
                            Id = Guid.NewGuid(), BeerId = leffeTripleBeerId, WholesalerId = wholesalerId, Quantity = 50, Beer = leffeTripleBeer
                        }
                    }
                }
            };

            var breweryContextMock = new Mock <BreweryContext>();

            breweryContextMock.Setup(x => x.Wholesalers).ReturnsDbSet(wholesalers);

            var wholesalerService = new WholesalerService(breweryContextMock.Object);

            // Act
            var result = await wholesalerService.CalculateEstimate(order);

            // Assert
            const double expectedPriceWithoutVatAndDiscount = 3.5 * 24 + 5 * 24;

            result.IsSuccess.Should().BeTrue();
            result.Value.Discount.Should().Be(0.2);
            result.Value.TotalQuantity.Should().Be(48);
            result.Value.TotalPriceWithoutVatAndDiscount.Should().Be(expectedPriceWithoutVatAndDiscount);
            result.Value.TotalPriceWithVatAndDiscount.Should().Be(expectedPriceWithoutVatAndDiscount * 1.21 * 0.8);
        }
        public async Task CalculateEstimate_Buy7Beers_ReturnsSuccessResultWithNoDiscount()
        {
            // Arrange
            var wholesalerId      = new Guid("9D01DFC3-1227-427B-8E02-FCEC6522215B");
            var leffeBruneBeerId  = new Guid("253F30AC-ACF7-4577-AD8F-F7252B0979C8");
            var leffeTripleBeerId = new Guid("F781DCCB-E4D9-4CD6-9684-0FDFB7E136CA");

            var order = new Order()
            {
                WholesalerId = wholesalerId,
                Items        = new List <OrderItem>
                {
                    new OrderItem {
                        BeerId = leffeBruneBeerId, Quantity = 4
                    },
                    new OrderItem {
                        BeerId = leffeTripleBeerId, Quantity = 3
                    }
                }
            };

            var leffeBruneBeer = new Beer()
            {
                Id              = leffeBruneBeerId,
                Name            = "Leffe Brune",
                PriceWithoutVat = 3.5
            };

            var leffeTripleBeer = new Beer()
            {
                Id              = leffeBruneBeerId,
                Name            = "Leffe Triple",
                PriceWithoutVat = 5
            };

            var wholesalers = new List <Wholesaler>
            {
                new Wholesaler
                {
                    Id     = wholesalerId,
                    Name   = "Test Wholesaler",
                    Stocks = new List <Stock>
                    {
                        new Stock {
                            Id = Guid.NewGuid(), BeerId = leffeBruneBeerId, WholesalerId = wholesalerId, Quantity = 20, Beer = leffeBruneBeer
                        },
                        new Stock {
                            Id = Guid.NewGuid(), BeerId = leffeTripleBeerId, WholesalerId = wholesalerId, Quantity = 10, Beer = leffeTripleBeer
                        }
                    }
                }
            };

            var breweryContextMock = new Mock <BreweryContext>();

            breweryContextMock.Setup(x => x.Wholesalers).ReturnsDbSet(wholesalers);

            var wholesalerService = new WholesalerService(breweryContextMock.Object);

            // Act
            var result = await wholesalerService.CalculateEstimate(order);

            // Assert
            var expectedPriceWithoutVatAndDiscount = 3.5 * 4 + 5 * 3;

            result.IsSuccess.Should().BeTrue();
            result.Value.Discount.Should().Be(0);
            result.Value.TotalQuantity.Should().Be(7);
            result.Value.TotalPriceWithoutVatAndDiscount.Should().Be(expectedPriceWithoutVatAndDiscount);
            result.Value.TotalPriceWithVatAndDiscount.Should().Be(expectedPriceWithoutVatAndDiscount * 1.21);
            result.Value.Items.Should().HaveCount(2);

            var leffeBruneBeerEstimateItem = result.Value.Items.First(i => i.BeerId == leffeBruneBeerId);

            leffeBruneBeerEstimateItem.BeerId.Should().Be(leffeBruneBeerId);
            leffeBruneBeerEstimateItem.BeerName.Should().Be("Leffe Brune");
            leffeBruneBeerEstimateItem.Quantity.Should().Be(4);
            leffeBruneBeerEstimateItem.UnitPriceWithoutVat.Should().Be(3.5);
            leffeBruneBeerEstimateItem.TotalPriceWithoutVat.Should().Be(4 * 3.5);
        }