CodeCleanup

pull/385/head
tidusjar 9 years ago
parent 2d0b87b558
commit a5899ff4af

@ -1,33 +1,31 @@
#region Copyright #region Copyright
/************************************************************************ // /************************************************************************
Copyright (c) 2016 Jamie Rees // Copyright (c) 2016 Jamie Rees
File: NotificationServiceTests.cs // File: UserRequestLimitResetterTests.cs
Created By: Jamie Rees // Created By: Jamie Rees
//
Permission is hereby granted, free of charge, to any person obtaining // Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the // a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including // "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, // without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to // distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to // permit persons to whom the Software is furnished to do so, subject to
the following conditions: // the following conditions:
//
The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
************************************************************************/ // ************************************************************************/
#endregion #endregion
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Moq; using Moq;
@ -37,7 +35,6 @@ using PlexRequests.Core;
using PlexRequests.Core.SettingModels; using PlexRequests.Core.SettingModels;
using PlexRequests.Services.Interfaces; using PlexRequests.Services.Interfaces;
using PlexRequests.Services.Jobs; using PlexRequests.Services.Jobs;
using PlexRequests.Services.Notification;
using PlexRequests.Store; using PlexRequests.Store;
using PlexRequests.Store.Models; using PlexRequests.Store.Models;
using PlexRequests.Store.Repository; using PlexRequests.Store.Repository;
@ -51,13 +48,6 @@ namespace PlexRequests.Services.Tests
[TestFixture] [TestFixture]
public class UserRequestLimitResetterTests public class UserRequestLimitResetterTests
{ {
public UserRequestLimitResetter Resetter { get; set; }
private Mock<IJobRecord> JobMock { get; set; }
private Mock<IRepository<RequestLimit>> RepoMock { get; set; }
private Mock<ISettingsService<PlexRequestSettings>> SettingsMock { get; set; }
private Mock<IJobExecutionContext> ContextMock { get; set; }
private Fixture F { get; set; }
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
@ -79,24 +69,19 @@ namespace PlexRequests.Services.Tests
JobMock.Verify(x => x.Record(It.IsAny<string>()), Times.Once()); JobMock.Verify(x => x.Record(It.IsAny<string>()), Times.Once());
} }
[Test] public UserRequestLimitResetter Resetter { get; set; }
public void ResetTurnedOff() private Mock<IJobRecord> JobMock { get; set; }
{ private Mock<IRepository<RequestLimit>> RepoMock { get; set; }
SetupSettings(0, 0, 0); private Mock<ISettingsService<PlexRequestSettings>> SettingsMock { get; set; }
Resetter = new UserRequestLimitResetter(JobMock.Object, RepoMock.Object, SettingsMock.Object); private Mock<IJobExecutionContext> ContextMock { get; set; }
private Fixture F { get; set; }
Resetter.Execute(ContextMock.Object);
RepoMock.Verify(x => x.Delete(It.IsAny<RequestLimit>()), Times.Never);
}
[TestCaseSource(nameof(ResetData))] [TestCaseSource(nameof(ResetData))]
public void Reset(int movie, int tv, int album, RequestType type) public void Reset(int movie, int tv, int album, RequestType type)
{ {
SetupSettings(movie, tv, album); SetupSettings(movie, tv, album);
RepoMock.Setup(x => x.GetAll()).Returns(F.Build<RequestLimit>() RepoMock.Setup(x => x.GetAll())
.With(x => x.FirstRequestDate, DateTime.Now.AddDays(-8)) .Returns(F.Build<RequestLimit>().With(x => x.FirstRequestDate, DateTime.Now.AddDays(-8)).With(x => x.RequestType, type).CreateMany());
.With(x => x.RequestType, type).CreateMany());
Resetter = new UserRequestLimitResetter(JobMock.Object, RepoMock.Object, SettingsMock.Object); Resetter = new UserRequestLimitResetter(JobMock.Object, RepoMock.Object, SettingsMock.Object);
@ -109,9 +94,8 @@ namespace PlexRequests.Services.Tests
public void DoNotReset(int days, RequestType type) public void DoNotReset(int days, RequestType type)
{ {
SetupSettings(1, 1, 1); SetupSettings(1, 1, 1);
RepoMock.Setup(x => x.GetAll()).Returns(F.Build<RequestLimit>() RepoMock.Setup(x => x.GetAll())
.With(x => x.FirstRequestDate, DateTime.Now.AddDays(days)) .Returns(F.Build<RequestLimit>().With(x => x.FirstRequestDate, DateTime.Now.AddDays(days)).With(x => x.RequestType, type).CreateMany());
.With(x => x.RequestType, type).CreateMany());
Resetter = new UserRequestLimitResetter(JobMock.Object, RepoMock.Object, SettingsMock.Object); Resetter = new UserRequestLimitResetter(JobMock.Object, RepoMock.Object, SettingsMock.Object);
@ -125,7 +109,7 @@ namespace PlexRequests.Services.Tests
new TestCaseData(1, 0, 0, RequestType.Movie).SetName("Reset Movies"), new TestCaseData(1, 0, 0, RequestType.Movie).SetName("Reset Movies"),
new TestCaseData(0, 1, 0, RequestType.TvShow).SetName("Reset TV Shows"), new TestCaseData(0, 1, 0, RequestType.TvShow).SetName("Reset TV Shows"),
new TestCaseData(0, 0, 1, RequestType.Album).SetName("Reset Albums"), new TestCaseData(0, 0, 1, RequestType.Album).SetName("Reset Albums"),
new TestCaseData(1,1,1,RequestType.Album).SetName("Reset Albums with all enabled"), new TestCaseData(1, 1, 1, RequestType.Album).SetName("Reset Albums with all enabled")
}; };
private static readonly IEnumerable<TestCaseData> DoNotResetData = new List<TestCaseData> private static readonly IEnumerable<TestCaseData> DoNotResetData = new List<TestCaseData>
@ -138,7 +122,7 @@ namespace PlexRequests.Services.Tests
new TestCaseData(-4, RequestType.Movie).SetName("-4 Day(s)"), new TestCaseData(-4, RequestType.Movie).SetName("-4 Day(s)"),
new TestCaseData(-5, RequestType.TvShow).SetName("-5 Day(s)"), new TestCaseData(-5, RequestType.TvShow).SetName("-5 Day(s)"),
new TestCaseData(-6, RequestType.Movie).SetName("-6 Day(s)"), new TestCaseData(-6, RequestType.Movie).SetName("-6 Day(s)"),
new TestCaseData(0, RequestType.TvShow).SetName("0 Day(s)"), new TestCaseData(0, RequestType.TvShow).SetName("0 Day(s)")
}; };
private void SetupSettings(int movie, int tv, int album) private void SetupSettings(int movie, int tv, int album)
@ -147,5 +131,15 @@ namespace PlexRequests.Services.Tests
.Returns(new PlexRequestSettings { MovieWeeklyRequestLimit = movie, TvWeeklyRequestLimit = tv, AlbumWeeklyRequestLimit = album }); .Returns(new PlexRequestSettings { MovieWeeklyRequestLimit = movie, TvWeeklyRequestLimit = tv, AlbumWeeklyRequestLimit = album });
} }
[Test]
public void ResetTurnedOff()
{
SetupSettings(0, 0, 0);
Resetter = new UserRequestLimitResetter(JobMock.Object, RepoMock.Object, SettingsMock.Object);
Resetter.Execute(ContextMock.Object);
RepoMock.Verify(x => x.Delete(It.IsAny<RequestLimit>()), Times.Never);
}
} }
} }

@ -43,38 +43,26 @@ namespace PlexRequests.Services.Jobs
{ {
public class UserRequestLimitResetter : IJob public class UserRequestLimitResetter : IJob
{ {
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
public UserRequestLimitResetter(IJobRecord record, IRepository<RequestLimit> repo, ISettingsService<PlexRequestSettings> settings) public UserRequestLimitResetter(IJobRecord record, IRepository<RequestLimit> repo, ISettingsService<PlexRequestSettings> settings)
{ {
Record = record; Record = record;
Repo = repo; Repo = repo;
Settings = settings; Settings = settings;
} }
private IJobRecord Record { get; } private IJobRecord Record { get; }
private IRepository<RequestLimit> Repo { get; } private IRepository<RequestLimit> Repo { get; }
private ISettingsService<PlexRequestSettings> Settings { get; } private ISettingsService<PlexRequestSettings> Settings { get; }
private static Logger Log = LogManager.GetCurrentClassLogger(); public void AlbumLimit(PlexRequestSettings s, IEnumerable<RequestLimit> allUsers)
public void Execute(IJobExecutionContext context)
{
try
{
var settings = Settings.GetSettings();
var users = Repo.GetAll();
var requestLimits = users as RequestLimit[] ?? users.ToArray();
MovieLimit(settings, requestLimits);
TvLimit(settings, requestLimits);
AlbumLimit(settings, requestLimits);
}
catch (Exception e)
{ {
Log.Error(e); if (s.AlbumWeeklyRequestLimit == 0)
}
finally
{ {
Record.Record(JobNames.RequestLimitReset); return; // The limit has not been set
} }
CheckAndDelete(allUsers, RequestType.Album);
} }
public void MovieLimit(PlexRequestSettings s, IEnumerable<RequestLimit> allUsers) public void MovieLimit(PlexRequestSettings s, IEnumerable<RequestLimit> allUsers)
@ -95,15 +83,6 @@ namespace PlexRequests.Services.Jobs
CheckAndDelete(allUsers, RequestType.TvShow); CheckAndDelete(allUsers, RequestType.TvShow);
} }
public void AlbumLimit(PlexRequestSettings s, IEnumerable<RequestLimit> allUsers)
{
if (s.AlbumWeeklyRequestLimit == 0)
{
return; // The limit has not been set
}
CheckAndDelete(allUsers, RequestType.Album);
}
private void CheckAndDelete(IEnumerable<RequestLimit> allUsers, RequestType type) private void CheckAndDelete(IEnumerable<RequestLimit> allUsers, RequestType type)
{ {
var users = allUsers.Where(x => x.RequestType == type); var users = allUsers.Where(x => x.RequestType == type);
@ -116,5 +95,27 @@ namespace PlexRequests.Services.Jobs
} }
} }
} }
public void Execute(IJobExecutionContext context)
{
try
{
var settings = Settings.GetSettings();
var users = Repo.GetAll();
var requestLimits = users as RequestLimit[] ?? users.ToArray();
MovieLimit(settings, requestLimits);
TvLimit(settings, requestLimits);
AlbumLimit(settings, requestLimits);
}
catch (Exception e)
{
Log.Error(e);
}
finally
{
Record.Record(JobNames.RequestLimitReset);
}
}
} }
} }
Loading…
Cancel
Save