diff --git a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp index 7f5f0127..6d2d7301 100644 --- a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp +++ b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp @@ -127,6 +127,7 @@ namespace namespace CalculatorApp::ViewModel::DataLoaders { + bool CurrencyHttpClient::ForceWebFailure = false; void CurrencyHttpClient::Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage) { m_sourceCurrencyCode = sourceCurrencyCode; diff --git a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h index 05d58a60..79d042e3 100644 --- a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h +++ b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h @@ -8,6 +8,7 @@ namespace CalculatorApp::ViewModel::DataLoaders class CurrencyHttpClient { public: + static bool ForceWebFailure; void Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage); Platform::String ^ GetCurrencyMetadata() const; diff --git a/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj b/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj index 9c06b2c7..842c1279 100644 --- a/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj +++ b/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj @@ -319,7 +319,6 @@ - @@ -377,7 +376,7 @@ - + diff --git a/src/CalcViewModelCopyForUT/CurrencyHttpClient.cpp b/src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp similarity index 75% rename from src/CalcViewModelCopyForUT/CurrencyHttpClient.cpp rename to src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp index 988354e2..3d62330b 100644 --- a/src/CalcViewModelCopyForUT/CurrencyHttpClient.cpp +++ b/src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp @@ -2,7 +2,7 @@ // Licensed under the MIT License. #include "pch.h" -#include "CalcViewModel/DataLoaders/CurrencyHttpClient.h" +#include "DataLoaders/CurrencyHttpClient.h" namespace { @@ -14,6 +14,7 @@ namespace namespace CalculatorApp::ViewModel::DataLoaders { + bool CurrencyHttpClient::ForceWebFailure = false; void CurrencyHttpClient::Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage) { m_sourceCurrencyCode = sourceCurrencyCode; @@ -22,12 +23,20 @@ namespace CalculatorApp::ViewModel::DataLoaders Platform::String ^ CurrencyHttpClient::GetCurrencyMetadata() const { + if (ForceWebFailure) + { + throw ref new Platform::Exception(E_FAIL, L"Mocked Network Failure: failed to load currency metadata"); + } (void)m_responseLanguage; // to be used in production. return ref new Platform::String(MockCurrencyStaticData); } Platform::String ^ CurrencyHttpClient::GetCurrencyRatios() const { + if (ForceWebFailure) + { + throw ref new Platform::Exception(E_FAIL, L"Mocked Network Failure: failed to load currency metadata"); + } (void)m_sourceCurrencyCode; // to be used in production. return ref new Platform::String(MockCurrencyConverterData); } diff --git a/src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp b/src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp index c4ac80af..cf94ec50 100644 --- a/src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp +++ b/src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp @@ -52,6 +52,27 @@ private: namespace CalculatorUnitTests { + namespace + { + template + auto ScopeGuard(F&& f) + { + struct ScopeExit + { + explicit ScopeExit(F&& ef) + : ExitFunctor(std::forward(ef)) + { + } + ~ScopeExit() + { + ExitFunctor(); + } + F ExitFunctor; + }; + return ScopeExit{ std::forward(f) }; + } + } + constexpr auto sc_Language_EN = L"en-US"; String^ SerializeContent(const std::vector& data) @@ -165,6 +186,8 @@ TEST_METHOD(LoadFromCache_Fail_OlderThanADay) dayOld.UniversalTime = now.UniversalTime - CurrencyDataLoaderConstants::DayDuration - 1; InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, dayOld); + auto guard = ScopeGuard([] { CurrencyHttpClient::ForceWebFailure = false; }); + CurrencyHttpClient::ForceWebFailure = true; CurrencyDataLoader loader{ L"en-US" }; bool didLoad = loader.TryLoadDataFromCacheAsync().get(); @@ -245,19 +268,10 @@ TEST_METHOD(LoadFromCache_Success) VERIFY_IS_TRUE(loader.LoadedFromCache()); } -TEST_METHOD(LoadFromWeb_Fail_ClientIsNullptr) -{ - CurrencyDataLoader loader{ L"en-US" }; - - bool didLoad = loader.TryLoadDataFromWebAsync().get(); - - VERIFY_IS_FALSE(didLoad); - VERIFY_IS_FALSE(loader.LoadFinished()); - VERIFY_IS_FALSE(loader.LoadedFromWeb()); -} - TEST_METHOD(LoadFromWeb_Fail_WebException) { + auto guard = ScopeGuard([] { CurrencyHttpClient::ForceWebFailure = false; }); + CurrencyHttpClient::ForceWebFailure = true; CurrencyDataLoader loader{ L"en-US" }; bool didLoad = loader.TryLoadDataFromWebAsync().get();