Ejemplo n.º 1
0
        public async Task GetBestDeal_When_API_OneCompany_ReturningZero()
        {
            var request = new BestOfferRequest
            {
                Source      = "S1",
                Destination = "D1",
                Carton      = new int[] { 4, 4, 4 }
            };
            var fedxResponse = new BestOfferResponse
            {
                BestPrice   = 0,
                CompanyName = "FedX"
            };
            var premierResponse = new BestOfferResponse
            {
                BestPrice   = 90,
                CompanyName = "Premier"
            };
            var rx2Response = new BestOfferResponse
            {
                BestPrice   = 120,
                CompanyName = "Rx2Go"
            };

            SetupGetOfferMethod(request, fedxResponse, premierResponse, rx2Response);

            int expectedValue = 90;
            var bestDeal      = await _offersService.GetBestDealAsync(request);

            var actualValue = bestDeal.BestPrice;

            Assert.AreEqual(actualValue, expectedValue);
        }
Ejemplo n.º 2
0
        public async Task <BestOfferResponse> GetOfferAsync(BestOfferRequest request)
        {
            var baseUrl = _configuration.GetValue <string>("PremierAPIConfig:BaseURL");
            var apiKey  = _configuration.GetValue <string>("PremierAPIConfig:ApiKey");

            var offerResponse = new BestOfferResponse();

            offerResponse.CompanyName = _configuration.GetValue <string>("PremierAPIConfig:CompanyName");

            var postData = new PremierAPIRequest
            {
                Source      = request.Source,
                Destination = request.Destination,
                Packages    = string.Join(",", request.Carton)
            };

            var response = await _restClient.PostRequestAsync <PremierAPIRequest, PremierAPIResponse>(baseUrl, postData, apiKey, MediaTypeNames.Application.Xml);

            if (response == default(PremierAPIResponse))
            {
                offerResponse.BestPrice = decimal.Zero;
            }
            else
            {
                offerResponse.BestPrice = response.Quote;
            }

            return(offerResponse);
        }
Ejemplo n.º 3
0
        public async Task GetBestDeal_When_API_ReturningNegativeValue()
        {
            var request = new BestOfferRequest
            {
                Source      = "S1",
                Destination = "D1",
                Carton      = new int[] { 4, 4, 4 }
            };
            BestOfferResponse fedxResponse = new BestOfferResponse
            {
                BestPrice   = -80,
                CompanyName = "Premier"
            };;
            var premierResponse = new BestOfferResponse
            {
                BestPrice   = -100,
                CompanyName = "Premier"
            };
            var rx2Response = new BestOfferResponse
            {
                BestPrice   = -120,
                CompanyName = "Rx2Go"
            };

            SetupGetOfferMethod(request, fedxResponse, premierResponse, rx2Response);

            BestOfferResponse expectedResult = null;
            var actualResult = await _offersService.GetBestDealAsync(request);

            Assert.AreEqual(actualResult, expectedResult);
        }
Ejemplo n.º 4
0
        public async Task <BestOfferResponse> GetOfferAsync(BestOfferRequest request)
        {
            var baseUrl       = _configuration.GetValue <string>("FedXAPIConfig:BaseURL");
            var apiKey        = _configuration.GetValue <string>("FedXAPIConfig:ApiKey");
            var offerResponse = new BestOfferResponse();

            offerResponse.CompanyName = _configuration.GetValue <string>("FedXAPIConfig:CompanyName");

            var postData = new FedXAPIRequest
            {
                Consignee = request.Source,
                Consignor = request.Destination,
                Cartons   = request.Carton
            };

            var response = await _restClient.PostRequestAsync <object, FedXAPIResponse>(baseUrl, postData, apiKey);

            if (response == default(FedXAPIResponse))
            {
                offerResponse.BestPrice = decimal.Zero;
            }
            else
            {
                offerResponse.BestPrice = response.Amount;
            }

            return(offerResponse);
        }
Ejemplo n.º 5
0
        public async Task <BestOfferResponse> GetBestDealAsync(BestOfferRequest request)
        {
            List <Task <BestOfferResponse> > listOffers = new List <Task <BestOfferResponse> >();

            foreach (var offerClient in _offerClients)
            {
                listOffers.Add(offerClient.GetOfferAsync(request));
            }

            var listOffersData = await Task.WhenAll(listOffers);

            // Negative value handling
            var     bestPrice      = listOffersData.Where(a => a != null && a.BestPrice > 0);
            decimal bestPriceValue = decimal.Zero;

            if (bestPrice.Count() > 0)
            {
                bestPriceValue = bestPrice.Min(a => a.BestPrice);
            }

            var filteredData            = listOffersData.Where(a => a != null);
            BestOfferResponse bestOffer = filteredData != null?filteredData.FirstOrDefault(a => a.BestPrice == bestPriceValue) : null;

            return(bestOffer);
        }
Ejemplo n.º 6
0
        public async Task GetBestDeal_When_No_Offer_Return()
        {
            var request = new BestOfferRequest
            {
                Source      = "S1",
                Destination = "D1",
                Carton      = new int[] { 4, 4, 4 }
            };

            BestOfferResponse response = null;

            _offersService.Setup(offerService => offerService.GetBestDealAsync(request)).ReturnsAsync(response);

            int expectedResult = 404;
            var actualBestDeal = await _offerController.GetBestDealAsync(request);

            Assert.AreEqual(((ObjectResult)actualBestDeal).StatusCode, expectedResult);
        }
Ejemplo n.º 7
0
        public async Task GetBestDeal_When_AllCompanies_ReturningNull()
        {
            var request = new BestOfferRequest
            {
                Source      = "S1",
                Destination = "D1",
                Carton      = new int[] { 4, 4, 4 }
            };

            BestOfferResponse fedxResponse    = null;
            BestOfferResponse premierResponse = null;
            BestOfferResponse rx2Response     = null;

            SetupGetOfferMethod(request, fedxResponse, premierResponse, rx2Response);

            BestOfferResponse expectedResult = null;
            var actualResult = await _offersService.GetBestDealAsync(request);

            Assert.AreEqual(actualResult, expectedResult);
        }
Ejemplo n.º 8
0
        public async Task GetBestDeal_ShouldReturnBestDeal()
        {
            var request = new BestOfferRequest
            {
                Source      = "S1",
                Destination = "D1",
                Carton      = new int[] { 4, 4, 4 }
            };

            var response = new BestOfferResponse {
                CompanyName = "FedX", BestPrice = 110
            };

            _offersService.Setup(offerService => offerService.GetBestDealAsync(request)).ReturnsAsync(response);


            int expectedResult = 110;
            var bestDeal       = await _offerController.GetBestDealAsync(request);


            var actualResult = ((BestOfferResponse)((ObjectResult)bestDeal).Value).BestPrice;

            Assert.AreEqual(actualResult, expectedResult);
        }
Ejemplo n.º 9
0
 private void SetupGetOfferMethod(BestOfferRequest request, BestOfferResponse fedxResponse, BestOfferResponse premierResponse, BestOfferResponse rx2Response)
 {
     _fedXAPIClientMock.Setup(a => a.GetOfferAsync(request)).ReturnsAsync(fedxResponse);
     _premierAPIClientMock.Setup(a => a.GetOfferAsync(request)).ReturnsAsync(premierResponse);
     _rX2GoAPIClientMock.Setup(a => a.GetOfferAsync(request)).ReturnsAsync(rx2Response);
 }