From 2a7e10364ec43bd5ec3d0eb7535fcff6f135ce98 Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Sun, 2 Feb 2020 13:05:14 -0500 Subject: [PATCH] Added basic unit tests for all controllers --- Aurora.test/ControllerTests/EventTests.cs | 100 ++++++++++++++++++ .../ControllerTests/MediaControllerTest.cs | 39 ++++--- .../ControllerTests/MembersControllerTest.cs | 23 +--- .../ControllerTests/PartyControllerTest.cs | 22 +--- Aurora.test/ControllerTests/Setup.cs | 39 +++++++ Aurora/App.xaml.cs | 2 + .../Server/Controllers/Constructor.cs | 13 ++- .../Server/Controllers/PartyController.cs | 2 - .../Server/EventManager/EventManager.cs | 2 +- .../Server/EventManager/IEventManager.cs | 60 +++++++++++ .../Services/Server/Server/ServerService.cs | 15 ++- 11 files changed, 255 insertions(+), 62 deletions(-) create mode 100644 Aurora.test/ControllerTests/EventTests.cs create mode 100644 Aurora.test/ControllerTests/Setup.cs create mode 100644 Aurora/Services/Server/EventManager/IEventManager.cs diff --git a/Aurora.test/ControllerTests/EventTests.cs b/Aurora.test/ControllerTests/EventTests.cs new file mode 100644 index 0000000..16fba6f --- /dev/null +++ b/Aurora.test/ControllerTests/EventTests.cs @@ -0,0 +1,100 @@ + +using NUnit.Framework; +using Aurora.Proto.PartyV2; +using Aurora.Services.Server; +using Aurora.Services.Server.EventManager; +using Grpc.Core; +using System.Threading.Tasks; +using System.Threading; +using Autofac; +namespace Aurora.test.ControllerTests +{ + public class EventTest + { + private RemotePartyService.RemotePartyServiceClient _remotePartyService; + private Channel _channel; + private IContainer _container; + private IServerService _serverService; + + #region Setup + [SetUp] + public void Setup() + { + _container = SetupUtil.SetupOneTime(); + _remotePartyService = SetupUtil.Setup(ref _container, ref _serverService, ref _channel); + } + + [TearDown] + public async Task TearDown() + { + await _serverService.Stop(); + await _channel.ShutdownAsync(); + _container.Dispose(); + } + #endregion Setup + [Test] + [TestCase(EventType.MediaPlaying)] + public void Asdf(EventType value) + { + Assert.AreEqual(1, 1); + } + + [Test] + [TestCase(EventType.MediaPlaying)] + [TestCase(EventType.MediaStopped)] + [TestCase(EventType.MemberCreated)] + [TestCase(EventType.MemberDeleted)] + public async Task TestEventSubscriptions(EventType value) + { + using (var scope = _container.BeginLifetimeScope()) + { + IEventManager eventManager = scope.Resolve(); + + //Create new party member + Member member = _remotePartyService.CreateMember(new CreateMemberRequest() + { + Parent = "party1", + Member = new Member() + { + UserName = "newMember1", + IpAddress = ServerService.GetLocalIPAddress(), + } + }); + + //Subscribe to event type + _remotePartyService.CreateEventSubscription(new CreateEventSubscriptionRequest() + { + Parent = member.Name, + EventSubscription = new EventSubscription() + { + Type = value + } + }); + + BaseEvent @event = new BaseEvent + { + EventType = value, + }; + //Fire event + + CancellationTokenSource eventCancellationTokenSource = new CancellationTokenSource(); + BaseEvent newEvent = null; + + Task.Run(async () => { await Task.Delay(1000); eventManager.FireEvent(@event); }); + + using (AsyncServerStreamingCall eventStream = _remotePartyService + .GetEvents(new GetEventsRequest() { Parent = member.Name })) + { + while ((!eventCancellationTokenSource.Token.IsCancellationRequested && + await eventStream.ResponseStream.MoveNext(eventCancellationTokenSource.Token))) + { + newEvent = new BaseEvent(eventStream.ResponseStream.Current); + break; + } + }; + + Assert.AreEqual(newEvent.EventType, value); + } + } + } +} \ No newline at end of file diff --git a/Aurora.test/ControllerTests/MediaControllerTest.cs b/Aurora.test/ControllerTests/MediaControllerTest.cs index 0b296fe..9819313 100644 --- a/Aurora.test/ControllerTests/MediaControllerTest.cs +++ b/Aurora.test/ControllerTests/MediaControllerTest.cs @@ -1,9 +1,6 @@ using NUnit.Framework; using Aurora.Proto.PartyV2; using Aurora.Services.Server; -using Aurora.Services.Library; -using Aurora.Services.Settings; -using Aurora.test.Models.Mock; using Grpc.Core; using System.Threading.Tasks; using System.Linq; @@ -19,19 +16,11 @@ namespace Aurora.test.ControllerTests private IContainer _container; private IServerService _serverService; + #region Setup [OneTimeSetUp] public void SetupOneTime() { - ContainerBuilder builder = new ContainerBuilder(); - builder.RegisterType().As().SingleInstance(); - builder.RegisterInstance(new SettingsServiceMock() - { - Username = "Test User 1", - DefaultPort = 4005, - LibraryLocation = string.Format("{0}/Resources", Directory.GetCurrentDirectory()) - }).SingleInstance(); - builder.RegisterType().As().SingleInstance(); - _container = builder.Build(); + _container = SetupUtil.SetupOneTime(); } [OneTimeTearDown] @@ -43,10 +32,7 @@ namespace Aurora.test.ControllerTests [SetUp] public void Setup() { - _serverService = _container.Resolve(); - _serverService.Start("testParty", "asdf"); - _channel = new Channel(string.Format("{0}:{1}", ServerService.GetLocalIPAddress(), 8080), ChannelCredentials.Insecure); - _remotePartyService = new RemotePartyService.RemotePartyServiceClient(_channel); + _remotePartyService = SetupUtil.Setup(ref _container, ref _serverService, ref _channel); } [TearDown] @@ -55,6 +41,7 @@ namespace Aurora.test.ControllerTests await _serverService.Stop(); await _channel.ShutdownAsync(); } + #endregion Setup [Test] public void TestNotEmpty() @@ -69,5 +56,23 @@ namespace Aurora.test.ControllerTests Assert.NotZero(resp.Media.Count); Assert.AreEqual(resp.Media.Count, 5); } + + [Test] + public void GetMediaTest() + { + ListMediaResponse resp = _remotePartyService.ListMedia(new ListMediaRequest() + { + Parent = "testParty", + PageSize = 5 + }); + + Media media = _remotePartyService.GetMedia(new GetMediaRequest() + { + Name = resp.Media[0].Name + }); + + Assert.NotNull(media); + Assert.AreEqual(media.Name, resp.Media[0].Name); + } } } \ No newline at end of file diff --git a/Aurora.test/ControllerTests/MembersControllerTest.cs b/Aurora.test/ControllerTests/MembersControllerTest.cs index a74a184..a12111f 100644 --- a/Aurora.test/ControllerTests/MembersControllerTest.cs +++ b/Aurora.test/ControllerTests/MembersControllerTest.cs @@ -1,13 +1,9 @@ using NUnit.Framework; using Aurora.Proto.PartyV2; using Aurora.Services.Server; -using Aurora.Services.Library; -using Aurora.Services.Settings; -using Aurora.test.Models.Mock; using Grpc.Core; using System.Threading.Tasks; using System.Linq; -using System.IO; using Autofac; namespace Aurora.test.ControllerTests @@ -20,19 +16,11 @@ namespace Aurora.test.ControllerTests private IContainer _container; private IServerService _serverService; + #region Setup [OneTimeSetUp] public void SetupOneTime() { - ContainerBuilder builder = new ContainerBuilder(); - builder.RegisterType().As().SingleInstance(); - builder.RegisterInstance(new SettingsServiceMock() - { - Username = "Test User 1", - DefaultPort = 4005, - LibraryLocation = string.Format("{0}/Resource", Directory.GetCurrentDirectory()) - }).SingleInstance(); - builder.RegisterType().As().SingleInstance(); - _container = builder.Build(); + _container = SetupUtil.SetupOneTime(); } [OneTimeTearDown] @@ -44,10 +32,7 @@ namespace Aurora.test.ControllerTests [SetUp] public void Setup() { - _serverService = _container.Resolve(); - _serverService.Start("testParty", "asdf"); - _channel = new Channel(string.Format("{0}:{1}", ServerService.GetLocalIPAddress(), 8080), ChannelCredentials.Insecure); - _remotePartyService = new RemotePartyService.RemotePartyServiceClient(_channel); + _remotePartyService = SetupUtil.Setup(ref _container, ref _serverService, ref _channel); } [TearDown] @@ -56,6 +41,8 @@ namespace Aurora.test.ControllerTests await _serverService.Stop(); await _channel.ShutdownAsync(); } + #endregion Setup + [Test] public void DefaultTest() diff --git a/Aurora.test/ControllerTests/PartyControllerTest.cs b/Aurora.test/ControllerTests/PartyControllerTest.cs index fbbb61b..a08cda5 100644 --- a/Aurora.test/ControllerTests/PartyControllerTest.cs +++ b/Aurora.test/ControllerTests/PartyControllerTest.cs @@ -1,11 +1,7 @@ using System.Threading.Tasks; -using System.IO; using NUnit.Framework; using Aurora.Proto.PartyV2; using Aurora.Services.Server; -using Aurora.Services.Library; -using Aurora.Services.Settings; -using Aurora.test.Models.Mock; using Grpc.Core; using Autofac; @@ -18,19 +14,11 @@ namespace Aurora.test.ControllerTests private IContainer _container; private IServerService _serverService; + #region Setup [OneTimeSetUp] public void SetupOneTime() { - ContainerBuilder builder = new ContainerBuilder(); - builder.RegisterType().As().SingleInstance(); - builder.RegisterInstance(new SettingsServiceMock() - { - Username = "Test User 1", - DefaultPort = 4005, - LibraryLocation = string.Format("{0}/Resource", Directory.GetCurrentDirectory()) - }).SingleInstance(); - builder.RegisterType().As().SingleInstance(); - _container = builder.Build(); + _container = SetupUtil.SetupOneTime(); } [OneTimeTearDown] @@ -42,10 +30,7 @@ namespace Aurora.test.ControllerTests [SetUp] public void Setup() { - _serverService = _container.Resolve(); - _serverService.Start("testParty", "asdf"); - _channel = new Channel(string.Format("{0}:{1}", ServerService.GetLocalIPAddress(), 8080), ChannelCredentials.Insecure); - _remotePartyService = new RemotePartyService.RemotePartyServiceClient(_channel); + _remotePartyService = SetupUtil.Setup(ref _container, ref _serverService, ref _channel); } [TearDown] @@ -54,6 +39,7 @@ namespace Aurora.test.ControllerTests await _serverService.Stop(); await _channel.ShutdownAsync(); } + #endregion Setup [Test] public void DefaultTest() diff --git a/Aurora.test/ControllerTests/Setup.cs b/Aurora.test/ControllerTests/Setup.cs new file mode 100644 index 0000000..46d53c8 --- /dev/null +++ b/Aurora.test/ControllerTests/Setup.cs @@ -0,0 +1,39 @@ +using Autofac; +using Aurora.Proto.PartyV2; +using Aurora.Services.Server; +using Aurora.Services.Library; +using Aurora.Services.Settings; +using Aurora.Services.Server.EventManager; +using Aurora.test.Models.Mock; +using System.IO; +using Grpc.Core; + +namespace Aurora.test.ControllerTests +{ + public class SetupUtil + { + public static IContainer SetupOneTime() + { + ContainerBuilder builder = new ContainerBuilder(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterInstance(new SettingsServiceMock() + { + Username = "Test User 1", + DefaultPort = 4005, + LibraryLocation = string.Format("{0}/Resources", Directory.GetCurrentDirectory()) + }).SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + return builder.Build(); + } + + public static RemotePartyService.RemotePartyServiceClient Setup(ref IContainer container, ref IServerService serverService, ref Channel channel) + { + serverService = container.Resolve(); + serverService.Start("testParty", "asdf"); + channel = new Channel(string.Format("{0}:{1}", ServerService.GetLocalIPAddress(), 8080), ChannelCredentials.Insecure); + return new RemotePartyService.RemotePartyServiceClient(channel); + } + + } +} \ No newline at end of file diff --git a/Aurora/App.xaml.cs b/Aurora/App.xaml.cs index 33e3757..b38d816 100644 --- a/Aurora/App.xaml.cs +++ b/Aurora/App.xaml.cs @@ -7,6 +7,7 @@ using Aurora.Design.Views.Profile; using Aurora.Design.Views.Songs; using Aurora.Design.Views.Stations; using Aurora.Services.ClientService; +using Aurora.Services.Server.EventManager; using Autofac; using LibVLCSharp.Shared; using Xamarin.Forms; @@ -31,6 +32,7 @@ namespace Aurora _builder.RegisterType().As().SingleInstance(); _builder.RegisterType().As().SingleInstance(); _builder.RegisterType().As().SingleInstance(); + _builder.RegisterType().As().SingleInstance(); _builder.RegisterType().SingleInstance(); _builder.RegisterType(); _builder.RegisterType(); diff --git a/Aurora/Services/Server/Controllers/Constructor.cs b/Aurora/Services/Server/Controllers/Constructor.cs index 960d65f..a0c78bd 100644 --- a/Aurora/Services/Server/Controllers/Constructor.cs +++ b/Aurora/Services/Server/Controllers/Constructor.cs @@ -3,7 +3,9 @@ using System.Collections.ObjectModel; using System.Collections.Generic; using Aurora.Proto.PartyV2; using Aurora.Services.Library; +using Aurora.Services.Settings; using Aurora.Models.Media; +using Aurora.Services.Server.EventManager; using Autofac; namespace Aurora.Services.Server.Controllers @@ -11,11 +13,13 @@ namespace Aurora.Services.Server.Controllers public partial class RemotePartyController : RemotePartyService.RemotePartyServiceBase { private ILibraryService _libraryService; + private ISettingsService _settingsService; + private IEventManager _eventManager; /// /// Constructor for partial class /// - public RemotePartyController(string partyName, string description, ILibraryService libraryService) + public RemotePartyController(string partyName, string description, ILibraryService libraryService, ISettingsService settingsService, IEventManager eventManager) { this._startDateTime = DateTime.UtcNow; this._displayName = partyName; @@ -24,10 +28,11 @@ namespace Aurora.Services.Server.Controllers this._mediaList = new SortedList(); _libraryService = libraryService; + this._settingsService = settingsService; - string userName = "testUser"; + string userName = _settingsService.Username; - this._eventManager = new EventManager.EventManager(); + this._eventManager = eventManager; this._hostMember = new Member() { @@ -54,7 +59,7 @@ namespace Aurora.Services.Server.Controllers metadata = media.Metadata as AudioMetadata; Media data = new Media(); - data.Name = media.Id; + data.Name = string.Format("{0}/{1}", partyName, media.Id); if (metadata.Title != null) { data.Title = metadata.Title; diff --git a/Aurora/Services/Server/Controllers/PartyController.cs b/Aurora/Services/Server/Controllers/PartyController.cs index d8ad019..1f43962 100644 --- a/Aurora/Services/Server/Controllers/PartyController.cs +++ b/Aurora/Services/Server/Controllers/PartyController.cs @@ -15,8 +15,6 @@ namespace Aurora.Services.Server.Controllers private Member _hostMember; private DateTime _startDateTime; - private EventManager.EventManager _eventManager; - public override Task GetParty(Proto.General.Empty request, Grpc.Core.ServerCallContext context) { Party party = new Party() diff --git a/Aurora/Services/Server/EventManager/EventManager.cs b/Aurora/Services/Server/EventManager/EventManager.cs index b6e56f1..a258428 100644 --- a/Aurora/Services/Server/EventManager/EventManager.cs +++ b/Aurora/Services/Server/EventManager/EventManager.cs @@ -6,7 +6,7 @@ using Aurora.Proto.PartyV2; namespace Aurora.Services.Server.EventManager { - public class EventManager + public class EventManager : IEventManager { #region Fields private Dictionary> _subscriptionList; diff --git a/Aurora/Services/Server/EventManager/IEventManager.cs b/Aurora/Services/Server/EventManager/IEventManager.cs new file mode 100644 index 0000000..11bf8d1 --- /dev/null +++ b/Aurora/Services/Server/EventManager/IEventManager.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using Aurora.Proto.PartyV2; + +namespace Aurora.Services.Server.EventManager +{ + public interface IEventManager + { + /// + /// Get the list of event type subscriptions for a given sessionIdentifier id. + /// + /// sessionIdentifier Id + /// + List GetSubscriptionList(string sessionIdentifier); + + /// + /// Get the number of event subscriptions for a given sessionIdentifier + /// + /// sessionIdentifier Id + /// + int GetSubscriptionCount(string sessionIdentifier); + + /// + /// Add a new subscription + /// + /// + /// + bool AddSubscription(string sessionIdentifier, EventType type); + + /// + /// Add a list of subscriptions. This unsubscribes from unused events. + /// + /// The browser sessionIdentifier id. + /// The list of event types to subscribe to. + void AddSubscriptionList(string sessionIdentifier, List types); + + /// + /// Unsubscribe from a given event type. + /// + /// sessionIdentifier Id + /// Event Type to be removed + void RemoveSubscription(string sessionIdentifier, EventType type); + + void RemoveSubscriptionList(string sessionIdentifier, List types); + + /// + /// Remove all subscriptons for a given sessionIdentifier. + /// + /// sessionIdentifier Id + void RemoveAllSubscriptions(string sessionIdentifier); + + void AddEventHandler(Action action, Action cancel, string sessionIdentifierId); + + void RemoveEventHandler(string sessionIdentifierId); + + void CancelEventStream(string sessionIdentifierId); + + void FireEvent(BaseEvent bEvent); + } +} \ No newline at end of file diff --git a/Aurora/Services/Server/Server/ServerService.cs b/Aurora/Services/Server/Server/ServerService.cs index 5ce7996..7052c7f 100644 --- a/Aurora/Services/Server/Server/ServerService.cs +++ b/Aurora/Services/Server/Server/ServerService.cs @@ -4,7 +4,9 @@ using System.Net; using System.Net.Sockets; using Grpc.Core; using Aurora.Services.Server.Controllers; +using Aurora.Services.Settings; using Aurora.Services.Library; +using Aurora.Services.Server.EventManager; using Aurora.Proto.PartyV2; @@ -16,6 +18,8 @@ namespace Aurora.Services.Server private string _hostname; private Grpc.Core.Server _server; private ILibraryService _libraryService; + private ISettingsService _settingsService; + private IEventManager _eventManager; //Implementation class declarations private RemotePartyController _remotePartyController; @@ -23,10 +27,12 @@ namespace Aurora.Services.Server /// /// Constructor. Registers GRPC service implementations. /// - public ServerService(ILibraryService libraryService) + public ServerService(ILibraryService libraryService, ISettingsService settingsService, IEventManager eventManager) { string host = GetLocalIPAddress(); this._libraryService = libraryService; + this._settingsService = settingsService; + this._eventManager = eventManager; if (string.IsNullOrWhiteSpace(host)) { throw new Exception("This device must have a valid IP address"); @@ -71,7 +77,12 @@ namespace Aurora.Services.Server }; //Construct implementations - _remotePartyController = new RemotePartyController(partyName, description, _libraryService); + _remotePartyController = new RemotePartyController( + partyName, + description, + _libraryService, + _settingsService, + _eventManager); // Register grpc RemoteService with singleton server service RegisterService(RemotePartyService.BindService(_remotePartyController));