using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AutoMapper; using AutoMapper.Configuration; using Bogus; using FluentAssertions; using Moq; using NUnit.Framework; using WotBlitzStatisticsPro.Common.Dictionaries; using WotBlitzStatisticsPro.Common.Model; using WotBlitzStatisticsPro.DataAccess; using WotBlitzStatisticsPro.Logic.Dictionaries; using WotBlitzStatisticsPro.Logic.Mappers; using WotBlitzStatisticsPro.WgApiClient; using WotBlitzStatisticsPro.WgApiClient.Model; namespace WotBlitzStatisticsPro.Tests.DictionariesTests { [TestFixture] public class StaticDictionaryUpdaterTests { private StaticDictionariesUpdater _staticDictionariesUpdater; private Mock _wargamingDictionariesApiClientMock; private Mock _dataAccessorMock; private readonly WotEncyclopediaInfoResponse[] _encyclopediaInfoResponse = new WotEncyclopediaInfoResponse[3]; private readonly WotClanMembersDictionaryResponse[] _clanMembersDictionary = new WotClanMembersDictionaryResponse[3]; [SetUp] public void Init() { // Generating fake data _encyclopediaInfoResponse[0] = GetEncyclopediaInfoResponse("en"); _encyclopediaInfoResponse[1] = GetEncyclopediaInfoResponse("ru", _encyclopediaInfoResponse[0]); _encyclopediaInfoResponse[2] = GetEncyclopediaInfoResponse("de", _encyclopediaInfoResponse[0]); _clanMembersDictionary[0] = GetClanMembersDictionaryResponse("en"); _clanMembersDictionary[1] = GetClanMembersDictionaryResponse("ru", _clanMembersDictionary[0]); _clanMembersDictionary[2] = GetClanMembersDictionaryResponse("de", _clanMembersDictionary[0]); // Real mapper instance var configurationExp = new MapperConfigurationExpression(); configurationExp.AddProfile(); var mapperConfiguration = new MapperConfiguration(configurationExp); IMapper mapper = new Mapper(mapperConfiguration); // Wargaming client mock _wargamingDictionariesApiClientMock = new Mock(); _wargamingDictionariesApiClientMock.Setup(w => w.GetStaticDictionariesAsync(It.IsAny(), RequestLanguage.En)) .ReturnsAsync((_encyclopediaInfoResponse[0], _clanMembersDictionary[0])); _wargamingDictionariesApiClientMock.Setup(w => w.GetStaticDictionariesAsync(It.IsAny(), RequestLanguage.Ru)) .ReturnsAsync((_encyclopediaInfoResponse[1], _clanMembersDictionary[1])); _wargamingDictionariesApiClientMock.Setup(w => w.GetStaticDictionariesAsync(It.IsAny(), RequestLanguage.De)) .ReturnsAsync((_encyclopediaInfoResponse[2], _clanMembersDictionary[2])); // MongoDb data accessor mock _dataAccessorMock = new Mock(); _staticDictionariesUpdater = new StaticDictionariesUpdater( _wargamingDictionariesApiClientMock.Object, _dataAccessorMock.Object, mapper); } [Test] public async Task ShouldCallWargamingApi3Times() { await _staticDictionariesUpdater.Update(); // Should call wargaming API once for each language _wargamingDictionariesApiClientMock.Verify(d => d.GetStaticDictionariesAsync(It.IsAny(), RequestLanguage.En), Times.Once); _wargamingDictionariesApiClientMock.Verify(d => d.GetStaticDictionariesAsync(It.IsAny(), RequestLanguage.Ru), Times.Once); _wargamingDictionariesApiClientMock.Verify(d => d.GetStaticDictionariesAsync(It.IsAny(), RequestLanguage.De), Times.Once); } [Test] public async Task ShouldSaveAppropriateLanguagesDictionary() { List languageDictionary = null; _dataAccessorMock .Setup(d => d.UpdateLanguages(It.IsAny>())) .Callback((List dictionary) => languageDictionary = dictionary); await _staticDictionariesUpdater.Update(); var sourceLanguages = _encyclopediaInfoResponse[0].Languages.ToArray(); languageDictionary.Should().NotBeNull(); languageDictionary.Should().HaveCount(sourceLanguages.Length); for (int i = 0; i < sourceLanguages.Length; i++) { languageDictionary[i].LanguageId.Should().Be(sourceLanguages[i].Key); languageDictionary[i].LanguageName.Should() .Be(sourceLanguages[i].Value); } } [Test] public async Task ShouldSaveAppropriateNationsDictionary() { List nations = null; _dataAccessorMock .Setup(d => d.UpdateNations(It.IsAny>())) .Callback((List dictionary) => nations = dictionary); await _staticDictionariesUpdater.Update(); var sourceNationsEn = _encyclopediaInfoResponse[0].VehicleNations.ToArray(); var sourceNationsRu = _encyclopediaInfoResponse[1].VehicleNations.ToArray(); var sourceNationsDe = _encyclopediaInfoResponse[2].VehicleNations.ToArray(); nations.Should().NotBeNull(); nations.Should().HaveCount(sourceNationsEn.Length); for (int i = 0; i < sourceNationsEn.Length; i++) { nations[i].NationId.Should().Be(sourceNationsEn[i].Key); nations[i].NationNames.First(n => n.Language == RequestLanguage.En).Value.Should() .Be(sourceNationsEn[i].Value); nations[i].NationNames.First(n => n.Language == RequestLanguage.Ru).Value.Should() .Be(sourceNationsRu[i].Value); nations[i].NationNames.First(n => n.Language == RequestLanguage.De).Value.Should() .Be(sourceNationsDe[i].Value); } } [Test] public async Task ShouldSaveAppropriateVehicleTypesDictionary() { List vehicleTypes = null; _dataAccessorMock .Setup(d => d.UpdateVehicleTypes(It.IsAny>())) .Callback((List dictionary) => vehicleTypes = dictionary); await _staticDictionariesUpdater.Update(); var vehicleTypesEn = _encyclopediaInfoResponse[0].VehicleTypes.ToArray(); var vehicleTypesRu = _encyclopediaInfoResponse[1].VehicleTypes.ToArray(); var vehicleTypesDe = _encyclopediaInfoResponse[2].VehicleTypes.ToArray(); vehicleTypes.Should().NotBeNull(); vehicleTypes.Should().HaveCount(vehicleTypesEn.Length); for (int i = 0; i < vehicleTypesEn.Length; i++) { vehicleTypes[i].VehicleTypeId.Should().Be(vehicleTypesEn[i].Key); vehicleTypes[i].VehicleTypeNames.First(n => n.Language == RequestLanguage.En).Value.Should() .Be(vehicleTypesEn[i].Value); vehicleTypes[i].VehicleTypeNames.First(n => n.Language == RequestLanguage.Ru).Value.Should() .Be(vehicleTypesRu[i].Value); vehicleTypes[i].VehicleTypeNames.First(n => n.Language == RequestLanguage.De).Value.Should() .Be(vehicleTypesDe[i].Value); } } [Test] public async Task ShouldSaveAppropriateClanRolesDictionary() { List clanRoles = null; _dataAccessorMock .Setup(d => d.UpdateClanRoles(It.IsAny>())) .Callback((List dictionary) => clanRoles = dictionary); await _staticDictionariesUpdater.Update(); var clanRoleEn = _clanMembersDictionary[0].ClanRoles.ToArray(); var clanRoleRu = _clanMembersDictionary[1].ClanRoles.ToArray(); var clanRoleDe = _clanMembersDictionary[2].ClanRoles.ToArray(); clanRoles.Should().NotBeNull(); clanRoles.Should().HaveCount(clanRoleEn.Length); for (int i = 0; i < clanRoleEn.Length; i++) { clanRoles[i].ClanRoleId.Should().Be(clanRoleEn[i].Key); clanRoles[i].ClanRoleNames.First(n => n.Language == RequestLanguage.En).Value.Should() .Be(clanRoleEn[i].Value); clanRoles[i].ClanRoleNames.First(n => n.Language == RequestLanguage.Ru).Value.Should() .Be(clanRoleRu[i].Value); clanRoles[i].ClanRoleNames.First(n => n.Language == RequestLanguage.De).Value.Should() .Be(clanRoleDe[i].Value); } } [Test] public async Task ShouldSaveAppropriateAchievementsSectionsDictionary() { List achievementsSections = null; _dataAccessorMock .Setup(d => d.UpdateAchievementsSections(It.IsAny>())) .Callback((List dictionary) => achievementsSections = dictionary); await _staticDictionariesUpdater.Update(); var asEn = _encyclopediaInfoResponse[0].AchievementSections.ToArray(); var asRu = _encyclopediaInfoResponse[1].AchievementSections.ToArray(); var asDe = _encyclopediaInfoResponse[2].AchievementSections.ToArray(); achievementsSections.Should().NotBeNull(); achievementsSections.Should().HaveCount(asEn.Length); for (int i = 0; i < asEn.Length; i++) { achievementsSections[i].AchievementSectionId.Should().Be(asEn[i].Key); achievementsSections[i].AchievementSectionNames.First(n => n.Language == RequestLanguage.En).Value.Should() .Be(asEn[i].Value.Name); achievementsSections[i].AchievementSectionNames.First(n => n.Language == RequestLanguage.Ru).Value.Should() .Be(asRu[i].Value.Name); achievementsSections[i].AchievementSectionNames.First(n => n.Language == RequestLanguage.De).Value.Should() .Be(asDe[i].Value.Name); } } #region Fixtures private static WotEncyclopediaInfoResponse GetEncyclopediaInfoResponse(string language, WotEncyclopediaInfoResponse baseEncyclopedia = null) { return new WotEncyclopediaInfoResponse { AchievementSections = baseEncyclopedia == null ? FakeAchievementSections(16, language) : FakeAchievementSections(language, baseEncyclopedia.AchievementSections), Languages = baseEncyclopedia == null ? FakeDictionary(14) : baseEncyclopedia.Languages, VehicleNations = baseEncyclopedia == null ? FakeDictionary(8, language) : FakeDictionary(language, baseEncyclopedia.VehicleNations), VehicleTypes = baseEncyclopedia == null ? FakeDictionary(4, language) : FakeDictionary(language, baseEncyclopedia.VehicleTypes) }; } private static WotClanMembersDictionaryResponse GetClanMembersDictionaryResponse(string language, WotClanMembersDictionaryResponse baseClanRoles = null) { return new WotClanMembersDictionaryResponse { ClanRoles = baseClanRoles == null ? FakeDictionary(3, language) : FakeDictionary(language, baseClanRoles.ClanRoles) }; } private static Dictionary FakeDictionary(int elementsCount, string language = "en") { var result = new Dictionary(); var faker = new Faker(language); for (int i = 0; i < elementsCount; i++) { result[faker.Lorem.Word()] = faker.Lorem.Word(); } return result; } private static Dictionary FakeDictionary(string language, Dictionary baseDictionary) { var result = new Dictionary(); var faker = new Faker(language); foreach (var baseValue in baseDictionary) { result[baseValue.Key] = faker.Lorem.Word(); } return result; } private static Dictionary FakeAchievementSections(int elementsCount, string language = "en") { var result = new Dictionary(); var faker = new Faker(language); for (int i = 0; i < elementsCount; i++) { result[faker.Lorem.Word()] = new WotEncyclopediaInfoAchievementSection {Name = faker.Lorem.Word()}; } return result; } private static Dictionary FakeAchievementSections(string language, Dictionary baseDictionary) { var result = new Dictionary(); var faker = new Faker(language); foreach (var baseValue in baseDictionary) { result[baseValue.Key] = new WotEncyclopediaInfoAchievementSection { Name = faker.Lorem.Word() }; } return result; } #endregion } }