Path: redb-route/redb.Route.TestKit/AdviceWithBuilder.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/redb.Route.TestKit/AdviceWithBuilder.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Definitions;
namespace redb.Route.TestKit;
///
/// Rewrites one route definition before the context starts (Apache Camel AdviceWith):
/// swap the consumer, send matching To steps to mocks, or weave steps in, out and around
/// existing ones. The route under test is not modified for testability — the advice is.
/// Obtained through .
///
public sealed class AdviceWithBuilder
{
private readonly RouteDefinition _route;
internal AdviceWithBuilder(RouteDefinition route)
{
_route = route ?? throw new ArgumentNullException(nameof(route));
}
/// The route id being advised (explicit RouteId or the endpoint-derived fallback).
public string RouteId { get; internal init; } = string.Empty;
/// Replaces the route's From endpoint (Camel replaceFromWith): kafka://orders becomes direct://test-in.
public AdviceWithBuilder ReplaceFrom(string uri)
{
ArgumentException.ThrowIfNullOrWhiteSpace(uri);
_route.From(uri);
return this;
}
///
/// Sends every static To whose URI matches one of to the mock
/// standing in for it () instead of the original endpoint — the
/// original is never called. Masks: exact URI, scheme://prefix*, regex:....
///
public AdviceWithBuilder MockEndpoints(params string[] uriMasks)
{
if (uriMasks is not { Length: > 0 })
throw new ArgumentException("At least one URI mask is required.", nameof(uriMasks));
foreach (var slot in DefinitionTree.Slots(_route))
{
if (slot.Node is not ToDefinition to || !uriMasks.Any(mask => UriMask.IsMatch(mask, to.Uri)))
continue;
var mock = new ToDefinition(MockUri.For(to.Uri)) { StepId = to.StepId, Parent = slot.Owner };
slot.Owner.Outputs[slot.Index] = mock;
}
return this;
}
/// Same as ; the Camel name for "mock and do not call the original", kept for recognisability.
public AdviceWithBuilder MockEndpointsAndSkip(params string[] uriMasks) => MockEndpoints(uriMasks);
/// Selects the steps named via the Id("...") DSL verb.
public WeaveTarget WeaveById(string id)
{
ArgumentException.ThrowIfNullOrWhiteSpace(id);
return Select($"id '{id}'", s => s.Node is ProcessorDefinition { StepId: { } stepId } && string.Equals(stepId, id, StringComparison.OrdinalIgnoreCase));
}
/// Selects the static To steps whose URI matches .
public WeaveTarget WeaveByToUri(string uriMask)
{
ArgumentException.ThrowIfNullOrWhiteSpace(uriMask);
return Select($"To URI mask '{uriMask}'", s => s.Node is ToDefinition to && UriMask.IsMatch(uriMask, to.Uri));
}
/// Selects every step of definition type (e.g. ThrottleDefinition).
public WeaveTarget WeaveByType() where T : IProcessorDefinition
=> Select($"type {typeof(T).Name}", s => s.Node is T);
/// Adds the steps built by at the very beginning of the route.
public AdviceWithBuilder WeaveAddFirst(Action steps)
{
ArgumentNullException.ThrowIfNull(steps);
DefinitionTree.InsertAll(_route, 0, WeaveTarget.Build(steps));
return this;
}
/// Adds the steps built by at the very end of the route.
public AdviceWithBuilder WeaveAddLast(Action steps)
{
ArgumentNullException.ThrowIfNull(steps);
DefinitionTree.InsertAll(_route, _route.Outputs.Count, WeaveTarget.Build(steps));
return this;
}
private WeaveTarget Select(string what, Func match)
{
var slots = DefinitionTree.Slots(_route).Where(match).ToArray();
if (slots.Length == 0)
throw new InvalidOperationException($"AdviceWith on route '{RouteId}': no step matches {what}.");
return new WeaveTarget(this, slots);
}
}