2021-03-12 17:46:28 +01:00
|
|
|
using System.IO;
|
2021-06-07 14:29:16 +02:00
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading;
|
2021-03-12 17:46:28 +01:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Moq;
|
2021-06-07 14:29:16 +02:00
|
|
|
using Moq.Protected;
|
2021-03-12 17:46:28 +01:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using WotBlitzStatisticsPro.Common.Model;
|
|
|
|
|
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline;
|
|
|
|
|
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline.OperationContext;
|
|
|
|
|
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations;
|
2021-06-04 23:04:03 +02:00
|
|
|
using WotBlitzStatisticsPro.Logic.Model;
|
2021-03-12 17:46:28 +01:00
|
|
|
|
|
|
|
|
namespace WotBlitzStatisticsPro.Tests.OperationStepsTests
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class GetAccountInfoOperationTest: OperationsStepsTestBase
|
|
|
|
|
{
|
|
|
|
|
private GetAccountInfoOperation _operation;
|
|
|
|
|
private AccountInformationPipelineContextData _contextData;
|
2021-06-04 23:04:03 +02:00
|
|
|
private StatisticsCache _cache;
|
2021-03-12 17:46:28 +01:00
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
InitAutoMapper();
|
|
|
|
|
InitWgApiClient();
|
2021-06-04 23:04:03 +02:00
|
|
|
_cache = new StatisticsCache();
|
2021-03-12 17:46:28 +01:00
|
|
|
_operation = new GetAccountInfoOperation(
|
|
|
|
|
WargamingApiClient,
|
|
|
|
|
Mapper,
|
2021-06-04 23:04:03 +02:00
|
|
|
(new Mock<ILogger<GetAccountInfoOperation>>()).Object,
|
|
|
|
|
_cache);
|
2021-03-12 17:46:28 +01:00
|
|
|
|
|
|
|
|
_contextData = new AccountInformationPipelineContextData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task ShouldGetAndSaveAccountInfoInformation()
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
var accountInfoSerialized = JsonConvert.SerializeObject(_contextData.AccountInfo);
|
|
|
|
|
var accountInfoHistorySerialized = JsonConvert.SerializeObject(_contextData.AccountInfoHistory);
|
|
|
|
|
|
|
|
|
|
var expectedAccountInfo =
|
2021-03-12 17:56:13 +01:00
|
|
|
await File.ReadAllTextAsync(GetFixturePath("MappedAccountInfo.json"));
|
2021-03-12 17:46:28 +01:00
|
|
|
accountInfoSerialized.Should().Be(expectedAccountInfo);
|
|
|
|
|
var expectedAccountInfoHistory =
|
2021-03-12 17:56:13 +01:00
|
|
|
await File.ReadAllTextAsync(GetFixturePath("MappedAccountHistory.json"));
|
2021-03-12 17:46:28 +01:00
|
|
|
accountInfoHistorySerialized.Should().Be(expectedAccountInfoHistory);
|
2021-06-04 23:04:03 +02:00
|
|
|
|
2021-06-07 14:29:16 +02:00
|
|
|
var accountCache = _cache.GetAccountData(AccountId);
|
|
|
|
|
accountCache.Should().NotBeNull();
|
|
|
|
|
accountCache?.AccountInfo.Should().NotBeNull();
|
|
|
|
|
accountCache?.AccountInfoHistory.Should().NotBeNull();
|
2021-06-04 23:04:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task ShouldReadDataFromCacheIfItIsNotEmpty()
|
|
|
|
|
{
|
2021-06-07 14:29:16 +02:00
|
|
|
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<Task<HttpResponseMessage>>("SendAsync", Times.Never(),
|
|
|
|
|
ItExpr.IsAny<HttpRequestMessage>(),
|
|
|
|
|
ItExpr.IsAny<CancellationToken>());
|
2021-03-12 17:46:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|