Search in sources :

Example 1 with PredicateArgsEvent

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);
}
Also used : PredicateArgsEvent(org.springframework.cloud.gateway.event.PredicateArgsEvent) HashMap(java.util.HashMap) GroupWeightConfig(org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter.GroupWeightConfig) WeightConfig(org.springframework.cloud.gateway.support.WeightConfig) Test(org.junit.Test)

Example 2 with PredicateArgsEvent

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);
    }
}
Also used : PredicateArgsEvent(org.springframework.cloud.gateway.event.PredicateArgsEvent) Tuple(org.springframework.tuple.Tuple)

Aggregations

PredicateArgsEvent (org.springframework.cloud.gateway.event.PredicateArgsEvent)2 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 GroupWeightConfig (org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter.GroupWeightConfig)1 WeightConfig (org.springframework.cloud.gateway.support.WeightConfig)1 Tuple (org.springframework.tuple.Tuple)1