diff --git a/WotBlitzStatisticsPro.Tests/OperationStepsTests/GetAccountInfoOperationTest.cs b/WotBlitzStatisticsPro.Tests/OperationStepsTests/GetAccountInfoOperationTest.cs index 35b9805..6f79313 100644 --- a/WotBlitzStatisticsPro.Tests/OperationStepsTests/GetAccountInfoOperationTest.cs +++ b/WotBlitzStatisticsPro.Tests/OperationStepsTests/GetAccountInfoOperationTest.cs @@ -1,8 +1,11 @@ using System.IO; +using System.Net.Http; +using System.Threading; using System.Threading.Tasks; using FluentAssertions; using Microsoft.Extensions.Logging; using Moq; +using Moq.Protected; using Newtonsoft.Json; using NUnit.Framework; using WotBlitzStatisticsPro.Common.Model; @@ -56,20 +59,32 @@ namespace WotBlitzStatisticsPro.Tests.OperationStepsTests await File.ReadAllTextAsync(GetFixturePath("MappedAccountHistory.json")); accountInfoHistorySerialized.Should().Be(expectedAccountInfoHistory); - // ToDo: Check cache - Assert.Fail("Implement test"); - } - - [Test] - public async Task ShouldGetAndSaveAccountInfoInformationIfCacheExpired() - { - Assert.Fail("Implement test"); + var accountCache = _cache.GetAccountData(AccountId); + accountCache.Should().NotBeNull(); + accountCache?.AccountInfo.Should().NotBeNull(); + accountCache?.AccountInfoHistory.Should().NotBeNull(); } [Test] public async Task ShouldReadDataFromCacheIfItIsNotEmpty() { - Assert.Fail("Implement test"); + var accountInfo = GetAccountInfoFromFixture(); + var accountInfoHistory = GetAccountInfoHistoryFromFixture(); + _cache.SetAccountData(AccountId, new AccountDataCache(accountInfo, accountInfoHistory)); + + var context = new OperationContext(new AccountRequest(AccountId, Realm, Language)); + context.AddOrReplace(_contextData); + + await _operation.Invoke(context, null); + + _contextData.AccountInfo.Should().NotBeNull(); + _contextData.AccountInfoHistory.Should().NotBeNull(); + + HttpHandlerMock + .Protected() + .Verify>("SendAsync", Times.Never(), + ItExpr.IsAny(), + ItExpr.IsAny()); } } } \ No newline at end of file diff --git a/WotBlitzStatisticsPro.Tests/OperationStepsTests/GetTanksInfoOperationTest.cs b/WotBlitzStatisticsPro.Tests/OperationStepsTests/GetTanksInfoOperationTest.cs index 54c7344..2df911b 100644 --- a/WotBlitzStatisticsPro.Tests/OperationStepsTests/GetTanksInfoOperationTest.cs +++ b/WotBlitzStatisticsPro.Tests/OperationStepsTests/GetTanksInfoOperationTest.cs @@ -1,8 +1,11 @@ using System.IO; +using System.Net.Http; +using System.Threading; using System.Threading.Tasks; using FluentAssertions; using Microsoft.Extensions.Logging; using Moq; +using Moq.Protected; using Newtonsoft.Json; using NUnit.Framework; using WotBlitzStatisticsPro.Logic.AccountInformationPipeline; @@ -57,20 +60,32 @@ namespace WotBlitzStatisticsPro.Tests.OperationStepsTests await File.ReadAllTextAsync(GetFixturePath("MappedTanksHistory.json")); tanksHistorySerialized.Should().Be(expectedTanksHistory); - // ToDo: Check cache - Assert.Fail("Implement test"); - } - - [Test] - public async Task ShouldGetAndSaveTankInfoInformationIfCacheExpired() - { - Assert.Fail("Implement test"); + var tanksCache = _cache.GetTanksData(AccountId); + tanksCache.Should().NotBeNull(); + tanksCache?.TankInfo.Should().NotBeNull(); + tanksCache?.TankInfoHistory.Should().NotBeNull(); } [Test] public async Task ShouldReadDataFromCacheIfItIsNotEmpty() { - Assert.Fail("Implement test"); + var tanksInfo = GetTanksInfoRomFixture(); + var tanksInfoHistory = GetTanksInfoHistoryFromFixture(); + _cache.SetTanksData(AccountId, new TanksDataCache(tanksInfo, tanksInfoHistory)); + + var context = new OperationContext(new AccountRequest(AccountId, Realm, Language)); + context.AddOrReplace(_contextData); + + await _operation.Invoke(context, null); + + _contextData.Tanks.Should().NotBeNull(); + _contextData.TanksHistory.Should().NotBeNull(); + + HttpHandlerMock + .Protected() + .Verify>("SendAsync", Times.Never(), + ItExpr.IsAny(), + ItExpr.IsAny()); } } diff --git a/WotBlitzStatisticsPro.Tests/OperationStepsTests/OperationsStepsTestBase.cs b/WotBlitzStatisticsPro.Tests/OperationStepsTests/OperationsStepsTestBase.cs index d44a680..b499b26 100644 --- a/WotBlitzStatisticsPro.Tests/OperationStepsTests/OperationsStepsTestBase.cs +++ b/WotBlitzStatisticsPro.Tests/OperationStepsTests/OperationsStepsTestBase.cs @@ -38,6 +38,8 @@ namespace WotBlitzStatisticsPro.Tests.OperationStepsTests protected Mock DictionariesDataAccessorMock; protected Mock WargamingDataAccessorMock; + protected Mock HttpHandlerMock; + protected void InitAutoMapper() { var config = new MapperConfiguration(cfg => { @@ -82,24 +84,26 @@ namespace WotBlitzStatisticsPro.Tests.OperationStepsTests Content = new StringContent(File.ReadAllText(tanksInfoFilePath)), }; - var handlerMock = new Mock(); - handlerMock + HttpHandlerMock = new Mock(); + HttpHandlerMock .Protected() .Setup>( "SendAsync", ItExpr.Is(m => m.RequestUri.ToString() == playerInfoRequestUrl), ItExpr.IsAny()) - .ReturnsAsync(playerInfoResponse); - handlerMock + .ReturnsAsync(playerInfoResponse) + .Verifiable(); + HttpHandlerMock .Protected() .Setup>( "SendAsync", ItExpr.Is(m => m.RequestUri.ToString() == tanksInfoRequestUrl), ItExpr.IsAny()) - .ReturnsAsync(tanksInfoResponse); + .ReturnsAsync(tanksInfoResponse) + .Verifiable(); var client = new WargamingApiClient( - new HttpClient(handlerMock.Object), + new HttpClient(HttpHandlerMock.Object), settingsMock.Object, (new Mock>()).Object); WargamingApiClient = client; @@ -125,17 +129,11 @@ namespace WotBlitzStatisticsPro.Tests.OperationStepsTests protected void FillAccountAndTanks(AccountInformationPipelineContextData contextData) { - var mappedAccountInfoHistory = File.ReadAllText(GetFixturePath("MappedAccountHistory.json")); - var mappedTanks = File.ReadAllText(GetFixturePath("MappedTanks.json")); - var mappedTanksHistory = File.ReadAllText(GetFixturePath("MappedTanksHistory.json")); contextData.AccountInfo = GetAccountInfoFromFixture(); - contextData.AccountInfoHistory = - JsonConvert.DeserializeObject(mappedAccountInfoHistory); - contextData.Tanks = JsonConvert.DeserializeObject>(mappedTanks); - contextData.TanksHistory = - JsonConvert.DeserializeObject>(mappedTanksHistory); - + contextData.AccountInfoHistory = GetAccountInfoHistoryFromFixture(); + contextData.Tanks = GetTanksInfoRomFixture(); + contextData.TanksHistory = GetTanksInfoHistoryFromFixture(); } protected AccountInfo GetAccountInfoFromFixture() @@ -144,6 +142,24 @@ namespace WotBlitzStatisticsPro.Tests.OperationStepsTests return JsonConvert.DeserializeObject(mappedAccountInfo); } + protected AccountInfoHistory GetAccountInfoHistoryFromFixture() + { + var mappedAccountInfoHistory = File.ReadAllText(GetFixturePath("MappedAccountHistory.json")); + return JsonConvert.DeserializeObject(mappedAccountInfoHistory); + } + + protected List GetTanksInfoRomFixture() + { + var mappedTanks = File.ReadAllText(GetFixturePath("MappedTanks.json")); + return JsonConvert.DeserializeObject>(mappedTanks); + } + + protected Dictionary GetTanksInfoHistoryFromFixture() + { + var mappedTanksHistory = File.ReadAllText(GetFixturePath("MappedTanksHistory.json")); + return JsonConvert.DeserializeObject>(mappedTanksHistory); + } + protected string GetFixturePath(string fixtureFileName) { return Path.Combine(TestContext.CurrentContext.TestDirectory, "Fixtures", fixtureFileName);