From 482cbc20a32b5d724bb067b91e57d51136c3062a Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 24 May 2013 18:51:25 -0700 Subject: [PATCH] Notification schema added to server side. --- .../Notifications/NotificationSchemaModule.cs | 42 ++++++++++ NzbDrone.Api/NzbDrone.Api.csproj | 1 + .../NotificationServiceFixture.cs | 80 +++++++++++++++++++ NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 1 + .../Notifications/NotificationService.cs | 27 +++++++ UI/Calendar/CalendarCollection.js | 2 +- 6 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 NzbDrone.Api/Notifications/NotificationSchemaModule.cs create mode 100644 NzbDrone.Core.Test/NotificationTests/NotificationServiceFixture.cs diff --git a/NzbDrone.Api/Notifications/NotificationSchemaModule.cs b/NzbDrone.Api/Notifications/NotificationSchemaModule.cs new file mode 100644 index 000000000..b5e839384 --- /dev/null +++ b/NzbDrone.Api/Notifications/NotificationSchemaModule.cs @@ -0,0 +1,42 @@ +using System.Collections.Generic; +using NzbDrone.Api.ClientSchema; +using NzbDrone.Common.Reflection; +using NzbDrone.Core.Annotations; +using NzbDrone.Core.Notifications; +using Omu.ValueInjecter; + +namespace NzbDrone.Api.Notifications +{ + public class NotificationSchemaModule : NzbDroneRestModule + { + private readonly INotificationService _notificationService; + + public NotificationSchemaModule(INotificationService notificationService) + : base("notification/schema") + { + _notificationService = notificationService; + + GetResourceAll = GetAll; + } + + private List GetAll() + { + //Need to get all the possible Notification's same as we would for settiings (but keep them empty) + + var notifications = _notificationService.Schema(); + + var result = new List(notifications.Count); + + foreach (var notification in notifications) + { + var notificationResource = new NotificationResource(); + notificationResource.InjectFrom(notification); + notificationResource.Fields = SchemaBuilder.GenerateSchema(notification.Settings); + + result.Add(notificationResource); + } + + return result; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Api/NzbDrone.Api.csproj b/NzbDrone.Api/NzbDrone.Api.csproj index aa3daa5ce..d994c721b 100644 --- a/NzbDrone.Api/NzbDrone.Api.csproj +++ b/NzbDrone.Api/NzbDrone.Api.csproj @@ -119,6 +119,7 @@ + diff --git a/NzbDrone.Core.Test/NotificationTests/NotificationServiceFixture.cs b/NzbDrone.Core.Test/NotificationTests/NotificationServiceFixture.cs new file mode 100644 index 000000000..f3dea7777 --- /dev/null +++ b/NzbDrone.Core.Test/NotificationTests/NotificationServiceFixture.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Common.Composition; +using NzbDrone.Core.Lifecycle; +using NzbDrone.Core.Notifications; +using NzbDrone.Core.Notifications.Growl; +using NzbDrone.Core.Notifications.Plex; +using NzbDrone.Core.Notifications.Prowl; +using NzbDrone.Core.Notifications.Smtp; +using NzbDrone.Core.Notifications.Xbmc; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.NotificationTests +{ + public class NotificationServiceFixture : DbTest + { + private List _notifications; + + [SetUp] + public void Setup() + { + _notifications = new List(); + + _notifications.Add(new Xbmc(null, null)); + _notifications.Add(new PlexClient(null)); + _notifications.Add(new PlexServer(null)); + _notifications.Add(new Smtp(null)); + _notifications.Add(new Growl(null)); + _notifications.Add(new Prowl(null)); + + Mocker.SetConstant>(_notifications); + } + + [Test] + public void getting_list_of_indexers_should_be_empty_by_default() + { + Mocker.SetConstant(Mocker.Resolve()); + + var notifications = Subject.All().ToList(); + notifications.Should().BeEmpty(); + } + + [Test] + public void should_be_able_to_get_schema_for_all_notifications() + { + Mocker.SetConstant(Mocker.Resolve()); + + Mocker.GetMock().Setup(s => s.Resolve(typeof (Xbmc))) + .Returns(new Xbmc(null, null)); + + Mocker.GetMock().Setup(s => s.Resolve(typeof(PlexClient))) + .Returns(new PlexClient(null)); + + Mocker.GetMock().Setup(s => s.Resolve(typeof(PlexServer))) + .Returns(new PlexServer(null)); + + Mocker.GetMock().Setup(s => s.Resolve(typeof(Smtp))) + .Returns(new Smtp(null)); + + Mocker.GetMock().Setup(s => s.Resolve(typeof(Growl))) + .Returns(new Growl(null)); + + Mocker.GetMock().Setup(s => s.Resolve(typeof(Prowl))) + .Returns(new Prowl(null)); + + var notifications = Subject.Schema().ToList(); + notifications.Should().NotBeEmpty(); + notifications.Should().NotContain(c => c.Settings == null); + notifications.Should().NotContain(c => c.Instance == null); + notifications.Should().NotContain(c => c.Name == null); + notifications.Select(c => c.Name).Should().OnlyHaveUniqueItems(); + notifications.Select(c => c.Instance).Should().OnlyHaveUniqueItems(); + notifications.Select(c => c.Id).Should().OnlyHaveUniqueItems(); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index a329ce542..ea8b3676d 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -146,6 +146,7 @@ + diff --git a/NzbDrone.Core/Notifications/NotificationService.cs b/NzbDrone.Core/Notifications/NotificationService.cs index ccc0672b4..c4715e3cf 100644 --- a/NzbDrone.Core/Notifications/NotificationService.cs +++ b/NzbDrone.Core/Notifications/NotificationService.cs @@ -15,6 +15,7 @@ public interface INotificationService { List All(); Notification Get(int id); + List Schema(); } public class NotificationService @@ -49,6 +50,32 @@ public Notification Get(int id) return ToNotification(_notificationRepository.Get(id)); } + public List Schema() + { + var notifications = new List(); + + int i = 1; + foreach (var notification in _notifications) + { + i++; + var type = notification.GetType(); + + var newNotification = new Notification(); + newNotification.Instance = (INotification)_container.Resolve(type); + newNotification.Id = i; + newNotification.Name = notification.Name; + + var instanceType = newNotification.Instance.GetType(); + var baseGenArgs = instanceType.BaseType.GetGenericArguments(); + newNotification.Settings = (INotifcationSettings) Activator.CreateInstance(baseGenArgs[0]); + newNotification.Implementation = type.Name; + + notifications.Add(newNotification); + } + + return notifications; + } + private Notification ToNotification(NotificationDefinition definition) { var notification = new Notification(); diff --git a/UI/Calendar/CalendarCollection.js b/UI/Calendar/CalendarCollection.js index 047638819..b1daaf248 100644 --- a/UI/Calendar/CalendarCollection.js +++ b/UI/Calendar/CalendarCollection.js @@ -4,7 +4,7 @@ define(['app', 'Series/EpisodeModel'], function () { url : NzbDrone.Constants.ApiRoot + '/calendar', model : NzbDrone.Series.EpisodeModel, comparator: function (model) { - return model.get('start'); + return model.get('airDate'); } }); }); \ No newline at end of file