Skip to content

Commit ebf80b2

Browse files
author
Morten Laursen
committed
feat: add PublishAsync method for serialized messages
1 parent 784f989 commit ebf80b2

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

src/Atc.Azure.Messaging/EventHub/EventHubPublisher.cs

+11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ public Task PublishAsync(
2626
cancellationToken);
2727
}
2828

29+
public Task PublishAsync(
30+
string message,
31+
IDictionary<string, string>? messageProperties = null,
32+
CancellationToken cancellationToken = default)
33+
{
34+
return PerformPublishAsync(
35+
message,
36+
messageProperties,
37+
cancellationToken);
38+
}
39+
2940
private Task PerformPublishAsync(
3041
string messageBody,
3142
IDictionary<string, string>? messageProperties = null,

src/Atc.Azure.Messaging/EventHub/IEventHubPublisher.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Atc.Azure.Messaging.EventHub;
22

3-
/// <summary>
4-
/// Publisher responsible for publishing objects with metadata to a specific EventHub.
3+
/// <summary>
4+
/// Publisher responsible for publishing objects with metadata to a specific EventHub.
55
/// </summary>
66
/// <remarks>
77
/// Is safe to cache and use in singletons for the lifetime of the application.
@@ -12,4 +12,9 @@ Task PublishAsync(
1212
object message,
1313
IDictionary<string, string>? messageProperties = null,
1414
CancellationToken cancellationToken = default);
15+
16+
Task PublishAsync(
17+
string message,
18+
IDictionary<string, string>? messageProperties = null,
19+
CancellationToken cancellationToken = default);
1520
}

test/Atc.Azure.Messaging.Tests/EventHub/EventHubPublisherTests.cs

+29-4
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ internal async Task PublishAsync_Calls_Client_With_Correct_MessageBody(
2929
[Frozen, Substitute] EventHubProducerClient client,
3030
[Frozen] IMessagePayloadSerializer serializer,
3131
EventHubPublisher sut,
32-
string messageBody,
32+
object messageBody,
33+
string serializedMessage,
3334
IDictionary<string, string> properties,
3435
CancellationToken cancellationToken)
3536
{
3637
serializer
37-
.Serialize<object>(default!)
38-
.ReturnsForAnyArgs(messageBody);
38+
.Serialize(messageBody)
39+
.ReturnsForAnyArgs(serializedMessage);
3940
await sut.PublishAsync(
4041
messageBody,
4142
properties,
@@ -49,7 +50,31 @@ await sut.PublishAsync(
4950
.Should()
5051
.BeEquivalentTo(
5152
Encoding.UTF8.GetBytes(
52-
messageBody));
53+
serializedMessage));
54+
}
55+
56+
[Theory, AutoNSubstituteData]
57+
internal async Task PublishAsync_Calls_Client_With_Correct_Serialized_MessageBody(
58+
[Frozen, Substitute] EventHubProducerClient client,
59+
EventHubPublisher sut,
60+
string serializedMessage,
61+
IDictionary<string, string> properties,
62+
CancellationToken cancellationToken)
63+
{
64+
await sut.PublishAsync(
65+
serializedMessage,
66+
properties,
67+
cancellationToken);
68+
69+
var data = client
70+
.ReceivedCallWithArgument<EventData[]>()
71+
.Single();
72+
73+
data.Body.ToArray()
74+
.Should()
75+
.BeEquivalentTo(
76+
Encoding.UTF8.GetBytes(
77+
serializedMessage));
5378
}
5479

5580
[Theory, AutoNSubstituteData]

0 commit comments

Comments
 (0)