|
| 1 | +using System.Text.Json; |
| 2 | +using Vulthil.Messaging.Abstractions.Publishers; |
| 3 | +using Vulthil.Messaging.Transport; |
1 | 4 | using Vulthil.SharedKernel.Outbox; |
2 | 5 | using Vulthil.xUnit; |
3 | 6 |
|
@@ -26,4 +29,123 @@ public async Task DispatchAsyncThrowsDescriptiveErrorWhenMessageTypeCannotBeReso |
26 | 29 | // Assert |
27 | 30 | exception.Message.ShouldContain("Vulthil.Nonexistent.PhantomMessage"); |
28 | 31 | } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public async Task DispatchAsyncPublishesThePayloadWithTheCapturedMessageIdCorrelationIdRoutingKeyAndHeaders() |
| 35 | + { |
| 36 | + // Arrange |
| 37 | + var jsonOptions = new JsonSerializerOptions(); |
| 38 | + GetMock<IMessageConfigurationProvider>().Setup(provider => provider.JsonSerializerOptions).Returns(jsonOptions); |
| 39 | + Func<IPublishContext, ValueTask>? capturedConfigure = null; |
| 40 | + GetMock<ITransportPublisher>() |
| 41 | + .Setup(publisher => publisher.PublishAsync(It.IsAny<TestMessage>(), It.IsAny<Func<IPublishContext, ValueTask>>(), It.IsAny<CancellationToken>())) |
| 42 | + .Callback<TestMessage, Func<IPublishContext, ValueTask>, CancellationToken>((_, configure, _) => capturedConfigure = configure) |
| 43 | + .Returns(Task.CompletedTask); |
| 44 | + var metadata = new BrokerOutboxMetadata |
| 45 | + { |
| 46 | + MessageId = "stable-message-id", |
| 47 | + CorrelationId = "correlation-1", |
| 48 | + RoutingKey = "routing-key-1", |
| 49 | + Headers = new Dictionary<string, object?> { ["tenant"] = "acme" }, |
| 50 | + }; |
| 51 | + var dispatcher = CreateInstance<BrokerOutboxDispatcher>(); |
| 52 | + var message = new OutboxMessageData( |
| 53 | + Id: Guid.NewGuid(), |
| 54 | + Type: typeof(TestMessage).FullName!, |
| 55 | + Content: JsonSerializer.Serialize(new TestMessage("hello"), jsonOptions), |
| 56 | + TraceParent: null, |
| 57 | + TraceState: null, |
| 58 | + Destination: OutboxDestination.Publish, |
| 59 | + Metadata: JsonSerializer.Serialize(metadata, jsonOptions)); |
| 60 | + |
| 61 | + // Act |
| 62 | + await dispatcher.DispatchAsync(message, CancellationToken); |
| 63 | + |
| 64 | + // Assert |
| 65 | + GetMock<ITransportPublisher>().Verify( |
| 66 | + publisher => publisher.PublishAsync(It.Is<TestMessage>(m => m.Value == "hello"), It.IsAny<Func<IPublishContext, ValueTask>>(), CancellationToken), |
| 67 | + Times.Once); |
| 68 | + capturedConfigure.ShouldNotBeNull(); |
| 69 | + var appliedContext = new PublishContext(); |
| 70 | + await capturedConfigure!(appliedContext); |
| 71 | + appliedContext.MessageId.ShouldBe("stable-message-id"); |
| 72 | + appliedContext.CorrelationId.ShouldBe("correlation-1"); |
| 73 | + appliedContext.RoutingKey.ShouldBe("routing-key-1"); |
| 74 | + appliedContext.Headers["tenant"].ShouldBe("acme"); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public async Task DispatchAsyncSendsThePayloadToTheCapturedDestinationAddressWithFidelity() |
| 79 | + { |
| 80 | + // Arrange |
| 81 | + var jsonOptions = new JsonSerializerOptions(); |
| 82 | + GetMock<IMessageConfigurationProvider>().Setup(provider => provider.JsonSerializerOptions).Returns(jsonOptions); |
| 83 | + var sendEndpoint = new Mock<ISendEndpoint>(); |
| 84 | + Func<IPublishContext, ValueTask>? capturedConfigure = null; |
| 85 | + sendEndpoint |
| 86 | + .Setup(endpoint => endpoint.SendAsync(It.IsAny<TestMessage>(), It.IsAny<Func<IPublishContext, ValueTask>>(), It.IsAny<CancellationToken>())) |
| 87 | + .Callback<TestMessage, Func<IPublishContext, ValueTask>, CancellationToken>((_, configure, _) => capturedConfigure = configure) |
| 88 | + .Returns(Task.CompletedTask); |
| 89 | + var destinationAddress = new Uri("queue:order-commands"); |
| 90 | + GetMock<ITransportSendEndpointProvider>() |
| 91 | + .Setup(provider => provider.GetSendEndpointAsync(destinationAddress, It.IsAny<CancellationToken>())) |
| 92 | + .ReturnsAsync(sendEndpoint.Object); |
| 93 | + var metadata = new BrokerOutboxMetadata |
| 94 | + { |
| 95 | + MessageId = "stable-send-id", |
| 96 | + DestinationAddress = destinationAddress.ToString(), |
| 97 | + Headers = new Dictionary<string, object?> { ["tenant"] = "acme" }, |
| 98 | + }; |
| 99 | + var dispatcher = CreateInstance<BrokerOutboxDispatcher>(); |
| 100 | + var message = new OutboxMessageData( |
| 101 | + Id: Guid.NewGuid(), |
| 102 | + Type: typeof(TestMessage).FullName!, |
| 103 | + Content: JsonSerializer.Serialize(new TestMessage("hello"), jsonOptions), |
| 104 | + TraceParent: null, |
| 105 | + TraceState: null, |
| 106 | + Destination: OutboxDestination.Send, |
| 107 | + Metadata: JsonSerializer.Serialize(metadata, jsonOptions)); |
| 108 | + |
| 109 | + // Act |
| 110 | + await dispatcher.DispatchAsync(message, CancellationToken); |
| 111 | + |
| 112 | + // Assert |
| 113 | + sendEndpoint.Verify( |
| 114 | + endpoint => endpoint.SendAsync(It.Is<TestMessage>(m => m.Value == "hello"), It.IsAny<Func<IPublishContext, ValueTask>>(), CancellationToken), |
| 115 | + Times.Once); |
| 116 | + capturedConfigure.ShouldNotBeNull(); |
| 117 | + var appliedContext = new PublishContext(); |
| 118 | + await capturedConfigure!(appliedContext); |
| 119 | + appliedContext.MessageId.ShouldBe("stable-send-id"); |
| 120 | + appliedContext.Headers["tenant"].ShouldBe("acme"); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public async Task DispatchAsyncThrowsDescriptiveErrorWhenASendMessageHasNoDestinationAddress() |
| 125 | + { |
| 126 | + // Arrange |
| 127 | + var jsonOptions = new JsonSerializerOptions(); |
| 128 | + GetMock<IMessageConfigurationProvider>().Setup(provider => provider.JsonSerializerOptions).Returns(jsonOptions); |
| 129 | + var dispatcher = CreateInstance<BrokerOutboxDispatcher>(); |
| 130 | + var message = new OutboxMessageData( |
| 131 | + Id: Guid.NewGuid(), |
| 132 | + Type: typeof(TestMessage).FullName!, |
| 133 | + Content: JsonSerializer.Serialize(new TestMessage("hello"), jsonOptions), |
| 134 | + TraceParent: null, |
| 135 | + TraceState: null, |
| 136 | + Destination: OutboxDestination.Send, |
| 137 | + Metadata: null); |
| 138 | + |
| 139 | + // Act |
| 140 | + var exception = await Should.ThrowAsync<InvalidOperationException>( |
| 141 | + () => dispatcher.DispatchAsync(message, CancellationToken)); |
| 142 | + |
| 143 | + // Assert |
| 144 | + exception.Message.ShouldContain("missing its destination address"); |
| 145 | + GetMock<ITransportSendEndpointProvider>().Verify( |
| 146 | + provider => provider.GetSendEndpointAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()), |
| 147 | + Times.Never); |
| 148 | + } |
| 149 | + |
| 150 | + private sealed record TestMessage(string Value); |
29 | 151 | } |
0 commit comments