public async Task PublishPendingEvents_deletes_pending_events(
            FakeUserCreated created,
            FakeUsernameChanged usernameChanged)
        {
            // Arrange
            var sourceId = Guid.NewGuid();

            var events = new DomainEvent[] { created, usernameChanged };

            RaiseEvents(sourceId, events);

            using (var db = new DataContext())
            {
                foreach (DomainEvent e in events)
                {
                    var envelope = new Envelope(e);
                    db.PendingEvents.Add(PendingEvent.FromEnvelope(envelope, serializer));
                }
                await db.SaveChangesAsync();
            }

            // Act
            await sut.PublishPendingEvents(sourceId, CancellationToken.None);

            // Assert
            using (var db = new DataContext())
            {
                bool actual = await db
                              .PendingEvents
                              .Where(e => e.AggregateId == sourceId)
                              .AnyAsync();

                actual.Should().BeFalse();
            }
        }
Exemple #2
0
        public void FromEnvelope_has_guard_clause_for_invalid_message()
        {
            var    envelope = new Envelope(new object());
            Action action   = () => PendingEvent.FromEnvelope(envelope, serializer);

            action.ShouldThrow <ArgumentException>().Where(x => x.ParamName == "envelope");
        }
        public async Task PublishPendingEvents_commits_once(
            FakeUserCreated created,
            FakeUsernameChanged usernameChanged)
        {
            // Arrange
            var sourceId = Guid.NewGuid();

            var events = new DomainEvent[] { created, usernameChanged };

            RaiseEvents(sourceId, events);

            Mock.Get(mockDbContext.PendingEvents)
            .SetupData(events
                       .Select(e => new Envelope(e))
                       .Select(e => PendingEvent.FromEnvelope(e, serializer))
                       .ToList());

            var sut = new SqlEventPublisher(
                () => mockDbContext, serializer, messageBus);

            // Act
            await sut.PublishPendingEvents(sourceId, CancellationToken.None);

            // Assert
            Mock.Get(mockDbContext).Verify(
                x => x.SaveChangesAsync(CancellationToken.None),
                Times.Once());
        }
Exemple #4
0
 private Envelope RestoreEnvelope(PendingEvent pendingEvent) =>
 new Envelope(
     pendingEvent.MessageId,
     _serializer.Deserialize(pendingEvent.EventJson),
     pendingEvent.OperationId,
     pendingEvent.CorrelationId,
     pendingEvent.Contributor);
Exemple #5
0
        public void FromEnvelope_sets_Version_correctly()
        {
            FakeUserCreated domainEvent = _fixture.Create <FakeUserCreated>();
            var             envelope    = new Envelope(domainEvent);
            var             actual      = PendingEvent.FromEnvelope(envelope, _serializer);

            actual.Version.Should().Be(domainEvent.Version);
        }
Exemple #6
0
        public void FromEnvelope_sets_AggregateId_correctly()
        {
            FakeUserCreated domainEvent = _fixture.Create <FakeUserCreated>();
            var             envelope    = new Envelope(domainEvent);
            var             actual      = PendingEvent.FromEnvelope(envelope, _serializer);

            actual.AggregateId.Should().Be(domainEvent.SourceId);
        }
Exemple #7
0
        public void FromEnvelope_sets_MessageId_correctly()
        {
            var domainEvent = fixture.Create <FakeUserCreated>();
            var envelope    = new Envelope(domainEvent);
            var actual      = PendingEvent.FromEnvelope(envelope, serializer);

            actual.MessageId.Should().Be(envelope.MessageId);
        }
Exemple #8
0
        public void FromEnvelope_sets_OperationId_correctly()
        {
            FakeUserCreated domainEvent = _fixture.Create <FakeUserCreated>();
            string          operationId = $"{Guid.NewGuid()}";
            var             envelope    = new Envelope(Guid.NewGuid(), domainEvent, operationId);
            var             actual      = PendingEvent.FromEnvelope(envelope, _serializer);

            actual.OperationId.Should().Be(operationId);
        }
Exemple #9
0
        public void FromEnvelope_sets_Contributor_correctly()
        {
            FakeUserCreated domainEvent = _fixture.Create <FakeUserCreated>();
            string          contributor = _fixture.Create <string>();
            var             envelope    = new Envelope(Guid.NewGuid(), domainEvent, contributor: contributor);
            var             actual      = PendingEvent.FromEnvelope(envelope, _serializer);

            actual.Contributor.Should().Be(contributor);
        }
Exemple #10
0
        public void FromEnvelope_sets_CorrelationId_correctly()
        {
            var domainEvent   = fixture.Create <FakeUserCreated>();
            var correlationId = Guid.NewGuid();
            var envelope      = new Envelope(correlationId, domainEvent);
            var actual        = PendingEvent.FromEnvelope(envelope, serializer);

            actual.CorrelationId.Should().Be(correlationId);
        }
Exemple #11
0
        public void FromEnvelope_sets_EventJson_correctly()
        {
            var domainEvent = fixture.Create <FakeUserCreated>();
            var envelope    = new Envelope(domainEvent);

            var actual = PendingEvent.FromEnvelope(envelope, serializer);

            object message = serializer.Deserialize(actual.EventJson);

            message.Should().BeOfType <FakeUserCreated>();
            message.ShouldBeEquivalentTo(domainEvent);
        }
Exemple #12
0
        private void InsertEvent(
            EventStoreDbContext context,
            IDomainEvent domainEvent,
            string operationId,
            Guid?correlationId,
            string contributor)
        {
            var envelope = new Envelope(Guid.NewGuid(), domainEvent, operationId, correlationId, contributor);

            context.PersistentEvents.Add(PersistentEvent.FromEnvelope(envelope, _serializer));
            context.PendingEvents.Add(PendingEvent.FromEnvelope(envelope, _serializer));
        }
Exemple #13
0
        public async Task FlushPendingEvents_sends_events_correctly()
        {
            // Arrange
            FakeUserCreated     created         = _fixture.Create <FakeUserCreated>();
            FakeUsernameChanged usernameChanged = _fixture.Create <FakeUsernameChanged>();
            var    sourceId      = Guid.NewGuid();
            string operationId   = _fixture.Create <string>();
            var    correlationId = Guid.NewGuid();
            string contributor   = _fixture.Create <string>();

            var domainEvents = new DomainEvent[] { created, usernameChanged };

            RaiseEvents(sourceId, domainEvents);

            var envelopes = new List <Envelope>();

            using (var db = new DataContext())
            {
                foreach (DomainEvent domainEvent in domainEvents)
                {
                    var envelope = new Envelope(Guid.NewGuid(), domainEvent, operationId, correlationId, contributor);
                    envelopes.Add(envelope);
                    db.PendingEvents.Add(PendingEvent.FromEnvelope(envelope, _serializer));
                }

                await db.SaveChangesAsync();
            }

            List <Envelope> batch = null;

            Mock.Get(_messageBus)
            .Setup(
                x =>
                x.Send(
                    It.IsAny <IEnumerable <Envelope> >(),
                    It.IsAny <CancellationToken>()))
            .Callback <IEnumerable <Envelope>, CancellationToken>((b, t) => batch = b.ToList())
            .Returns(Task.FromResult(true));

            // Act
            await _sut.FlushPendingEvents(sourceId, CancellationToken.None);

            // Assert
            Mock.Get(_messageBus).Verify(
                x =>
                x.Send(
                    It.IsAny <IEnumerable <Envelope> >(),
                    CancellationToken.None),
                Times.Once());
            batch.ShouldAllBeEquivalentTo(envelopes, opts => opts.RespectingRuntimeTypes());
        }
Exemple #14
0
 private void InsertEvents(
     EventStoreDbContext context,
     List <IDomainEvent> events,
     Guid?correlationId)
 {
     foreach (IDomainEvent domainEvent in events)
     {
         var envelope =
             correlationId == null
             ? new Envelope(domainEvent)
             : new Envelope(correlationId.Value, domainEvent);
         context.PersistentEvents.Add(PersistentEvent.FromEnvelope(envelope, _serializer));
         context.PendingEvents.Add(PendingEvent.FromEnvelope(envelope, _serializer));
     }
 }
        public void FromEnvelope_generates_PendingEvent_correctly()
        {
            var domainEvent               = new SomeDomainEvent();
            var messageId                 = Guid.NewGuid();
            var correlationId             = Guid.NewGuid();
            var envelope                  = new Envelope(messageId, domainEvent, correlationId: correlationId);
            IMessageSerializer serializer = new JsonMessageSerializer();

            var actual = PendingEvent.FromEnvelope(envelope, serializer);

            actual.AggregateId.Should().Be(domainEvent.SourceId);
            actual.Version.Should().Be(domainEvent.Version);
            actual.MessageId.Should().Be(messageId);
            actual.CorrelationId.Should().Be(correlationId);
            actual.EventJson.Should().Be(serializer.Serialize(domainEvent));
        }
        public async Task PublishPendingEvents_sends_events(
            FakeUserCreated created,
            FakeUsernameChanged usernameChanged)
        {
            // Arrange
            var sourceId = Guid.NewGuid();

            var events = new DomainEvent[] { created, usernameChanged };

            RaiseEvents(sourceId, events);

            var envelopes = new List <Envelope>();

            using (var db = new DataContext())
            {
                foreach (DomainEvent e in events)
                {
                    var envelope = new Envelope(e);
                    envelopes.Add(envelope);
                    db.PendingEvents.Add(PendingEvent.FromEnvelope(envelope, serializer));
                }
                await db.SaveChangesAsync();
            }

            List <Envelope> batch = null;

            Mock.Get(messageBus)
            .Setup(
                x =>
                x.SendBatch(
                    It.IsAny <IEnumerable <Envelope> >(),
                    It.IsAny <CancellationToken>()))
            .Callback <IEnumerable <Envelope>, CancellationToken>((b, t) => batch = b.ToList())
            .Returns(Task.FromResult(true));

            // Act
            await sut.PublishPendingEvents(sourceId, CancellationToken.None);

            // Assert
            Mock.Get(messageBus).Verify(
                x =>
                x.SendBatch(
                    It.IsAny <IEnumerable <Envelope> >(),
                    CancellationToken.None),
                Times.Once());
            batch.ShouldAllBeEquivalentTo(envelopes, opts => opts.RespectingRuntimeTypes());
        }
Exemple #17
0
        public async Task FlushPendingEvents_sends_events_correctly()
        {
            // Arrange
            var created         = new FakeUserCreated();
            var usernameChanged = new FakeUsernameChanged();
            var sourceId        = Guid.NewGuid();

            var events = new DomainEvent[] { created, usernameChanged };

            events.Raise(sourceId);

            var envelopes = new List <Envelope>();

            using (var db = new FakeEventStoreDbContext(_dbContextOptions))
            {
                var serializer = new JsonMessageSerializer();
                foreach (DomainEvent e in events)
                {
                    var envelope = new Envelope(
                        messageId: Guid.NewGuid(),
                        correlationId: Guid.NewGuid(),
                        contributor: Guid.NewGuid().ToString(),
                        message: e);
                    envelopes.Add(envelope);
                    db.PendingEvents.Add(PendingEvent.FromEnvelope <FakeUser>(envelope, serializer));
                }

                await db.SaveChangesAsync();
            }

            var messageBus = new MessageBus();

            var sut = new SqlEventPublisher(
                () => new FakeEventStoreDbContext(_dbContextOptions),
                new JsonMessageSerializer(),
                messageBus);

            // Act
            await sut.FlushPendingEvents <FakeUser>(sourceId, CancellationToken.None);

            // Assert
            messageBus.Sent.ShouldAllBeEquivalentTo(envelopes, opts => opts.RespectingRuntimeTypes());
        }
Exemple #18
0
        public async Task FlushPendingEvents_deletes_pending_events()
        {
            // Arrange
            var sourceId = Guid.NewGuid();

            var created         = new FakeUserCreated();
            var usernameChanged = new FakeUsernameChanged();
            var events          = new DomainEvent[] { created, usernameChanged };

            events.Raise(sourceId);

            using (var db = new FakeEventStoreDbContext(_dbContextOptions))
            {
                var serializer = new JsonMessageSerializer();
                foreach (DomainEvent e in events)
                {
                    var envelope = new Envelope(e);
                    db.PendingEvents.Add(PendingEvent.FromEnvelope <FakeUser>(envelope, serializer));
                }

                await db.SaveChangesAsync();
            }

            var sut = new SqlEventPublisher(
                () => new FakeEventStoreDbContext(_dbContextOptions),
                new JsonMessageSerializer(),
                Mock.Of <IMessageBus>());

            // Act
            await sut.FlushPendingEvents <FakeUser>(sourceId);

            // Assert
            using (var db = new FakeEventStoreDbContext(_dbContextOptions))
            {
                bool actual = await db
                              .PendingEvents
                              .Where(e => e.AggregateId == sourceId)
                              .AnyAsync();

                actual.Should().BeFalse();
            }
        }