public IActionResult Get(RequestFX requestFx) { ResponseFX responseFx = currencyConverterService.ConvertCurrency(requestFx); if (responseFx is null) { return(this.BadRequest()); } return(this.Ok(responseFx)); }
public void WhenCallingCurrencyConverterWithInvalidParametersReturnNull(string amount, string source, string target) { var request = new RequestFX() { Amount = amount, SourceCurrency = source, TargetCurrency = target }; var result = this.currencyConverter.ConvertCurrency(request); Assert.IsNull(result); }
public void WhenCallingCurrencyConverterWithUnfoundTargetCurrencyReturnNull() { var request = new RequestFX() { Amount = "1.00", SourceCurrency = "GBP", TargetCurrency = "USD" }; this.exchangeRates.Setup(e => e.RetrieveLatestJsonRates(It.IsAny <string>())).ReturnsAsync(this.rates); var result = this.currencyConverter.ConvertCurrency(request); Assert.IsNull(result); }
public void WhenCallingGetWithInvalidRequestReturnBadRequest() { var request = new RequestFX() { Amount = "1.20", SourceCurrency = "GBP", TargetCurrency = "EUR" }; this.converter.Setup(c => c.ConvertCurrency(request)).Returns((ResponseFX)null); var result = this.controller.Get(request) as BadRequestResult; Assert.AreEqual(400, result.StatusCode); }
private bool ValidateRequest(RequestFX requestFx) { if (string.IsNullOrWhiteSpace(requestFx.Amount)) { return(false); } if (string.IsNullOrWhiteSpace(requestFx.SourceCurrency)) { return(false); } if (string.IsNullOrWhiteSpace(requestFx.TargetCurrency)) { return(false); } return(true); }
public void WhenCallingGetWithValidRequestReturnOK() { var request = new RequestFX() { Amount = "1.20", SourceCurrency = "GBP", TargetCurrency = "EUR" }; this.converter.Setup(c => c.ConvertCurrency(request)).Returns(this.response); var result = this.controller.Get(request) as OkObjectResult; Assert.IsNotNull(result); Assert.AreEqual(this.response, result.Value); Assert.AreEqual(200, result.StatusCode); }
public ResponseFX ConvertCurrency(RequestFX requestFx) { if (!this.ValidateRequest(requestFx)) { return(null); } var rates = this.exchangeRates.RetrieveLatestJsonRates(requestFx.SourceCurrency).Result.Rates; if (rates.ContainsKey(requestFx.TargetCurrency)) { var convertedAmount = Convert.ToDouble(requestFx.Amount) * Convert.ToDouble(rates[requestFx.TargetCurrency]); return(new ResponseFX() { TargetCurrency = requestFx.TargetCurrency, Amount = convertedAmount }); } return(null); }
public void WhenCallingCurrencyConverterWithValidParametersReturnSuccessful() { var request = new RequestFX() { Amount = "1.00", SourceCurrency = "GBP", TargetCurrency = "EUR" }; this.exchangeRates.Setup(e => e.RetrieveLatestJsonRates(It.IsAny <string>())).ReturnsAsync(this.rates); var responseFx = new ResponseFX() { Amount = 1.16, TargetCurrency = "EUR" }; var result = this.currencyConverter.ConvertCurrency(request); Assert.IsNotNull(result); Assert.AreEqual(responseFx.Amount, result.Amount); Assert.AreEqual(responseFx.TargetCurrency, result.TargetCurrency); }