public void TestObserverJobExecution_When_User_Was_Under_Attack_Gameplay_Returns_Attack() { var villageRepoMock = new Mock <IVillageRepository>(); villageRepoMock.Setup(x => x.GetVillages(FakeDataProvider.TravianUserName)) .Returns(Task.FromResult((IEnumerable <VillageModel>)FakeDataProvider.GetVillagesFromDatabase(true))); _serviceBuilder = _serviceBuilder .WithService(villageRepoMock.Object); var observerJob = new ObserverJob(_serviceBuilder.Build()); var context = new Mock <IJobExecutionContext>(); var jobDetail = new Mock <IJobDetail>(); var data = new JobExecutionData { TravianUser = FakeDataProvider.GetUser(PlayerStatus.UNDER_ATTACK, true) }; jobDetail.Setup(x => x.JobDataMap[AbstractJob.JobExecutionDataKey]).Returns(data); context.Setup(x => x.JobDetail).Returns(jobDetail.Object); Assert.DoesNotThrowAsync(async() => await observerJob.Execute(data)); _logger.Verify(x => x.Log(LogLevel.Error, It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Never); _cmdFactoryMock.Verify(x => x.GetQueueableCommand(nameof(PrepareToAttackCommand), It.IsAny <long>()), Times.Once); _travianUserRepoMock.Verify(x => x.ReplacePlayerDataVillages(It.IsAny <TravianUser>()), Times.Once); Assert.AreEqual(2, _reportMsgs.Count); Assert.IsTrue(_reportMsgs[1].Contains($"Village [test_village_under_attack] is under attack.")); Assert.IsTrue(_reportMsgs[1].Contains(FakeDataProvider.NewAttackDateTime.ToDisplayStringApplyTimeZone("UTC+2"))); Assert.AreEqual(1, _updates.Count); Assert.AreEqual(PlayerStatus.UNDER_ATTACK, _updates.First().PlayerData.Status); }
public void Test_Prepare_To_Attack() { var actionProviderMock = new Mock <IActionProvider>(); actionProviderMock.Setup(x => x.GetActionsForPlayer(It.IsNotNull <TravianUser>())) .Returns(Task.FromResult((IEnumerable <GameAction>)Builder <GameAction> .CreateListOfSize(2) .TheFirst(1) .With(x => x.Action = GameActionType.TRAIN_ARMY) .With(x => x.Village = Builder <Village> .CreateNew().With(y => y.Name = "test_village_1").Build()) .TheNext(1) .With(x => x.Action = GameActionType.SEND_RESOURCES) .With(x => x.Village = Builder <Village> .CreateNew().With(y => y.Name = "test_village_2").Build()) .Build())); var gameplayMock = new Mock <IGameplayClient>(); gameplayMock .Setup(x => x.ExecuteActions(It.IsNotNull <TravianUser>(), It.IsNotNull <List <GameAction> >())) .Returns(Task.FromResult(Builder <BaseScenarioResult> .CreateNew().Build())); _serviceBuilder = _serviceBuilder .WithService(actionProviderMock.Object) .WithService(gameplayMock.Object); var job = new PrepareToAttackJob(_serviceBuilder.Build()); //var context = new Mock<IJobExecutionContext>(); //var jobDetail = new Mock<IJobDetail>(); var data = new JobExecutionData { TravianUser = FakeDataProvider.GetUser(PlayerStatus.UNDER_ATTACK, true), JobType = typeof(PrepareToAttackJob) }; //jobDetail.Setup(x => x.JobDataMap[AbstractJob.JobExecutionDataKey]).Returns(data); //context.Setup(x => x.JobDetail).Returns(jobDetail.Object); Assert.DoesNotThrowAsync(async() => await job.Execute(data)); _serviceBuilder.LoggerMock.Verify(x => x.Log( LogLevel.Error, It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Never); Assert.AreEqual(2, _serviceBuilder.Messages.Count); Assert.AreEqual($"The village test_village_1 was prepared to attack with following action: [TRAIN_ARMY].", _serviceBuilder.Messages[0]); Assert.AreEqual($"The village test_village_2 was prepared to attack with following action: [SEND_RESOURCES].", _serviceBuilder.Messages[1]); }
public async Task Test_Get_Actions_For_Player_When_Only_Capital_Is_Under_Attack_And_Features_Off() { var villages = Builder <VillageModel> .CreateListOfSize(3) .TheFirst(1) .With(x => x.CoordinateX = 0) .With(x => x.CoordinateY = 0) .With(x => x.IsCapital = true) .With(x => x.Attacks = Builder <AttackModel> .CreateListOfSize(1).All().With(y => y.DateTime = DateTimeOffset.UtcNow.AddMinutes(1)).Build().ToList()) .Build(); var user = FakeDataProvider.GetUser(PlayerStatus.UNDER_ATTACK); var actionProvider = BuildAction(villages); var actions = (await actionProvider.GetActionsForPlayer(user)).ToList(); Assert.AreEqual(0, actions.Count); }
public void TestObserverJobExecution_When_It_Was_Quiet_And_Gameplay_Returns_Attack() { var villageRepoMock = new Mock <IVillageRepository>(); villageRepoMock.Setup(x => x.GetVillages(FakeDataProvider.TravianUserName)) .Returns(Task.FromResult((IEnumerable <VillageModel>)FakeDataProvider.GetVillagesFromDatabase(false))); _serviceBuilder = _serviceBuilder .WithService(villageRepoMock.Object); var observerJob = new ObserverJob(_serviceBuilder.Build()); var context = new Mock <IJobExecutionContext>(); var jobDetail = new Mock <IJobDetail>(); var data = new JobExecutionData { TravianUser = FakeDataProvider.GetUser(PlayerStatus.ALL_QUIET, true) }; jobDetail.Setup(x => x.JobDataMap[AbstractJob.JobExecutionDataKey]).Returns(data); context.Setup(x => x.JobDetail).Returns(jobDetail.Object); Assert.DoesNotThrowAsync(async() => await observerJob.Execute(data)); _logger.Verify(x => x.Log(LogLevel.Error, It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Never); _cmdFactoryMock.Verify(x => x.GetQueueableCommand(nameof(PrepareToAttackCommand), It.IsAny <long>()), Times.Exactly(2)); _travianUserRepoMock.Verify(x => x.ReplacePlayerDataVillages(It.IsAny <TravianUser>()), Times.Once); Assert.AreEqual(7, _reportMsgs.Count); Assert.IsTrue(_reportMsgs[0].Contains($"Player {FakeDataProvider.TravianUserName} has been scanned")); Assert.IsTrue(_reportMsgs[1].Contains($"Player {FakeDataProvider.TravianUserName} has been scanned")); Assert.IsTrue(_reportMsgs[2].Contains($"Player {FakeDataProvider.TravianUserName} is under attack:")); Assert.IsTrue(_reportMsgs[2].Contains("The village [test_village_under_attack] is under attack")); Assert.IsTrue(_reportMsgs[3].Contains($"Player {FakeDataProvider.TravianUserName} is under attack:")); Assert.IsTrue(_reportMsgs[3].Contains("The village [test_village_under_attack] is under attack")); Assert.AreEqual(_reportMsgs[4], $"New incoming attacks discovered for player [{FakeDataProvider.TravianUserName}]"); Assert.IsTrue(_reportMsgs[5].Contains($"Village [{FakeDataProvider.TestVillageName}] is under attack")); Assert.IsTrue(_reportMsgs[5].Contains($"The attack date time:")); Assert.IsTrue(_reportMsgs[5].Contains($"The intruder:")); Assert.IsTrue(_reportMsgs[6].Contains($"Village [{FakeDataProvider.TestVillageName}] is under attack")); Assert.IsTrue(_reportMsgs[6].Contains($"The attack date time:")); Assert.IsTrue(_reportMsgs[6].Contains($"The intruder:")); Assert.AreEqual(2, _updates.Count); Assert.AreEqual(PlayerStatus.UNDER_ATTACK, _updates.First().PlayerData.Status); }
public void SetUp() { var botUser = Builder <BotUser> .CreateNew().With(x => x.UserName = FakeDataProvider.BotUserName).Build(); var mgr = new Mock <IBotUserProvider>(); mgr.Setup(x => x.FindByNameAsync(FakeDataProvider.BotUserName)) .Returns(Task.FromResult(botUser)); mgr.Setup(x => x.FindByChatId(It.IsAny <long>())) .Returns(botUser); IMapper mapper = new Mapper( new MapperConfiguration( configure => { configure.AddProfile <MappingProfile>(); }) ); var user = FakeDataProvider.GetUser(PlayerStatus.ALL_QUIET); _travianUserRepoMock = new Mock <ITravianUserRepository>(); _travianUserRepoMock.Setup(x => x.GetActiveUser(FakeDataProvider.BotUserName)) .Returns(Task.FromResult(user)); _logger = new Mock <ILogger <AbstractCommand> >(); var botService = new Mock <IBotService>(); botService.Setup(x => x.SendTextMessageAsync(It.IsAny <long>(), It.IsAny <string>())) .Returns(Task.FromResult(new Message())); var unitProvider = new UnitsProviderFake(); _villageRepoMock = new Mock <IVillageRepository>(); _serviceBuilder = FakeServiceProviderBuilder.Builder() .WithService(mapper) .WithService(botService.Object) .WithService(mgr.Object) .WithService(_logger.Object) .WithService(unitProvider.GetUnitRepository()) .WithService(_travianUserRepoMock.Object); }
public void TestObserverJobExecution_When_User_Was_Under_Attack_Gameplay_Returns_All_Quiet() { var villageRepoMock = new Mock <IVillageRepository>(); villageRepoMock.Setup(x => x.GetVillages(FakeDataProvider.TravianUserName)) .Returns(Task.FromResult((IEnumerable <VillageModel>)FakeDataProvider.GetVillagesFromDatabase(false))); var user = FakeDataProvider.GetUser(PlayerStatus.UNDER_ATTACK); var result = Builder <BaseScenarioResult> .CreateNew() .With(x => x.Villages = Builder <Village> .CreateListOfSize(2).All().With(y => y.Attacks = null).Build().ToList()) .Build(); var gameplayMock = new Mock <IGameplayClient>(); gameplayMock.Setup(x => x.RunScan(It.IsAny <TravianUser>(), It.IsAny <bool>())).Returns(Task.FromResult(result)); _serviceBuilder = _serviceBuilder .WithService(villageRepoMock.Object) .WithService(gameplayMock.Object); var observerJob = new ObserverJob(_serviceBuilder.Build()); var context = new Mock <IJobExecutionContext>(); var jobDetail = new Mock <IJobDetail>(); var data = new JobExecutionData { TravianUser = FakeDataProvider.GetUser(PlayerStatus.UNDER_ATTACK, true) }; jobDetail.Setup(x => x.JobDataMap[AbstractJob.JobExecutionDataKey]).Returns(data); context.Setup(x => x.JobDetail).Returns(jobDetail.Object); Assert.DoesNotThrowAsync(async() => await observerJob.Execute(data)); _logger.Verify(x => x.Log(LogLevel.Error, It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Never); _travianUserRepoMock.Verify(x => x.ReplacePlayerDataVillages(It.IsAny <TravianUser>()), Times.Once); _travianUserRepoMock.Verify(x => x.ReplacePlayerData(It.IsAny <TravianUser>()), Times.Once); _travianUserRepoMock.Verify(x => x.ReplacePlayerData(It.IsAny <TravianUser>()), Times.Once); Assert.AreEqual(2, _updates.Count); Assert.AreEqual(PlayerStatus.ALL_QUIET, _updates.First().PlayerData.Status); }
public async Task Test_Get_Actions_For_Player_When_Capital_And_Another_Village_Are_Under_Attack() { var swordsman = await _unitRepo.GetUnit("swordsman", DAL.Models.GameModels.Enums.Tribe.GAUL); var theutates_thunder = await _unitRepo.GetUnit("theutates_thunder", DAL.Models.GameModels.Enums.Tribe.GAUL); var trebuchet = await _unitRepo.GetUnit("trebuchet", DAL.Models.GameModels.Enums.Tribe.GAUL); var phalang = await _unitRepo.GetUnit("phalang", DAL.Models.GameModels.Enums.Tribe.GAUL); var villages = Builder <VillageModel> .CreateListOfSize(3) .TheFirst(1) .With(y => y.VillageName = "uservillage1") .With(y => y.IsCapital = true) .With(y => y.CoordinateX = 0) .With(y => y.CoordinateY = 0) .With(y => y.Types = new List <VillageType> { VillageType.OFFENCE }) .With(y => y.IsSaveResourcesFeatureOn = true) .With(y => y.IsSaveTroopsFeatureOn = true) .With(y => y.PreferableUnits = new List <string> { "swordsman", "theutates_thunder", "trebuchet" }) .With(y => y.Attacks = Builder <AttackModel> .CreateListOfSize(2) .TheFirst(1) .With(x => x.DateTime = DateTimeOffset.UtcNow.AddMinutes(10)) .TheNext(1) .With(x => x.DateTime = DateTimeOffset.UtcNow.AddMinutes(200)) .Build() .ToList()) .TheNext(1) .With(y => y.VillageName = "uservillage2") .With(y => y.CoordinateX = -2) .With(y => y.CoordinateY = -3) .With(y => y.IsCapital = false) .With(y => y.Types = new List <VillageType> { VillageType.DEFFENCE, VillageType.RESOURCES }) .TheLast(1) .With(y => y.VillageName = "uservillage3") .With(y => y.CoordinateX = -5) .With(y => y.CoordinateY = -2) .With(y => y.IsCapital = false) .With(y => y.IsSaveResourcesFeatureOn = true) .With(y => y.IsSaveTroopsFeatureOn = true) .With(y => y.Types = new List <VillageType> { VillageType.DEFFENCE, VillageType.RESOURCES }) .With(y => y.Attacks = Builder <AttackModel> .CreateListOfSize(2) .TheFirst(1) .With(x => x.DateTime = DateTimeOffset.UtcNow.AddMinutes(10)) .TheNext(1) .With(x => x.DateTime = DateTimeOffset.UtcNow.AddMinutes(30)) .Build() .ToList()) .With(y => y.PreferableUnits = new List <string> { "phalang" }) .Build(); var user = FakeDataProvider.GetUser(PlayerStatus.UNDER_ATTACK); var actionProvider = BuildAction(villages); var actions = (await actionProvider.GetActionsForPlayer(user)).ToList(); Assert.AreEqual(5, actions.Count); Assert.AreEqual(GameActionType.SEND_ARMY, actions[0].Action); Assert.AreEqual(GameActionType.TRAIN_ARMY, actions[1].Action); Assert.AreEqual(GameActionType.SEND_RESOURCES, actions[2].Action); Assert.AreEqual(GameActionType.TRAIN_ARMY, actions[3].Action); Assert.AreEqual(GameActionType.SEND_ARMY, actions[4].Action); Assert.IsNotNull((actions[1] as TrainArmyAction).UnitsToTrain); Assert.IsTrue((actions[1] as TrainArmyAction).UnitsToTrain.Any()); Assert.AreEqual(typeof(SendResourcesAction), actions[2].GetType()); Assert.AreEqual((actions[2] as SendResourcesAction).To.Name, "uservillage2"); Assert.IsNotNull((actions[3] as TrainArmyAction).UnitsToTrain); Assert.IsTrue((actions[3] as TrainArmyAction).UnitsToTrain.Any()); Assert.AreEqual(3, (actions[1] as TrainArmyAction).UnitsToTrain.Count); Assert.AreEqual(swordsman.LocalizedNameRu, (actions[1] as TrainArmyAction).UnitsToTrain.Keys.ToList()[0]); Assert.AreEqual(theutates_thunder.LocalizedNameRu, (actions[1] as TrainArmyAction).UnitsToTrain.Keys.ToList()[1]); Assert.AreEqual(trebuchet.LocalizedNameRu, (actions[1] as TrainArmyAction).UnitsToTrain.Keys.ToList()[2]); Assert.AreEqual(1, (actions[3] as TrainArmyAction).UnitsToTrain.Count); Assert.AreEqual(phalang.LocalizedNameRu, (actions[3] as TrainArmyAction).UnitsToTrain.Keys.ToList()[0]); }
public void Test_One_Village_With_Building_Being_Built() { var testVillage = PrepareTestOneVillage(); var resultVillage = _serviceProviderBuilder.Mapper.Map <Village>(testVillage); var finishTime = TimeSpan.FromMinutes(30); resultVillage.Dorf1BuildTimeLeft = finishTime; resultVillage.CanBuild = false; resultVillage.Resources = new Resources { Lumber = 100, Clay = 100, Iron = 100, Crop = 100 }; resultVillage.Warehourse = 800; resultVillage.Granary = 800; var infoResult = Builder <BaseScenarioResult> .CreateNew() .With(x => x.Success = true) .With(x => x.Villages = new List <Village> { resultVillage }) .Build(); var gamePlayMock = new Mock <IGameplayClient>(); gamePlayMock.Setup(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <GameAction> >())) .Returns(Task.FromResult(infoResult)); gamePlayMock.Setup(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <BuildAction> >())) .Returns(Task.FromResult(new BaseScenarioResult())); _serviceProviderBuilder.WithService(gamePlayMock.Object); var cmd = new FakeCommand(); var cmdMock = new Mock <ICommandFactory>(); cmdMock.Setup(x => x.GetQueueableCommand(nameof(BuildCommand), It.IsAny <long>())) .Returns(cmd); _serviceProviderBuilder.WithService(cmdMock.Object); var job = new BuildingPlanExecutionJob(_serviceProviderBuilder.Build()); var data = new JobExecutionData { TravianUser = FakeDataProvider.GetUser(PlayerStatus.ALL_QUIET, true), JobType = typeof(BuildingPlanExecutionJob) }; var now = DateTimeOffset.Now; Assert.DoesNotThrowAsync(async() => await job.Execute(data)); _serviceProviderBuilder.LoggerMock.Verify(x => x.Log( LogLevel.Error, It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Never); gamePlayMock.Verify(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <GameAction> >()), Times.Once); gamePlayMock.Verify(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <BuildAction> >()), Times.Never); var diff = cmd.Start - now - finishTime; Assert.IsTrue(diff <= TimeSpan.FromSeconds(3)); // will fail while debugging because of date time now Assert.IsTrue(diff >= TimeSpan.FromSeconds(2)); }
private AbstractJob PrepareOneVillageTest(BaseScenarioResult buildScenarioResult, BaseScenarioResult infoScenarioResult) { var testVillage = PrepareTestOneVillage(isBuildingFeatureOn: true); var resultVillage = _serviceProviderBuilder.Mapper.Map <Village>(testVillage); var finishTime = TimeSpan.FromMinutes(30); resultVillage.Dorf1BuildTimeLeft = finishTime; resultVillage.CanBuild = true; resultVillage.Resources = new Resources { Lumber = 100, Clay = 100, Iron = 100, Crop = 100 }; resultVillage.Warehourse = 800; resultVillage.Granary = 800; var infoResult = Builder <BaseScenarioResult> .CreateNew() .With(x => x.Success = true) .With(x => x.Villages = new List <Village> { resultVillage }) .Build(); var actionProviderMock = new Mock <IActionProvider>(); actionProviderMock.Setup(x => x.GetBuildActions(resultVillage, It.IsAny <IEnumerable <BuildingModel> >())) .Returns(Task.FromResult((true, (IEnumerable <BuildAction>) new List <BuildAction> { new BuildAction { Village = resultVillage, BuildingId = "test", BuildingSlot = "test", Level = 1 } }))); _serviceProviderBuilder.WithService(actionProviderMock.Object); var buildActionResult = Builder <BaseScenarioResult> .CreateNew() .With(x => x.Success = true) .With(x => x.Villages = new List <Village> { Builder <Village> .CreateNew() .With(y => y.CanBuild = false) .With(y => y.Dorf1BuildTimeLeft = TimeSpan.FromMinutes(5)) .Build() }) .Build(); var gamePlayMock = new Mock <IGameplayClient>(); gamePlayMock.Setup(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <GameAction> >())) .Returns(Task.FromResult(infoResult)); gamePlayMock.Setup(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <BuildAction> >())) .Returns(Task.FromResult(buildActionResult)); _serviceProviderBuilder.WithService(gamePlayMock.Object); var cmd = new FakeCommand(); var cmdMock = new Mock <ICommandFactory>(); cmdMock.Setup(x => x.GetQueueableCommand(nameof(BuildCommand), It.IsAny <long>())) .Returns(cmd); _serviceProviderBuilder.WithService(cmdMock.Object); var job = new BuildingPlanExecutionJob(_serviceProviderBuilder.Build()); var data = new JobExecutionData { TravianUser = FakeDataProvider.GetUser(PlayerStatus.ALL_QUIET, true), JobType = typeof(BuildingPlanExecutionJob) }; return(job); }
public async Task Test_Three_Villages_Build_Action_Returns_Error(BuildErrorType errorType) { var testVillage = GetVillage(isBuildingFeatureOn: true); var resultVillage = _serviceProviderBuilder.Mapper.Map <Village>(testVillage); var finishTime = TimeSpan.FromMinutes(30); resultVillage.Dorf1BuildTimeLeft = finishTime; resultVillage.CanBuild = true; resultVillage.Resources = new Resources { Lumber = 100, Clay = 100, Iron = 100, Crop = 100 }; resultVillage.Warehourse = 800; resultVillage.Granary = 800; var testVillage2 = GetVillage(isBuildingFeatureOn: true); var resultVillage2 = _serviceProviderBuilder.Mapper.Map <Village>(testVillage2); var finishTime2 = TimeSpan.FromMinutes(45); resultVillage2.Dorf1BuildTimeLeft = finishTime2; resultVillage2.CanBuild = true; resultVillage2.Resources = new Resources { Lumber = 100, Clay = 100, Iron = 100, Crop = 100 }; resultVillage2.Warehourse = 800; resultVillage2.Granary = 800; var testVillage3 = GetVillage(isBuildingFeatureOn: false); resultVillage2.Resources = new Resources { Lumber = 100, Clay = 100, Iron = 100, Crop = 100 }; resultVillage2.Warehourse = 800; resultVillage2.Granary = 800; var allVillages = new List <VillageModel> { testVillage, testVillage2, testVillage3 }; var villageRepo = new Mock <IVillageRepository>(); villageRepo.Setup(x => x.GetVillages(_serviceProviderBuilder.TravianUser.UserName)) .Returns(Task.FromResult((IEnumerable <VillageModel>)allVillages)); villageRepo.Setup(x => x.GetVillage(It.IsAny <int>(), It.IsAny <int>())) .Returns((int x, int y) => { return(Task.FromResult(allVillages.FirstOrDefault(z => z.CoordinateX == x && z.CoordinateY == y))); }); _serviceProviderBuilder.WithService(villageRepo.Object); var infoResult = Builder <BaseScenarioResult> .CreateNew() .With(x => x.Success = true) .With(x => x.Villages = new List <Village> { resultVillage2 }) .Build(); var actionProviderMock = new Mock <IActionProvider>(); actionProviderMock.Setup(x => x.GetBuildActions(resultVillage, It.IsAny <IEnumerable <BuildingModel> >())) .Returns(Task.FromResult((true, (IEnumerable <BuildAction>) new List <BuildAction> { new BuildAction { Village = resultVillage, BuildingId = "test", BuildingSlot = "test", Level = 1 } }))); actionProviderMock.Setup(x => x.GetBuildActions(resultVillage2, It.IsAny <IEnumerable <BuildingModel> >())) .Returns(Task.FromResult((true, (IEnumerable <BuildAction>) new List <BuildAction> { new BuildAction { Village = resultVillage2, BuildingId = "test", BuildingSlot = "test", Level = 1 } }))); _serviceProviderBuilder.WithService(actionProviderMock.Object); var buildActionResult = Builder <BaseScenarioResult> .CreateNew() .With(x => x.Success = true) .With(x => x.Villages = new List <Village> { Builder <Village> .CreateNew() .With(y => y.CanBuild = false) .With(y => y.Dorf1BuildTimeLeft = TimeSpan.FromMinutes(2)) .With(y => y.CoordinateX = resultVillage.CoordinateX) .With(y => y.CoordinateY = resultVillage.CoordinateY) .With(y => y.Warehourse = 800) .With(y => y.Granary = 800) .With(y => y.Resources = Builder <Resources> .CreateNew().Build()) .With(y => y.ResourcesProduction = Builder <Resources> .CreateNew().Build()) .Build(), Builder <Village> .CreateNew() .With(y => y.CanBuild = false) .With(y => y.Dorf1BuildTimeLeft = TimeSpan.FromMinutes(5)) .With(y => y.CoordinateX = resultVillage2.CoordinateX) .With(y => y.CoordinateY = resultVillage2.CoordinateY) .Build() }) .With(x => x.Errors = new List <ScenarioError> { Builder <BuildScenarioError> .CreateNew() .With(y => y.BuildErrorType = errorType) .With(y => y.Village = resultVillage) .Build() }) .Build(); var gamePlayMock = new Mock <IGameplayClient>(); gamePlayMock.Setup(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <GameAction> >())) .Returns(Task.FromResult(infoResult)); gamePlayMock.Setup(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <BuildAction> >())) .Returns(Task.FromResult(buildActionResult)); _serviceProviderBuilder.WithService(gamePlayMock.Object); var cmd = new FakeCommand(); var cmdMock = new Mock <ICommandFactory>(); cmdMock.Setup(x => x.GetQueueableCommand(nameof(BuildCommand), It.IsAny <long>())) .Returns(cmd); _serviceProviderBuilder.WithService(cmdMock.Object); var job = new BuildingPlanExecutionJob(_serviceProviderBuilder.Build()); var travianUser = FakeDataProvider.GetUser(PlayerStatus.ALL_QUIET, true); var data = new JobExecutionData { TravianUser = travianUser, JobType = typeof(BuildingPlanExecutionJob) }; var now = DateTimeOffset.Now; Assert.DoesNotThrowAsync(async() => await job.Execute(data)); _serviceProviderBuilder.LoggerMock.Verify(x => x.Log( LogLevel.Error, It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Never); gamePlayMock.Verify(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <BuildAction> >()), Times.Once); var expected = errorType == BuildErrorType.NoSpaceInQueue ? TimeSpan.FromMinutes(2) : Calculator.CalculateTimeForUnit( resultVillage, _serviceProviderBuilder.Mapper.Map <Village>(testVillage3), await _serviceProviderBuilder.UnitRepository.GetTrader(travianUser.PlayerData.Tribe)); var diff = cmd.Start - now - TimeSpan.FromMinutes(2); Assert.IsTrue(diff <= TimeSpan.FromSeconds(3)); // will fail while debugging because of date time now Assert.IsTrue(diff >= TimeSpan.FromSeconds(2)); buildActionResult = Builder <BaseScenarioResult> .CreateNew() .With(x => x.Success = true) .With(x => x.Villages = new List <Village> { Builder <Village> .CreateNew() .With(y => y.CanBuild = true) .With(y => y.CoordinateX = resultVillage.CoordinateX) .With(y => y.CoordinateY = resultVillage.CoordinateY) .Build(), Builder <Village> .CreateNew() .With(y => y.CanBuild = false) .With(y => y.Dorf1BuildTimeLeft = TimeSpan.FromMinutes(5)) .With(y => y.CoordinateX = resultVillage2.CoordinateX) .With(y => y.CoordinateY = resultVillage2.CoordinateY) .Build() }) .With(x => x.Errors = new List <ScenarioError> { Builder <BuildScenarioError> .CreateNew() .With(y => y.BuildErrorType = errorType) .With(y => y.Village = resultVillage) .Build() }) .Build(); gamePlayMock = new Mock <IGameplayClient>(); gamePlayMock.Setup(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <GameAction> >())) .Returns(Task.FromResult(infoResult)); gamePlayMock.Setup(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <BuildAction> >())) .Returns(Task.FromResult(buildActionResult)); _serviceProviderBuilder.WithService(gamePlayMock.Object); job = new BuildingPlanExecutionJob(_serviceProviderBuilder.Build()); now = DateTimeOffset.Now; Assert.DoesNotThrowAsync(async() => await job.Execute(data)); _serviceProviderBuilder.LoggerMock.Verify(x => x.Log( LogLevel.Error, It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Never); gamePlayMock.Verify(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <BuildAction> >()), Times.Once); diff = cmd.Start - now; Assert.IsTrue(diff <= TimeSpan.FromSeconds(3)); // will fail while debugging because of date time now Assert.IsTrue(diff >= TimeSpan.FromSeconds(2)); }
[TestCase(true, BuildErrorType.NoSpaceInQueue)] // test one village with build action returns no space in queue error //[TestCase(true, BuildErrorType.NotEnoughResources)] public void Test_One_Village_Rescheduling_Next_Execution_Time(bool noSpaceInQueueError, BuildErrorType buildErrorType) { var testVillage = PrepareTestOneVillage(isBuildingFeatureOn: true); var resultVillage = _serviceProviderBuilder.Mapper.Map <Village>(testVillage); var finishTime = TimeSpan.FromMinutes(30); resultVillage.Dorf1BuildTimeLeft = finishTime; resultVillage.CanBuild = true; resultVillage.Resources = new Resources { Lumber = 100, Clay = 100, Iron = 100, Crop = 100 }; resultVillage.Warehourse = 800; resultVillage.Granary = 800; var infoResult = Builder <BaseScenarioResult> .CreateNew() .With(x => x.Success = true) .With(x => x.Villages = new List <Village> { resultVillage }) .Build(); var actionProviderMock = new Mock <IActionProvider>(); actionProviderMock.Setup(x => x.GetBuildActions(resultVillage, It.IsAny <IEnumerable <BuildingModel> >())) .Returns(Task.FromResult((true, (IEnumerable <BuildAction>) new List <BuildAction> { new BuildAction { Village = resultVillage, BuildingId = "test", BuildingSlot = "test", Level = 1 } }))); _serviceProviderBuilder.WithService(actionProviderMock.Object); var buildActionResult = Builder <BaseScenarioResult> .CreateNew() .With(x => x.Success = true) .With(x => x.Villages = new List <Village> { Builder <Village> .CreateNew() .With(y => y.CoordinateX = resultVillage.CoordinateX) .With(y => y.CoordinateY = resultVillage.CoordinateY) .With(y => y.CanBuild = false) .With(y => y.Dorf1BuildTimeLeft = TimeSpan.FromMinutes(5)) .Build() }) .With(x => x.Errors = noSpaceInQueueError ? new List <ScenarioError> { Builder <BuildScenarioError> .CreateNew() .With(y => y.BuildErrorType = buildErrorType) .With(y => y.Village = resultVillage) .Build() } : new List <ScenarioError>()) .Build(); var gamePlayMock = new Mock <IGameplayClient>(); gamePlayMock.Setup(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <GameAction> >())) .Returns(Task.FromResult(infoResult)); gamePlayMock.Setup(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <BuildAction> >())) .Returns(Task.FromResult(buildActionResult)); _serviceProviderBuilder.WithService(gamePlayMock.Object); var cmd = new FakeCommand(); var cmdMock = new Mock <ICommandFactory>(); cmdMock.Setup(x => x.GetQueueableCommand(nameof(BuildCommand), It.IsAny <long>())) .Returns(cmd); _serviceProviderBuilder.WithService(cmdMock.Object); var job = new BuildingPlanExecutionJob(_serviceProviderBuilder.Build()); var data = new JobExecutionData { TravianUser = FakeDataProvider.GetUser(PlayerStatus.ALL_QUIET, true), JobType = typeof(BuildingPlanExecutionJob) }; var now = DateTimeOffset.Now; Assert.DoesNotThrowAsync(async() => await job.Execute(data)); _serviceProviderBuilder.LoggerMock.Verify(x => x.Log( LogLevel.Error, It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Never); gamePlayMock.Verify(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <BuildAction> >()), Times.Once); var diff = cmd.Start - now - TimeSpan.FromMinutes(5); Assert.IsTrue(diff <= TimeSpan.FromSeconds(3)); // will fail while debugging because of date time now Assert.IsTrue(diff >= TimeSpan.FromSeconds(2)); }