use of org.springframework.cloud.gateway.event.PredicateArgsEvent in project spring-cloud-gateway by spring-cloud.
the class WeightCalculatorWebFilterTests method receivesPredicateArgsEvent.
@Test
public void receivesPredicateArgsEvent() {
WeightCalculatorWebFilter filter = mock(WeightCalculatorWebFilter.class);
doNothing().when(filter).addWeightConfig(any(WeightConfig.class));
doCallRealMethod().when(filter).handle(any(PredicateArgsEvent.class));
HashMap<String, Object> args = new HashMap<>();
args.put("weight.group", "group1");
args.put("weight.weight", "1");
PredicateArgsEvent event = new PredicateArgsEvent(this, "routeA", args);
filter.handle(event);
ArgumentCaptor<WeightConfig> configCaptor = ArgumentCaptor.forClass(WeightConfig.class);
verify(filter).addWeightConfig(configCaptor.capture());
WeightConfig weightConfig = configCaptor.getValue();
assertThat(weightConfig.getGroup()).isEqualTo("group1");
assertThat(weightConfig.getRouteId()).isEqualTo("routeA");
assertThat(weightConfig.getWeight()).isEqualTo(1);
}
use of org.springframework.cloud.gateway.event.PredicateArgsEvent in project spring-cloud-gateway by spring-cloud.
the class RouteDefinitionRouteLocator method lookup.
@SuppressWarnings("unchecked")
private Predicate<ServerWebExchange> lookup(RouteDefinition route, PredicateDefinition predicate) {
RoutePredicateFactory<Object> factory = this.predicates.get(predicate.getName());
if (factory == null) {
throw new IllegalArgumentException("Unable to find RoutePredicateFactory with name " + predicate.getName());
}
Map<String, String> args = predicate.getArgs();
if (logger.isDebugEnabled()) {
logger.debug("RouteDefinition " + route.getId() + " applying " + args + " to " + predicate.getName());
}
if (!factory.isConfigurable()) {
Tuple tuple = getTuple(factory, args, this.parser, this.beanFactory);
return factory.apply(tuple);
} else {
Map<String, Object> properties = factory.shortcutType().normalize(args, factory, this.parser, this.beanFactory);
Object config = factory.newConfig();
ConfigurationUtils.bind(config, properties, factory.shortcutFieldPrefix(), predicate.getName(), validator);
if (this.publisher != null) {
this.publisher.publishEvent(new PredicateArgsEvent(this, route.getId(), properties));
}
return factory.apply(config);
}
}
Aggregations