using System; using System.Collections.Generic; using Moq; using NUnit.Framework; using NzbDrone.Common.Messaging; using NzbDrone.Common.Messaging.Tracking; using NzbDrone.Test.Common; namespace NzbDrone.Common.Test.EventingTests { [TestFixture] public class MessageAggregatorCommandTests : TestBase { private Mock> _executorA; private Mock> _executorB; [SetUp] public void Setup() { _executorA = new Mock>(); _executorB = new Mock>(); Mocker.GetMock() .Setup(c => c.Build(typeof(IExecute))) .Returns(_executorA.Object); Mocker.GetMock() .Setup(c => c.Build(typeof(IExecute))) .Returns(_executorB.Object); Mocker.GetMock() .Setup(c => c.TrackIfNew(It.IsAny())) .Returns(new TrackedCommand(new CommandA(), CommandState.Running)); Mocker.GetMock() .Setup(c => c.TrackIfNew(It.IsAny())) .Returns(new TrackedCommand(new CommandB(), CommandState.Running)); } [Test] public void should_publish_command_to_executor() { var commandA = new CommandA(); Mocker.GetMock() .Setup(c => c.TrackIfNew(commandA)) .Returns(new TrackedCommand(commandA, CommandState.Running)); Subject.PublishCommand(commandA); _executorA.Verify(c => c.Execute(commandA), Times.Once()); } [Test] public void should_publish_command_by_with_optional_arg_using_name() { Mocker.GetMock().Setup(c => c.GetImplementations(typeof(ICommand))) .Returns(new List { typeof(CommandA), typeof(CommandB) }); Subject.PublishCommand(typeof(CommandA).FullName); _executorA.Verify(c => c.Execute(It.IsAny()), Times.Once()); } [Test] public void should_not_publish_to_incompatible_executor() { var commandA = new CommandA(); Mocker.GetMock() .Setup(c => c.TrackIfNew(commandA)) .Returns(new TrackedCommand(commandA, CommandState.Running)); Subject.PublishCommand(commandA); _executorA.Verify(c => c.Execute(commandA), Times.Once()); _executorB.Verify(c => c.Execute(It.IsAny()), Times.Never()); } [Test] public void broken_executor_should_throw_the_exception() { var commandA = new CommandA(); _executorA.Setup(c => c.Execute(It.IsAny())) .Throws(new NotImplementedException()); Assert.Throws(() => Subject.PublishCommand(commandA)); } } public class CommandA : ICommand { public String CommandId { get; private set; } // ReSharper disable UnusedParameter.Local public CommandA(int id = 0) // ReSharper restore UnusedParameter.Local { CommandId = HashUtil.GenerateCommandId(); } } public class CommandB : ICommand { public String CommandId { get; private set; } public CommandB() { CommandId = HashUtil.GenerateCommandId(); } } }