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);
}
}
}