public async Task return_false_when_concurrency_conflict_when_commit_events()
        {
            var mockEvent = new Mock <IBusinessEvent>();
            var events    = new List <IBusinessEvent>()
            {
                mockEvent.Object
            }.ToImmutableList();
            var eventType       = mockEvent.Object.GetType().Name;
            var dataText        = "{}";
            var dataBytes       = Encoding.Unicode.GetBytes(dataText);
            var unresolvedEvent = new UnresolvedBusinessEvent(eventType, dataBytes);

            var mockResolver            = new Mock <IBusinessEventResolver>();
            var mockState               = new Mock <IAggregateRootState>();
            var mockStreamClientFactory = new Mock <IStreamClientFactory>();
            var mockStreamClient        = new Mock <IStreamClient>();

            var ar = new TestAggregateRoot(new AggregateRootDependencies <IAggregateRootState>(null, null, null, mockStreamClientFactory.Object, mockResolver.Object), null, null);

            mockResolver.Setup(x => x.CanUnresolve(mockEvent.Object)).Returns(true);
            mockResolver.Setup(x => x.Unresolve(mockEvent.Object)).Returns(unresolvedEvent);
            mockStreamClientFactory.Setup(x => x.Create(It.IsAny <string>())).Returns(mockStreamClient.Object);
            mockStreamClient.Setup(x => x.CommitEventsToStreamAsync(It.IsAny <string>(), It.IsAny <long?>(), It.IsAny <IEnumerable <CommitEvent> >())).ReturnsAsync(CommitResult.ConcurrencyConflict);

            var result = await ar.TestCommitEventsAsync(events, null, null, null);

            Assert.False(result);
        }
        public async Task do_nothing_with_empty_events_and_return_true_when_commit_events()
        {
            var ar = new TestAggregateRoot(new AggregateRootDependencies <IAggregateRootState>(null, null, null, null, null), null, null);

            // Will throw null reference if attempts to process events because dependencies are null.
            var result = await ar.TestCommitEventsAsync(ImmutableList <IBusinessEvent> .Empty, null, null, null);

            Assert.True(result);
        }
        public async Task throw_when_can_not_unresolve_event_when_commit_events()
        {
            var mockEvent = new Mock <IBusinessEvent>();
            var events    = new List <IBusinessEvent>()
            {
                mockEvent.Object
            }.ToImmutableList();
            var mockResolver = new Mock <IBusinessEventResolver>();

            var ar = new TestAggregateRoot(new AggregateRootDependencies <IAggregateRootState>(null, null, null, null, mockResolver.Object), null, null);

            mockResolver.Setup(x => x.CanUnresolve(mockEvent.Object)).Returns(false);

            await Assert.ThrowsAsync <InvalidOperationException>(() => ar.TestCommitEventsAsync(events, null, null, null));
        }
        public async Task commit_and_return_true_when_commit_events()
        {
            var regionId = "x";
            var streamId = "s";
            var streamPositionCheckpoint = 0;
            var mockEvent = new Mock <IBusinessEvent>();
            var events    = new List <IBusinessEvent>()
            {
                mockEvent.Object
            }.ToImmutableList();
            var eventType       = mockEvent.Object.GetType().Name;
            var dataText        = "{}";
            var dataBytes       = Encoding.Unicode.GetBytes(dataText);
            var unresolvedEvent = new UnresolvedBusinessEvent(eventType, dataBytes);

            var mockResolver            = new Mock <IBusinessEventResolver>();
            var mockStreamClientFactory = new Mock <IStreamClientFactory>();
            var mockStreamClient        = new Mock <IStreamClient>();

            var ar = new TestAggregateRoot(new AggregateRootDependencies <IAggregateRootState>(null, null, null, mockStreamClientFactory.Object, mockResolver.Object), null, null);

            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.Success);

            var result = await ar.TestCommitEventsAsync(events, regionId, streamId, streamPositionCheckpoint);

            Assert.True(result);

            mockStreamClient.Verify(x =>
                                    x.CommitEventsToStreamAsync(
                                        streamId, streamPositionCheckpoint,
                                        It.Is <IEnumerable <CommitEvent> >(ces => ces.FirstOrDefault(ce => ce.EventType == eventType && Encoding.Unicode.GetString(ce.Data) == dataText) != null)
                                        )
                                    );
        }