CodeCleanup

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

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