Path: redb-route/tests/redb.Route.Tests/Processors/EnrichProcessorTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests/Processors/EnrichProcessorTests.cs
----
using AwesomeAssertions;
using NSubstitute;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Tests.Processors;
/// Tests for .
public class EnrichProcessorTests
{
private static (IRouteContext context, IProducer producer) SetupContext(string uri)
{
var producer = Substitute.For();
var endpoint = Substitute.For();
endpoint.CreateProducer().Returns(producer);
var context = Substitute.For();
context.GetEndpoint(uri).Returns(endpoint);
return (context, producer);
}
[Fact]
public async Task Process_MergesEnrichedData()
{
var (context, producer) = SetupContext("direct://lookup");
producer.When(p => p.Process(Arg.Any(), Arg.Any()))
.Do(ci =>
{
var ex = ci.Arg();
ex.In.Headers["enriched"] = "yes";
ex.In.Body = "enriched-body";
});
var enricher = new EnrichProcessor(context, "direct://lookup",
mergeStrategy: (original, enriched) =>
{
original.In.Headers["enriched"] = enriched.In.Headers["enriched"];
original.In.Body = $"{original.In.Body}+{enriched.In.Body}";
return original;
});
var exchange = new Exchange(new Message("original-body"));
await enricher.Process(exchange);
exchange.In.Body.Should().Be("original-body+enriched-body");
exchange.In.Headers["enriched"].Should().Be("yes");
}
private sealed class TrackingResource : IAsyncDisposable
{
public int DisposeCount;
public ValueTask DisposeAsync()
{
Interlocked.Increment(ref DisposeCount);
return ValueTask.CompletedTask;
}
}
[Fact]
public async Task Process_HandsTheResourceExchangesResourcesOverToTheOriginal()
{
var (context, producer) = SetupContext("direct://stream");
var resource = new TrackingResource();
producer.When(p => p.Process(Arg.Any(), Arg.Any()))
.Do(ci =>
{
// A streamed SQL result: the body holds a connection that is released when its exchange ends.
var ex = ci.Arg();
ExchangeResources.ReleaseWithExchange(ex, resource);
ex.In.Body = "stream";
});
var enricher = new EnrichProcessor(context, "direct://stream",
mergeStrategy: (original, enriched) =>
{
original.In.Body = enriched.In.Body;
return original;
});
var exchange = new Exchange(new Message("original"));
await enricher.Process(exchange);
exchange.In.Body.Should().Be("stream");
resource.DisposeCount.Should().Be(0,
"as Apache Camel's Enrich hands the resource exchange's completions over, the stream lives until the original ends");
await exchange.DisposeAsync();
resource.DisposeCount.Should().Be(1);
}
[Fact]
public async Task Process_SendsCloneToResource()
{
IExchange? sentExchange = null;
var (context, producer) = SetupContext("direct://resource");
producer.When(p => p.Process(Arg.Any(), Arg.Any()))
.Do(ci => sentExchange = ci.Arg());
var enricher = new EnrichProcessor(context, "direct://resource",
mergeStrategy: (orig, _) => orig);
var original = new Exchange(new Message("data"));
await enricher.Process(original);
sentExchange.Should().NotBeNull();
sentExchange.Should().NotBeSameAs(original);
sentExchange!.In.Body.Should().Be("data");
sentExchange.Pattern.Should().Be(ExchangePattern.InOut);
}
[Fact]
public async Task Process_CachesProducer()
{
var (context, producer) = SetupContext("direct://resource");
var enricher = new EnrichProcessor(context, "direct://resource",
mergeStrategy: (orig, _) => orig);
await enricher.Process(new Exchange(new Message("data")));
await enricher.Process(new Exchange(new Message("data2")));
// Producer created once, reused across calls
context.GetEndpoint("direct://resource")
.Received(1).CreateProducer();
}
[Fact]
public async Task Process_PreservesOriginalWhenMergeReturnsOriginal()
{
var (context, producer) = SetupContext("direct://resource");
producer.When(p => p.Process(Arg.Any(), Arg.Any()))
.Do(ci => ci.Arg().In.Body = "enriched");
var enricher = new EnrichProcessor(context, "direct://resource",
mergeStrategy: (orig, _) => orig); // Ignore enrichment
var exchange = new Exchange(new Message("keep-me"));
await enricher.Process(exchange);
exchange.In.Body.Should().Be("keep-me");
}
[Fact]
public void Constructor_NullContext_Throws()
{
var act = () => new EnrichProcessor(null!, "direct://a", (a, b) => a);
act.Should().Throw().WithParameterName("context");
}
[Fact]
public void Constructor_NullUri_Throws()
{
var act = () => new EnrichProcessor(Substitute.For(), (string)null!, (a, b) => a);
act.Should().Throw().WithParameterName("resourceUri");
}
[Fact]
public void Constructor_NullMerge_Throws()
{
var act = () => new EnrichProcessor(Substitute.For(), "direct://a", null!);
act.Should().Throw().WithParameterName("mergeStrategy");
}
}
/// Tests for .
public class PollEnrichProcessorTests
{
private static (IRouteContext context, IProducer producer) SetupContext(string uri)
{
var producer = Substitute.For();
var endpoint = Substitute.For();
endpoint.CreateProducer().Returns(producer);
var context = Substitute.For();
context.GetEndpoint(uri).Returns(endpoint);
return (context, producer);
}
[Fact]
public async Task Process_SuccessfulPoll_MergesData()
{
var (context, producer) = SetupContext("seda://queue");
producer.When(p => p.Process(Arg.Any(), Arg.Any()))
.Do(ci => ci.Arg().In.Body = "polled-data");
var pollEnricher = new PollEnrichProcessor(context, "seda://queue",
mergeStrategy: (original, polled) =>
{
original.In.Body = $"{original.In.Body}+{polled!.In.Body}";
return original;
},
timeout: TimeSpan.FromSeconds(5));
var exchange = new Exchange(new Message("base"));
await pollEnricher.Process(exchange);
exchange.In.Body.Should().Be("base+polled-data");
}
[Fact]
public async Task Process_Timeout_MergeReceivesNull()
{
var producer = Substitute.For();
// NSubstitute's When..Do with async void doesn't delay the Task return;
// use Returns with an actual async task instead.
producer.Process(Arg.Any(), Arg.Any())
.Returns(async ci =>
{
var ct = ci.Arg();
await Task.Delay(TimeSpan.FromSeconds(5), ct);
});
var endpoint = Substitute.For();
endpoint.CreateProducer().Returns(producer);
var context = Substitute.For();
context.GetEndpoint("seda://slow").Returns(endpoint);
var pollEnricher = new PollEnrichProcessor(context, "seda://slow",
mergeStrategy: (original, polled) =>
{
original.In.Body = polled is null ? "timeout" : "got-data";
return original;
},
timeout: TimeSpan.FromMilliseconds(50));
var exchange = new Exchange(new Message("base"));
await pollEnricher.Process(exchange);
exchange.In.Body.Should().Be("timeout");
}
[Fact]
public async Task Process_SetsInOutPattern()
{
IExchange? sent = null;
var (context, producer) = SetupContext("seda://queue");
producer.When(p => p.Process(Arg.Any(), Arg.Any()))
.Do(ci => sent = ci.Arg());
var pollEnricher = new PollEnrichProcessor(context, "seda://queue",
mergeStrategy: (orig, _) => orig);
await pollEnricher.Process(new Exchange(new Message("data")));
sent.Should().NotBeNull();
sent!.Pattern.Should().Be(ExchangePattern.InOut);
}
[Fact]
public void Constructor_NullContext_Throws()
{
var act = () => new PollEnrichProcessor(null!, "seda://a", (a, b) => a);
act.Should().Throw().WithParameterName("context");
}
[Fact]
public void Constructor_NullUri_Throws()
{
var act = () => new PollEnrichProcessor(Substitute.For(), (string)null!, (a, b) => a);
act.Should().Throw().WithParameterName("resourceUri");
}
[Fact]
public void Constructor_NullMerge_Throws()
{
var act = () => new PollEnrichProcessor(Substitute.For(), "seda://a", null!);
act.Should().Throw().WithParameterName("mergeStrategy");
}
}