2013-04-27 02:03:34 +00:00
|
|
|
|
using System;
|
2013-05-13 02:52:55 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-04-27 02:03:34 +00:00
|
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
2013-09-11 06:33:47 +00:00
|
|
|
|
using NzbDrone.Core.Messaging;
|
|
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
|
|
|
|
using NzbDrone.Core.Messaging.Tracking;
|
2013-04-27 02:03:34 +00:00
|
|
|
|
using NzbDrone.Test.Common;
|
|
|
|
|
|
2013-09-11 06:33:47 +00:00
|
|
|
|
namespace NzbDrone.Common.Test.MessagingTests
|
2013-04-27 02:03:34 +00:00
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2013-05-08 05:47:15 +00:00
|
|
|
|
public class MessageAggregatorCommandTests : TestBase<MessageAggregator>
|
2013-04-27 02:03:34 +00:00
|
|
|
|
{
|
2013-05-08 05:47:15 +00:00
|
|
|
|
private Mock<IExecute<CommandA>> _executorA;
|
|
|
|
|
private Mock<IExecute<CommandB>> _executorB;
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void Setup()
|
2013-04-27 02:03:34 +00:00
|
|
|
|
{
|
2013-05-08 05:47:15 +00:00
|
|
|
|
_executorA = new Mock<IExecute<CommandA>>();
|
|
|
|
|
_executorB = new Mock<IExecute<CommandB>>();
|
2013-04-27 02:03:34 +00:00
|
|
|
|
|
2013-05-08 05:47:15 +00:00
|
|
|
|
Mocker.GetMock<IServiceFactory>()
|
|
|
|
|
.Setup(c => c.Build(typeof(IExecute<CommandA>)))
|
|
|
|
|
.Returns(_executorA.Object);
|
2013-04-27 02:03:34 +00:00
|
|
|
|
|
2013-05-08 05:47:15 +00:00
|
|
|
|
Mocker.GetMock<IServiceFactory>()
|
|
|
|
|
.Setup(c => c.Build(typeof(IExecute<CommandB>)))
|
|
|
|
|
.Returns(_executorB.Object);
|
2013-04-27 02:03:34 +00:00
|
|
|
|
|
2013-09-01 04:23:21 +00:00
|
|
|
|
|
|
|
|
|
Mocker.GetMock<ITrackCommands>()
|
2013-09-11 06:33:47 +00:00
|
|
|
|
.Setup(c => c.FindExisting(It.IsAny<Command>()))
|
|
|
|
|
.Returns<Command>(null);
|
2013-05-08 05:47:15 +00:00
|
|
|
|
}
|
2013-04-27 02:03:34 +00:00
|
|
|
|
|
|
|
|
|
[Test]
|
2013-05-08 05:47:15 +00:00
|
|
|
|
public void should_publish_command_to_executor()
|
2013-04-27 02:03:34 +00:00
|
|
|
|
{
|
|
|
|
|
var commandA = new CommandA();
|
|
|
|
|
|
2013-05-08 05:47:15 +00:00
|
|
|
|
Subject.PublishCommand(commandA);
|
2013-04-27 02:03:34 +00:00
|
|
|
|
|
2013-05-08 05:47:15 +00:00
|
|
|
|
_executorA.Verify(c => c.Execute(commandA), Times.Once());
|
2013-04-27 02:03:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-13 02:52:55 +00:00
|
|
|
|
[Test]
|
2013-05-21 02:41:24 +00:00
|
|
|
|
public void should_publish_command_by_with_optional_arg_using_name()
|
2013-05-13 02:52:55 +00:00
|
|
|
|
{
|
2013-09-11 06:33:47 +00:00
|
|
|
|
Mocker.GetMock<IServiceFactory>().Setup(c => c.GetImplementations(typeof(Command)))
|
2013-05-13 02:52:55 +00:00
|
|
|
|
.Returns(new List<Type> { typeof(CommandA), typeof(CommandB) });
|
|
|
|
|
|
|
|
|
|
Subject.PublishCommand(typeof(CommandA).FullName);
|
|
|
|
|
_executorA.Verify(c => c.Execute(It.IsAny<CommandA>()), Times.Once());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-04-27 02:03:34 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void should_not_publish_to_incompatible_executor()
|
|
|
|
|
{
|
|
|
|
|
var commandA = new CommandA();
|
|
|
|
|
|
2013-05-08 05:47:15 +00:00
|
|
|
|
Subject.PublishCommand(commandA);
|
2013-04-27 02:03:34 +00:00
|
|
|
|
|
2013-05-08 05:47:15 +00:00
|
|
|
|
_executorA.Verify(c => c.Execute(commandA), Times.Once());
|
|
|
|
|
_executorB.Verify(c => c.Execute(It.IsAny<CommandB>()), Times.Never());
|
2013-04-27 02:03:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void broken_executor_should_throw_the_exception()
|
|
|
|
|
{
|
|
|
|
|
var commandA = new CommandA();
|
|
|
|
|
|
2013-05-08 05:47:15 +00:00
|
|
|
|
_executorA.Setup(c => c.Execute(It.IsAny<CommandA>()))
|
2013-04-27 02:03:34 +00:00
|
|
|
|
.Throws(new NotImplementedException());
|
|
|
|
|
|
2013-05-08 05:47:15 +00:00
|
|
|
|
Assert.Throws<NotImplementedException>(() => Subject.PublishCommand(commandA));
|
2013-04-27 02:03:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 06:33:47 +00:00
|
|
|
|
public class CommandA : Command
|
2013-04-27 02:03:34 +00:00
|
|
|
|
{
|
2013-05-13 02:52:55 +00:00
|
|
|
|
public CommandA(int id = 0)
|
|
|
|
|
{
|
|
|
|
|
}
|
2013-04-27 02:03:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 06:33:47 +00:00
|
|
|
|
public class CommandB : Command
|
2013-04-27 02:03:34 +00:00
|
|
|
|
{
|
|
|
|
|
|
2013-08-28 00:51:54 +00:00
|
|
|
|
public CommandB()
|
|
|
|
|
{
|
|
|
|
|
}
|
2013-04-27 02:03:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|