public void Service_Layer_Integration_Test() { //Arrange String[] basket = new String[] { "Apple", "Banana", "Orange", "Banana", "Orange", "Banana" }; ISaleService saleService = new SaleServiceImpl( productRepository.Object, onSalePromotionRepository.Object, groupSalePromotionRepository.Object, additionalSalePromotionRepository.Object); foreach (var product in basket) { saleService.AddProduct(product); } //Act & Assert Assert.AreEqual(6.75m, saleService.GetReceipt().GetTotalRegularPrice()); //Arrange Promotions onSalePromotions.Add(new OnSalePromotion() { Id = "Apple", OnSalePrice = 0.6m, Description = "Apple 60c", StartDate = DateTime.Now.AddMonths(-6), EndDate = DateTime.Now.AddMonths(6) }); //Act & Assert Assert.AreEqual(6.60m, saleService.GetReceipt().GetTotalPayment()); //Arrange Promotions groupSalePromotions.Add(new GroupSalePromotion() { Id = "Banana", Description = "Banana $2 for 3", GroupQuantity = 3, GroupPrice = 2.0m, StartDate = DateTime.Now.AddMonths(-6), EndDate = DateTime.Now.AddMonths(6) }); //Act & Assert Assert.AreEqual(5.60m, saleService.GetReceipt().GetTotalPayment()); //Arrange Promotions additionalSalePromotions.Add(new AdditionalSalePromotion() { Id = "Orange", BuyQuantity = 1, Description = "Orange BOGO", LastProductDiscountPercent = 0, StartDate = DateTime.Now.AddMonths(-6), EndDate = DateTime.Now.AddMonths(6) }); //Act & Assert Assert.AreEqual(4.10m, saleService.GetReceipt().GetTotalPayment()); //Banana OnSale and GroupSale are mixed. Let's find best price for customer //Arrange Promotions onSalePromotions.Add(new OnSalePromotion() { Id = "Banana", OnSalePrice = 0.8m, Description = "Banana 80c", StartDate = DateTime.Now.AddMonths(-6), EndDate = DateTime.Now.AddMonths(6) }); //Act & Assert Assert.AreEqual(4.10m, saleService.GetReceipt().GetTotalPayment()); }
public void Checkout() { try { IRepository <Product> productRepository = new FileRepository <Product>(Path.Combine("..\\..\\Files", "product.json")); IRepository <OnSalePromotion> onSalePromotionRepository = new FileRepository <OnSalePromotion>(Path.Combine("..\\..\\Files", "onsale.json")); IRepository <GroupSalePromotion> groupSalePromotionRepository = new FileRepository <GroupSalePromotion>(Path.Combine("..\\..\\Files", "groupsale.json")); IRepository <AdditionalSalePromotion> additionalSalePromotionRepository = new FileRepository <AdditionalSalePromotion>(Path.Combine("..\\..\\Files", "additionalsale.json")); // Simple promotion data validation IPromotionValidator promotionValidator; promotionValidator = new OnSalePromotionValidator(onSalePromotionRepository.GetAll()); promotionValidator.Validate(); promotionValidator = new GroupSalePromotionValidator(groupSalePromotionRepository.GetAll()); promotionValidator.Validate(); promotionValidator = new AdditionalSalePromotionValidator(additionalSalePromotionRepository.GetAll()); promotionValidator.Validate(); // Read basket data List <Product> productsInBasket = JsonConvert.DeserializeObject <List <Product> >(File.ReadAllText(Path.Combine("..\\..\\Files", "basket.json"))); // Service Layer Facade ISaleService saleService = new SaleServiceImpl(productRepository, onSalePromotionRepository, groupSalePromotionRepository, additionalSalePromotionRepository); foreach (var item in productsInBasket) { saleService.AddProduct(item.Id); } Receipt receipt = saleService.GetReceipt(); ReceiptPrinter receiptPrinter = new ConsoleReceiptPrinter(receipt); receiptPrinter.Print(); } catch (ProductException pdex) { Console.WriteLine("=== " + pdex.Message + " ==="); } catch (PromotionException pmex) { Console.WriteLine("=== " + pmex.Message + " ==="); return; } catch (Exception ex) { Console.WriteLine("=== Woops, there's something wrong..." + ex.ToString() + " ==="); } }