2021-03-12 17:46:28 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using AutoMapper;
|
2021-06-04 23:04:03 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2021-03-12 17:46:28 +01:00
|
|
|
using Moq;
|
|
|
|
|
using Moq.Protected;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using WotBlitzStatisticsPro.Common;
|
|
|
|
|
using WotBlitzStatisticsPro.Common.Dictionaries;
|
|
|
|
|
using WotBlitzStatisticsPro.Common.Model;
|
|
|
|
|
using WotBlitzStatisticsPro.DataAccess;
|
|
|
|
|
using WotBlitzStatisticsPro.DataAccess.Model;
|
|
|
|
|
using WotBlitzStatisticsPro.DataAccess.Model.Accounts;
|
|
|
|
|
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline;
|
|
|
|
|
using WotBlitzStatisticsPro.Logic.Mappers;
|
|
|
|
|
using WotBlitzStatisticsPro.WgApiClient;
|
|
|
|
|
using WotBlitzStatisticsPro.WgApiClient.Model;
|
|
|
|
|
|
|
|
|
|
namespace WotBlitzStatisticsPro.Tests.OperationStepsTests
|
|
|
|
|
{
|
|
|
|
|
public class OperationsStepsTestBase
|
|
|
|
|
{
|
|
|
|
|
protected const long AccountId = 571050560;
|
|
|
|
|
protected const string ApplicationId = "demo";
|
|
|
|
|
protected const RealmType Realm = RealmType.Eu;
|
|
|
|
|
protected const RequestLanguage Language = RequestLanguage.En;
|
|
|
|
|
|
|
|
|
|
protected IMapper Mapper;
|
|
|
|
|
protected IWargamingApiClient WargamingApiClient;
|
|
|
|
|
protected IWargamingTanksApiClient WargamingTanksApiClient;
|
|
|
|
|
protected Mock<IDictionariesDataAccessor> DictionariesDataAccessorMock;
|
|
|
|
|
protected Mock<IWargamingAccountDataAccessor> WargamingDataAccessorMock;
|
|
|
|
|
|
2021-06-07 14:29:16 +02:00
|
|
|
protected Mock<HttpMessageHandler> HttpHandlerMock;
|
|
|
|
|
|
2021-03-12 17:46:28 +01:00
|
|
|
protected void InitAutoMapper()
|
|
|
|
|
{
|
|
|
|
|
var config = new MapperConfiguration(cfg => {
|
|
|
|
|
cfg.AddProfile<AccountDtoProfile>();
|
|
|
|
|
cfg.AddProfile<AccountInfoProfile>();
|
|
|
|
|
cfg.AddProfile<AccountInfoStatisticsProfile>();
|
|
|
|
|
cfg.AddProfile<AccountSearchResponseProfile>();
|
2021-03-23 14:08:49 +01:00
|
|
|
cfg.AddProfile<AchievementsProfile>();
|
2021-03-12 17:46:28 +01:00
|
|
|
cfg.AddProfile<ClanSearchResponseProfile>();
|
|
|
|
|
cfg.AddProfile<DictionaryLanguageMapper>();
|
|
|
|
|
cfg.AddProfile<ShortStatisticsProfile>();
|
|
|
|
|
cfg.AddProfile<TankInfoProfile>();
|
|
|
|
|
cfg.AddProfile<TankInfoStatisticsProfile>();
|
|
|
|
|
cfg.AddProfile<VehicleDictionaryProfile>();
|
|
|
|
|
cfg.AddProfile<VehiclePriceProfile>();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Mapper = new Mapper(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void InitWgApiClient()
|
|
|
|
|
{
|
|
|
|
|
var settingsMock = new Mock<IWargamingApiSettings>();
|
|
|
|
|
settingsMock.SetupGet(s => s.ApplicationId).Returns(ApplicationId);
|
|
|
|
|
|
2021-03-12 17:56:13 +01:00
|
|
|
var playerInfoFilePath = GetFixturePath("PlayerInfo50.json");
|
|
|
|
|
var tanksInfoFilePath = GetFixturePath("TanksInfo50.json");
|
|
|
|
|
var playerAchievementsFilePath = GetFixturePath("PlayerAchievements50.json");
|
|
|
|
|
var tanksAchievementsFilePath = GetFixturePath("TanksAchievemnts50.json");
|
2021-03-12 17:46:28 +01:00
|
|
|
|
|
|
|
|
var playerInfoRequestUrl = $"https://api.wotblitz.eu/wotb/account/info/?application_id={ApplicationId}&language=en&account_id={AccountId}";
|
|
|
|
|
var playerInfoResponse = new HttpResponseMessage
|
|
|
|
|
{
|
|
|
|
|
StatusCode = HttpStatusCode.OK,
|
|
|
|
|
Content = new StringContent(File.ReadAllText(playerInfoFilePath)),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var tanksInfoRequestUrl = $"https://api.wotblitz.eu/wotb/tanks/stats/?application_id={ApplicationId}&language=en&account_id={AccountId}";
|
|
|
|
|
var tanksInfoResponse = new HttpResponseMessage
|
|
|
|
|
{
|
|
|
|
|
StatusCode = HttpStatusCode.OK,
|
|
|
|
|
Content = new StringContent(File.ReadAllText(tanksInfoFilePath)),
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-07 14:29:16 +02:00
|
|
|
HttpHandlerMock = new Mock<HttpMessageHandler>();
|
|
|
|
|
HttpHandlerMock
|
2021-03-12 17:46:28 +01:00
|
|
|
.Protected()
|
|
|
|
|
.Setup<Task<HttpResponseMessage>>(
|
|
|
|
|
"SendAsync",
|
|
|
|
|
ItExpr.Is<HttpRequestMessage>(m => m.RequestUri.ToString() == playerInfoRequestUrl),
|
|
|
|
|
ItExpr.IsAny<CancellationToken>())
|
2021-06-07 14:29:16 +02:00
|
|
|
.ReturnsAsync(playerInfoResponse)
|
|
|
|
|
.Verifiable();
|
|
|
|
|
HttpHandlerMock
|
2021-03-12 17:46:28 +01:00
|
|
|
.Protected()
|
|
|
|
|
.Setup<Task<HttpResponseMessage>>(
|
|
|
|
|
"SendAsync",
|
|
|
|
|
ItExpr.Is<HttpRequestMessage>(m => m.RequestUri.ToString() == tanksInfoRequestUrl),
|
|
|
|
|
ItExpr.IsAny<CancellationToken>())
|
2021-06-07 14:29:16 +02:00
|
|
|
.ReturnsAsync(tanksInfoResponse)
|
|
|
|
|
.Verifiable();
|
2021-03-12 17:46:28 +01:00
|
|
|
|
2021-06-04 23:04:03 +02:00
|
|
|
var client = new WargamingApiClient(
|
2021-06-07 14:29:16 +02:00
|
|
|
new HttpClient(HttpHandlerMock.Object),
|
2021-06-04 23:04:03 +02:00
|
|
|
settingsMock.Object,
|
|
|
|
|
(new Mock<ILogger<WagramingApiClientBase>>()).Object);
|
2021-03-12 17:46:28 +01:00
|
|
|
WargamingApiClient = client;
|
|
|
|
|
WargamingTanksApiClient = client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void InitDataAccessors()
|
|
|
|
|
{
|
2021-03-12 17:56:13 +01:00
|
|
|
var tankopediaFile = File.ReadAllText(GetFixturePath("Tankopedia.json"));
|
2021-03-12 17:46:28 +01:00
|
|
|
var vehicles = JsonConvert.DeserializeObject<List<VehiclesDictionary>>(tankopediaFile);
|
|
|
|
|
|
|
|
|
|
var tankTires = vehicles.Select(v => new {TankId = v.TankId, Tier = v.Tier})
|
|
|
|
|
.ToDictionary(k => k.TankId, v => v.Tier);
|
|
|
|
|
|
|
|
|
|
DictionariesDataAccessorMock = new Mock<IDictionariesDataAccessor>();
|
|
|
|
|
DictionariesDataAccessorMock.Setup(d => d.GetTankTires(It.IsAny<long[]>())).ReturnsAsync(tankTires);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WargamingDataAccessorMock = new Mock<IWargamingAccountDataAccessor>();
|
|
|
|
|
WargamingDataAccessorMock.Setup(d => d.ReadAccountInfo(AccountId))
|
|
|
|
|
.ReturnsAsync(GetAccountInfoFromFixture());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void FillAccountAndTanks(AccountInformationPipelineContextData contextData)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
contextData.AccountInfo = GetAccountInfoFromFixture();
|
2021-06-07 14:29:16 +02:00
|
|
|
contextData.AccountInfoHistory = GetAccountInfoHistoryFromFixture();
|
|
|
|
|
contextData.Tanks = GetTanksInfoRomFixture();
|
|
|
|
|
contextData.TanksHistory = GetTanksInfoHistoryFromFixture();
|
2021-03-12 17:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected AccountInfo GetAccountInfoFromFixture()
|
|
|
|
|
{
|
2021-03-12 17:56:13 +01:00
|
|
|
var mappedAccountInfo = File.ReadAllText(GetFixturePath("MappedAccountInfo.json"));
|
2021-03-12 17:46:28 +01:00
|
|
|
return JsonConvert.DeserializeObject<AccountInfo>(mappedAccountInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-07 14:29:16 +02:00
|
|
|
protected AccountInfoHistory GetAccountInfoHistoryFromFixture()
|
|
|
|
|
{
|
|
|
|
|
var mappedAccountInfoHistory = File.ReadAllText(GetFixturePath("MappedAccountHistory.json"));
|
|
|
|
|
return JsonConvert.DeserializeObject<AccountInfoHistory>(mappedAccountInfoHistory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected List<TankInfo> GetTanksInfoRomFixture()
|
|
|
|
|
{
|
|
|
|
|
var mappedTanks = File.ReadAllText(GetFixturePath("MappedTanks.json"));
|
|
|
|
|
return JsonConvert.DeserializeObject<List<TankInfo>>(mappedTanks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected Dictionary<long, TankInfoHistory> GetTanksInfoHistoryFromFixture()
|
|
|
|
|
{
|
|
|
|
|
var mappedTanksHistory = File.ReadAllText(GetFixturePath("MappedTanksHistory.json"));
|
|
|
|
|
return JsonConvert.DeserializeObject<Dictionary<long, TankInfoHistory>>(mappedTanksHistory);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 17:56:13 +01:00
|
|
|
protected string GetFixturePath(string fixtureFileName)
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(TestContext.CurrentContext.TestDirectory, "Fixtures", fixtureFileName);
|
|
|
|
|
}
|
2021-03-12 17:46:28 +01:00
|
|
|
}
|
|
|
|
|
}
|