Search in sources :

Example 6 with Tuple

use of org.springframework.tuple.Tuple 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)

Example 7 with Tuple

use of org.springframework.tuple.Tuple in project spring-cloud-gateway by spring-cloud.

the class RequestRateLimiterGatewayFilterFactoryTests method assertFilterFactory.

private void assertFilterFactory(KeyResolver keyResolver, String key, boolean allowed, HttpStatus expectedStatus) {
    Tuple args = tuple().build();
    when(rateLimiter.isAllowed("myroute", key)).thenReturn(Mono.just(new Response(allowed, 1)));
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    exchange.getResponse().setStatusCode(HttpStatus.OK);
    exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR, Route.builder().id("myroute").predicate(ex -> true).uri("http://localhost").build());
    when(this.filterChain.filter(exchange)).thenReturn(Mono.empty());
    RequestRateLimiterGatewayFilterFactory factory = this.context.getBean(RequestRateLimiterGatewayFilterFactory.class);
    GatewayFilter filter = factory.apply(config -> config.setKeyResolver(keyResolver));
    Mono<Void> response = filter.filter(exchange, this.filterChain);
    response.subscribe(aVoid -> assertThat(exchange.getResponse().getStatusCode()).isEqualTo(expectedStatus));
}
Also used : Response(org.springframework.cloud.gateway.filter.ratelimit.RateLimiter.Response) DirtiesContext(org.springframework.test.annotation.DirtiesContext) TupleBuilder.tuple(org.springframework.tuple.TupleBuilder.tuple) GatewayFilterChain(org.springframework.cloud.gateway.filter.GatewayFilterChain) ServerWebExchangeUtils(org.springframework.cloud.gateway.support.ServerWebExchangeUtils) Response(org.springframework.cloud.gateway.filter.ratelimit.RateLimiter.Response) RateLimiter(org.springframework.cloud.gateway.filter.ratelimit.RateLimiter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) BaseWebClientTests(org.springframework.cloud.gateway.test.BaseWebClientTests) Autowired(org.springframework.beans.factory.annotation.Autowired) Qualifier(org.springframework.beans.factory.annotation.Qualifier) KeyResolver(org.springframework.cloud.gateway.filter.ratelimit.KeyResolver) SpringRunner(org.springframework.test.context.junit4.SpringRunner) RANDOM_PORT(org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT) MockBean(org.springframework.boot.test.mock.mockito.MockBean) EnableAutoConfiguration(org.springframework.boot.autoconfigure.EnableAutoConfiguration) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Import(org.springframework.context.annotation.Import) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) ApplicationContext(org.springframework.context.ApplicationContext) HttpStatus(org.springframework.http.HttpStatus) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Tuple(org.springframework.tuple.Tuple) SpringBootConfiguration(org.springframework.boot.SpringBootConfiguration) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Bean(org.springframework.context.annotation.Bean) Route(org.springframework.cloud.gateway.route.Route) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Tuple(org.springframework.tuple.Tuple) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter)

Aggregations

Tuple (org.springframework.tuple.Tuple)7 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 PredicateArgsEvent (org.springframework.cloud.gateway.event.PredicateArgsEvent)2 GatewayFilter (org.springframework.cloud.gateway.filter.GatewayFilter)2 TupleBuilder (org.springframework.tuple.TupleBuilder)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 RunWith (org.junit.runner.RunWith)1 Mockito.when (org.mockito.Mockito.when)1