public void WhenReceivedEventIsNotNullThenShouldReturnEventContext()
        {
            var serializer    = ServiceProvider.GetRequiredService <IEventSerializer>();
            var factory       = ServiceProvider.GetRequiredService <IEventContextFactory>();
            var @event        = new CreateTestEvent();
            var streamId      = Guid.NewGuid().ToString();
            var causationId   = CausationId.From(Guid.NewGuid().ToString());
            var correlationId = CorrelationId.New();
            var actor         = "test-user";

            var message = new Message
            {
                MessageId      = @event.Id.ToString(),
                Body           = Encoding.UTF8.GetBytes(serializer.Serialize(@event)),
                Label          = nameof(CreateTestEvent),
                CorrelationId  = correlationId.ToString(),
                UserProperties =
                {
                    { nameof(IEventContext <IEvent> .StreamId),    streamId.ToString()    },
                    { nameof(IEventContext <IEvent> .CausationId), causationId.ToString() },
                    { nameof(IEventContext <IEvent> .Actor),       actor                  },
                    { nameof(IEventContext <IEvent> .Timestamp),   @event.Timestamp       },
                },
            };

            var context = factory.CreateContext(message);

            context.StreamId.Should().Be(streamId);
            context.CausationId.Should().Be(causationId);
            context.CorrelationId.Should().Be(correlationId);
            context.Payload.Should().BeOfType <CreateTestEvent>();
            context.Timestamp.Should().Be(@event.Timestamp);
            context.Actor.Should().Be(Actor.From(actor));
        }
        public IEventContext <IEvent> CreateContext(Entities.Event dbEvent)
        {
            if (dbEvent == null)
            {
                throw new ArgumentNullException(nameof(dbEvent));
            }

            if (!_eventTypeCache.TryGet(dbEvent.Type, out var type))
            {
                throw new ArgumentException($"Could not find event type for '{dbEvent.Type}'");
            }

            var @event = (IEvent)_eventDeserializer.Deserialize(dbEvent.Data, type);

            if (_cache.TryGetValue(type, out var activator))
            {
                return(activator(dbEvent.StreamId, @event, dbEvent.CorrelationId, dbEvent.CausationId, @event.Timestamp, Actor.From(dbEvent.Actor)));
            }

            activator = BuildActivator(typeof(EventContext <>).MakeGenericType(type));

            _cache.TryAdd(type, activator);

            var correlationId = dbEvent.CorrelationId != null?CorrelationId.From(dbEvent.CorrelationId) : (CorrelationId?)null;

            var causationId = dbEvent.CausationId != null?CausationId.From(dbEvent.CorrelationId) : (CausationId?)null;

            return(activator(dbEvent.StreamId, @event, correlationId, causationId, @event.Timestamp, Actor.From(dbEvent.Actor)));
        }
        public void WhenReceivedEventIsNotNullThenShouldReturnEventContext()
        {
            var serializer    = ServiceProvider.GetRequiredService <IEventSerializer>();
            var factory       = ServiceProvider.GetRequiredService <IEventContextFactory>();
            var properties    = Mock.Of <MQ.Impl.BasicProperties>();
            var @event        = new CreateTestEvent();
            var causationId   = CausationId.From(Guid.NewGuid().ToString());
            var correlationId = CorrelationId.New();
            var actor         = "test-user";

            properties.Headers = new Dictionary <string, object>
            {
                { nameof(IEventContext <IEvent> .CausationId), causationId },
                { nameof(IEventContext <IEvent> .CorrelationId), correlationId },
                { nameof(IEventContext <IEvent> .Actor), actor },
            };

            var message = new ReceivedMessage(new BasicDeliverEventArgs
            {
                RoutingKey      = nameof(CreateTestEvent),
                Body            = Encoding.UTF8.GetBytes(serializer.Serialize(@event)),
                BasicProperties = properties,
            });
            var context = factory.CreateContext(message);

            context.CausationId.Should().Be(causationId);
            context.CorrelationId.Should().Be(correlationId);
            context.Payload.Should().BeOfType <CreateTestEvent>();
            context.Timestamp.Should().Be(@event.Timestamp);
            context.Actor.Should().Be(Actor.From(actor));
        }
        public IEventContext <IEvent> CreateContext(ReceivedMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (!_eventTypeCache.TryGet(message.RoutingKey, out var type))
            {
                throw new ArgumentException($"Could not find event type for '{message.RoutingKey}'");
            }

            var eventData = Encoding.UTF8.GetString(message.Body);
            var @event    = (IEvent)_eventDeserializer.Deserialize(eventData, type);

            string        streamId      = null;
            CorrelationId?correlationId = null;
            CausationId?  causationId   = null;
            string        actor         = null;

            if (message.BasicProperties.Headers != null)
            {
                if (message.BasicProperties.Headers.ContainsKey(nameof(IEventContext <IEvent> .StreamId)))
                {
                    streamId = message.BasicProperties.Headers[nameof(IEventContext <IEvent> .StreamId)]?.ToString();
                }

                if (message.BasicProperties.Headers.ContainsKey(nameof(IEventContext <IEvent> .CorrelationId)))
                {
                    var value = message.BasicProperties.Headers[nameof(IEventContext <IEvent> .CorrelationId)]?.ToString();
                    correlationId = value != null?CorrelationId.From(value) : (CorrelationId?)null;
                }

                if (message.BasicProperties.Headers.ContainsKey(nameof(IEventContext <IEvent> .CausationId)))
                {
                    var value = message.BasicProperties.Headers[nameof(IEventContext <IEvent> .CausationId)]?.ToString();
                    causationId = value != null?CausationId.From(value) : (CausationId?)null;
                }

                if (message.BasicProperties.Headers.ContainsKey(nameof(IEventContext <IEvent> .Actor)))
                {
                    actor = message.BasicProperties.Headers[nameof(IEventContext <IEvent> .Actor)]?.ToString();
                }
            }

            if (_cache.TryGetValue(type, out var activator))
            {
                return(activator(streamId, @event, correlationId, causationId, @event.Timestamp, Actor.From(actor ?? "<Unknown>")));
            }

            activator = BuildActivator(typeof(EventContext <>).MakeGenericType(type));

            _cache.TryAdd(type, activator);

            return(activator(streamId, @event, correlationId, causationId, @event.Timestamp, Actor.From(actor ?? "<Unknown>")));
        }
        public override int GetHashCode()
        {
            unchecked
            {
                var hashCode = CausationId != null?CausationId.GetHashCode() : 0;

                hashCode = (hashCode * 397) ^ (TimeStamp != null ? TimeStamp.GetHashCode() : 0);
                hashCode = (hashCode * 397) ^ (Body != null ? Body.GetHashCode() : 0);
                return(hashCode);
            }
        }
Exemple #6
0
        public override CausationId?ReadJson(JsonReader reader, Type objectType, CausationId?existingValue, bool hasExistingValue,
                                             JsonSerializer serializer)
        {
            var value = serializer.Deserialize <string>(reader);

            if (value == null)
            {
                return(null);
            }

            return(CausationId.From(value));
        }
Exemple #7
0
 private async Task SendCommand(string dayId, CorrelationId correlationId, CausationId causationId)
 {
     await _commandStore.Send(new ArchiveDaySchedule(dayId), new CommandMetadata(correlationId, causationId));
 }
        public void WhenCreateCalledWithEventThenShouldReturnEntityWithExpectedPropertiesPopulated()
        {
            var services = new ServiceCollection();

            services.AddOpenEventSourcing()
            .AddEntityFrameworkCoreInMemory()
            .AddJsonSerializers();

            var serviceProvider   = services.BuildServiceProvider();
            var eventModelFactory = serviceProvider.GetRequiredService <IEventModelFactory>();
            var eventSerializer   = serviceProvider.GetRequiredService <IEventSerializer>();

            var @event  = new FakeEvent(subject: Guid.NewGuid().ToString(), version: 1);
            var context = new EventContext <FakeEvent>(streamId: @event.Subject, @event: @event, correlationId: CorrelationId.New(), causationId: CausationId.From(Guid.NewGuid().ToString()), timestamp: DateTimeOffset.UtcNow, actor: Actor.From("test-user"));
            var entity  = eventModelFactory.Create(streamId: @event.Subject, context);

            entity.StreamId.Should().Be(@event.Subject);
            entity.CausationId.Should().Be(context.CausationId);
            entity.CorrelationId.Should().Be(context.CorrelationId);
            entity.Data.Should().Be(eventSerializer.Serialize(@event));
            entity.Id.Should().Be(@event.Id);
            entity.Name.Should().Be(nameof(FakeEvent));
            entity.Timestamp.Should().Be(@event.Timestamp);
            entity.Type.Should().Be(@event.GetType().FullName);
            entity.Actor.Should().Be(context.Actor);
        }