public async Task <GetExchangeResponse> GetExchangeAsync(string currency) { GetExchangeResponse result = null; try { var response = await _client.GetAsync($"{_baseURL}/{currency}"); if (response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStringAsync(); var jsonExchange = JArray.Parse(content); return(new GetExchangeResponse() { BuyPrice = (decimal)jsonExchange[0], SellPrice = (decimal)jsonExchange[1] }); } } catch (Exception exception) { _logger.LogError(exception, "Unhandled exception"); } return(result); }
public async Task <GetExchangeResponse> GetExchange() { GetExchangeResponse result = null; try { result = await _externalService.GetExchangeAsync(CurrencyName); } catch (Exception exception) { _logger.LogError(exception, "Unhandled exception"); } return(result); }
public async Task GetExchange_OnCall_Success() { // Arrange var exchange = new GetExchangeResponse { BuyPrice = 30.0M, SellPrice = 40.0M }; _mockExternalService.GetExchangeAsync(_target.CurrencyName) .Returns(Task.FromResult(exchange)); // Act var result = await _target.GetExchange(); // Assert Assert.IsNotNull(result); }
public async Task BuyCurrency_OnIValidAmount_ThrowsException() { // Arrange var exchange = new GetExchangeResponse { BuyPrice = 30.0M, SellPrice = 40.0M }; _mockExternalService.GetExchangeAsync(_target.CurrencyName) .Returns(Task.FromResult(exchange)); _mockExchangeRepository.GetCurrentTotal(Arg.Any <int>(), "USD", Arg.Any <DateTime>()) .Returns(Task.FromResult(151M)); var amountInPesos = 2000; // Act && Assert await Assert.ThrowsExceptionAsync <Exception>(async() => await _target.BuyCurrency(1, amountInPesos)); await _mockExchangeRepository.DidNotReceive().SaveTransaction(Arg.Any <ExchangePurchases>()); }
public async Task BuyCurrency_OnValidAmount_Success() { // Arrange var exchange = new GetExchangeResponse { BuyPrice = 30.0M, SellPrice = 40.0M }; _mockExternalService.GetExchangeAsync(_target.CurrencyName) .Returns(Task.FromResult(exchange)); _mockExchangeRepository.GetCurrentTotal(Arg.Any <int>(), "USD", Arg.Any <DateTime>()) .Returns(Task.FromResult(120M)); var amountInPesos = 2000; // Act var result = await _target.BuyCurrency(1, amountInPesos); // Assert Assert.IsTrue(result.BoughtAmount > 0.00M); }
public async Task <GetExchangeResponse> GetExchange() { GetExchangeResponse result = null; try { var dollarExchange = await _dollarController.GetExchange(); result = new GetExchangeResponse { BuyPrice = dollarExchange.BuyPrice / 4, SellPrice = dollarExchange.SellPrice / 4 }; } catch (Exception exception) { _logger.LogError(exception, "Unhandled exception"); } return(result); }
public override async Task <GetExchangeResponse> GetExchange(GetExchangeRequest request, ServerCallContext context) { BsonDocument bson; try { bson = mongoCollection.Find($"{{ _id: ObjectId('{request.Id}') }}").FirstOrDefault(); } catch (Exception ex) { throw new RpcException(new Status(StatusCode.Unknown, $"Could get Exchange with id {request.Id} -> {ex.Message}")); } if (bson == null) { throw new RpcException(new Status(StatusCode.NotFound, $"Could not find Exchange with id {request.Id}")); } var exchange = new Exchange() { Country = bson.GetValue("country").AsString, Id = bson.GetValue("id").AsString, Name = bson.GetValue("name").AsString, HasTradingIncentive = bson.GetValue("has_trading_incentive").AsString, Image = bson.GetValue("image").AsString, TradeVolume24HBtcNormalized = bson.GetValue("trade_volume_24h_btc_normalized").AsString, TrustScore = bson.GetValue("trust_score").AsString, TrustScoreRank = bson.GetValue("score_rank").AsString, Url = bson.GetValue("url").AsString, YearEstablished = bson.GetValue("year_established").AsString }; var response = new GetExchangeResponse { Result = exchange }; return(await Task.FromResult(response)); }