Skip to content

Commit

Permalink
Keep Type of MotorCloudEvent after serialization (#273)
Browse files Browse the repository at this point in the history
* Keep Type of MotorCloudEvent after serialization

* Dotnet Format

* Use simpler constructor for MotorCloudEvent
  • Loading branch information
rngcntr authored Jul 29, 2021
1 parent 6d0031a commit db45639
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/Motor.Extensions.Hosting.CloudEvents/MotorCloudEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ public MotorCloudEvent(
IApplicationNameService applicationNameService,
TData data,
Uri source,
params KeyValuePair<CloudEventAttribute, object>[] extensions) : this(applicationNameService, data, source,
null, null, null, extensions)
params KeyValuePair<CloudEventAttribute, object>[] extensions) : this(applicationNameService, data, null,
source, null, null, null, extensions)
{
}

public MotorCloudEvent(
IApplicationNameService applicationNameService,
TData data,
string? type,
Uri source,
string? id,
DateTimeOffset? time,
Expand All @@ -51,7 +52,7 @@ public MotorCloudEvent(
BaseEvent = new CloudEvent(CloudEventsSpecVersion.Default);
foreach (var (key, value) in extensions) BaseEvent[key] = value;
BaseEvent.Id = id ?? Guid.NewGuid().ToString();
BaseEvent.Type = typeof(TData).Name;
BaseEvent.Type = type ?? typeof(TData).Name;
BaseEvent.Source = source;
BaseEvent.Time = time ?? DateTimeOffset.UtcNow;
BaseEvent.DataContentType = contentType ?? new ContentType().ToString();
Expand Down Expand Up @@ -111,15 +112,15 @@ private static MotorCloudEvent<T> CreateCloudEvent<T>(IApplicationNameService ap
IEnumerable<KeyValuePair<CloudEventAttribute, object>>? extensions = null)
where T : class
{
return new(applicationNameService, data, applicationNameService.GetSource(),
return new MotorCloudEvent<T>(applicationNameService, data, applicationNameService.GetSource(),
extensions?.ToArray() ?? Array.Empty<KeyValuePair<CloudEventAttribute, object>>());
}

public MotorCloudEvent<T> CreateNew<T>(T data, bool useOldIdentifier = false)
where T : class
{
return useOldIdentifier
? new MotorCloudEvent<T>(_applicationNameService, data, BaseEvent.Source!, BaseEvent.Id, BaseEvent.Time,
? new MotorCloudEvent<T>(_applicationNameService, data, BaseEvent.Type, BaseEvent.Source!, BaseEvent.Id, BaseEvent.Time,
BaseEvent.DataContentType, BaseEvent.GetPopulatedAttributes().ToArray())
: CreateCloudEvent(_applicationNameService, data, BaseEvent.GetPopulatedAttributes());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static MotorCloudEvent<byte[]> ToMotorCloudEvent(this Message<string?, by
throw new ArgumentException("Source property of CloudEvent is null");
}
var motorCloudEvent = new MotorCloudEvent<byte[]>(applicationNameService, (byte[])cloudEvent.Data,
cloudEvent.Source, cloudEvent.Id, cloudEvent.Time, null);
cloudEvent.Type, cloudEvent.Source, cloudEvent.Id, cloudEvent.Time, cloudEvent.DataContentType);
foreach (var (key, value) in cloudEvent.GetPopulatedAttributes())
{
if (motorCloudEvent.GetAttribute(key.Name) is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static MotorCloudEvent<byte[]> ExtractCloudEvent(this IBasicProperties se
}

var cloudEvent = new MotorCloudEvent<byte[]>(applicationNameService, body.ToArray(),
new Uri("rabbitmq://notset"), null, null, self.ContentType);
null, new Uri("rabbitmq://notset"), null, null, self.ContentType);

if (self.IsPriorityPresent())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,27 @@ public void CreateNew_UseOldIdentifierFromOld_IdAndDateAreNewCreated()
}

[Fact]
public void CreateNew_UseOldIdentifierFromOld_TypeMatchesActualDataType()
public void CreateNew_UseOldIdentifierFromOld_TypeMatchesOldDataType()
{
var oldEvent =
new MotorCloudEvent<string>(GetApplicationNameService(), " ", new Uri("test://non"));
var expectedData = new List<string>();

var newEvent = oldEvent.CreateNew(expectedData, true);

Assert.Equal(nameof(String), oldEvent.Type);
Assert.Equal(nameof(String), newEvent.Type);
}

[Fact]
public void CreateNew_DoNotUseOldIdentifierFromOld_TypeMatchesNewDataType()
{
var oldEvent =
new MotorCloudEvent<string>(GetApplicationNameService(), " ", new Uri("test://non"));
var expectedData = new List<string>();

var newEvent = oldEvent.CreateNew(expectedData, false);

Assert.Equal(nameof(String), oldEvent.Type);
Assert.Equal(typeof(List<string>).Name, newEvent.Type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ public async Task PublishMessageAsync_ContextToPassed_ContextPassed()
It.IsAny<CancellationToken>()), Times.Once);
}

[Fact]
public async Task PublishMessageAsync_CloudEventOfTypeString_PublishedCloudEventHasTypeString()
{
var bytesPublisher = new Mock<ITypedMessagePublisher<byte[]>>();
var typedMessagePublisher = CreateTypedMessagePublisher(bytesPublisher.Object);
var motorEvent = MotorCloudEvent.CreateTestCloudEvent("test");

await typedMessagePublisher.PublishMessageAsync(motorEvent);

bytesPublisher.Verify(t => t.PublishMessageAsync(
It.Is<MotorCloudEvent<byte[]>>(it => it.Type == motorEvent.Type),
It.IsAny<CancellationToken>()), Times.Once);
}

private static TypedMessagePublisher<string, ITypedMessagePublisher<byte[]>> CreateTypedMessagePublisher(
ITypedMessagePublisher<byte[]>? publisher = null, IMessageSerializer<string>? serializer = null,
IMessageEncoder? encoder = null)
Expand Down

0 comments on commit db45639

Please sign in to comment.