Add test case on nested RouterFunction attributes
This commit is contained in:
committed by
Arjen Poutsma
parent
571543285a
commit
5b1bda5c7c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,42 +16,58 @@
|
||||
|
||||
package org.springframework.web.servlet.function;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
class AttributesTestVisitor implements RouterFunctions.Visitor {
|
||||
|
||||
private Deque<Map<String, Object>> nestedAttributes = new LinkedList<>();
|
||||
|
||||
@Nullable
|
||||
private Map<String, Object> attributes;
|
||||
|
||||
private List<List<Map<String, Object>>> routerFunctionsAttributes = new LinkedList<>();
|
||||
|
||||
private int visitCount;
|
||||
|
||||
public List<List<Map<String, Object>>> routerFunctionsAttributes() {
|
||||
return this.routerFunctionsAttributes;
|
||||
}
|
||||
|
||||
public int visitCount() {
|
||||
return this.visitCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startNested(RequestPredicate predicate) {
|
||||
nestedAttributes.addFirst(attributes);
|
||||
attributes = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endNested(RequestPredicate predicate) {
|
||||
attributes = nestedAttributes.removeFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void route(RequestPredicate predicate, HandlerFunction<?> handlerFunction) {
|
||||
assertThat(this.attributes).isNotNull();
|
||||
this.attributes = null;
|
||||
Stream<Map<String, Object>> current = Optional.ofNullable(attributes).stream();
|
||||
Stream<Map<String, Object>> nested = nestedAttributes.stream().filter(Objects::nonNull);
|
||||
routerFunctionsAttributes.add(Stream.concat(current, nested).collect(Collectors.toUnmodifiableList()));
|
||||
attributes = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,7 +76,6 @@ class AttributesTestVisitor implements RouterFunctions.Visitor {
|
||||
|
||||
@Override
|
||||
public void attributes(Map<String, Object> attributes) {
|
||||
assertThat(attributes).containsExactly(entry("foo", "bar"), entry("baz", "qux"));
|
||||
this.attributes = attributes;
|
||||
this.visitCount++;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,6 +17,8 @@
|
||||
package org.springframework.web.servlet.function;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
@@ -226,12 +228,28 @@ class RouterFunctionBuilderTests {
|
||||
atts.put("foo", "bar");
|
||||
atts.put("baz", "qux");
|
||||
})
|
||||
.path("/atts", b1 -> b1
|
||||
.GET("/3", request -> ServerResponse.ok().build())
|
||||
.withAttribute("foo", "bar")
|
||||
.GET("/4", request -> ServerResponse.ok().build())
|
||||
.withAttribute("baz", "qux")
|
||||
.path("/5", b2 -> b2
|
||||
.GET(request -> ServerResponse.ok().build())
|
||||
.withAttribute("foo", "n3"))
|
||||
.withAttribute("foo", "n2")
|
||||
)
|
||||
.withAttribute("foo", "n1")
|
||||
.build();
|
||||
|
||||
AttributesTestVisitor visitor = new AttributesTestVisitor();
|
||||
route.accept(visitor);
|
||||
assertThat(visitor.visitCount()).isEqualTo(2);
|
||||
assertThat(visitor.routerFunctionsAttributes()).containsExactly(
|
||||
List.of(Map.of("foo", "bar", "baz", "qux")),
|
||||
List.of(Map.of("foo", "bar", "baz", "qux")),
|
||||
List.of(Map.of("foo", "bar"), Map.of("foo", "n1")),
|
||||
List.of(Map.of("baz", "qux"), Map.of("foo", "n1")),
|
||||
List.of(Map.of("foo", "n3"), Map.of("foo", "n2"), Map.of("foo", "n1"))
|
||||
);
|
||||
assertThat(visitor.visitCount()).isEqualTo(7);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,6 +17,8 @@
|
||||
package org.springframework.web.servlet.function;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -24,7 +26,10 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.web.servlet.handler.PathPatternsTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.http.HttpMethod.GET;
|
||||
import static org.springframework.web.servlet.function.RequestPredicates.GET;
|
||||
import static org.springframework.web.servlet.function.RequestPredicates.method;
|
||||
import static org.springframework.web.servlet.function.RequestPredicates.path;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -120,11 +125,28 @@ class RouterFunctionTests {
|
||||
.withAttributes(atts -> {
|
||||
atts.put("foo", "bar");
|
||||
atts.put("baz", "qux");
|
||||
}));
|
||||
}))
|
||||
.and(RouterFunctions.nest(path("/atts"),
|
||||
RouterFunctions.route(GET("/3"), request -> ServerResponse.ok().build())
|
||||
.withAttribute("foo", "bar")
|
||||
.and(RouterFunctions.route(GET("/4"), request -> ServerResponse.ok().build())
|
||||
.withAttribute("baz", "qux"))
|
||||
.and(RouterFunctions.nest(path("/5"),
|
||||
RouterFunctions.route(method(GET), request -> ServerResponse.ok().build())
|
||||
.withAttribute("foo", "n3"))
|
||||
.withAttribute("foo", "n2")))
|
||||
.withAttribute("foo", "n1"));
|
||||
|
||||
AttributesTestVisitor visitor = new AttributesTestVisitor();
|
||||
route.accept(visitor);
|
||||
assertThat(visitor.visitCount()).isEqualTo(2);
|
||||
assertThat(visitor.routerFunctionsAttributes()).containsExactly(
|
||||
List.of(Map.of("foo", "bar", "baz", "qux")),
|
||||
List.of(Map.of("foo", "bar", "baz", "qux")),
|
||||
List.of(Map.of("foo", "bar"), Map.of("foo", "n1")),
|
||||
List.of(Map.of("baz", "qux"), Map.of("foo", "n1")),
|
||||
List.of(Map.of("foo", "n3"), Map.of("foo", "n2"), Map.of("foo", "n1"))
|
||||
);
|
||||
assertThat(visitor.visitCount()).isEqualTo(7);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user