Example #1
0
        private async Task <GetCryptoCurrencyQuoteResponse> CryptoCurrencyQuoteOnlyCallCoinMarketCapApproach(string cryptoCurrencySymbol, int cryptoCurrencyId)
        {
            var cryptoCurrencyPriceTasks = new List <Task <CryptoCurrencyQuoteDto> >();

            _currencies.ForEach(targetCurrency =>
            {
                cryptoCurrencyPriceTasks.Add(GetCryptoCurrencyQuote(cryptoCurrencyId, targetCurrency));
            });

            await Task.WhenAll(cryptoCurrencyPriceTasks).ConfigureAwait(false);

            var result = new GetCryptoCurrencyQuoteResponse()
            {
                Success = true,
                CryptoCurrencySymbol = cryptoCurrencySymbol,
                Quotes  = new Dictionary <string, decimal>(),
                Message = "Success"
            };

            foreach (var task in cryptoCurrencyPriceTasks)
            {
                var cryptoCurrencyQuote = await task;
                result.Quotes.Add(cryptoCurrencyQuote.BaseCurrency, cryptoCurrencyQuote.Success ? decimal.Round(cryptoCurrencyQuote.Price, _maximumFloatingPointDigit) : 0);
            }

            return(result);
        }
        public async Task Run_DisplayMenuAndDisplayBTCQuoteThenExit_CheckConsoleAndCryptoCurrencyServiceMethods()
        {
            // Arrange
            Initialize();
            string cryptoSymbol = "BTN";

            _console.Setup(x => x.Clear());
            _console.Setup(x => x.WriteLine(It.IsAny <string>(), It.IsAny <ConsoleColor>()));
            _console.SetupSequence(x => x.ReadLine()).Returns(cryptoSymbol).Returns("e");

            _logger.Setup(x => x.Log(It.Is <LogLevel>(a => a == LogLevel.Information), It.IsAny <EventId>(), It.Is <It.IsAnyType>((v, t) => true), It.IsAny <Exception>(), It.Is <Func <It.IsAnyType, Exception, string> >((v, t) => true))).Verifiable();


            GetCryptoCurrencyQuoteResponse getCryptoCurrencyQuoteResponse = new GetCryptoCurrencyQuoteResponse();
            GetCryptoCurrencyQuoteRequest  getCryptoCurrencyQuoteRequest  = null;

            _cryptoCurrencyService.Setup(x => x.GetCryptoCurrencyQuoteAsync(It.IsAny <GetCryptoCurrencyQuoteRequest>()))
            .Callback <GetCryptoCurrencyQuoteRequest>(x => getCryptoCurrencyQuoteRequest = x)
            .ReturnsAsync(getCryptoCurrencyQuoteResponse);


            // Act
            await _applicationService.Run(new string[] { });

            // Assert
            _console.Verify(x => x.WriteLine(It.Is <string>(input => input == "(C)rypto Currencies"), It.IsAny <ConsoleColor>()), Times.Exactly(2));
            _console.Verify(x => x.WriteLine(It.Is <string>(input => input == "(E)xit"), It.IsAny <ConsoleColor>()), Times.Exactly(2));
            _console.Verify(x => x.WriteLine(It.Is <string>(input => input == "Choose An Option Or Enter Crypto Currency Symbol:"), It.IsAny <ConsoleColor>()), Times.Exactly(2));
            _cryptoCurrencyService.Verify(x => x.GetCryptoCurrenciesAsync(), Times.Never);
            _cryptoCurrencyService.Verify(x => x.GetCryptoCurrencyQuoteAsync(It.IsAny <GetCryptoCurrencyQuoteRequest>()), Times.Once);
            Assert.NotNull(getCryptoCurrencyQuoteRequest);
            Assert.NotNull(getCryptoCurrencyQuoteRequest.CryptoCurrencySymbol);
            Assert.Equal(cryptoSymbol.ToLower(), getCryptoCurrencyQuoteRequest.CryptoCurrencySymbol.ToLower());
        }
Example #3
0
        private async Task <GetCryptoCurrencyQuoteResponse> CryptoCurrencyQuoteWithExchangeApproach(string cryptoCurrencySymbol, int cryptoCurrencyId)
        {
            var result = new GetCryptoCurrencyQuoteResponse();
            var cryptoCurrencyQuote = await GetCryptoCurrencyQuote(cryptoCurrencyId, _baseCurrency).ConfigureAwait(false);

            if (!cryptoCurrencyQuote.Success)
            {
                result.Message = cryptoCurrencyQuote.Message;
                return(result);
            }

            var getExchangeData = new GetExchangeData
            {
                BaseCurrency     = _baseCurrency.ToString(),
                TargetCurrencies = _currencies
            };

            var exchangeRates = await _exchangeRatesProxy.GetExchangeRateAsync(getExchangeData).ConfigureAwait(false);

            if (exchangeRates == null || exchangeRates.Rates == null)
            {
                result.Message = "Can Not Exchange Base Currency Rate ";
                return(result);
            }

            result.Quotes = _currencies.ToDictionary(a => a, t => t == _baseCurrency ?
                                                     decimal.Round(cryptoCurrencyQuote.Price, _maximumFloatingPointDigit):
                                                     GetPriceInTargetCurrency(t, cryptoCurrencyQuote.Price, exchangeRates));

            result.Success = true;
            result.CryptoCurrencySymbol = cryptoCurrencySymbol;
            result.Message = "Success";
            return(result);
        }