Skip to content
Permalink
Browse files

mocking request

  • Loading branch information...
ignatandrei committed Oct 31, 2019
1 parent 655dcea commit d1e3eda3ca27f2614e6965500bf3e7ed28902f21
@@ -11,36 +11,45 @@ namespace InfoValutarShared
{
public class GetNBRExchange
{
private static readonly HttpClient httpClient = new HttpClient();
private readonly HttpClient httpClient;

public GetNBRExchange(HttpMessageHandler handler = null)
{
if (handler != null)
httpClient = new HttpClient(handler, disposeHandler: false);
else
httpClient = new HttpClient();
}
public async IAsyncEnumerable<ExchangeRates> GetActualRates()
{
var xml = await httpClient.GetStringAsync("https://www.bnr.ro/nbrfxrates.xml");
//Console.WriteLine($"{xml}");
var serializer = new XmlSerializer(typeof(DataSet));
DataSet result;
using (var reader = new StringReader(xml))
{
var result = serializer.Deserialize(reader) as DataSet;
var val = result.Body.Cube.First();
var date = val.date;
string orig = "BNR";
foreach (var item in val.Rate)
result = serializer.Deserialize(reader) as DataSet;
}
var val = result.Body.Cube.First();
var date = val.date;
string orig = "BNR";
foreach (var item in val.Rate)
{
var exch = new ExchangeRates();
exch.Bank = orig;
exch.date = date;
exch.ExchangeTo = "RON";
exch.ExchangeFrom = item.currency;
exch.ExchangeValue = item.Value;
if (!string.IsNullOrWhiteSpace(item.multiplier))
{
var exch = new ExchangeRates();
exch.Bank = orig;
exch.date = date;
exch.ExchangeTo = "RON";
exch.ExchangeFrom = item.currency;
exch.ExchangeValue = item.Value;
if (!string.IsNullOrWhiteSpace(item.multiplier))
{
exch.ExchangeValue = exch.ExchangeValue / (decimal)int.Parse(item.multiplier);
}
yield return exch;
exch.ExchangeValue = exch.ExchangeValue / (decimal)int.Parse(item.multiplier);
}
yield return exch;
}


}
}
}
}

@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://www.bnr.ro/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bnr.ro/xsd nbrfxrates.xsd">
<Header>
<Publisher>National Bank of Romania</Publisher>
<PublishingDate>2019-10-30</PublishingDate>
<MessageType>DR</MessageType>
</Header>
<Body>
<Subject>Reference rates</Subject>
<OrigCurrency>RON</OrigCurrency>
<Cube date="2019-10-30">
<Rate currency="AED">1.1644</Rate>
<Rate currency="AUD">2.9388</Rate>
<Rate currency="BGN">2.4323</Rate>
<Rate currency="BRL">1.0696</Rate>
<Rate currency="CAD">3.2706</Rate>
<Rate currency="CHF">4.3120</Rate>
<Rate currency="CNY">0.6060</Rate>
<Rate currency="CZK">0.1865</Rate>
<Rate currency="DKK">0.6368</Rate>
<Rate currency="EGP">0.2646</Rate>
<Rate currency="EUR">4.7572</Rate>
<Rate currency="GBP">5.5130</Rate>
<Rate currency="HRK">0.6373</Rate>
<Rate currency="HUF" multiplier="100">1.4426</Rate>
<Rate currency="INR">0.0603</Rate>
<Rate currency="JPY" multiplier="100">3.9293</Rate>
<Rate currency="KRW" multiplier="100">0.3666</Rate>
<Rate currency="MDL">0.2448</Rate>
<Rate currency="MXN">0.2239</Rate>
<Rate currency="NOK">0.4638</Rate>
<Rate currency="NZD">2.7196</Rate>
<Rate currency="PLN">1.1155</Rate>
<Rate currency="RSD">0.0405</Rate>
<Rate currency="RUB">0.0670</Rate>
<Rate currency="SEK">0.4403</Rate>
<Rate currency="THB">0.1415</Rate>
<Rate currency="TRY">0.7444</Rate>
<Rate currency="UAH">0.1711</Rate>
<Rate currency="USD">4.2765</Rate>
<Rate currency="XAU">205.3068</Rate>
<Rate currency="XDR">5.8888</Rate>
<Rate currency="ZAR">0.2923</Rate>
</Cube>
</Body>
</DataSet>
@@ -1,13 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<None Remove="Data\20191020bnr.txt" />
</ItemGroup>

<ItemGroup>
<Content Include="Data\20191020bnr.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="RichardSzalay.MockHttp" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
@@ -1,5 +1,7 @@
using InfoValutarShared;
using RichardSzalay.MockHttp;
using System;
using System.IO;
using System.Threading.Tasks;
using Xunit;

@@ -21,5 +23,24 @@ await foreach(var e in nbr.GetActualRates())
}
Assert.True(false, "Should find EUR");
}
[Fact]
public async Task TestParsing()
{
var response = await File.ReadAllTextAsync(Path.Combine("data", "20191020bnr.txt"));
var m = new MockHttpMessageHandler();
m.When("https://www.bnr.ro/nbrfxrates.xml")
.Respond("application/text", response);

var nbr = new GetNBRExchange(m);
await foreach (var e in nbr.GetActualRates())
{
if (e.ExchangeFrom == "EUR")
{
Assert.Equal(4.7572m, e.ExchangeValue,4);
return;
}
}
Assert.True(false, "Should find EUR");
}
}
}

0 comments on commit d1e3eda

Please sign in to comment.
You can’t perform that action at this time.