Files

92 lines
3.3 KiB
C#
Raw Permalink Normal View History

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.Logic.AccountInformationPipeline;
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline.OperationContext;
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations;
using WotBlitzStatisticsPro.Logic.Model;
2021-03-12 17:46:28 +01:00
namespace WotBlitzStatisticsPro.Tests.OperationStepsTests
{
[TestFixture]
public class GetTanksInfoOperationTest: OperationsStepsTestBase
{
private GetTanksInfoOperation _operation;
private AccountInformationPipelineContextData _contextData;
private StatisticsCache _cache;
2021-03-12 17:46:28 +01:00
[SetUp]
public void Init()
{
InitAutoMapper();
InitWgApiClient();
_cache = new StatisticsCache();
2021-03-12 17:46:28 +01:00
_operation = new GetTanksInfoOperation(
WargamingTanksApiClient,
Mapper,
(new Mock<ILogger<GetTanksInfoOperation>>()).Object,
_cache);
2021-03-12 17:46:28 +01:00
_contextData = new AccountInformationPipelineContextData();
}
[Test]
public async Task ShouldGetAndSaveTanksInformation()
{
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();
var tanksSerialized = JsonConvert.SerializeObject(_contextData.Tanks);
var tanksHistorySerialized = JsonConvert.SerializeObject(_contextData.TanksHistory);
var expectedTanksInfo =
2021-03-12 17:56:13 +01:00
await File.ReadAllTextAsync(GetFixturePath("MappedTanks.json"));
2021-03-12 17:46:28 +01:00
tanksSerialized.Should().Be(expectedTanksInfo);
var expectedTanksHistory =
2021-03-12 17:56:13 +01:00
await File.ReadAllTextAsync(GetFixturePath("MappedTanksHistory.json"));
2021-03-12 17:46:28 +01:00
tanksHistorySerialized.Should().Be(expectedTanksHistory);
2021-06-07 14:29:16 +02:00
var tanksCache = _cache.GetTanksData(AccountId);
tanksCache.Should().NotBeNull();
tanksCache?.TankInfo.Should().NotBeNull();
tanksCache?.TankInfoHistory.Should().NotBeNull();
}
[Test]
public async Task ShouldReadDataFromCacheIfItIsNotEmpty()
{
2021-06-07 14:29:16 +02:00
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<Task<HttpResponseMessage>>("SendAsync", Times.Never(),
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>());
2021-03-12 17:46:28 +01:00
}
}
}