public static SpendSummary SpendSummary(this Supplier supplier, IInvoiceRepository invoiceRepository) { var spendSummary = new SpendSummary() { Years = new List <SpendDetail>() }; spendSummary.Name = supplier.Name; var spendDetails = new List <SpendDetail>(); var invoices = invoiceRepository.Get(supplier.Id); var result = invoices.GroupBy(x => x.InvoiceDate.Year) .Select(g => new { Year = g.Key, TotalSpend = g.Sum(x => x.Amount) }).ToList(); result.ForEach(x => spendSummary.Years.Add( new SpendDetail() { Year = x.Year, TotalSpend = x.TotalSpend })); return(spendSummary); }
public void SpendService_ExternalCustomer_Spend_FailoverCase_ObsoleteDate_Test() { // Arrange int supplierId = 4; SpendService spendService = container.Resolve <ExternalSpendService>(); // Act SpendSummary result = spendService.GetTotalSpend(supplierId); // Assert - See Exception }
public void SpendService_ExternalCustomer_Name_Test() { // Arrange int supplierId = 2; SpendService spendService = container.Resolve <ExternalSpendService>(); // Act SpendSummary result = spendService.GetTotalSpend(supplierId); // Assert Assert.IsNotNull(result); Assert.AreEqual("Supplier External", result.Name); }
public void SpendService_ExternalCustomer_Spend_ReturnToNormalAfter1Minute_Test() { // Arrange Mock <ICircuitBreaker> mockedInstance = new Mock <ICircuitBreaker>(); mockedInstance.Setup(mc => mc.ClosedTimeSeconds).Returns(5); // Adjust Timeout to 5 Seconds mockedInstance.Setup(mc => mc.GetSpendDetail(2)).Returns( // Set Data new List <SpendDetail>( new SpendDetail[] { new SpendDetail() { TotalSpend = 1001, Year = 2018 }, new SpendDetail() { TotalSpend = 1001, Year = 2017 } } )); UnityContainer container = new UnityContainer(); container.RegisterType <ISupplierDataService, SupplierDataServiceStub>(); container.RegisterType <ISupplierService, SupplierService>(); container.RegisterType <IInvoiceRepository, InvoiceRepositoryStub>(); container.RegisterType <IExternalSpendService, ExternalInvoiceServiceStub>(); container.RegisterType <ICircuitBreaker, ExternalSpendServiceInvoker>(); container.RegisterType <IFailoverInvoiceService, FailoverInvoiceServiceStub>(); SpendService spendService = container.Resolve <ExternalSpendService>(); SpendSummary resultFailover = spendService.GetTotalSpend(3); Thread.Sleep(1000 * 5 + 1); // Wait 6 Seconds // Act container.RegisterInstance <ICircuitBreaker>(mockedInstance.Object); spendService = container.Resolve <ExternalSpendService>(); SpendSummary resultNormal = spendService.GetTotalSpend(2); // Assert Assert.IsNotNull(resultFailover); Assert.AreEqual("Supplier External (Failover)", resultFailover.Name); Assert.AreEqual(1, resultFailover.Years.Count); Assert.AreEqual(900, resultFailover.Years[0].TotalSpend); Assert.IsNotNull(resultNormal); Assert.AreEqual("Supplier External", resultNormal.Name); Assert.AreEqual(2, resultNormal.Years.Count); Assert.AreEqual(1001, resultNormal.Years[0].TotalSpend); Assert.AreEqual(1001, resultNormal.Years[1].TotalSpend); }
public void Test() { var list = new List <SpendDetail> { new SpendDetail(2017, 1000.0m), new SpendDetail(2017, 2000.0m) }; var spendSummary = new SpendSummary("Supplier1", list); var mock = new Mock <ISpendService>(); mock.Setup(m => m.GetTotalSpend(1)).Returns(spendSummary); mock.Verify(); }
public void SpendService_ExternalCustomer_Spend_FailoverCase_Test() { // Arrange int supplierId = 3; SpendService spendService = container.Resolve <ExternalSpendService>(); // Act SpendSummary result = spendService.GetTotalSpend(supplierId); // Assert Assert.IsNotNull(result); Assert.AreEqual("Supplier External (Failover)", result.Name); Assert.AreEqual(1, result.Years.Count); Assert.AreEqual(900, result.Years[0].TotalSpend); }
public void SpendService_ExternalCustomer_Spend_ActionCase_Test() { // Arrange int supplierId = 2; SpendService spendService = container.Resolve <ExternalSpendService>(); // Act SpendSummary result = spendService.GetTotalSpend(supplierId); // Assert Assert.IsNotNull(result); Assert.AreEqual(2, result.Years.Count); Assert.AreEqual(1000, result.Years[0].TotalSpend); Assert.AreEqual(1000, result.Years[1].TotalSpend); }
public void TestMethod_External() { int supplierId = 1; List <SpendDetail> sd = new List <SpendDetail>(); sd.Add(new SpendDetail() { TotalSpend = 5000, Year = DateTime.Now.Year }); sd.Add(new SpendDetail() { TotalSpend = 7000, Year = DateTime.Now.AddYears(-1).Year }); sd.Add(new SpendDetail() { TotalSpend = 8000, Year = DateTime.Now.AddYears(-2).Year }); sd.Add(new SpendDetail() { TotalSpend = 9000, Year = DateTime.Now.AddYears(-3).Year }); SpendSummary expectedspsum = new SpendSummary() { Name = "Bob", Years = sd }; SpendSummary actualspsum = new SpendSummary(); actualspsum = new SpendService().GetTotalSpend(supplierId); Assert.AreEqual(expectedspsum.Name, actualspsum.Name); if (expectedspsum.Years.Count == actualspsum.Years.Count) { foreach (SpendDetail spd in actualspsum.Years) { Assert.IsNotNull(expectedspsum.Years.Find(x => x.TotalSpend == spd.TotalSpend && x.Year == spd.Year)); } } else { Assert.Fail(); } }
public SpendSummary GetTotalSpend(int supplierId) { SpendSummary summary = null; supplierRepository.WithSupplierCompany(supplier => { if (supplier == null) { throw new ApplicationException(string.Format("Invalide Supplier Id {0}", supplierId)); } summary = new SpendSummary { Name = supplier.Name, Years = GetSpendDetails(supplier) }; }, supplierId); return(summary); }
public SpendSummary TryGetSpendSummaryFromExternalService(Supplier theSupplier, Func <string, ExternalInvoice[]> externalInvoiceService) { var theSpendSummary = new SpendSummary() { Years = new List <SpendDetail>() }; ExternalInvoice[] invoices = null; try { invoices = externalInvoiceService.Invoke(theSupplier.Id.ToString()); //ExternalInvoiceService.GetInvoices(theSupplier.Id.ToString()); } catch // external service failed { invoices = null; } if (invoices == null) { EventExternalInvoiceServiceFailed(this, new ServiceManagerArgs() { supplier = theSupplier }); } else { #region Calculate TotalSpend var result = invoices.GroupBy(x => x.Year) .Select(g => new { Year = g.Key, TotalSpend = g.Sum(x => x.TotalAmount) }).ToList(); result.ForEach(x => theSpendSummary.Years.Add( new SpendDetail() { Year = x.Year, TotalSpend = x.TotalSpend })); #endregion } return(theSpendSummary); }
public SpendSummary TryGetSpendSummaryFromFailoverService(Supplier theSupplier, FailoverInvoiceCollection failOverInvoices) { var theSpendSummary = new SpendSummary() { Years = new List <SpendDetail>() }; TimeSpan diff = DateTime.Today - failOverInvoices.Timestamp; if (diff.Days > 30) { EventDataNotRefreshed(this, new ServiceManagerArgs() { supplier = theSupplier }); } else { #region Calculate TotalSpend var result = failOverInvoices.Invoices.GroupBy(x => x.Year) .Select(g => new { Year = g.Key, TotalSpend = g.Sum(x => x.TotalAmount) }).ToList(); result.ForEach(x => this.spendSummary.Years.Add( new SpendDetail() { Year = x.Year, TotalSpend = x.TotalSpend })); #endregion } return(theSpendSummary); }