Exemplo n.º 1
0
        public void ShouldReturnCorrectExternalData()
        {
            // arranage
            External.ExternalInvoiceService.IsServiceDown = false;
            External.ExternalInvoiceService.Invoices      = new Dictionary <string, External.ExternalInvoice[]>()
            {
                { "1001", new [] { new External.ExternalInvoice()
                                   {
                                       Year = 2018, TotalAmount = 100
                                   },
                                   new External.ExternalInvoice()
                                   {
                                       Year = 2017, TotalAmount = 200
                                   } } }
            };

            //act
            var instance = new ExternalInvoiceService();
            var actual   = instance.GetSpendDetails(1001);

            // asset
            Assert.IsNotNull(actual);
            var year2018 = actual.Where(y => y.Year == 2018).First();

            Assert.AreEqual(100, year2018.TotalSpend);
            var year2017 = actual.Where(y => y.Year == 2017).First();

            Assert.AreEqual(200, year2017.TotalSpend);
        }
Exemplo n.º 2
0
        public void ShouldBeExternalInvoiceServiceType()
        {
            // arrange, act
            var instance = new ExternalInvoiceService();

            // assert
            Assert.AreEqual(InvoiceServiceType.External, instance.ServiceType);
        }
Exemplo n.º 3
0
        public void ShouldThowExternalServiceExcpetionWhenExternalServieIsDown()
        {
            // arranage
            External.ExternalInvoiceService.IsServiceDown = true;

            //act
            var instance = new ExternalInvoiceService();
            var actual   = instance.GetSpendDetails(1001);

            // assert by attribure decoration
        }
Exemplo n.º 4
0
        public ExternalInvoice[] GetInvoices(string supplierId)
        {
            var externalInvoicesOriginal = ExternalInvoiceService.GetInvoices(supplierId);

            return(externalInvoicesOriginal);
        }
        public SpendSummary GetTotalSpend()
        {
            var watch            = Stopwatch.StartNew();
            var tokenSource      = new CancellationTokenSource();
            CancellationToken ct = tokenSource.Token;
            int counter          = 1;

            ct.ThrowIfCancellationRequested();
            bool timeOut = true;



            Task <ExternalInvoice[]> tsk = new Task <ExternalInvoice[]>(() => ExternalInvoiceService.GetInvoices(_supplier.Id.ToString()), ct);

            tsk.Start();
            while (timeOut && counter <= 3)
            {
                if (tsk.IsCompleted)
                {
                    timeOut = false;
                    break;
                }

                if (!(tsk.IsCompleted) && watch.ElapsedMilliseconds >= 30000)
                {
                    tokenSource.Cancel();
                }

                if (ct.IsCancellationRequested)
                {
                    try {
                        timeOut = false;
                        counter++;
                        ct.ThrowIfCancellationRequested();
                    }
                    catch
                    { }
                    finally
                    {
                        tokenSource = new CancellationTokenSource();
                        ct          = tokenSource.Token;
                        watch.Restart();
                        tsk = new Task <ExternalInvoice[]>(() => ExternalInvoiceService.GetInvoices(_supplier.Id.ToString()), tokenSource.Token);
                        tsk.Start();
                    }
                }
            }
            ExternalInvoice[] invoices = tsk.Result;

            if (invoices.Count() == 0)
            {
                FailoverInvoiceService    failoverInvoiceService = new FailoverInvoiceService();
                FailoverInvoiceCollection collection             = failoverInvoiceService.GetInvoices(_supplier.Id);
                invoices = collection.Invoices;
            }

            var sd = (from supplier in invoices
                      group supplier by supplier.Year into eGroup
                      select new SpendDetail()
            {
                Year = eGroup.Key,
                TotalSpend = eGroup.Sum(x => x.TotalAmount)
            });

            return(new SpendSummary()
            {
                Name = _supplier.Name,
                Years = sd.ToList()
            });
        }
Exemplo n.º 6
0
        public ExternalInvoice[] GetInvoices(string supplierId)
        {
            var result = ExternalInvoiceService.GetInvoices(supplierId);

            return(result);
        }
 public ExternalInvoiceExtended(Supplier supplier)
 {
     _supplier        = supplier;
     _externalInvoice = ExternalInvoiceService.GetInvoices(_supplier.Id.ToString());
 }