Exemplo n.º 1
0
        public async Task return_duplicate_command_id_validation_error_when_handle_generic_command()
        {
            var commandId   = "cId";
            var mockCommand = new Mock <ICommand>();
            var mockCommandValidationResult = new Mock <ICommandValidationResult>();
            var mockStreamIdBuilder         = new Mock <IStreamIdBuilder>();
            var mockStreamClientFactory     = new Mock <IStreamClientFactory>();
            var mockStreamClient            = new Mock <IStreamClient>();
            var mockStateRepo = new Mock <IAggregateRootStateRepo <IAggregateRootState> >();
            var mockState     = new Mock <IAggregateRootState>();
            var cancelSource  = new CancellationTokenSource();

            var dependencies = new AggregateRootDependencies <IAggregateRootState>(NullStandardLogger.Instance, mockStateRepo.Object, mockStreamIdBuilder.Object, mockStreamClientFactory.Object, null);
            var ar           = new TestAggregateRoot(dependencies, null, null);

            mockCommandValidationResult.Setup(x => x.IsValid).Returns(true);
            mockCommand.Setup(x => x.ValidateSemantics()).Returns(mockCommandValidationResult.Object);
            mockCommand.Setup(x => x.GetRegionId()).Returns(string.Empty);
            mockCommand.Setup(x => x.GetAggregateRootId()).Returns(string.Empty);
            mockCommand.Setup(x => x.GetCommandId()).Returns(commandId);
            mockStreamIdBuilder.Setup(x => x.Build(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(string.Empty);
            mockStreamClientFactory.Setup(x => x.Create(It.IsAny <string>())).Returns(mockStreamClient.Object);
            mockStreamClient.Setup(x => x.FirstPositionInStream).Returns(0);
            mockStateRepo.Setup(x => x.LoadAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>())).ReturnsAsync(mockState.Object);
            mockState.Setup(x => x.IsCausalIdInHistoryAsync(commandId)).ReturnsAsync(true);

            var result = await ar.HandleGenericCommandAsync(mockCommand.Object, cancelSource.Token);

            Assert.False(result.IsSuccess);
            Assert.Contains("Duplicate command id.", result.Errors);
        }
Exemplo n.º 2
0
        public async Task build_correct_stream_id_when_handle_generic_command()
        {
            var regionId          = "x";
            var context           = "ctx";
            var aggregateRootName = "ar";
            var aggregateRootId   = "1";

            var mockCommand = new Mock <ICommand>();
            var mockCommandValidationResult = new Mock <ICommandValidationResult>();
            var mockStreamIdBuilder         = new Mock <IStreamIdBuilder>();

            var dependencies = new AggregateRootDependencies <IAggregateRootState>(NullStandardLogger.Instance, null, mockStreamIdBuilder.Object, null, null);
            var ar           = new TestAggregateRoot(dependencies, context, aggregateRootName);

            mockCommandValidationResult.Setup(x => x.IsValid).Returns(true);;
            mockCommand.Setup(x => x.ValidateSemantics()).Returns(mockCommandValidationResult.Object);
            mockCommand.Setup(x => x.GetRegionId()).Returns(regionId);
            mockCommand.Setup(x => x.GetAggregateRootId()).Returns(aggregateRootId);

            try
            {
                await ar.HandleGenericCommandAsync(mockCommand.Object, CancellationToken.None);
            }
            catch (Exception)
            {
                // Ignore exception.
            }

            mockStreamIdBuilder.Verify(x => x.Build(regionId, context, aggregateRootName, aggregateRootId));
        }
Exemplo n.º 3
0
        public async Task handle_command_should_create_state()
        {
            var regionId    = "x";
            var streamId    = "sId";
            var mockCommand = new Mock <ICommand>();
            var mockCommandValidationResult = new Mock <ICommandValidationResult>();
            var mockStreamIdBuilder         = new Mock <IStreamIdBuilder>();
            var mockStateRepo = new Mock <IAggregateRootStateRepo <IAggregateRootState> >();
            var cancelSource  = new CancellationTokenSource();

            var dependencies = new AggregateRootDependencies <IAggregateRootState>(NullStandardLogger.Instance, mockStateRepo.Object, mockStreamIdBuilder.Object, null, null);
            var ar           = new TestAggregateRoot(dependencies, null, null);

            mockCommandValidationResult.Setup(x => x.IsValid).Returns(true);;
            mockCommand.Setup(x => x.ValidateSemantics()).Returns(mockCommandValidationResult.Object);
            mockCommand.Setup(x => x.GetRegionId()).Returns(regionId);
            mockCommand.Setup(x => x.GetAggregateRootId()).Returns(string.Empty);
            mockStreamIdBuilder.Setup(x => x.Build(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(streamId);

            try
            {
                await ar.HandleGenericCommandAsync(mockCommand.Object, cancelSource.Token);
            }
            catch (Exception)
            {
                // Ignore exception.
            }

            mockStateRepo.Verify(x => x.LoadAsync(regionId, streamId, cancelSource.Token));
        }
Exemplo n.º 4
0
        public async Task rethrow_exceptions_when_handle_generic_command()
        {
            var mockCommand  = new Mock <ICommand>();
            var dependencies = new AggregateRootDependencies <IAggregateRootState>(NullStandardLogger.Instance, null, null, null, null);
            var ar           = new TestAggregateRoot(dependencies, null, null);

            mockCommand.Setup(x => x.ValidateSemantics()).Throws(new TestException());

            await Assert.ThrowsAsync <TestException>(() => ar.HandleGenericCommandAsync(mockCommand.Object, CancellationToken.None));
        }
Exemplo n.º 5
0
        public async Task return_handler_errors_when_handle_generic_command()
        {
            var aggregateRootName = "ar";
            var streamId          = "sId";
            var commandId         = "cId";
            var error             = "state error";
            var errors            = new List <string>()
            {
                error
            }.ToImmutableList();

            var mockCommand = new Mock <ICommand>();
            var mockCommandValidationResultForSemantics = new Mock <ICommandValidationResult>();
            var mockCommandResult       = new Mock <ICommandResult>();
            var mockStreamIdBuilder     = new Mock <IStreamIdBuilder>();
            var mockStreamClientFactory = new Mock <IStreamClientFactory>();
            var mockStreamClient        = new Mock <IStreamClient>();
            var mockStateRepo           = new Mock <IAggregateRootStateRepo <IAggregateRootState> >();
            var mockState    = new Mock <IAggregateRootState>();
            var cancelSource = new CancellationTokenSource();

            var dependencies = new AggregateRootDependencies <IAggregateRootState>(NullStandardLogger.Instance, mockStateRepo.Object, mockStreamIdBuilder.Object, mockStreamClientFactory.Object, null);
            var ar           = new TestAggregateRoot(dependencies, null, aggregateRootName);

            mockCommandValidationResultForSemantics.Setup(x => x.IsValid).Returns(true);
            mockCommand.Setup(x => x.ValidateSemantics()).Returns(mockCommandValidationResultForSemantics.Object);
            mockCommand.Setup(x => x.GetRegionId()).Returns(string.Empty);
            mockCommand.Setup(x => x.GetAggregateRootId()).Returns(string.Empty);
            mockCommand.Setup(x => x.GetCommandId()).Returns(commandId);
            mockStreamIdBuilder.Setup(x => x.Build(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(streamId);
            mockStreamClientFactory.Setup(x => x.Create(It.IsAny <string>())).Returns(mockStreamClient.Object);
            mockStreamClient.Setup(x => x.FirstPositionInStream).Returns(0);
            mockStateRepo.Setup(x => x.LoadAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>())).ReturnsAsync(mockState.Object);
            mockState.Setup(x => x.IsCausalIdInHistoryAsync(commandId)).ReturnsAsync(false);
            mockCommandResult.Setup(x => x.IsSuccess).Returns(false);
            mockCommandResult.Setup(x => x.Errors).Returns(errors);

            ar.HandleDelegate = (_1, _2, _3) => Task.FromResult <ICommandResult>(mockCommandResult.Object);

            var result = await ar.HandleGenericCommandAsync(mockCommand.Object, cancelSource.Token);

            Assert.False(result.IsSuccess);
            Assert.Contains(error, result.Errors);
        }
Exemplo n.º 6
0
        public async Task return_semantic_validation_errors_when_handle_generic_command()
        {
            var error  = "validation error";
            var errors = new List <string>()
            {
                error
            }.ToImmutableList();
            var mockCommand = new Mock <ICommand>();
            var mockCommandValidationResult = new Mock <ICommandValidationResult>();
            var dependencies = new AggregateRootDependencies <IAggregateRootState>(NullStandardLogger.Instance, null, null, null, null);
            var ar           = new TestAggregateRoot(dependencies, null, null);

            mockCommandValidationResult.Setup(x => x.IsValid).Returns(false);
            mockCommandValidationResult.Setup(x => x.Errors).Returns(errors);
            mockCommand.Setup(x => x.ValidateSemantics()).Returns(mockCommandValidationResult.Object);

            var result = await ar.HandleGenericCommandAsync(mockCommand.Object, CancellationToken.None);

            Assert.False(result.IsSuccess);
            Assert.Contains(error, result.Errors);
        }
Exemplo n.º 7
0
        public async Task process_command_to_committed_events_and_return_success_handler_result_when_handle_generic_command()
        {
            var regionId          = "x";
            var aggregateRootName = "ar";
            var aggregateRootId   = "1";
            var streamId          = "sId";
            var commandId         = "cId";
            var mockEvent         = new Mock <IBusinessEvent>();
            var events            = new List <IBusinessEvent>()
            {
                mockEvent.Object
            }.ToImmutableList();
            var eventType             = mockEvent.Object.GetType().Name;
            var eventDataText         = "{}";
            var eventDataBytes        = Encoding.Unicode.GetBytes(eventDataText);
            var unresolvedEvent       = new UnresolvedBusinessEvent(eventType, eventDataBytes);
            var firstPositionInStream = 1;
            var hydratedStreamEvent   = new StreamEvent(streamId, firstPositionInStream, null, "whatever", new byte[] { });
            var commitResult          = CommitResult.Success;

            var mockCommand = new Mock <ICommand>();
            var mockCommandValidationResult = new Mock <ICommandValidationResult>();
            var mockStreamIdBuilder         = new Mock <IStreamIdBuilder>();
            var mockStreamClientFactory     = new Mock <IStreamClientFactory>();
            var mockStreamClient            = new Mock <IStreamClient>();
            var mockStateRepo     = new Mock <IAggregateRootStateRepo <IAggregateRootState> >();
            var mockState         = new Mock <IAggregateRootState>();
            var mockCommandResult = new Mock <ICommandResult>();
            var mockResolver      = new Mock <IBusinessEventResolver>();
            var cancelSource      = new CancellationTokenSource();

            var dependencies = new AggregateRootDependencies <IAggregateRootState>(NullStandardLogger.Instance, mockStateRepo.Object, mockStreamIdBuilder.Object, mockStreamClientFactory.Object, mockResolver.Object);
            var ar           = new TestAggregateRoot(dependencies, null, aggregateRootName);

            mockCommandValidationResult.Setup(x => x.IsValid).Returns(true);
            mockCommand.Setup(x => x.ValidateSemantics()).Returns(mockCommandValidationResult.Object);
            mockCommand.Setup(x => x.GetRegionId()).Returns(regionId);
            mockCommand.Setup(x => x.GetAggregateRootId()).Returns(aggregateRootId);
            mockCommand.Setup(x => x.GetCommandId()).Returns(commandId);
            mockStreamIdBuilder.Setup(x => x.Build(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(streamId);
            mockStateRepo.Setup(x => x.LoadAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>())).ReturnsAsync(mockState.Object);
            mockState.Setup(x => x.IsCausalIdInHistoryAsync(commandId)).ReturnsAsync(false);
            mockState.Setup(x => x.StreamPositionCheckpoint).Returns(firstPositionInStream);
            mockCommandResult.Setup(x => x.IsSuccess).Returns(true);
            mockCommandResult.Setup(x => x.Events).Returns(events);
            mockResolver.Setup(x => x.CanUnresolve(mockEvent.Object)).Returns(true);
            mockResolver.Setup(x => x.Unresolve(mockEvent.Object)).Returns(unresolvedEvent);
            mockStreamClientFactory.Setup(x => x.Create(regionId)).Returns(mockStreamClient.Object);
            mockStreamClient.Setup(x => x.CommitEventsToStreamAsync(It.IsAny <string>(), It.IsAny <long?>(), It.IsAny <IEnumerable <CommitEvent> >())).ReturnsAsync(commitResult);

            ar.HandleDelegate = (pState, pCommand, pToken) =>
            {
                if (pState != mockState.Object || pCommand != mockCommand.Object || pToken != cancelSource.Token)
                {
                    throw new Exception();
                }
                return(Task.FromResult <ICommandResult>(mockCommandResult.Object));
            };

            var result = await ar.HandleGenericCommandAsync(mockCommand.Object, cancelSource.Token);

            mockStreamClient.Verify(x => x.CommitEventsToStreamAsync(streamId, firstPositionInStream, It.IsAny <IEnumerable <CommitEvent> >()));
            Assert.True(result.IsSuccess);
            Assert.Contains(mockEvent.Object, result.Events);
        }