public void Buy_NotEnoughQuantity_ThrowException() { NotEnoughInventoryException expectedExcetpion = null; try { double price = _store.Buy( "Isaac Asimov - Robot series", "Isaac Asimov - Foundation", "Isaac Asimov - Foundation", "Isaac Asimov - Foundation", "J.K Rowling - Goblet Of fire", "J.K Rowling - Goblet Of fire", "J.K Rowling - Goblet Of fire"); } catch (NotEnoughInventoryException exception) { expectedExcetpion = exception; } Assert.IsNotNull(expectedExcetpion); List <INameQuantity> missing = expectedExcetpion.Missing.ToList(); Assert.AreEqual(2, missing.Count); INameQuantity asimov = missing.SingleOrDefault(e => e.Name == "Isaac Asimov - Foundation"); Assert.IsNotNull(asimov); Assert.AreEqual(2, asimov.Quantity); INameQuantity rowling = missing.SingleOrDefault(e => e.Name == "J.K Rowling - Goblet Of fire"); Assert.IsNotNull(rowling); Assert.AreEqual(1, rowling.Quantity); }
public void When_BuyMoreThanAvaliableBooks_Expect_Exception() { IBasketCalculator calc = new InfrastructureLayer.BasketCalculators.BasketCalcTechRule(); NotEnoughInventoryException exception = Assert.Throws <NotEnoughInventoryException>(() => calc.CalculateCatalogPrice(new List <Catalog>() { Book2, Book2, Book2 })); Assert.AreEqual(Book2.Name, exception.Missing.First().Name); Assert.AreEqual(Book2.Quantity, exception.Missing.First().Quantity); }
public void Buy_ShouldThrowExceptionWithListOfBookNotAvailable_IfQuantityIsNotEnough() { // Arrange string[] bookNames = new string[] { "Isaac Asimov - Foundation", "Isaac Asimov - Foundation" }; StoreService storeService = new StoreService(); storeService.Import(VALID_JSON_STRING, out string errorMessage); //Act NotEnoughInventoryException ex = Assert.Throws <NotEnoughInventoryException>(() => storeService.Buy(bookNames)); // Assert Assert.NotEmpty(ex.Missing); Assert.Single(ex.Missing); Assert.Equal(bookNames[0], ex.Missing.FirstOrDefault().Name); Assert.NotNull(ex.Message); Assert.Equal("NotEnoughInventoryException", ex.Message); }
public NotEnoughInventoryException ValidateBasket(List <Basket> basketToValidate, IEnumerable <Catalog> catalogData) { NotEnoughInventoryException exception = new NotEnoughInventoryException(); foreach (var product in basketToValidate) { var stock = catalogData.Where(n => n.Name == product.Name).First().Quantity; if (product.Quantity > stock) { NameQuantity missing = new NameQuantity();; missing.createException(product.Name, product.Quantity); exception.AddException(missing); } } if (exception.Missing != null) { throw exception; } else { return(null); } }