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 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 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 void JobChanged(ObserverPoller poller, ObserverServer server, ObserverJob job, ChangeType changeType) { var settings = Data.Settings; var enabled = (server?.Healthy ?? true) && (job?.Enabled ?? true); switch (changeType) { case ChangeType.BuildCompleted: if (!(enabled && settings.EnableNotifications) || settings.AlertOnChangesOnly) break; _notifyIcon.ShowBalloonTip(2000, "Build Complete", $"Job: {job?.DisplayName} is no longer building", ToolTipIcon.Warning); break; case ChangeType.BuildStarted: if (!(enabled && settings.EnableNotifications) || settings.AlertOnChangesOnly) break; _notifyIcon.ShowBalloonTip(2000, "Build Started", $"Job: {job?.DisplayName} is now building", ToolTipIcon.Warning); break; case ChangeType.BuildStatusChange: if (!(enabled && settings.EnableNotifications)) break; _notifyIcon.ShowBalloonTip(2000, "Status Change", $"Job: {job?.DisplayName} is now {job?.Status}", ToolTipIcon.Warning); break; case ChangeType.MissingJob: _notifyIcon.ShowBalloonTip(2000, "Job No Longer Exists", $"Job: {job?.DisplayName} no longer exists", ToolTipIcon.Warning); break; case ChangeType.NewJobFound: _notifyIcon.ShowBalloonTip(2000, "New Job", $"Job: {job?.DisplayName} has been found", ToolTipIcon.Warning); break; case ChangeType.ErrorPollingServer: _notifyIcon.ShowBalloonTip(10000, "Error Polling Server", $"Server: '{server?.Name}' at '{server?.Url}' could not be polled", ToolTipIcon.Error); break; case ChangeType.ErrorPollingJob: _notifyIcon.ShowBalloonTip(10000, "Error Polling Job", $"Job: '{job?.DisplayName ?? job?.Name}' in '{server?.Name}' could not be polled", ToolTipIcon.Error); break; default: throw new ArgumentOutOfRangeException(nameof(changeType), changeType, null); } if (enabled && settings.EnableSounds) { switch (changeType) { case ChangeType.BuildCompleted: case ChangeType.BuildStarted: SystemSounds.Beep.Play(); break; case ChangeType.BuildStatusChange: SystemSounds.Hand.Play(); break; case ChangeType.MissingJob: case ChangeType.ErrorPollingServer: case ChangeType.ErrorPollingJob: SystemSounds.Asterisk.Play(); break; } } }