public void WhenCreatingHandlerFromTwoDescriptorsAndDependencyResolverThenGetActivatorDepencyResolver() { // Assign ICommandHandlerActivator activator = new DefaultCommandHandlerActivator(); CommandHandlerRequest request = new CommandHandlerRequest(this.config, this.command.Object); CommandHandlerDescriptor descriptor1 = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); CommandHandlerDescriptor descriptor2 = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); int[] i = { 0 }; this.dependencyResolver .When(() => i[0] < 1) .Setup(resolver => resolver.GetService(typeof(SimpleCommandHandler))) .Returns(null) .Callback(() => i[0]++); this.dependencyResolver .When(() => i[0] >= 1) .Setup(resolver => resolver.GetService(typeof(SimpleCommandHandler))) .Returns(new SimpleCommandHandler()); // Act activator.Create(request, descriptor1); ICommandHandler commandHandler = activator.Create(request, descriptor2); // Assert Assert.NotNull(commandHandler); Assert.IsType(typeof(SimpleCommandHandler), commandHandler); Assert.Equal(0, descriptor2.Properties.Count); this.dependencyResolver.Verify(resolver => resolver.GetService(typeof(SimpleCommandHandler)), Times.Exactly(2)); }
public void WhenCreatingHandlerThrowExceptionThenRethowsInvalidOperationException() { // Arrange ICommandHandlerActivator activator = new DefaultCommandHandlerActivator(); CommandHandlerRequest request = new CommandHandlerRequest(this.config, this.command.Object); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); this.dependencyResolver .Setup(resolver => resolver.GetService(typeof(SimpleCommandHandler))) .Throws <CommandHandlerNotFoundException>(); bool exceptionRaised = false; // Act try { activator.Create(request, descriptor); } catch (InvalidOperationException) { exceptionRaised = true; } // Assert Assert.True(exceptionRaised); }
public void CreateTranscientHandler_InstancianteEachTime() { // Arrange var config = new ProcessorConfiguration(); config.DefaultHandlerLifetime = HandlerLifetime.Transient; ICommandHandlerActivator activator = new DefaultCommandHandlerActivator(); CommandHandlerRequest request1 = new CommandHandlerRequest(config, this.command.Object); CommandHandlerRequest request2 = new CommandHandlerRequest(config, this.command.Object); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); CommandHandlerDescriptor descriptor2 = new CommandHandlerDescriptor(config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); // Act var handler1 = activator.Create(request1, descriptor); var handler2 = activator.Create(request1, descriptor); var handler3 = activator.Create(request2, descriptor); var handler4 = activator.Create(request1, descriptor2); // Assert Assert.NotNull(handler1); Assert.NotNull(handler2); Assert.NotNull(handler3); Assert.NotNull(handler4); Assert.NotSame(handler1, handler2); Assert.NotSame(handler1, handler3); Assert.NotSame(handler1, handler4); Assert.NotSame(handler2, handler3); Assert.NotSame(handler2, handler4); Assert.NotSame(handler3, handler4); }
CommandHandlerDescriptor ICommandHandlerSelector.SelectHandler(CommandHandlerRequest request) { CommandHandlerDescriptor handlerDescriptor = null; this.traceWriter.TraceBeginEnd( request, TraceCategories.HandlersCategory, TraceLevel.Info, this.innerSelector.GetType().Name, SelectActionMethodName, beginTrace: null, execute: () => handlerDescriptor = this.innerSelector.SelectHandler(request), endTrace: tr => { tr.Message = Error.Format( Resources.TraceHandlerSelectedMessage, FormattingUtilities.HandlerDescriptorToString(handlerDescriptor)); }, errorTrace: null); // Intercept returned HttpActionDescriptor with a tracing version if (handlerDescriptor != null && !(handlerDescriptor is CommandHandlerDescriptorTracer)) { return(new CommandHandlerDescriptorTracer(handlerDescriptor, this.traceWriter)); } return(handlerDescriptor); }
public void WhenProcessingUnknowHandlerThenThrowsHandlerNotFoundException() { // Arrange Mock <ICommandHandlerActivator> activator = new Mock <ICommandHandlerActivator>(MockBehavior.Strict); activator .Setup(a => a.Create(It.IsAny <CommandHandlerRequest>(), It.IsAny <CommandHandlerDescriptor>())) .Returns <MessageHandler>(null); this.configuration.Services.Replace(typeof(ICommandHandlerActivator), activator.Object); Mock <ICommandHandlerSelector> selector = new Mock <ICommandHandlerSelector>(MockBehavior.Strict); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.configuration, typeof(ValidCommand), typeof(ValidCommandHandler)); selector.Setup(s => s.SelectHandler(It.IsAny <CommandHandlerRequest>())).Returns(descriptor); this.configuration.Services.Replace(typeof(ICommandHandlerSelector), selector.Object); ValidCommand command = new ValidCommand(); MessageProcessor processor = this.CreatTestableProcessor(); // Act Assert.ThrowsAsync <CommandHandlerNotFoundException>(async() => await processor.ProcessAsync(command)); // Assert activator.Verify(a => a.Create(It.IsAny <CommandHandlerRequest>(), It.IsAny <CommandHandlerDescriptor>()), Times.Once()); selector.Verify(a => a.SelectHandler(It.IsAny <CommandHandlerRequest>()), Times.Once()); }
public void WhenExecutedFilterVaryByUserThenCacheIsUpdated() { // Arrange CacheAttribute filter = this.CreateAttribute(new MemoryCache("test")); filter.Duration = 10; filter.VaryByUser = true; filter.VaryByParams = CacheAttribute.VaryByParamsNone; CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); SimpleCommand command1 = new SimpleCommand { Property1 = 1, Property2 = "test1" }; CommandHandlerRequest request1 = new CommandHandlerRequest(this.config, command1); CommandHandlerContext context1 = new CommandHandlerContext(request1, descriptor); context1.User = new GenericPrincipal(new GenericIdentity("user1"), null); CommandHandlerExecutedContext executedContext1 = new CommandHandlerExecutedContext(context1, null); executedContext1.Response = new HandlerResponse(request1, "result1"); SimpleCommand command2 = new SimpleCommand { Property1 = 1, Property2 = "test1" }; CommandHandlerRequest request2 = new CommandHandlerRequest(this.config, command2); CommandHandlerContext context2 = new CommandHandlerContext(request2, descriptor); context2.User = new GenericPrincipal(new GenericIdentity("user2"), null); CommandHandlerExecutedContext executedContext2 = new CommandHandlerExecutedContext(context2, null); executedContext2.Response = new HandlerResponse(request2, "result2"); SimpleCommand command3 = new SimpleCommand { Property1 = 1, Property2 = "test1" }; CommandHandlerRequest request3 = new CommandHandlerRequest(this.config, command3); CommandHandlerContext context3 = new CommandHandlerContext(request3, descriptor); context3.User = new GenericPrincipal(new GenericIdentity("user1"), null); CommandHandlerExecutedContext executedContext3 = new CommandHandlerExecutedContext(context3, null); executedContext3.Response = new HandlerResponse(request3, "result3"); // Act filter.OnCommandExecuting(context1); filter.OnCommandExecuted(executedContext1); filter.OnCommandExecuting(context2); filter.OnCommandExecuted(executedContext2); filter.OnCommandExecuting(context3); filter.OnCommandExecuted(executedContext3); // Assert Assert.Equal("result1", executedContext1.Response.Value); Assert.Equal("result2", executedContext2.Response.Value); Assert.Equal("result1", executedContext3.Response.Value); }
private static CommandHandlerNotFoundException CreateHandlerNotFoundException(CommandHandlerDescriptor descriptor) { Contract.Requires(descriptor != null); if (descriptor.ReturnType == typeof(VoidResult)) { return new CommandHandlerNotFoundException(descriptor.MessageType); } return new CommandHandlerNotFoundException(descriptor.MessageType, descriptor.ReturnType); }
public void WhenCreatingHandlerWithoutParametersThenThrowsArgumentNullException() { // Assign ICommandHandlerActivator activator = new DefaultCommandHandlerActivator(); CommandHandlerRequest request = new CommandHandlerRequest(this.config, this.command.Object); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); // Act // Assert ExceptionAssert.ThrowsArgumentNull(() => activator.Create(null, null), "request"); ExceptionAssert.ThrowsArgumentNull(() => activator.Create(null, descriptor), "request"); ExceptionAssert.ThrowsArgumentNull(() => activator.Create(request, null), "descriptor"); }
public void WhenCreatingHandlerWithoutParametersThenThrowsArgumentNullException() { // Assign ICommandHandlerActivator activator = new DefaultCommandHandlerActivator(); CommandHandlerRequest request = new CommandHandlerRequest(this.config, this.command.Object); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); // Act // Assert ExceptionAssert.ThrowsArgumentNull(() => activator.Create(null, null), "request"); ExceptionAssert.ThrowsArgumentNull(() => activator.Create(null, descriptor), "request"); ExceptionAssert.ThrowsArgumentNull(() => activator.Create(request, null), "descriptor"); }
private static QueuePolicy GetQueuePolicy(CommandHandlerRequest request) { if (request == null) { throw Error.ArgumentNull("request"); } ServicesContainer servicesContainer = request.Configuration.Services; ICommandHandlerSelector handlerSelector = servicesContainer.GetHandlerSelector(); CommandHandlerDescriptor descriptor = handlerSelector.SelectHandler(request); return(descriptor.QueuePolicy); }
public void WhenGettingFilterPipelineThenReturnsDistinctValues() { // Arrange CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); // Act var filters = descriptor.GetFilterPipeline(); // Assert Assert.NotNull(filters); Assert.Equal(2, filters.Count()); Assert.Equal(1, filters.Select(f => f.Instance).OfType <IExceptionFilter>().Count()); Assert.Equal(1, filters.Select(f => f.Instance).OfType <ICommandHandlerFilter>().Count()); }
public void WhenGettingFiltersThenReturnsValues() { // Arrange CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); // Act var filters = descriptor.GetFilters(); // Assert Assert.NotNull(filters); Assert.Equal(4, filters.Count()); Assert.Equal(1, filters.OfType <IExceptionFilter>().Count()); Assert.Equal(3, filters.OfType <ICommandHandlerFilter>().Count()); }
public void WhenExecutedFilterVaryByParamsSetIncorrectlyThenCacheIsAlwaysUsed() { // Arrange CacheAttribute filter = this.CreateAttribute(new MemoryCache("test")); filter.Duration = 10; filter.VaryByParams = "XXXX"; CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); SimpleCommand command1 = new SimpleCommand { Property1 = 1, Property2 = "test1" }; CommandHandlerRequest request1 = new CommandHandlerRequest(this.config, command1); CommandHandlerContext context1 = new CommandHandlerContext(request1, descriptor); CommandHandlerExecutedContext executedContext1 = new CommandHandlerExecutedContext(context1, null); executedContext1.SetResponse("result1"); SimpleCommand command2 = new SimpleCommand { Property1 = 2, Property2 = "test2" }; CommandHandlerRequest request2 = new CommandHandlerRequest(this.config, command2); CommandHandlerContext context2 = new CommandHandlerContext(request2, descriptor); CommandHandlerExecutedContext executedContext2 = new CommandHandlerExecutedContext(context2, null); executedContext2.SetResponse("result2"); SimpleCommand command3 = new SimpleCommand { Property1 = 2, Property2 = "test3" }; CommandHandlerRequest request3 = new CommandHandlerRequest(this.config, command3); CommandHandlerContext context3 = new CommandHandlerContext(request3, descriptor); CommandHandlerExecutedContext executedContext3 = new CommandHandlerExecutedContext(context3, null); executedContext3.SetResponse("result3"); // Act filter.OnCommandExecuting(context1); filter.OnCommandExecuted(executedContext1); filter.OnCommandExecuting(context2); filter.OnCommandExecuted(executedContext2); filter.OnCommandExecuting(context3); filter.OnCommandExecuted(executedContext3); // Assert Assert.Equal("result1", executedContext1.Response.Value); Assert.Equal("result1", executedContext2.Response.Value); Assert.Equal("result1", executedContext3.Response.Value); }
public void WhenExecutingFilterThenCacheIsChecked() { // Arrange CacheAttribute filter = this.CreateAttribute(); SimpleCommand command = new SimpleCommand { Property1 = 12, Property2 = "test" }; CommandHandlerRequest request = new CommandHandlerRequest(this.config, command); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); CommandHandlerContext context = new CommandHandlerContext(request, descriptor); // Act filter.OnCommandExecuting(context); // Assert this.cache.Verify(c => c.Get(It.IsAny<string>(), It.IsAny<string>()), Times.Once()); Assert.Null(context.Response); }
public void WhenCreatingHandlerFromActivatorThenReturnsHandler() { // Assign ICommandHandlerActivator activator = new DefaultCommandHandlerActivator(); CommandHandlerRequest request = new CommandHandlerRequest(this.config, this.command.Object); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); this.dependencyResolver.Setup(resolver => resolver.GetService(typeof(SimpleCommandHandler))).Returns(null); // Act ICommandHandler commandHandler = activator.Create(request, descriptor); // Assert Assert.NotNull(commandHandler); Assert.IsType(typeof(SimpleCommandHandler), commandHandler); Assert.Equal(0, descriptor.Properties.Count); this.dependencyResolver.Verify(resolver => resolver.GetService(typeof(SimpleCommandHandler)), Times.Once()); }
public void WhenExecutingFilterToIgnoreThenCacheIsIgnored() { // Arrange CacheAttribute filter = this.CreateAttribute(); SimpleCommand command = new SimpleCommand { Property1 = 12, Property2 = "test" }; SimpleCommand cachedCommand = new SimpleCommand { Property1 = 12, Property2 = "test in cache" }; CommandHandlerRequest request = new CommandHandlerRequest(this.config, command); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(NotCachedCommand), typeof(NotCachedCommandHandler)); CommandHandlerContext context = new CommandHandlerContext(request, descriptor); this.cache.Setup(c => c.Get(It.IsAny<string>(), It.IsAny<string>())).Returns(new CacheAttribute.CacheEntry(cachedCommand)); // Act filter.OnCommandExecuting(context); // Assert this.cache.Verify(c => c.Get(It.IsAny<string>(), It.IsAny<string>()), Times.Never()); Assert.Null(context.Response); }
public void WhenCreatingInstanceWithParameterCtorThenPropertiesAreDefined() { // Arrange CommandHandlerRequest request = new CommandHandlerRequest(this.config, this.command.Object); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); // Act CommandHandlerContext context = new CommandHandlerContext(request, descriptor); // Assert Assert.Same(this.config, context.Configuration); Assert.Same(request, context.Request); Assert.Same(request.Command, context.Command); Assert.Same(descriptor, context.Descriptor); Assert.NotNull(context.Request); Assert.NotNull(context.Items); Assert.Equal(0, context.Items.Count); }
public void WhenCreatingHandlerFromActivatorThenReturnsHandler() { // Assign ICommandHandlerActivator activator = new DefaultCommandHandlerActivator(); CommandHandlerRequest request = new CommandHandlerRequest(this.config, this.command.Object); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); this.dependencyResolver.Setup(resolver => resolver.GetService(typeof(SimpleCommandHandler))).Returns(null); // Act ICommandHandler commandHandler = activator.Create(request, descriptor); // Assert Assert.NotNull(commandHandler); Assert.IsType(typeof(SimpleCommandHandler), commandHandler); Assert.Equal(0, descriptor.Properties.Count); this.dependencyResolver.Verify(resolver => resolver.GetService(typeof(SimpleCommandHandler)), Times.Once()); }
public void WhenExecutingFilterThenCacheIsChecked() { // Arrange CacheAttribute filter = this.CreateAttribute(); SimpleCommand command = new SimpleCommand { Property1 = 12, Property2 = "test" }; CommandHandlerRequest request = new CommandHandlerRequest(this.config, command); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); CommandHandlerContext context = new CommandHandlerContext(request, descriptor); // Act filter.OnCommandExecuting(context); // Assert this.cache.Verify(c => c.Get(It.IsAny <string>(), It.IsAny <string>()), Times.Once()); Assert.Null(context.Response); }
public void WhenCreatingInstanceWithParameterCtorThenPropertiesAreDefined() { // Arrange CommandHandlerRequest request = new CommandHandlerRequest(this.config, this.command.Object); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); // Act CommandHandlerContext context = new CommandHandlerContext(request, descriptor); // Assert Assert.Same(this.config, context.Configuration); Assert.Same(request, context.Request); Assert.Same(request.Command, context.Command); Assert.Same(descriptor, context.Descriptor); Assert.NotNull(context.Request); Assert.NotNull(context.Items); Assert.Equal(0, context.Items.Count); }
/// <summary> /// Initializes a new instance of the <see cref="CommandHandlerContext"/> class. /// </summary> /// <param name="request">The handler request.</param> /// <param name="descriptor">The handler descriptor.</param> public CommandHandlerContext(CommandHandlerRequest request, CommandHandlerDescriptor descriptor) : this() { if (request == null) { throw Error.ArgumentNull("request"); } if (request.Configuration == null) { throw Error.Argument("request"); } this.Configuration = request.Configuration; this.Request = request; this.Command = request.Command; this.Descriptor = descriptor; this.User = this.Configuration.Services.GetPrincipalProvider().Principal; }
public void WhenExecutedFilterWithoutKeyThenCacheIsNotUpdated() { // Arrange CacheAttribute filter = this.CreateAttribute(); SimpleCommand command = new SimpleCommand { Property1 = 12, Property2 = "test" }; CommandHandlerRequest request = new CommandHandlerRequest(this.config, command); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); CommandHandlerContext context = new CommandHandlerContext(request, descriptor); CommandHandlerExecutedContext executedContext = new CommandHandlerExecutedContext(context, null); // Act filter.OnCommandExecuted(executedContext); // Assert this.cache.Verify(c => c.Add(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <DateTimeOffset>(), It.IsAny <string>()), Times.Never()); }
/// <summary> /// Initializes a new instance of the <see cref="CommandHandlerContext"/> class. /// </summary> /// <param name="request">The handler request.</param> /// <param name="descriptor">The handler descriptor.</param> public CommandHandlerContext(CommandHandlerRequest request, CommandHandlerDescriptor descriptor) : this() { if (request == null) { throw Error.ArgumentNull("request"); } if (request.Configuration == null) { throw Error.Argument("request"); } this.Configuration = request.Configuration; this.Request = request; this.Command = request.Command; this.Descriptor = descriptor; this.User = this.Configuration.Services.GetPrincipalProvider().Principal; }
public void Ctor_HandlerLifetime() { // Arrange Mock <ICommandHandlerActivator> activator = new Mock <ICommandHandlerActivator>(MockBehavior.Strict); this.config.Services.Replace(typeof(ICommandHandlerActivator), activator.Object); // Act CommandHandlerDescriptor defaultDescriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); CommandHandlerDescriptor transcientDescriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(TranscientCommandHandler)); CommandHandlerDescriptor perRequestDescriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(PerRequestCommandHandler)); CommandHandlerDescriptor singletonDescriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SingletonCommandHandler)); // Assert Assert.Equal(this.config.DefaultHandlerLifetime, defaultDescriptor.Lifetime); Assert.Equal(HandlerLifetime.Transient, transcientDescriptor.Lifetime); Assert.Equal(HandlerLifetime.PerRequest, perRequestDescriptor.Lifetime); Assert.Equal(HandlerLifetime.Singleton, singletonDescriptor.Lifetime); }
public void WhenCreatingHandlerFromTwoDescriptorsThenFastCacheIsNotUsed() { // Assign ICommandHandlerActivator activator = new DefaultCommandHandlerActivator(); CommandHandlerRequest request1 = new CommandHandlerRequest(this.config, this.command.Object); CommandHandlerRequest request2 = new CommandHandlerRequest(this.config, this.command.Object); CommandHandlerDescriptor descriptor1 = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); CommandHandlerDescriptor descriptor2 = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); this.dependencyResolver.Setup(resolver => resolver.GetService(typeof(SimpleCommandHandler))).Returns(null); // Act activator.Create(request1, descriptor1); ICommandHandler commandHandler = activator.Create(request2, descriptor2); // Assert Assert.NotNull(commandHandler); Assert.IsType(typeof(SimpleCommandHandler), commandHandler); Assert.Equal(1, descriptor2.Properties.Count); this.dependencyResolver.Verify(resolver => resolver.GetService(typeof(SimpleCommandHandler)), Times.Exactly(2)); }
public void WhenExecutedFilterWithExceptionThenCacheIsNotUpdated() { // Arrange CacheAttribute filter = this.CreateAttribute(); SimpleCommand command = new SimpleCommand { Property1 = 12, Property2 = "test" }; CommandHandlerRequest request = new CommandHandlerRequest(this.config, command); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); CommandHandlerContext context = new CommandHandlerContext(request, descriptor); Exception exception = new Exception(); ExceptionDispatchInfo exceptionInfo = ExceptionDispatchInfo.Capture(exception); CommandHandlerExecutedContext executedContext = new CommandHandlerExecutedContext(context, exceptionInfo); // Act filter.OnCommandExecuted(executedContext); // Assert this.cache.Verify(c => c.Add(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <DateTimeOffset>(), It.IsAny <string>()), Times.Never()); Assert.Null(context.Response); }
private MessageHandler SetupHandlerImpl <TCommand>(MessageHandler commandHandler) where TCommand : ICommand { ProcessorConfiguration config = this.configuration; Mock <ICommandHandlerActivator> activator = new Mock <ICommandHandlerActivator>(MockBehavior.Strict); activator .Setup(a => a.Create(It.IsAny <CommandHandlerRequest>(), It.IsAny <CommandHandlerDescriptor>())) .Returns(commandHandler); config.Services.Replace(typeof(ICommandHandlerActivator), activator.Object); Mock <ICommandHandlerSelector> selector = new Mock <ICommandHandlerSelector>(MockBehavior.Strict); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(config, typeof(TCommand), commandHandler.GetType()); selector .Setup(s => s.SelectHandler(It.IsAny <CommandHandlerRequest>())) .Returns(descriptor); config.Services.Replace(typeof(ICommandHandlerSelector), selector.Object); return(commandHandler); }
public void WhenCreatingHandlerThenDelegateToActivator() { // Arrange Mock <ICommandHandlerActivator> activator = new Mock <ICommandHandlerActivator>(MockBehavior.Strict); this.config.Services.Replace(typeof(ICommandHandlerActivator), activator.Object); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); Mock <MessageHandler> expectedHandler = new Mock <MessageHandler>(); activator .Setup(a => a.Create(It.IsAny <CommandHandlerRequest>(), It.IsAny <CommandHandlerDescriptor>())) .Returns(expectedHandler.Object); CommandHandlerRequest request = new CommandHandlerRequest(new ProcessorConfiguration(), new Mock <ICommand>().Object); // Act var handler = descriptor.CreateHandler(request); // Assert Assert.NotNull(handler); Assert.Same(expectedHandler.Object, handler); activator.Verify(a => a.Create(It.IsAny <CommandHandlerRequest>(), It.IsAny <CommandHandlerDescriptor>()), Times.Once()); }
public void WhenExecutingFilterToIgnoreThenCacheIsIgnored() { // Arrange CacheAttribute filter = this.CreateAttribute(); SimpleCommand command = new SimpleCommand { Property1 = 12, Property2 = "test" }; SimpleCommand cachedCommand = new SimpleCommand { Property1 = 12, Property2 = "test in cache" }; CommandHandlerRequest request = new CommandHandlerRequest(this.config, command); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(NotCachedCommand), typeof(NotCachedCommandHandler)); CommandHandlerContext context = new CommandHandlerContext(request, descriptor); this.cache.Setup(c => c.Get(It.IsAny <string>(), It.IsAny <string>())).Returns(new CacheAttribute.CacheEntry(cachedCommand)); // Act filter.OnCommandExecuting(context); // Assert this.cache.Verify(c => c.Get(It.IsAny <string>(), It.IsAny <string>()), Times.Never()); Assert.Null(context.Response); }
public void Initialize(CommandHandlerSettings settings, CommandHandlerDescriptor descriptor) { settings.Services.Replace(typeof(IProxyBuilder), new Mock <IProxyBuilder>(MockBehavior.Loose).Object); }
public void Initialize(CommandHandlerSettings settings, CommandHandlerDescriptor descriptor) { }
private static bool ShouldIgnoreCache(CommandHandlerDescriptor descriptor) { Contract.Requires(descriptor != null); return descriptor.GetCustomAttributes<NoCacheAttribute>().Count > 0; }
public void CreateTranscientHandler_InstancianteEachTime() { // Arrange var config = new ProcessorConfiguration(); config.DefaultHandlerLifetime = HandlerLifetime.Transient; ICommandHandlerActivator activator = new DefaultCommandHandlerActivator(); CommandHandlerRequest request1 = new CommandHandlerRequest(config, this.command.Object); CommandHandlerRequest request2 = new CommandHandlerRequest(config, this.command.Object); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); CommandHandlerDescriptor descriptor2 = new CommandHandlerDescriptor(config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); // Act var handler1 = activator.Create(request1, descriptor); var handler2 = activator.Create(request1, descriptor); var handler3 = activator.Create(request2, descriptor); var handler4 = activator.Create(request1, descriptor2); // Assert Assert.NotNull(handler1); Assert.NotNull(handler2); Assert.NotNull(handler3); Assert.NotNull(handler4); Assert.NotSame(handler1, handler2); Assert.NotSame(handler1, handler3); Assert.NotSame(handler1, handler4); Assert.NotSame(handler2, handler3); Assert.NotSame(handler2, handler4); Assert.NotSame(handler3, handler4); }
public void WhenCreatingHandlerThrowExceptionThenRethowsInvalidOperationException() { // Arrange ICommandHandlerActivator activator = new DefaultCommandHandlerActivator(); CommandHandlerRequest request = new CommandHandlerRequest(this.config, this.command.Object); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); this.dependencyResolver .Setup(resolver => resolver.GetService(typeof(SimpleCommandHandler))) .Throws<CommandHandlerNotFoundException>(); bool exceptionRaised = false; // Act try { activator.Create(request, descriptor); } catch (InvalidOperationException) { exceptionRaised = true; } // Assert Assert.True(exceptionRaised); }
public void WhenCreatingHandlerFromTwoDescriptorsAndDependencyResolverThenGetActivatorDepencyResolver() { // Assign ICommandHandlerActivator activator = new DefaultCommandHandlerActivator(); CommandHandlerRequest request = new CommandHandlerRequest(this.config, this.command.Object); CommandHandlerDescriptor descriptor1 = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); CommandHandlerDescriptor descriptor2 = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); int[] i = { 0 }; this.dependencyResolver .When(() => i[0] < 1) .Setup(resolver => resolver.GetService(typeof(SimpleCommandHandler))) .Returns(null) .Callback(() => i[0]++); this.dependencyResolver .When(() => i[0] >= 1) .Setup(resolver => resolver.GetService(typeof(SimpleCommandHandler))) .Returns(new SimpleCommandHandler()); // Act activator.Create(request, descriptor1); ICommandHandler commandHandler = activator.Create(request, descriptor2); // Assert Assert.NotNull(commandHandler); Assert.IsType(typeof(SimpleCommandHandler), commandHandler); Assert.Equal(0, descriptor2.Properties.Count); this.dependencyResolver.Verify(resolver => resolver.GetService(typeof(SimpleCommandHandler)), Times.Exactly(2)); }
public void WhenCreatingHandlerFromTwoDescriptorsThenGetActivatorFromProperties() { // Assign ICommandHandlerActivator activator = new DefaultCommandHandlerActivator(); CommandHandlerRequest request1 = new CommandHandlerRequest(this.config, this.command.Object); CommandHandlerRequest request2 = new CommandHandlerRequest(this.config, this.command.Object); CommandHandlerDescriptor descriptor1 = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); CommandHandlerDescriptor descriptor2 = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); this.dependencyResolver.Setup(resolver => resolver.GetService(typeof(SimpleCommandHandler))).Returns(null); // Act activator.Create(request1, descriptor1); activator.Create(request2, descriptor2); ICommandHandler commandHandler = activator.Create(request2, descriptor2); // Assert Assert.NotNull(commandHandler); Assert.IsType(typeof(SimpleCommandHandler), commandHandler); Assert.Equal(1, descriptor2.Properties.Count); this.dependencyResolver.Verify(resolver => resolver.GetService(typeof(SimpleCommandHandler)), Times.Exactly(2)); }
public void WhenExecutedFilterWithExceptionThenCacheIsNotUpdated() { // Arrange CacheAttribute filter = this.CreateAttribute(); SimpleCommand command = new SimpleCommand { Property1 = 12, Property2 = "test" }; CommandHandlerRequest request = new CommandHandlerRequest(this.config, command); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); CommandHandlerContext context = new CommandHandlerContext(request, descriptor); Exception exception = new Exception(); ExceptionDispatchInfo exceptionInfo = ExceptionDispatchInfo.Capture(exception); CommandHandlerExecutedContext executedContext = new CommandHandlerExecutedContext(context, exceptionInfo); // Act filter.OnCommandExecuted(executedContext); // Assert this.cache.Verify(c => c.Add(It.IsAny<string>(), It.IsAny<object>(), It.IsAny<DateTimeOffset>(), It.IsAny<string>()), Times.Never()); Assert.Null(context.Response); }
public void WhenExecutedFilterWithKeyThenCacheIsUpdated() { // Arrange CacheAttribute filter = this.CreateAttribute(); SimpleCommand command = new SimpleCommand { Property1 = 12, Property2 = "test" }; CommandHandlerRequest request = new CommandHandlerRequest(this.config, command); CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); CommandHandlerContext context = new CommandHandlerContext(request, descriptor); filter.OnCommandExecuting(context); CommandHandlerExecutedContext executedContext = new CommandHandlerExecutedContext(context, null); // Act filter.OnCommandExecuted(executedContext); // Assert this.cache.Verify(c => c.Add(It.IsAny<string>(), It.IsAny<object>(), It.IsAny<DateTimeOffset>(), It.IsAny<string>()), Times.Once()); }
private static bool ShouldIgnoreCache(CommandHandlerDescriptor descriptor) { Contract.Requires(descriptor != null); return(descriptor.GetCustomAttributes <NoCacheAttribute>().Count > 0); }
public void WhenExecutedFilterVaryByUserThenCacheIsUpdated() { // Arrange CacheAttribute filter = this.CreateAttribute(new MemoryCache("test")); filter.Duration = 10; filter.VaryByUser = true; filter.VaryByParams = CacheAttribute.VaryByParamsNone; CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); SimpleCommand command1 = new SimpleCommand { Property1 = 1, Property2 = "test1" }; CommandHandlerRequest request1 = new CommandHandlerRequest(this.config, command1); CommandHandlerContext context1 = new CommandHandlerContext(request1, descriptor); context1.User = new GenericPrincipal(new GenericIdentity("user1"), null); CommandHandlerExecutedContext executedContext1 = new CommandHandlerExecutedContext(context1, null); executedContext1.Response = new HandlerResponse(request1, "result1"); SimpleCommand command2 = new SimpleCommand { Property1 = 1, Property2 = "test1" }; CommandHandlerRequest request2 = new CommandHandlerRequest(this.config, command2); CommandHandlerContext context2 = new CommandHandlerContext(request2, descriptor); context2.User = new GenericPrincipal(new GenericIdentity("user2"), null); CommandHandlerExecutedContext executedContext2 = new CommandHandlerExecutedContext(context2, null); executedContext2.Response = new HandlerResponse(request2, "result2"); SimpleCommand command3 = new SimpleCommand { Property1 = 1, Property2 = "test1" }; CommandHandlerRequest request3 = new CommandHandlerRequest(this.config, command3); CommandHandlerContext context3 = new CommandHandlerContext(request3, descriptor); context3.User = new GenericPrincipal(new GenericIdentity("user1"), null); CommandHandlerExecutedContext executedContext3 = new CommandHandlerExecutedContext(context3, null); executedContext3.Response = new HandlerResponse(request3, "result3"); // Act filter.OnCommandExecuting(context1); filter.OnCommandExecuted(executedContext1); filter.OnCommandExecuting(context2); filter.OnCommandExecuted(executedContext2); filter.OnCommandExecuting(context3); filter.OnCommandExecuted(executedContext3); // Assert Assert.Equal("result1", executedContext1.Response.Value); Assert.Equal("result2", executedContext2.Response.Value); Assert.Equal("result1", executedContext3.Response.Value); }
public void WhenExecutedFilterVaryByParamsSetIncorrectlyThenCacheIsAlwaysUsed() { // Arrange CacheAttribute filter = this.CreateAttribute(new MemoryCache("test")); filter.Duration = 10; filter.VaryByParams = "XXXX"; CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(this.config, typeof(SimpleCommand), typeof(SimpleCommandHandler)); SimpleCommand command1 = new SimpleCommand { Property1 = 1, Property2 = "test1" }; CommandHandlerRequest request1 = new CommandHandlerRequest(this.config, command1); CommandHandlerContext context1 = new CommandHandlerContext(request1, descriptor); CommandHandlerExecutedContext executedContext1 = new CommandHandlerExecutedContext(context1, null); executedContext1.SetResponse("result1"); SimpleCommand command2 = new SimpleCommand { Property1 = 2, Property2 = "test2" }; CommandHandlerRequest request2 = new CommandHandlerRequest(this.config, command2); CommandHandlerContext context2 = new CommandHandlerContext(request2, descriptor); CommandHandlerExecutedContext executedContext2 = new CommandHandlerExecutedContext(context2, null); executedContext2.SetResponse( "result2"); SimpleCommand command3 = new SimpleCommand { Property1 = 2, Property2 = "test3" }; CommandHandlerRequest request3 = new CommandHandlerRequest(this.config, command3); CommandHandlerContext context3 = new CommandHandlerContext(request3, descriptor); CommandHandlerExecutedContext executedContext3 = new CommandHandlerExecutedContext(context3, null); executedContext3.SetResponse("result3"); // Act filter.OnCommandExecuting(context1); filter.OnCommandExecuted(executedContext1); filter.OnCommandExecuting(context2); filter.OnCommandExecuted(executedContext2); filter.OnCommandExecuting(context3); filter.OnCommandExecuted(executedContext3); // Assert Assert.Equal("result1", executedContext1.Response.Value); Assert.Equal("result1", executedContext2.Response.Value); Assert.Equal("result1", executedContext3.Response.Value); }