Skip to content

Commit 5b1bda5

Browse files
christophejanpoutsma
authored andcommitted
Add test case on nested RouterFunction attributes
1 parent 5715432 commit 5b1bda5

File tree

6 files changed

+139
-28
lines changed

6 files changed

+139
-28
lines changed

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/AttributesTestVisitor.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,43 +16,60 @@
1616

1717
package org.springframework.web.reactive.function.server;
1818

19+
import java.util.Deque;
20+
import java.util.LinkedList;
21+
import java.util.List;
1922
import java.util.Map;
23+
import java.util.Objects;
24+
import java.util.Optional;
2025
import java.util.function.Function;
26+
import java.util.stream.Collectors;
27+
import java.util.stream.Stream;
2128

2229
import reactor.core.publisher.Mono;
2330

2431
import org.springframework.core.io.Resource;
2532
import org.springframework.lang.Nullable;
2633

27-
import static org.assertj.core.api.Assertions.assertThat;
28-
import static org.assertj.core.api.Assertions.entry;
29-
3034
/**
3135
* @author Arjen Poutsma
3236
*/
3337
class AttributesTestVisitor implements RouterFunctions.Visitor {
3438

39+
private Deque<Map<String, Object>> nestedAttributes = new LinkedList<>();
40+
3541
@Nullable
3642
private Map<String, Object> attributes;
3743

44+
private List<List<Map<String, Object>>> routerFunctionsAttributes = new LinkedList<>();
45+
3846
private int visitCount;
3947

48+
public List<List<Map<String, Object>>> routerFunctionsAttributes() {
49+
return this.routerFunctionsAttributes;
50+
}
51+
4052
public int visitCount() {
4153
return this.visitCount;
4254
}
4355

4456
@Override
4557
public void startNested(RequestPredicate predicate) {
58+
nestedAttributes.addFirst(attributes);
59+
attributes = null;
4660
}
4761

4862
@Override
4963
public void endNested(RequestPredicate predicate) {
64+
attributes = nestedAttributes.removeFirst();
5065
}
5166

5267
@Override
5368
public void route(RequestPredicate predicate, HandlerFunction<?> handlerFunction) {
54-
assertThat(this.attributes).isNotNull();
55-
this.attributes = null;
69+
Stream<Map<String, Object>> current = Optional.ofNullable(attributes).stream();
70+
Stream<Map<String, Object>> nested = nestedAttributes.stream().filter(Objects::nonNull);
71+
routerFunctionsAttributes.add(Stream.concat(current, nested).collect(Collectors.toUnmodifiableList()));
72+
attributes = null;
5673
}
5774

5875
@Override
@@ -61,7 +78,6 @@ public void resources(Function<ServerRequest, Mono<Resource>> lookupFunction) {
6178

6279
@Override
6380
public void attributes(Map<String, Object> attributes) {
64-
assertThat(attributes).containsExactly(entry("foo", "bar"), entry("baz", "qux"));
6581
this.attributes = attributes;
6682
this.visitCount++;
6783
}

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionBuilderTests.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
1818

1919
import java.io.IOException;
2020
import java.util.Collections;
21+
import java.util.List;
22+
import java.util.Map;
2123
import java.util.concurrent.atomic.AtomicInteger;
2224

2325
import org.junit.jupiter.api.Test;
@@ -236,12 +238,28 @@ public void attributes() {
236238
atts.put("foo", "bar");
237239
atts.put("baz", "qux");
238240
})
241+
.path("/atts", b1 -> b1
242+
.GET("/3", request -> ServerResponse.ok().build())
243+
.withAttribute("foo", "bar")
244+
.GET("/4", request -> ServerResponse.ok().build())
245+
.withAttribute("baz", "qux")
246+
.path("/5", b2 -> b2
247+
.GET(request -> ServerResponse.ok().build())
248+
.withAttribute("foo", "n3"))
249+
.withAttribute("foo", "n2")
250+
)
251+
.withAttribute("foo", "n1")
239252
.build();
240253

241254
AttributesTestVisitor visitor = new AttributesTestVisitor();
242255
route.accept(visitor);
243-
assertThat(visitor.visitCount()).isEqualTo(2);
256+
assertThat(visitor.routerFunctionsAttributes()).containsExactly(
257+
List.of(Map.of("foo", "bar", "baz", "qux")),
258+
List.of(Map.of("foo", "bar", "baz", "qux")),
259+
List.of(Map.of("foo", "bar"), Map.of("foo", "n1")),
260+
List.of(Map.of("baz", "qux"), Map.of("foo", "n1")),
261+
List.of(Map.of("foo", "n3"), Map.of("foo", "n2"), Map.of("foo", "n1"))
262+
);
263+
assertThat(visitor.visitCount()).isEqualTo(7);
244264
}
245-
246-
247265
}

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionTests.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
1717
package org.springframework.web.reactive.function.server;
1818

1919
import java.util.Collections;
20+
import java.util.List;
21+
import java.util.Map;
2022

2123
import org.junit.jupiter.api.Test;
2224
import reactor.core.publisher.Mono;
@@ -26,7 +28,10 @@
2628
import org.springframework.web.testfixture.server.MockServerWebExchange;
2729

2830
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.springframework.http.HttpMethod.GET;
2932
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
33+
import static org.springframework.web.reactive.function.server.RequestPredicates.method;
34+
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
3035

3136
/**
3237
* @author Arjen Poutsma
@@ -137,11 +142,28 @@ public void attributes() {
137142
.withAttributes(atts -> {
138143
atts.put("foo", "bar");
139144
atts.put("baz", "qux");
140-
}));
145+
}))
146+
.and(RouterFunctions.nest(path("/atts"),
147+
RouterFunctions.route(GET("/3"), request -> ServerResponse.ok().build())
148+
.withAttribute("foo", "bar")
149+
.and(RouterFunctions.route(GET("/4"), request -> ServerResponse.ok().build())
150+
.withAttribute("baz", "qux"))
151+
.and(RouterFunctions.nest(path("/5"),
152+
RouterFunctions.route(method(GET), request -> ServerResponse.ok().build())
153+
.withAttribute("foo", "n3"))
154+
.withAttribute("foo", "n2")))
155+
.withAttribute("foo", "n1"));
141156

142157
AttributesTestVisitor visitor = new AttributesTestVisitor();
143158
route.accept(visitor);
144-
assertThat(visitor.visitCount()).isEqualTo(2);
159+
assertThat(visitor.routerFunctionsAttributes()).containsExactly(
160+
List.of(Map.of("foo", "bar", "baz", "qux")),
161+
List.of(Map.of("foo", "bar", "baz", "qux")),
162+
List.of(Map.of("foo", "bar"), Map.of("foo", "n1")),
163+
List.of(Map.of("baz", "qux"), Map.of("foo", "n1")),
164+
List.of(Map.of("foo", "n3"), Map.of("foo", "n2"), Map.of("foo", "n1"))
165+
);
166+
assertThat(visitor.visitCount()).isEqualTo(7);
145167
}
146168

147169

spring-webmvc/src/test/java/org/springframework/web/servlet/function/AttributesTestVisitor.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,42 +16,58 @@
1616

1717
package org.springframework.web.servlet.function;
1818

19+
import java.util.Deque;
20+
import java.util.LinkedList;
21+
import java.util.List;
1922
import java.util.Map;
23+
import java.util.Objects;
2024
import java.util.Optional;
2125
import java.util.function.Function;
26+
import java.util.stream.Collectors;
27+
import java.util.stream.Stream;
2228

2329
import org.springframework.core.io.Resource;
2430
import org.springframework.lang.Nullable;
2531

26-
import static org.assertj.core.api.Assertions.assertThat;
27-
import static org.assertj.core.api.Assertions.entry;
28-
2932
/**
3033
* @author Arjen Poutsma
3134
*/
3235
class AttributesTestVisitor implements RouterFunctions.Visitor {
3336

37+
private Deque<Map<String, Object>> nestedAttributes = new LinkedList<>();
38+
3439
@Nullable
3540
private Map<String, Object> attributes;
3641

42+
private List<List<Map<String, Object>>> routerFunctionsAttributes = new LinkedList<>();
43+
3744
private int visitCount;
3845

46+
public List<List<Map<String, Object>>> routerFunctionsAttributes() {
47+
return this.routerFunctionsAttributes;
48+
}
49+
3950
public int visitCount() {
4051
return this.visitCount;
4152
}
4253

4354
@Override
4455
public void startNested(RequestPredicate predicate) {
56+
nestedAttributes.addFirst(attributes);
57+
attributes = null;
4558
}
4659

4760
@Override
4861
public void endNested(RequestPredicate predicate) {
62+
attributes = nestedAttributes.removeFirst();
4963
}
5064

5165
@Override
5266
public void route(RequestPredicate predicate, HandlerFunction<?> handlerFunction) {
53-
assertThat(this.attributes).isNotNull();
54-
this.attributes = null;
67+
Stream<Map<String, Object>> current = Optional.ofNullable(attributes).stream();
68+
Stream<Map<String, Object>> nested = nestedAttributes.stream().filter(Objects::nonNull);
69+
routerFunctionsAttributes.add(Stream.concat(current, nested).collect(Collectors.toUnmodifiableList()));
70+
attributes = null;
5571
}
5672

5773
@Override
@@ -60,7 +76,6 @@ public void resources(Function<ServerRequest, Optional<Resource>> lookupFunction
6076

6177
@Override
6278
public void attributes(Map<String, Object> attributes) {
63-
assertThat(attributes).containsExactly(entry("foo", "bar"), entry("baz", "qux"));
6479
this.attributes = attributes;
6580
this.visitCount++;
6681
}

spring-webmvc/src/test/java/org/springframework/web/servlet/function/RouterFunctionBuilderTests.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
1717
package org.springframework.web.servlet.function;
1818

1919
import java.io.IOException;
20+
import java.util.List;
21+
import java.util.Map;
2022
import java.util.Optional;
2123
import java.util.concurrent.atomic.AtomicInteger;
2224
import java.util.function.Consumer;
@@ -226,12 +228,28 @@ public void attributes() {
226228
atts.put("foo", "bar");
227229
atts.put("baz", "qux");
228230
})
231+
.path("/atts", b1 -> b1
232+
.GET("/3", request -> ServerResponse.ok().build())
233+
.withAttribute("foo", "bar")
234+
.GET("/4", request -> ServerResponse.ok().build())
235+
.withAttribute("baz", "qux")
236+
.path("/5", b2 -> b2
237+
.GET(request -> ServerResponse.ok().build())
238+
.withAttribute("foo", "n3"))
239+
.withAttribute("foo", "n2")
240+
)
241+
.withAttribute("foo", "n1")
229242
.build();
230243

231244
AttributesTestVisitor visitor = new AttributesTestVisitor();
232245
route.accept(visitor);
233-
assertThat(visitor.visitCount()).isEqualTo(2);
246+
assertThat(visitor.routerFunctionsAttributes()).containsExactly(
247+
List.of(Map.of("foo", "bar", "baz", "qux")),
248+
List.of(Map.of("foo", "bar", "baz", "qux")),
249+
List.of(Map.of("foo", "bar"), Map.of("foo", "n1")),
250+
List.of(Map.of("baz", "qux"), Map.of("foo", "n1")),
251+
List.of(Map.of("foo", "n3"), Map.of("foo", "n2"), Map.of("foo", "n1"))
252+
);
253+
assertThat(visitor.visitCount()).isEqualTo(7);
234254
}
235-
236-
237255
}

spring-webmvc/src/test/java/org/springframework/web/servlet/function/RouterFunctionTests.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,14 +17,19 @@
1717
package org.springframework.web.servlet.function;
1818

1919
import java.util.Collections;
20+
import java.util.List;
21+
import java.util.Map;
2022
import java.util.Optional;
2123

2224
import org.junit.jupiter.api.Test;
2325

2426
import org.springframework.web.servlet.handler.PathPatternsTestUtils;
2527

2628
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.springframework.http.HttpMethod.GET;
2730
import static org.springframework.web.servlet.function.RequestPredicates.GET;
31+
import static org.springframework.web.servlet.function.RequestPredicates.method;
32+
import static org.springframework.web.servlet.function.RequestPredicates.path;
2833

2934
/**
3035
* @author Arjen Poutsma
@@ -120,11 +125,28 @@ public void attributes() {
120125
.withAttributes(atts -> {
121126
atts.put("foo", "bar");
122127
atts.put("baz", "qux");
123-
}));
128+
}))
129+
.and(RouterFunctions.nest(path("/atts"),
130+
RouterFunctions.route(GET("/3"), request -> ServerResponse.ok().build())
131+
.withAttribute("foo", "bar")
132+
.and(RouterFunctions.route(GET("/4"), request -> ServerResponse.ok().build())
133+
.withAttribute("baz", "qux"))
134+
.and(RouterFunctions.nest(path("/5"),
135+
RouterFunctions.route(method(GET), request -> ServerResponse.ok().build())
136+
.withAttribute("foo", "n3"))
137+
.withAttribute("foo", "n2")))
138+
.withAttribute("foo", "n1"));
124139

125140
AttributesTestVisitor visitor = new AttributesTestVisitor();
126141
route.accept(visitor);
127-
assertThat(visitor.visitCount()).isEqualTo(2);
142+
assertThat(visitor.routerFunctionsAttributes()).containsExactly(
143+
List.of(Map.of("foo", "bar", "baz", "qux")),
144+
List.of(Map.of("foo", "bar", "baz", "qux")),
145+
List.of(Map.of("foo", "bar"), Map.of("foo", "n1")),
146+
List.of(Map.of("baz", "qux"), Map.of("foo", "n1")),
147+
List.of(Map.of("foo", "n3"), Map.of("foo", "n2"), Map.of("foo", "n1"))
148+
);
149+
assertThat(visitor.visitCount()).isEqualTo(7);
128150
}
129151

130152

0 commit comments

Comments
 (0)