public IMoeda GetCotacaoComBaseNoDolar(String SiglasDaMoeda) { IRedisConnectorHelper redisConnectorHelper = redisConnectorHelperFactory.Create(); String NomeCacheObject = "GetCotacaoComBaseNoDolar" + SiglasDaMoeda; var cacheValue = redisConnectorHelper.Get <IMoeda>(NomeCacheObject); if (cacheValue == null) { lock (GetMoedasLock) { cacheValue = redisConnectorHelper.Get <IMoeda>(NomeCacheObject); if (cacheValue == null) { HttpClient client = new HttpClient(); String baseurl = Configuration.GetSection("BASE_URL"); client.BaseAddress = new Uri(baseurl); String accesskey = Configuration.GetSection("ACCESS_KEY"); HttpResponseMessage response = client.GetAsync("live" + accesskey + "¤cies=" + SiglasDaMoeda).Result; if (response.IsSuccessStatusCode) { var retorno = JsonConvert.DeserializeXNode(response.Content.ReadAsStringAsync().Result, "Root"); var resultado = retorno.Root.Element("quotes").Elements().Select(c => moedaFactory.Create(Convert.ToString(c.Name), Convert.ToDecimal(c.Value))).First(); redisConnectorHelper.Set(NomeCacheObject, resultado, 5); return(resultado); } throw new Exception("Não foi possivel obter a cotacao da moeda desejada"); } } } return(cacheValue); }
public void TestDeConversaoDeMoedaAPIExternaComCotacaoNoCache() { IMoeda MoedaDolar = new Moeda("USD", 1); IMoeda MoedaReal = new Moeda("BRL", 3.85M); Mock <IDistributedCache> mckcache = new Mock <IDistributedCache>(); Mock <IConfigurationHelper> mckconfigurationHelper = new Mock <IConfigurationHelper>(); mckconfigurationHelper.Setup(x => x.GetSection("ACCESS_KEY")).Returns("?access_key=1503440cbd4d453ce74962abd00a82c2"); mckconfigurationHelper.Setup(x => x.GetSection("BASE_URL")).Returns("http://apilayer.net/api/"); RedisConnectorHelperFactory redisHelperFactory = new RedisConnectorHelperFactory(mckcache.Object); IRedisConnectorHelper redisHelper = redisHelperFactory.Create(); mckcache.Setup(x => x.Get("GetCotacaoComBaseNoDolarBRL")).Returns(Serialize(MoedaReal)); mckcache.Setup(x => x.Get("GetCotacaoComBaseNoDolarUSD")).Returns(Serialize(MoedaDolar)); IConversorService service = new ConversorService(new ConversorACLFactory(new MoedaFactory(), new RedisConnectorHelperFactory(mckcache.Object), mckconfigurationHelper.Object), new MoedaFactory()); ConverterMoedaRequest request = new ConverterMoedaRequest() { SiglaMoedaOrigem = "BRL", MoedaParaConversao = "USD", ValorParaConversao = 1M }; var result = service.ConverterMoeda(request); Assert.True(result != null); Assert.True(result.valor > 0 || result.valor < 0); }
public List <IMoeda> GetMoedas() { IRedisConnectorHelper redisConnectorHelper = redisConnectorHelperFactory.Create(); String NomeCacheObject = "GetMoedasList"; var cacheValue = redisConnectorHelper.Get <List <IMoeda> >(NomeCacheObject); if (cacheValue == null) { lock (GetMoedasLock) { cacheValue = redisConnectorHelper.Get <List <IMoeda> >(NomeCacheObject); if (cacheValue == null) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(Configuration.GetSection("BASE_URL")); HttpResponseMessage response = client.GetAsync("list" + Configuration.GetSection("ACCESS_KEY")).Result; if (response.IsSuccessStatusCode) { var retorno = JsonConvert.DeserializeXNode(response.Content.ReadAsStringAsync().Result, "Root"); var resultado = retorno.Root.Element("currencies").Elements().Select(c => moedaFactory.Create(c.Name.ToString(), c.Value)).ToList(); redisConnectorHelper.Set(NomeCacheObject, resultado, 120); return(resultado); } else { throw new Exception("Não foi possivel obter as moedas"); } } } } return(cacheValue); }
public void TestQueRetornaACotacaoDaMoedaDesejadaComBaseNoDolarComInformacoesEmCache() { IMoeda MoedaDolar = new Moeda("USD", 1); Mock <IDistributedCache> mckcache = new Mock <IDistributedCache>(); Mock <IConfigurationHelper> mckConfigurationHelper = new Mock <IConfigurationHelper>(); RedisConnectorHelperFactory redisHelperFactory = new RedisConnectorHelperFactory(mckcache.Object); IRedisConnectorHelper redisHelper = redisHelperFactory.Create(); mckcache.Setup(x => x.Get("GetCotacaoComBaseNoDolarUSD")).Returns(Serialize(MoedaDolar)); DistributedCacheEntryOptions distributedCacheEntryOptions = new DistributedCacheEntryOptions(); distributedCacheEntryOptions.SetAbsoluteExpiration(TimeSpan.FromMinutes(1)); mckcache.Setup(x => x.Set("GetCotacaoComBaseNoDolarUSD", Serialize(MoedaDolar), distributedCacheEntryOptions)); IConversorACL conversorACL = new ConversorACL(new MoedaFactory(), new RedisConnectorHelperFactory(mckcache.Object), mckConfigurationHelper.Object); IMoeda DolarResult = conversorACL.GetCotacaoComBaseNoDolar(MoedaDolar.SiglaMoeda); Assert.True(DolarResult.Valor.Equals(1)); IMoeda MoedaBRL = new Moeda("BRL", 3.86M); mckcache.Setup(x => x.Get("GetCotacaoComBaseNoDolarBRL")).Returns(Serialize(MoedaBRL)); mckcache.Setup(x => x.Set("GetCotacaoComBaseNoDolarBRL", Serialize(MoedaBRL), distributedCacheEntryOptions)); IMoeda RealResult = conversorACL.GetCotacaoComBaseNoDolar(MoedaBRL.SiglaMoeda); Assert.False(RealResult.Valor.Equals(DolarResult.Valor)); }