Exemplo n.º 1
0
        public async Task Commands_CanHandleCommandThatFiresOtherCommandsWithANestedError()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <ICommandHandler <TestCommandWithResult> >(new TestCommandWithResultHandler(mediator));
            handlerFactory.AddHandlersForCommandOrQuery <ICommandHandler <TestMultipleCommandWithResult> >(new TestMultipleCommandHandlerWithResult(mediator));

            // Act
            var result = await mediator.Send(new TestMultipleCommandWithResult { Name = "bar", Id = 999 });

            // Assert
            Assert.IsTrue(result.Result <TestCommandResult>() == null && result.ValidationResults.Any());
        }
Exemplo n.º 2
0
        public async Task Commands_CanHandleCommandThatFiresOtherCommandsWithAReturnValue()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <ICommandHandler <TestCommandWithResult> >(new TestCommandWithResultHandler(mediator));
            handlerFactory.AddHandlersForCommandOrQuery <ICommandHandler <TestMultipleCommandWithResult> >(new TestMultipleCommandHandlerWithResult(mediator));

            // Act
            var result = await mediator.Send(new TestMultipleCommandWithResult { Name = "bar" });

            // Assert
            Assert.IsTrue(result.Result <TestCommandResult>().ResultingValue == 5);
        }
Exemplo n.º 3
0
        public async Task Queries_ShouldAllowMultipleQueryDefinitionsPerObject()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <IQuery <SingleIdCriteria, FakeResult> >(new MultipleQueryTypesInOneObject());
            handlerFactory.AddHandlersForCommandOrQuery <IQuery <SingleNameCriteria, FakeResult> >(new MultipleQueryTypesInOneObject());

            // Act
            var resultByName = await mediator.Query <SingleNameCriteria, FakeResult>(new SingleNameCriteria { Name = "Name2" });

            var resultById = await mediator.Query <SingleIdCriteria, FakeResult>(new SingleIdCriteria { Id = 1 });

            // Assert
            Assert.IsTrue(resultByName.Id == 2);
            Assert.IsTrue(resultById.Id == 1);
        }
Exemplo n.º 4
0
        public async Task LookupQuery_CanLookupData()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <ILookupQuery <List <FakeResult> > >(new FindResultForLookup());

            // Act
            var result = await mediator.LoadList <List <FakeResult> >();

            // Assert
            Assert.IsTrue(result.Count == FakeDataStore.Results.Count);
        }
Exemplo n.º 5
0
        public async Task Queries_CanReturnPrimitiveTypes()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <IQuery <SingleNameCriteria, int> >(new FindPrimitiveTypeByCriteria());

            // Act
            var result = await mediator.Query <SingleNameCriteria, int>(new SingleNameCriteria { Name = "Name1" });

            // Assert
            Assert.IsTrue(result == 1);
        }
Exemplo n.º 6
0
        public async Task Queries_WhenCalledWithCriteria_ShouldReturnResult()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <IQuery <FakeRangeCriteria, List <FakeResult> > >(new FindFakeResultByRangeCriteria());

            // Act
            var result = await mediator.Query <FakeRangeCriteria, List <FakeResult> >(new FakeRangeCriteria { MinValue = 0, MaxValue = 5 });

            // Assert
            Assert.IsTrue(result.Count == 5);
        }
Exemplo n.º 7
0
        public async Task Commands_WhenExecuting_CanSuccessfulCompleteValidCommand()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <ICommandHandler <TestCommand> >(new TestCommandHandler(mediator));

            // Act
            var result = await mediator.Send(new TestCommand { Id = 1 });

            // Assert
            Assert.IsFalse(result.ValidationResults.Any());
        }
Exemplo n.º 8
0
        public async Task Queries_WhenCalledWithPrimitiveCriteria_ShouldReturnResult()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <IQuery <int, List <FakeResult> > >(new FindFakeResultByPrimitiveType());

            // Act
            var result = await mediator.Query <int, List <FakeResult> >(1);

            // Assert
            Assert.IsTrue(result.FirstOrDefault() != null);
            Assert.IsTrue(result.First().Id == 1);
        }
Exemplo n.º 9
0
        public async Task Commands_CanHandleCommandWithAReturnValueWithValidationFailures()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <ICommandHandler <TestCommandWithResult> >(new TestCommandWithResultHandler(mediator));

            // Act
            var result = await mediator.Send(new TestCommandWithResult { Id = 999 });

            var returnedValue = result.Result <TestCommandResult>();

            // Assert
            Assert.IsTrue(returnedValue == null && result.ValidationResults.Any());
        }
Exemplo n.º 10
0
        public async Task DeferEvents_CanDeferMultipleEvents()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForEvent(new List <IEventHandler <FakeEvent> >
            {
                Substitute.For <HandlerDeferringMultipleEvents>(mediator),
                Substitute.For <HandlerDeferringSingleEvent>(mediator),
                Substitute.For <HandlerWithoutAdditionalEvents>()
            });

            handlerFactory.AddHandlersForEvent(new List <IEventHandler <FakeEventFromHandler> >
            {
                Substitute.For <DependentEventHandler>()
            });

            handlerFactory.AddHandlersForCommandOrQuery <ICommandHandler <TestCommand> >(new TestCommandHandler(mediator));

            // Act
            var result = await mediator.Send(new TestCommand { Id = 1 });

            Assert.IsFalse(result.ValidationResults.Any());

            // Assert
            foreach (var handler in handlerFactory.GetHandlersFor <FakeEvent>())
            {
                handler.Received().Handle(Arg.Any <FakeEvent>()).FireAndForget();
            }

            foreach (var handler in handlerFactory.GetHandlersFor <FakeEventFromHandler>())
            {
                handler.Received().Handle(Arg.Any <FakeEventFromHandler>()).FireAndForget();
            }
        }