public void Create_returns_PersistentEvent_instance()
        {
            IFixture fixture = new Fixture();

            fixture.Register <IDomainEvent>(() => fixture.Create <SomeDomainEvent>());

            var actual = PersistentEvent.Create(
                fixture.Create <Type>(),
                fixture.Create <Envelope <IDomainEvent> >(),
                new JsonMessageSerializer());

            actual.Should().NotBeNull();
        }
        private async Task Save <T>(
            List <IDomainEvent> domainEvents,
            string operationId,
            Guid?correlationId,
            string contributor,
            CancellationToken cancellationToken)
            where T : class, IEventSourced
        {
            var envelopes = new List <Envelope <IDomainEvent> >(
                from domainEvent in domainEvents
                let messageId = Guid.NewGuid()
                                select new Envelope <IDomainEvent>(messageId, domainEvent, operationId, correlationId, contributor));

            var batch = new TableBatchOperation();

            foreach (Envelope <IDomainEvent> envelope in envelopes)
            {
                batch.Insert(PersistentEvent.Create(typeof(T), envelope, _serializer));
                batch.Insert(PendingEvent.Create(typeof(T), envelope, _serializer));
            }

            Guid sourceId = domainEvents.First().SourceId;

            if (correlationId.HasValue)
            {
                batch.Insert(Correlation.Create(typeof(T), sourceId, correlationId.Value));
            }

            try
            {
                await _eventTable.ExecuteBatch(batch, cancellationToken).ConfigureAwait(false);
            }
            catch (StorageException exception) when(correlationId.HasValue)
            {
                string filter = Correlation.GetFilter(typeof(T), sourceId, correlationId.Value);
                var    query  = new TableQuery <Correlation> {
                    FilterString = filter
                };

                if (await _eventTable.Any(query, cancellationToken).ConfigureAwait(false))
                {
                    throw new DuplicateCorrelationException(
                              typeof(T),
                              sourceId,
                              correlationId.Value,
                              exception);
                }

                throw;
            }
        }
        public void Create_sets_EventType_correctly()
        {
            IFixture        fixture     = new Fixture();
            SomeDomainEvent domainEvent = fixture.Create <SomeDomainEvent>();

            fixture.Inject <IDomainEvent>(domainEvent);

            var actual = PersistentEvent.Create(
                fixture.Create <Type>(),
                fixture.Create <Envelope <IDomainEvent> >(),
                new JsonMessageSerializer());

            actual.EventType.Should().Be(typeof(SomeDomainEvent).FullName);
        }
        public void Create_sets_RowKey_correctly()
        {
            IFixture        fixture     = new Fixture();
            SomeDomainEvent domainEvent = fixture.Create <SomeDomainEvent>();

            TestContext.WriteLine($"Version: {domainEvent.Version}");
            fixture.Inject <IDomainEvent>(domainEvent);

            var actual = PersistentEvent.Create(
                fixture.Create <Type>(),
                fixture.Create <Envelope <IDomainEvent> >(),
                new JsonMessageSerializer());

            actual.RowKey.Should().Be(PersistentEvent.GetRowKey(domainEvent.Version));
        }
        public void Create_sets_PartitionKey_correctly()
        {
            IFixture        fixture     = new Fixture();
            Type            sourceType  = fixture.Create <Type>();
            SomeDomainEvent domainEvent = fixture.Create <SomeDomainEvent>();

            TestContext.WriteLine($"SourceId: {domainEvent.SourceId}");
            fixture.Inject <IDomainEvent>(domainEvent);

            var actual = PersistentEvent.Create(
                sourceType,
                fixture.Create <Envelope <IDomainEvent> >(),
                new JsonMessageSerializer());

            actual.PartitionKey.Should().Be(AggregateEntity.GetPartitionKey(sourceType, domainEvent.SourceId));
        }
        public void Create_sets_Contributor_correctly()
        {
            IFixture        fixture     = new Fixture();
            SomeDomainEvent domainEvent = fixture.Create <SomeDomainEvent>();

            fixture.Inject <IDomainEvent>(domainEvent);
            Envelope <IDomainEvent> envelope = fixture.Create <Envelope <IDomainEvent> >();

            TestContext.WriteLine($"Contributor: {envelope.Contributor}");

            var actual = PersistentEvent.Create(
                fixture.Create <Type>(),
                envelope,
                new JsonMessageSerializer());

            actual.Contributor.Should().Be(envelope.Contributor);
        }
        public void Create_sets_EventJson_correctly()
        {
            IFixture        fixture     = new Fixture();
            SomeDomainEvent domainEvent = fixture.Create <SomeDomainEvent>();

            fixture.Inject <IDomainEvent>(domainEvent);
            var serializer = new JsonMessageSerializer();

            var actual = PersistentEvent.Create(
                fixture.Create <Type>(),
                fixture.Create <Envelope <IDomainEvent> >(),
                serializer);

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

            restored.Should().BeOfType <SomeDomainEvent>();
            restored.ShouldBeEquivalentTo(domainEvent);
        }
        public async Task SaveEvents_inserts_persistent_event_entities_correctly()
        {
            // Arrange
            var fixture = new Fixture();
            var user    = new FakeUser(Guid.NewGuid(), fixture.Create <string>());

            user.ChangeUsername(fixture.Create <string>());
            IList <IDomainEvent> domainEvents = user.FlushPendingEvents().ToList();
            string operationId   = fixture.Create <string>();
            var    correlationId = Guid.NewGuid();
            string contributor   = fixture.Create <string>();

            // Act
            await _sut.SaveEvents <FakeUser>(domainEvents, operationId, correlationId, contributor);

            // Assert
            string filter = PersistentEvent.GetFilter(typeof(FakeUser), user.Id);
            var    query  = new TableQuery <PersistentEvent> {
                FilterString = filter
            };
            IEnumerable <PersistentEvent> actual = await s_eventTable.ExecuteQuerySegmentedAsync(query, default);

            actual.ShouldAllBeEquivalentTo(
                from domainEvent in domainEvents
                let envelope = new Envelope <IDomainEvent>(
                    Guid.NewGuid(),
                    domainEvent,
                    operationId,
                    correlationId,
                    contributor)
                               select PersistentEvent.Create(typeof(FakeUser), envelope, _serializer),
                opts => opts
                .Excluding(e => e.MessageId)
                .Excluding(e => e.Timestamp)
                .Excluding(e => e.ETag)
                .WithStrictOrdering());
        }