173 lines
No EOL
6 KiB
C#
173 lines
No EOL
6 KiB
C#
using System;
|
|
using System.Threading;
|
|
using Microsoft.AspNet.SignalR;
|
|
using NSubstitute;
|
|
using NUnit.Framework;
|
|
using Plume.Core.Logging;
|
|
using Shuttle.Api.ClientAPI;
|
|
using Shuttle.Api.CommunicationDTO.IdentityDTO;
|
|
using Shuttle.Servers;
|
|
using Shuttle.Servers.Managers.Clients;
|
|
using Shuttle.Servers.Managers.Clients.Exceptions;
|
|
using Shuttle.TestsToMove.Mocks;
|
|
|
|
namespace Shuttle.TestsToMove.ManagersTests
|
|
{
|
|
[TestFixture]
|
|
public class ClientManager_Tests
|
|
{
|
|
private const string ConnectionId = "Test-test-test";
|
|
private readonly ILogger _logger = new MockLogger();
|
|
|
|
private IAfterEffectsClient _client;
|
|
private AdobeAppInfo _appInfo;
|
|
|
|
private TargetAppInfo _target;
|
|
private IHubContext<IAfterEffectsClient> _aeHubContext;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_client = Substitute.For<IAfterEffectsClient>();
|
|
_aeHubContext = Substitute.For<IHubContext<IAfterEffectsClient>>();
|
|
_aeHubContext.Clients.Client(Arg.Any<string>()).ReturnsForAnyArgs(_client);
|
|
|
|
_appInfo = new AdobeAppInfo()
|
|
{
|
|
ShuttleId = "Nicky McNickname",
|
|
HostEnvironment = new HostEnvironment()
|
|
{
|
|
AppId = "AEFT",
|
|
AppVersion = "15.0"
|
|
}
|
|
};
|
|
|
|
_target = TargetAppInfo.Empty;
|
|
}
|
|
|
|
[Test]
|
|
public void ThrowIfUnknown_UnknownConnectionId_ThrowsException()
|
|
{
|
|
var clientManager = new ClientManager(_logger, _aeHubContext, null);
|
|
|
|
Assert.Throws<UnknownConnectionIdException>(() => clientManager.ThrowIfUnknown(ConnectionId));
|
|
}
|
|
|
|
[Test]
|
|
public void ThrowIfUnknown_Null_ThrowsException()
|
|
{
|
|
var clientManager = new ClientManager(_logger, _aeHubContext, null);
|
|
|
|
Assert.Throws<ArgumentNullException>(() => clientManager.ThrowIfUnknown(null));
|
|
}
|
|
|
|
[Test]
|
|
public void RegisterConnection_ValidConnectionId_ConnectionRegistered()
|
|
{
|
|
var clientManager = new ClientManager(_logger, _aeHubContext, null);
|
|
|
|
clientManager.RegisterConnection(ConnectionId, _appInfo);
|
|
|
|
Assert.DoesNotThrow(() => clientManager.ThrowIfUnknown(ConnectionId));
|
|
}
|
|
|
|
[Test]
|
|
public void RegisterConnection_RegisterSameConnectionTwice_ThrowsException()
|
|
{
|
|
var clientManager = new ClientManager(_logger, _aeHubContext, null);
|
|
|
|
clientManager.RegisterConnection(ConnectionId, _appInfo);
|
|
|
|
Assert.Throws<DuplicatedConnectionIdException>(() => clientManager.RegisterConnection(ConnectionId, _appInfo));
|
|
}
|
|
|
|
[Test]
|
|
public void RegisterConnection_NullConnectionId_ThrowsException()
|
|
{
|
|
var clientManager = new ClientManager(_logger, _aeHubContext, null);
|
|
|
|
Assert.Throws<ArgumentNullException>(() => clientManager.RegisterConnection(null, _appInfo));
|
|
}
|
|
|
|
[Test]
|
|
public void RegisterConnection_NullAppInfo_ThrowsException()
|
|
{
|
|
var clientManager = new ClientManager(_logger, _aeHubContext, null);
|
|
|
|
Assert.Throws<ArgumentNullException>(() => clientManager.RegisterConnection(ConnectionId, null));
|
|
}
|
|
|
|
[Test]
|
|
public void RegisterConnection_NullClientApi_ThrowsException()
|
|
{
|
|
var _aeHubContext = Substitute.For<IHubContext<IAfterEffectsClient>>();
|
|
_aeHubContext.Clients.Client(Arg.Any<string>()).ReturnsForAnyArgs((IAfterEffectsClient)null);
|
|
var clientManager = new ClientManager(_logger, _aeHubContext, null);
|
|
|
|
Assert.Throws<ArgumentNullException>(() => clientManager.RegisterConnection(ConnectionId, _appInfo));
|
|
}
|
|
|
|
[Test]
|
|
public void UnregisterConnection_ValidConnectionId_ConnectionRemoved()
|
|
{
|
|
var clientManager = new ClientManager(_logger, _aeHubContext, null);
|
|
clientManager.RegisterConnection(ConnectionId, _appInfo);
|
|
|
|
clientManager.UnregisterConnection(ConnectionId, gracefullyDisconnected: true);
|
|
|
|
Assert.Throws<UnknownConnectionIdException>(() => clientManager.ThrowIfUnknown(ConnectionId));
|
|
}
|
|
|
|
[Test]
|
|
public void UnregisterConnection_UnknownConnectionId_DoesntThrowException()
|
|
{
|
|
var clientManager = new ClientManager(_logger, _aeHubContext, null);
|
|
|
|
Assert.DoesNotThrow(() => clientManager.UnregisterConnection(ConnectionId, gracefullyDisconnected: false));
|
|
}
|
|
|
|
[Test]
|
|
public void GetAppInfo_ValidConnectionId_ReturnsAppInfo()
|
|
{
|
|
var clientManager = new ClientManager(_logger, _aeHubContext, null);
|
|
clientManager.RegisterConnection(ConnectionId, _appInfo);
|
|
|
|
var targetAppInfo = clientManager.GetTargetAppInfo(ConnectionId);
|
|
|
|
Assert.IsNotNull(targetAppInfo);
|
|
Assert.AreEqual(ConnectionId, targetAppInfo.ConnectionId);
|
|
}
|
|
|
|
[Test]
|
|
public void GetClient_ValidConnectionId_ReturnsAppInfo()
|
|
{
|
|
var clientManager = new ClientManager(_logger, _aeHubContext, null);
|
|
clientManager.RegisterConnection(ConnectionId, _appInfo);
|
|
|
|
var client = clientManager.GetClient<IAfterEffectsClient>(ConnectionId);
|
|
|
|
Assert.AreEqual(_client, client);
|
|
}
|
|
|
|
[Test]
|
|
public void OnTargetConnection_ValidTarget_EventGetsRaised()
|
|
{
|
|
var clientManager = new ClientManager(_logger, _aeHubContext, null);
|
|
clientManager.OnTargetConnection += SetTarget;
|
|
clientManager.RegisterConnection(ConnectionId, _appInfo);
|
|
|
|
Thread.Sleep(500); // let time for the event to rise
|
|
|
|
var target = clientManager.GetTargetAppInfo(ConnectionId);
|
|
|
|
Assert.AreEqual(target, _target);
|
|
|
|
clientManager.OnTargetConnection -= SetTarget;
|
|
}
|
|
|
|
private void SetTarget(TargetAppInfo target)
|
|
{
|
|
_target = target;
|
|
}
|
|
}
|
|
} |