diff --git a/jobs/Backend/Task/.gitignore b/jobs/Backend/Task/.gitignore new file mode 100644 index 000000000..104b54414 --- /dev/null +++ b/jobs/Backend/Task/.gitignore @@ -0,0 +1,484 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from `dotnet new gitignore` + +# dotenv files +.env + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET +project.lock.json +project.fragment.lock.json +artifacts/ + +# Tye +.tye/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.tlog +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio 6 auto-generated project file (contains which files were open etc.) +*.vbp + +# Visual Studio 6 workspace and project file (working project files containing files to include in project) +*.dsw +*.dsp + +# Visual Studio 6 technical files +*.ncb +*.aps + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# Visual Studio History (VSHistory) files +.vshistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp + +# JetBrains Rider +*.sln.iml +.idea + +## +## Visual studio for Mac +## + + +# globs +Makefile.in +*.userprefs +*.usertasks +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.tar.gz +tarballs/ +test-results/ + +# Mac bundle stuff +*.dmg +*.app + +# content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# Vim temporary swap files +*.swp diff --git a/jobs/Backend/Task/ExchangeRateProvider.cs b/jobs/Backend/Task/ExchangeRateProvider.cs deleted file mode 100644 index 6f82a97fb..000000000 --- a/jobs/Backend/Task/ExchangeRateProvider.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - -namespace ExchangeRateUpdater -{ - public class ExchangeRateProvider - { - /// - /// Should return exchange rates among the specified currencies that are defined by the source. But only those defined - /// by the source, do not return calculated exchange rates. E.g. if the source contains "CZK/USD" but not "USD/CZK", - /// do not return exchange rate "USD/CZK" with value calculated as 1 / "CZK/USD". If the source does not provide - /// some of the currencies, ignore them. - /// - public IEnumerable GetExchangeRates(IEnumerable currencies) - { - return Enumerable.Empty(); - } - } -} diff --git a/jobs/Backend/Task/ExchangeRateUpdater.Domain/ApiExchangeRate.cs b/jobs/Backend/Task/ExchangeRateUpdater.Domain/ApiExchangeRate.cs new file mode 100644 index 000000000..d2710686d --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.Domain/ApiExchangeRate.cs @@ -0,0 +1,4 @@ +namespace ExchangeRateUpdater.Domain +{ + public record ApiExchangeRate(string CurrencyCode, decimal Rate, int Amount); +} diff --git a/jobs/Backend/Task/Currency.cs b/jobs/Backend/Task/ExchangeRateUpdater.Domain/Currency.cs similarity index 89% rename from jobs/Backend/Task/Currency.cs rename to jobs/Backend/Task/ExchangeRateUpdater.Domain/Currency.cs index f375776f2..041b2dfc7 100644 --- a/jobs/Backend/Task/Currency.cs +++ b/jobs/Backend/Task/ExchangeRateUpdater.Domain/Currency.cs @@ -1,4 +1,4 @@ -namespace ExchangeRateUpdater +namespace ExchangeRateUpdater.Domain { public class Currency { diff --git a/jobs/Backend/Task/ExchangeRate.cs b/jobs/Backend/Task/ExchangeRateUpdater.Domain/ExchangeRate.cs similarity index 93% rename from jobs/Backend/Task/ExchangeRate.cs rename to jobs/Backend/Task/ExchangeRateUpdater.Domain/ExchangeRate.cs index 58c5bb10e..9b748b70f 100644 --- a/jobs/Backend/Task/ExchangeRate.cs +++ b/jobs/Backend/Task/ExchangeRateUpdater.Domain/ExchangeRate.cs @@ -1,4 +1,4 @@ -namespace ExchangeRateUpdater +namespace ExchangeRateUpdater.Domain { public class ExchangeRate { diff --git a/jobs/Backend/Task/ExchangeRateUpdater.Domain/ExchangeRateUpdater.Domain.csproj b/jobs/Backend/Task/ExchangeRateUpdater.Domain/ExchangeRateUpdater.Domain.csproj new file mode 100644 index 000000000..fa71b7ae6 --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.Domain/ExchangeRateUpdater.Domain.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/jobs/Backend/Task/ExchangeRateUpdater.Domain/IExchangeRateApiClient.cs b/jobs/Backend/Task/ExchangeRateUpdater.Domain/IExchangeRateApiClient.cs new file mode 100644 index 000000000..ca5d1602d --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.Domain/IExchangeRateApiClient.cs @@ -0,0 +1,9 @@ +namespace ExchangeRateUpdater.Domain +{ + public interface IExchangeRateApiClient + { + Currency TargetCurrency { get; } + + Task> GetDailyExchangeRatesAsync(LanguageCode languageCode = LanguageCode.EN); + } +} diff --git a/jobs/Backend/Task/ExchangeRateUpdater.Domain/IExchangeRateApiClientFactory.cs b/jobs/Backend/Task/ExchangeRateUpdater.Domain/IExchangeRateApiClientFactory.cs new file mode 100644 index 000000000..6afd9ed1f --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.Domain/IExchangeRateApiClientFactory.cs @@ -0,0 +1,7 @@ +namespace ExchangeRateUpdater.Domain +{ + public interface IExchangeRateApiClientFactory + { + IExchangeRateApiClient CreateExchangeRateApiClient(string currencyCode); + } +} diff --git a/jobs/Backend/Task/ExchangeRateUpdater.Domain/IExchangeRateProvider.cs b/jobs/Backend/Task/ExchangeRateUpdater.Domain/IExchangeRateProvider.cs new file mode 100644 index 000000000..d74da657c --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.Domain/IExchangeRateProvider.cs @@ -0,0 +1,8 @@ +namespace ExchangeRateUpdater.Domain +{ + public interface IExchangeRateProvider + { + Task> GetExchangeRatesAsync( + IEnumerable currencies, string targetCurrencyCode); + } +} diff --git a/jobs/Backend/Task/ExchangeRateUpdater.Domain/LanguageCode.cs b/jobs/Backend/Task/ExchangeRateUpdater.Domain/LanguageCode.cs new file mode 100644 index 000000000..9afc6a9ff --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.Domain/LanguageCode.cs @@ -0,0 +1,8 @@ +namespace ExchangeRateUpdater.Domain +{ + public enum LanguageCode + { + EN, + CZ + } +} diff --git a/jobs/Backend/Task/ExchangeRateUpdater.Domain/WellKnownCurrencyCodes.cs b/jobs/Backend/Task/ExchangeRateUpdater.Domain/WellKnownCurrencyCodes.cs new file mode 100644 index 000000000..6d78f3b6a --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.Domain/WellKnownCurrencyCodes.cs @@ -0,0 +1,7 @@ +namespace ExchangeRateUpdater.Domain +{ + public static class WellKnownCurrencyCodes + { + public const string CZK = "CZK"; + } +} diff --git a/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/CzechNationalBank/CzechNationalBankExchangeRateApiClient.cs b/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/CzechNationalBank/CzechNationalBankExchangeRateApiClient.cs new file mode 100644 index 000000000..aa17cb6fe --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/CzechNationalBank/CzechNationalBankExchangeRateApiClient.cs @@ -0,0 +1,28 @@ +using ExchangeRateUpdater.Domain; +using Microsoft.Extensions.Logging; +using System.Net.Http.Json; + +namespace ExchangeRateUpdater.Infrastructure.CzechNationalBank +{ + public class CzechNationalBankExchangeRateApiClient( + HttpClient httpClient, + ILogger logger) : IExchangeRateApiClient + { + public Currency TargetCurrency => new(WellKnownCurrencyCodes.CZK); + + public async Task> GetDailyExchangeRatesAsync(LanguageCode languageCode = LanguageCode.EN) + { + try + { + var response = await httpClient.GetFromJsonAsync($"exrates/daily?lang={languageCode}"); + + return response.Rates.Select(x => new ApiExchangeRate(x.CurrencyCode, x.Rate, x.Amount)).ToArray(); + } + catch(Exception e) + { + logger.LogError(e, "Failed to get daily exchange rates from Czech National Bank API"); + throw; + } + } + } +} diff --git a/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/CzechNationalBank/CzechNationalBankExchangeRatesResponse.cs b/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/CzechNationalBank/CzechNationalBankExchangeRatesResponse.cs new file mode 100644 index 000000000..2eb4a6679 --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/CzechNationalBank/CzechNationalBankExchangeRatesResponse.cs @@ -0,0 +1,22 @@ +using System.Text.Json.Serialization; + +namespace ExchangeRateUpdater.Infrastructure.CzechNationalBank +{ + internal class CzechNationalBankExchangeRatesResponse + { + [JsonPropertyName("rates")] + public ExchangeRate[] Rates { get; init; } = []; + + internal class ExchangeRate + { + [JsonPropertyName("amount")] + public int Amount { get; init; } + + [JsonPropertyName("currencyCode")] + public string CurrencyCode { get; init; } = default!; + + [JsonPropertyName("rate")] + public decimal Rate { get; init; } + } + } +} diff --git a/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/ExchangeRateApiClientFactory.cs b/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/ExchangeRateApiClientFactory.cs new file mode 100644 index 000000000..702857e1b --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/ExchangeRateApiClientFactory.cs @@ -0,0 +1,23 @@ +using ExchangeRateUpdater.Domain; +using ExchangeRateUpdater.Infrastructure.CzechNationalBank; +using Microsoft.Extensions.Logging; + +namespace ExchangeRateUpdater.Infrastructure +{ + public class ExchangeRateApiClientFactory( + IHttpClientFactory httpClientFactory, + ILoggerFactory loggerFactory) : IExchangeRateApiClientFactory + { + public IExchangeRateApiClient CreateExchangeRateApiClient(string currencyCode) + { + return currencyCode switch + { + WellKnownCurrencyCodes.CZK => new CzechNationalBankExchangeRateApiClient( + httpClientFactory.CreateClient(HttpClientNames.CzechNationalBankApi), + loggerFactory.CreateLogger()), + + _ => throw new NotImplementedException($"Exchange rate API client not implemented for currency {currencyCode}"), + }; + } + } +} diff --git a/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/ExchangeRateUpdater.Infrastructure.csproj b/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/ExchangeRateUpdater.Infrastructure.csproj new file mode 100644 index 000000000..d0f0f4f1c --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/ExchangeRateUpdater.Infrastructure.csproj @@ -0,0 +1,18 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + diff --git a/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/HttpClientNames.cs b/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/HttpClientNames.cs new file mode 100644 index 000000000..6c1e95395 --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.Infrastructure/HttpClientNames.cs @@ -0,0 +1,7 @@ +namespace ExchangeRateUpdater.Infrastructure +{ + public static class HttpClientNames + { + public const string CzechNationalBankApi = nameof(CzechNationalBankApi); + } +} diff --git a/jobs/Backend/Task/ExchangeRateUpdater.UnitTests/ExchangeRateApiClientFactoryTests.cs b/jobs/Backend/Task/ExchangeRateUpdater.UnitTests/ExchangeRateApiClientFactoryTests.cs new file mode 100644 index 000000000..60a1f87f9 --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.UnitTests/ExchangeRateApiClientFactoryTests.cs @@ -0,0 +1,50 @@ +using ExchangeRateUpdater.Domain; +using ExchangeRateUpdater.Infrastructure; +using ExchangeRateUpdater.Infrastructure.CzechNationalBank; +using Microsoft.Extensions.Logging; +using Moq; + +namespace ExchangeRateUpdater.UnitTests +{ + public class ExchangeRateApiClientFactoryTests + { + [Fact] + public void CreateExchangeRateApiClient_ReturnsCorrectExchangeRateApiClient() + { + // Arrange + var factory = CreateExchangeRateApiClientFactory(); + + // Act + var exchangeRateApiClient = factory.CreateExchangeRateApiClient(WellKnownCurrencyCodes.CZK); + + // Assert + Assert.NotNull(exchangeRateApiClient); + Assert.IsType(exchangeRateApiClient); + } + + [Fact] + public void CreateExchangeRateApiClient_IfCurrencyCodeUnknown_ThrowsException() + { + // Arrange + var factory = CreateExchangeRateApiClientFactory(); + + // Act + var exception = Assert.Throws( + () => factory.CreateExchangeRateApiClient("INVALID")); + + // Assert + Assert.Equal("Exchange rate API client not implemented for currency INVALID", exception.Message); + } + + private static ExchangeRateApiClientFactory CreateExchangeRateApiClientFactory() + { + var httpClientFactoryMock = new Mock(); + httpClientFactoryMock.Setup(x => x.CreateClient(It.IsAny())) + .Returns(null); + + var loggerFactoryMock = new Mock(); + + return new(httpClientFactoryMock.Object, loggerFactoryMock.Object); + } + } +} diff --git a/jobs/Backend/Task/ExchangeRateUpdater.UnitTests/ExchangeRateProviderTests.cs b/jobs/Backend/Task/ExchangeRateUpdater.UnitTests/ExchangeRateProviderTests.cs new file mode 100644 index 000000000..0d00a5854 --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.UnitTests/ExchangeRateProviderTests.cs @@ -0,0 +1,151 @@ +using Moq; +using ExchangeRateUpdater.Domain; + +namespace ExchangeRateUpdater.UnitTests +{ + public class ExchangeRateProviderTests + { + [Fact] + public async Task GetExchangeRatesAsync_IgnoresCurrenciesNotProvidedByApi() + { + // Arrange + var targetCurrencyCode = "CZK"; + var requestedCurrencies = new Currency[] + { + new("USD"), new("EUR"), new("RUB") + }; + var exchangeRatesProvidedByApi = new ApiExchangeRate[] + { + new("USD", 1.2m, 1), new("EUR", 1.3m, 1) + }; + var apiClientFactoryMock = CreateExchangeApiClientFactoryMock(exchangeRatesProvidedByApi, targetCurrencyCode); + + var exchangeRateProvider = new ExchangeRateProvider(apiClientFactoryMock.Object); + + // Act + var actualExchangeRates = await exchangeRateProvider.GetExchangeRatesAsync(requestedCurrencies, targetCurrencyCode); + + // Assert + Assert.Equal(2, actualExchangeRates.Count); + + var actualSourceCurrencyCodes = actualExchangeRates.Select(x => x.SourceCurrency.Code).ToHashSet(); + var expectedSourceCurrencyCodes = new[] { "USD", "EUR" }.ToHashSet(); + Assert.Equal(expectedSourceCurrencyCodes, expectedSourceCurrencyCodes); + } + + [Fact] + public async Task GetExchangeRatesAsync_WhenCurrenciesEmpty_DoesNotCallExchangeRateApi() + { + // Arrange + var targetCurrencyCode = "CZK"; + var requestedCurrencies = Array.Empty(); + var apiClientFactoryMock = CreateExchangeApiClientFactoryMock([], targetCurrencyCode); + + var exchangeRateProvider = new ExchangeRateProvider(apiClientFactoryMock.Object); + + // Act + var actualExchangeRates = await exchangeRateProvider.GetExchangeRatesAsync(requestedCurrencies, targetCurrencyCode); + + // Assert + Assert.Empty(actualExchangeRates); + apiClientFactoryMock.Verify(x => x.CreateExchangeRateApiClient(It.IsAny()), Times.Never); + } + + [Fact] + public async Task GetExchangeRatesAsync_ReturnsDataOnlyForRequestedCurrencies() + { + // Arrange + var targetCurrencyCode = "CZK"; + var requestedCurrencies = new Currency[] + { + new("USD"), new("EUR"), new("RUB") + }; + var exchangeRatesProvidedByApi = new ApiExchangeRate[] + { + new("USD", 1.2m, 1), new("EUR", 1.3m, 1), new("KES", 1.2m, 1), new("RUB", 1.3m, 1), new("HBC", 1.2m, 1) + }; + var apiClientFactoryMock = CreateExchangeApiClientFactoryMock(exchangeRatesProvidedByApi, targetCurrencyCode); + + var exchangeRateProvider = new ExchangeRateProvider(apiClientFactoryMock.Object); + + // Act + var actualExchangeRates = await exchangeRateProvider.GetExchangeRatesAsync(requestedCurrencies, targetCurrencyCode); + + // Assert + Assert.Equal(3, actualExchangeRates.Count); + + var actualSourceCurrencyCodes = actualExchangeRates.Select(x => x.SourceCurrency.Code).ToHashSet(); + var expectedSourceCurrencyCodes = new[] { "USD", "EUR", "RUB" }.ToHashSet(); + Assert.Equal(expectedSourceCurrencyCodes, expectedSourceCurrencyCodes); + } + + [Fact] + public async Task GetExchangeRatesAsync_ReturnsCorrectTargetCurrency() + { + // Arrange + var targetCurrencyCode = "CZK"; + var requestedCurrencies = new Currency[] + { + new("USD"), new("EUR"), new("RUB") + }; + var exchangeRatesProvidedByApi = new ApiExchangeRate[] + { + new("USD", 1.2m, 1), new("EUR", 1.3m, 1), new("RUB", 1.3m, 1) + }; + var apiClientFactoryMock = CreateExchangeApiClientFactoryMock(exchangeRatesProvidedByApi, targetCurrencyCode); + + var exchangeRateProvider = new ExchangeRateProvider(apiClientFactoryMock.Object); + + // Act + var actualExchangeRates = await exchangeRateProvider.GetExchangeRatesAsync(requestedCurrencies, targetCurrencyCode); + + // Assert + Assert.NotEmpty(actualExchangeRates); + Assert.All(actualExchangeRates, rate => Assert.Equal(targetCurrencyCode, rate.TargetCurrency.Code)); + } + + [Fact] + public async Task GetExchangeRatesAsync_ReturnsCorrectRateValues() + { + // Arrange + var targetCurrencyCode = "CZK"; + var requestedCurrencies = new Currency[] + { + new("USD"), new("EUR"), new("RUB") + }; + var exchangeRatesProvidedByApi = new ApiExchangeRate[] + { + new("USD", 0.2m, 1), new("EUR", 1.8m, 10), new("RUB", 5.8m, 100) + }; + var apiClientFactoryMock = CreateExchangeApiClientFactoryMock(exchangeRatesProvidedByApi, targetCurrencyCode); + + var exchangeRateProvider = new ExchangeRateProvider(apiClientFactoryMock.Object); + + // Act + var actualExchangeRates = await exchangeRateProvider.GetExchangeRatesAsync(requestedCurrencies, targetCurrencyCode); + + // Assert + Assert.Equal(3, actualExchangeRates.Count); + Assert.All(exchangeRatesProvidedByApi, apiRate => + { + var expectedValue = apiRate.Rate / apiRate.Amount; + var actualValue = actualExchangeRates.FirstOrDefault(x => x.SourceCurrency.Code == apiRate.CurrencyCode)?.Value; + Assert.Equal(expectedValue, actualValue); + }); + } + + private static Mock CreateExchangeApiClientFactoryMock( + ApiExchangeRate[] exchangeRates, string targetCurrencyCode) + { + var exchangeRateApiClientMock = new Mock(); + exchangeRateApiClientMock.Setup(x => x.GetDailyExchangeRatesAsync(It.IsAny())) + .ReturnsAsync(exchangeRates); + + var apiClientFactoryMock = new Mock(); + apiClientFactoryMock.Setup(x => x.CreateExchangeRateApiClient(targetCurrencyCode)) + .Returns(exchangeRateApiClientMock.Object); + + return apiClientFactoryMock; + } + } +} \ No newline at end of file diff --git a/jobs/Backend/Task/ExchangeRateUpdater.UnitTests/ExchangeRateUpdater.UnitTests.csproj b/jobs/Backend/Task/ExchangeRateUpdater.UnitTests/ExchangeRateUpdater.UnitTests.csproj new file mode 100644 index 000000000..16fe2ff5d --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater.UnitTests/ExchangeRateUpdater.UnitTests.csproj @@ -0,0 +1,30 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + + + diff --git a/jobs/Backend/Task/ExchangeRateUpdater.csproj b/jobs/Backend/Task/ExchangeRateUpdater.csproj deleted file mode 100644 index 2fc654a12..000000000 --- a/jobs/Backend/Task/ExchangeRateUpdater.csproj +++ /dev/null @@ -1,8 +0,0 @@ - - - - Exe - net6.0 - - - \ No newline at end of file diff --git a/jobs/Backend/Task/ExchangeRateUpdater.sln b/jobs/Backend/Task/ExchangeRateUpdater.sln index 89be84daf..7d2cf926c 100644 --- a/jobs/Backend/Task/ExchangeRateUpdater.sln +++ b/jobs/Backend/Task/ExchangeRateUpdater.sln @@ -1,9 +1,15 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 14 -VisualStudioVersion = 14.0.25123.0 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35312.102 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExchangeRateUpdater", "ExchangeRateUpdater.csproj", "{7B2695D6-D24C-4460-A58E-A10F08550CE0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExchangeRateUpdater", "ExchangeRateUpdater\ExchangeRateUpdater.csproj", "{3A89D542-86FB-4F4B-B1FC-BE3E718ED7F0}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExchangeRateUpdater.Infrastructure", "ExchangeRateUpdater.Infrastructure\ExchangeRateUpdater.Infrastructure.csproj", "{FF665A8A-D3B6-4297-82A0-210955DC3879}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExchangeRateUpdater.UnitTests", "ExchangeRateUpdater.UnitTests\ExchangeRateUpdater.UnitTests.csproj", "{D7071F95-6A8D-4A90-9F6D-0A071D4E5741}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExchangeRateUpdater.Domain", "ExchangeRateUpdater.Domain\ExchangeRateUpdater.Domain.csproj", "{119DA11C-8429-4FEB-AEDA-3C577DE5E7A6}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -11,12 +17,27 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {7B2695D6-D24C-4460-A58E-A10F08550CE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7B2695D6-D24C-4460-A58E-A10F08550CE0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7B2695D6-D24C-4460-A58E-A10F08550CE0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7B2695D6-D24C-4460-A58E-A10F08550CE0}.Release|Any CPU.Build.0 = Release|Any CPU + {3A89D542-86FB-4F4B-B1FC-BE3E718ED7F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3A89D542-86FB-4F4B-B1FC-BE3E718ED7F0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3A89D542-86FB-4F4B-B1FC-BE3E718ED7F0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3A89D542-86FB-4F4B-B1FC-BE3E718ED7F0}.Release|Any CPU.Build.0 = Release|Any CPU + {FF665A8A-D3B6-4297-82A0-210955DC3879}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FF665A8A-D3B6-4297-82A0-210955DC3879}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FF665A8A-D3B6-4297-82A0-210955DC3879}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FF665A8A-D3B6-4297-82A0-210955DC3879}.Release|Any CPU.Build.0 = Release|Any CPU + {D7071F95-6A8D-4A90-9F6D-0A071D4E5741}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D7071F95-6A8D-4A90-9F6D-0A071D4E5741}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D7071F95-6A8D-4A90-9F6D-0A071D4E5741}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D7071F95-6A8D-4A90-9F6D-0A071D4E5741}.Release|Any CPU.Build.0 = Release|Any CPU + {119DA11C-8429-4FEB-AEDA-3C577DE5E7A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {119DA11C-8429-4FEB-AEDA-3C577DE5E7A6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {119DA11C-8429-4FEB-AEDA-3C577DE5E7A6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {119DA11C-8429-4FEB-AEDA-3C577DE5E7A6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C64F8D62-A166-4DDA-9840-BA3891BB8958} + EndGlobalSection EndGlobal diff --git a/jobs/Backend/Task/ExchangeRateUpdater/ExchangeRateProvider.cs b/jobs/Backend/Task/ExchangeRateUpdater/ExchangeRateProvider.cs new file mode 100644 index 000000000..87159afcb --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater/ExchangeRateProvider.cs @@ -0,0 +1,45 @@ +using ExchangeRateUpdater.Domain; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace ExchangeRateUpdater +{ + public class ExchangeRateProvider(IExchangeRateApiClientFactory exchangeRateApiClientFactory) : IExchangeRateProvider + { + /// + /// Should return exchange rates among the specified currencies that are defined by the source. But only those defined + /// by the source, do not return calculated exchange rates. E.g. if the source contains "CZK/USD" but not "USD/CZK", + /// do not return exchange rate "USD/CZK" with value calculated as 1 / "CZK/USD". If the source does not provide + /// some of the currencies, ignore them. + /// + public async Task> GetExchangeRatesAsync(IEnumerable currencies, string targetCurrencyCode) + { + if (!currencies.Any()) + { + return []; + } + + var exchangeRateApiClient = exchangeRateApiClientFactory.CreateExchangeRateApiClient(targetCurrencyCode); + + var apiExchangeRates = await exchangeRateApiClient.GetDailyExchangeRatesAsync(); + var apiExchangeRatesByCode = apiExchangeRates.ToDictionary(x => x.CurrencyCode); + + var targetCurreny = exchangeRateApiClient.TargetCurrency; + + return currencies + .DistinctBy(c => c.Code) + .Select(c => + { + var apiExchangeRate = apiExchangeRatesByCode.GetValueOrDefault(c.Code); + var value = apiExchangeRate == null + ? default + : apiExchangeRate.Rate / apiExchangeRate.Amount; + + return new ExchangeRate(c, targetCurreny, value); + }) + .Where(x => x.Value != default) + .ToArray(); + } + } +} diff --git a/jobs/Backend/Task/ExchangeRateUpdater/ExchangeRateUpdater.csproj b/jobs/Backend/Task/ExchangeRateUpdater/ExchangeRateUpdater.csproj new file mode 100644 index 000000000..936229909 --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater/ExchangeRateUpdater.csproj @@ -0,0 +1,30 @@ + + + + Exe + net8.0 + + + + + + + + + + + + + + + + + + + + + Always + + + + \ No newline at end of file diff --git a/jobs/Backend/Task/ExchangeRateUpdater/Extensions/ServiceCollectionExtensions.cs b/jobs/Backend/Task/ExchangeRateUpdater/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 000000000..ef0613163 --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,18 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Serilog; + +namespace ExchangeRateUpdater.Extensions +{ + internal static class ServiceCollectionExtensions + { + public static void AddSerilogLogging(this IServiceCollection services, IConfiguration configuration) + { + Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .CreateLogger(); + + services.AddSerilog(); + } + } +} diff --git a/jobs/Backend/Task/ExchangeRateUpdater/Program.cs b/jobs/Backend/Task/ExchangeRateUpdater/Program.cs new file mode 100644 index 000000000..998af2f75 --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater/Program.cs @@ -0,0 +1,78 @@ +using ExchangeRateUpdater.Domain; +using ExchangeRateUpdater.Extensions; +using ExchangeRateUpdater.Infrastructure; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Polly; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace ExchangeRateUpdater +{ + public static class Program + { + private static IEnumerable currencies = new[] + { + new Currency("USD"), + new Currency("EUR"), + new Currency("CZK"), + new Currency("JPY"), + new Currency("KES"), + new Currency("RUB"), + new Currency("THB"), + new Currency("TRY"), + new Currency("XYZ") + }; + + public static async Task Main(string[] args) + { + var builder = Host.CreateApplicationBuilder(args); + + builder.Services.AddSerilogLogging(builder.Configuration); + + builder.Services.AddHttpClient(HttpClientNames.CzechNationalBankApi, httpClient => + { + var baseUrl = builder.Configuration.GetValue("CzechNationalBankApiBaseUrl"); + + httpClient.BaseAddress = new Uri(baseUrl); + }) + .AddTransientHttpErrorPolicy(policyBuilder => + { + return policyBuilder.WaitAndRetryAsync(6, + retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); + }); + + builder.Services.AddTransient() + .AddTransient(); + + var app = builder.Build(); + + await RunTest(app); + } + + private static async Task RunTest(IHost app) + { + try + { + using var scope = app.Services.CreateScope(); + var provider = scope.ServiceProvider.GetRequiredService(); + var rates = await provider.GetExchangeRatesAsync(currencies, WellKnownCurrencyCodes.CZK); + + Console.WriteLine($"Successfully retrieved {rates.Count()} exchange rates:"); + foreach (var rate in rates) + { + Console.WriteLine(rate.ToString()); + } + } + catch (Exception e) + { + Console.WriteLine($"Could not retrieve exchange rates: '{e.Message}'."); + } + + Console.ReadLine(); + } + } +} diff --git a/jobs/Backend/Task/ExchangeRateUpdater/appsettings.json b/jobs/Backend/Task/ExchangeRateUpdater/appsettings.json new file mode 100644 index 000000000..7847bed51 --- /dev/null +++ b/jobs/Backend/Task/ExchangeRateUpdater/appsettings.json @@ -0,0 +1,10 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.Console" ], + "MinimumLevel": "Information", + "WriteTo": [ + { "Name": "Console" } + ] + }, + "CzechNationalBankApiBaseUrl": "https://api.cnb.cz/cnbapi/" +} \ No newline at end of file diff --git a/jobs/Backend/Task/Program.cs b/jobs/Backend/Task/Program.cs deleted file mode 100644 index 379a69b1f..000000000 --- a/jobs/Backend/Task/Program.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace ExchangeRateUpdater -{ - public static class Program - { - private static IEnumerable currencies = new[] - { - new Currency("USD"), - new Currency("EUR"), - new Currency("CZK"), - new Currency("JPY"), - new Currency("KES"), - new Currency("RUB"), - new Currency("THB"), - new Currency("TRY"), - new Currency("XYZ") - }; - - public static void Main(string[] args) - { - try - { - var provider = new ExchangeRateProvider(); - var rates = provider.GetExchangeRates(currencies); - - Console.WriteLine($"Successfully retrieved {rates.Count()} exchange rates:"); - foreach (var rate in rates) - { - Console.WriteLine(rate.ToString()); - } - } - catch (Exception e) - { - Console.WriteLine($"Could not retrieve exchange rates: '{e.Message}'."); - } - - Console.ReadLine(); - } - } -}