Added basic unit tests for all controllers

This commit is contained in:
watsonb8 2020-02-02 13:05:14 -05:00
parent 8231a18c3e
commit 2a7e10364e
11 changed files with 255 additions and 62 deletions

View File

@ -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<IEventManager>();
//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<BaseEvent> 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);
}
}
}
}

View File

@ -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<ServerService>().As<IServerService>().SingleInstance();
builder.RegisterInstance<ISettingsService>(new SettingsServiceMock()
{
Username = "Test User 1",
DefaultPort = 4005,
LibraryLocation = string.Format("{0}/Resources", Directory.GetCurrentDirectory())
}).SingleInstance();
builder.RegisterType<LibraryService>().As<ILibraryService>().SingleInstance();
_container = builder.Build();
_container = SetupUtil.SetupOneTime();
}
[OneTimeTearDown]
@ -43,10 +32,7 @@ namespace Aurora.test.ControllerTests
[SetUp]
public void Setup()
{
_serverService = _container.Resolve<IServerService>();
_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);
}
}
}

View File

@ -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<ServerService>().As<IServerService>().SingleInstance();
builder.RegisterInstance<ISettingsService>(new SettingsServiceMock()
{
Username = "Test User 1",
DefaultPort = 4005,
LibraryLocation = string.Format("{0}/Resource", Directory.GetCurrentDirectory())
}).SingleInstance();
builder.RegisterType<LibraryService>().As<ILibraryService>().SingleInstance();
_container = builder.Build();
_container = SetupUtil.SetupOneTime();
}
[OneTimeTearDown]
@ -44,10 +32,7 @@ namespace Aurora.test.ControllerTests
[SetUp]
public void Setup()
{
_serverService = _container.Resolve<IServerService>();
_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()

View File

@ -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<ServerService>().As<IServerService>().SingleInstance();
builder.RegisterInstance<ISettingsService>(new SettingsServiceMock()
{
Username = "Test User 1",
DefaultPort = 4005,
LibraryLocation = string.Format("{0}/Resource", Directory.GetCurrentDirectory())
}).SingleInstance();
builder.RegisterType<LibraryService>().As<ILibraryService>().SingleInstance();
_container = builder.Build();
_container = SetupUtil.SetupOneTime();
}
[OneTimeTearDown]
@ -42,10 +30,7 @@ namespace Aurora.test.ControllerTests
[SetUp]
public void Setup()
{
_serverService = _container.Resolve<IServerService>();
_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()

View File

@ -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<ServerService>().As<IServerService>().SingleInstance();
builder.RegisterInstance<ISettingsService>(new SettingsServiceMock()
{
Username = "Test User 1",
DefaultPort = 4005,
LibraryLocation = string.Format("{0}/Resources", Directory.GetCurrentDirectory())
}).SingleInstance();
builder.RegisterType<LibraryService>().As<ILibraryService>().SingleInstance();
builder.RegisterType<EventManager>().As<IEventManager>().SingleInstance();
return builder.Build();
}
public static RemotePartyService.RemotePartyServiceClient Setup(ref IContainer container, ref IServerService serverService, ref Channel channel)
{
serverService = container.Resolve<IServerService>();
serverService.Start("testParty", "asdf");
channel = new Channel(string.Format("{0}:{1}", ServerService.GetLocalIPAddress(), 8080), ChannelCredentials.Insecure);
return new RemotePartyService.RemotePartyServiceClient(channel);
}
}
}

View File

@ -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<SettingsService>().As<ISettingsService>().SingleInstance();
_builder.RegisterType<ClientService>().As<IClientService>().SingleInstance();
_builder.RegisterType<LibraryService>().As<ILibraryService>().SingleInstance();
_builder.RegisterType<EventManager>().As<IEventManager>().SingleInstance();
_builder.RegisterType<MainView>().SingleInstance();
_builder.RegisterType<AlbumsViewModel>();
_builder.RegisterType<ArtistsViewModel>();

View File

@ -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;
/// <summary>
/// Constructor for partial class
/// </summary>
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<string, Media>();
_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;

View File

@ -15,8 +15,6 @@ namespace Aurora.Services.Server.Controllers
private Member _hostMember;
private DateTime _startDateTime;
private EventManager.EventManager _eventManager;
public override Task<Party> GetParty(Proto.General.Empty request, Grpc.Core.ServerCallContext context)
{
Party party = new Party()

View File

@ -6,7 +6,7 @@ using Aurora.Proto.PartyV2;
namespace Aurora.Services.Server.EventManager
{
public class EventManager
public class EventManager : IEventManager
{
#region Fields
private Dictionary<string, List<EventType>> _subscriptionList;

View File

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using Aurora.Proto.PartyV2;
namespace Aurora.Services.Server.EventManager
{
public interface IEventManager
{
/// <summary>
/// Get the list of event type subscriptions for a given sessionIdentifier id.
/// </summary>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
/// <returns></returns>
List<EventType> GetSubscriptionList(string sessionIdentifier);
/// <summary>
/// Get the number of event subscriptions for a given sessionIdentifier
/// </summary>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
/// <returns></returns>
int GetSubscriptionCount(string sessionIdentifier);
/// <summary>
/// Add a new subscription
/// </summary>
/// <param name="sessionIdentifier"></param>
/// <param name="type"></param>
bool AddSubscription(string sessionIdentifier, EventType type);
/// <summary>
/// Add a list of subscriptions. This unsubscribes from unused events.
/// </summary>
/// <param name="sessionIdentifier">The browser sessionIdentifier id.</param>
/// <param name="types">The list of event types to subscribe to.</param>
void AddSubscriptionList(string sessionIdentifier, List<EventType> types);
/// <summary>
/// Unsubscribe from a given event type.
/// </summary>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
/// <param name="type">Event Type to be removed</param>
void RemoveSubscription(string sessionIdentifier, EventType type);
void RemoveSubscriptionList(string sessionIdentifier, List<EventType> types);
/// <summary>
/// Remove all subscriptons for a given sessionIdentifier.
/// </summary>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
void RemoveAllSubscriptions(string sessionIdentifier);
void AddEventHandler(Action<BaseEvent> action, Action cancel, string sessionIdentifierId);
void RemoveEventHandler(string sessionIdentifierId);
void CancelEventStream(string sessionIdentifierId);
void FireEvent(BaseEvent bEvent);
}
}

View File

@ -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
/// <summary>
/// Constructor. Registers GRPC service implementations.
/// </summary>
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));