19
19
import java .time .Duration ;
20
20
import java .time .Instant ;
21
21
import java .time .ZoneId ;
22
- import java .util .ArrayList ;
23
22
import java .util .Arrays ;
24
23
import java .util .Collections ;
25
- import java .util .List ;
26
24
27
25
import org .junit .Before ;
28
26
import org .junit .Test ;
27
+ import org .junit .runner .RunWith ;
28
+ import org .mockito .Mock ;
29
+ import org .mockito .junit .MockitoJUnitRunner ;
29
30
import reactor .core .publisher .Mono ;
30
31
31
32
import org .springframework .http .codec .ServerCodecConfigurer ;
32
- import org .springframework .lang .Nullable ;
33
33
import org .springframework .mock .http .server .reactive .test .MockServerHttpRequest ;
34
34
import org .springframework .mock .http .server .reactive .test .MockServerHttpResponse ;
35
35
import org .springframework .util .IdGenerator ;
43
43
import static org .junit .Assert .assertFalse ;
44
44
import static org .junit .Assert .assertNotNull ;
45
45
import static org .junit .Assert .assertNotSame ;
46
- import static org .junit .Assert .assertNull ;
47
- import static org .junit .Assert .assertSame ;
46
+ import static org .mockito .ArgumentMatchers .any ;
47
+ import static org .mockito .ArgumentMatchers .eq ;
48
+ import static org .mockito .Mockito .never ;
49
+ import static org .mockito .Mockito .verify ;
50
+ import static org .mockito .Mockito .when ;
48
51
49
52
/**
50
53
* Unit tests for {@link DefaultWebSessionManager}.
51
54
* @author Rossen Stoyanchev
55
+ * @author Rob Winch
52
56
*/
57
+ @ RunWith (MockitoJUnitRunner .class )
53
58
public class DefaultWebSessionManagerTests {
54
59
55
60
private static final Clock CLOCK = Clock .system (ZoneId .of ("GMT" ));
@@ -59,16 +64,19 @@ public class DefaultWebSessionManagerTests {
59
64
60
65
private DefaultWebSessionManager manager ;
61
66
62
- private TestWebSessionIdResolver idResolver ;
63
-
64
67
private ServerWebExchange exchange ;
65
68
69
+ @ Mock
70
+ private WebSessionIdResolver idResolver ;
71
+
72
+ @ Mock
73
+ private WebSessionStore store ;
66
74
67
75
@ Before
68
76
public void setUp () throws Exception {
69
77
this .manager = new DefaultWebSessionManager ();
70
- this .idResolver = new TestWebSessionIdResolver ();
71
78
this .manager .setSessionIdResolver (this .idResolver );
79
+ this .manager .setSessionStore (this .store );
72
80
73
81
MockServerHttpRequest request = MockServerHttpRequest .get ("/path" ).build ();
74
82
MockServerHttpResponse response = new MockServerHttpResponse ();
@@ -79,45 +87,60 @@ public void setUp() throws Exception {
79
87
80
88
@ Test
81
89
public void getSessionWithoutStarting () throws Exception {
82
- this .idResolver .setIdsToResolve (Collections .emptyList ());
90
+ when ( this .idResolver .resolveSessionIds ( this . exchange )). thenReturn (Collections .emptyList ());
83
91
WebSession session = this .manager .getSession (this .exchange ).block ();
84
- session .save ();
92
+ session .save (). block () ;
85
93
86
94
assertFalse (session .isStarted ());
87
95
assertFalse (session .isExpired ());
88
- assertNull (this .idResolver . getSavedId ());
89
- assertNull (this .manager . getSessionStore (). retrieveSession ( session . getId ()). block ());
96
+ verify (this .store , never ()). storeSession ( any ());
97
+ verify (this .idResolver , never ()). setSessionId ( any (), any ());
90
98
}
91
99
92
100
@ Test
93
101
public void startSessionExplicitly () throws Exception {
94
- this .idResolver .setIdsToResolve (Collections .emptyList ());
102
+ when (this .idResolver .resolveSessionIds (this .exchange )).thenReturn (Collections .emptyList ());
103
+ when (this .store .storeSession (any ())).thenReturn (Mono .empty ());
95
104
WebSession session = this .manager .getSession (this .exchange ).block ();
96
105
session .start ();
97
- session .save ();
106
+ session .save (). block () ;
98
107
99
108
String id = session .getId ();
100
- assertNotNull (this .idResolver .getSavedId ());
101
- assertEquals (id , this .idResolver .getSavedId ());
102
- assertSame (session , this .manager .getSessionStore ().retrieveSession (id ).block ());
109
+ verify (this .store ).storeSession (any ());
110
+ verify (this .idResolver ).setSessionId (any (), eq (id ));
103
111
}
104
112
105
113
@ Test
106
114
public void startSessionImplicitly () throws Exception {
107
- this .idResolver .setIdsToResolve (Collections .emptyList ());
115
+ when (this .idResolver .resolveSessionIds (this .exchange )).thenReturn (Collections .emptyList ());
116
+ when (this .store .storeSession (any ())).thenReturn (Mono .empty ());
108
117
WebSession session = this .manager .getSession (this .exchange ).block ();
109
118
session .getAttributes ().put ("foo" , "bar" );
110
- session .save ();
119
+ session .save (). block () ;
111
120
112
- assertNotNull (this .idResolver .getSavedId ());
121
+ verify (this .idResolver ).setSessionId (any (), any ());
122
+ verify (this .store ).storeSession (any ());
123
+ }
124
+
125
+ @ Test
126
+ public void exchangeWhenResponseSetCompleteThenSavesAndSetsId () throws Exception {
127
+ when (this .idResolver .resolveSessionIds (this .exchange )).thenReturn (Collections .emptyList ());
128
+ when (this .store .storeSession (any ())).thenReturn (Mono .empty ());
129
+ WebSession session = this .manager .getSession (this .exchange ).block ();
130
+ String id = session .getId ();
131
+ session .getAttributes ().put ("foo" , "bar" );
132
+ this .exchange .getResponse ().setComplete ().block ();
133
+
134
+ verify (this .idResolver ).setSessionId (any (), eq (id ));
135
+ verify (this .store ).storeSession (any ());
113
136
}
114
137
115
138
@ Test
116
139
public void existingSession () throws Exception {
117
140
DefaultWebSession existing = createDefaultWebSession ();
118
141
String id = existing .getId ();
119
- this .manager . getSessionStore (). storeSession ( existing );
120
- this .idResolver .setIdsToResolve (Collections .singletonList (id ));
142
+ when ( this .store . retrieveSession ( id )). thenReturn ( Mono . just ( existing ) );
143
+ when ( this .idResolver .resolveSessionIds ( this . exchange )). thenReturn (Collections .singletonList (id ));
121
144
122
145
WebSession actual = this .manager .getSession (this .exchange ).block ();
123
146
assertNotNull (actual );
@@ -130,19 +153,23 @@ public void existingSessionIsExpired() throws Exception {
130
153
existing .start ();
131
154
Instant lastAccessTime = Instant .now (CLOCK ).minus (Duration .ofMinutes (31 ));
132
155
existing = new DefaultWebSession (existing , lastAccessTime , s -> Mono .empty ());
133
- this .manager .getSessionStore ().storeSession (existing );
134
- this .idResolver .setIdsToResolve (Collections .singletonList ("1" ));
156
+ when (this .store .retrieveSession (existing .getId ())).thenReturn (Mono .just (existing ));
157
+ when (this .store .removeSession (existing .getId ())).thenReturn (Mono .empty ());
158
+ when (this .idResolver .resolveSessionIds (this .exchange )).thenReturn (Collections .singletonList (existing .getId ()));
135
159
136
160
WebSession actual = this .manager .getSession (this .exchange ).block ();
137
161
assertNotSame (existing , actual );
162
+ verify (this .store ).removeSession (existing .getId ());
163
+ verify (this .idResolver ).expireSession (any ());
138
164
}
139
165
140
166
@ Test
141
167
public void multipleSessionIds () throws Exception {
142
168
DefaultWebSession existing = createDefaultWebSession ();
143
169
String id = existing .getId ();
144
- this .manager .getSessionStore ().storeSession (existing );
145
- this .idResolver .setIdsToResolve (Arrays .asList ("neither-this" , "nor-that" , id ));
170
+ when (this .store .retrieveSession (any ())).thenReturn (Mono .empty ());
171
+ when (this .store .retrieveSession (id )).thenReturn (Mono .just (existing ));
172
+ when (this .idResolver .resolveSessionIds (this .exchange )).thenReturn (Arrays .asList ("neither-this" , "nor-that" , id ));
146
173
147
174
WebSession actual = this .manager .getSession (this .exchange ).block ();
148
175
assertNotNull (actual );
@@ -152,39 +179,4 @@ public void multipleSessionIds() throws Exception {
152
179
private DefaultWebSession createDefaultWebSession () {
153
180
return new DefaultWebSession (idGenerator , CLOCK , (s , session ) -> Mono .empty (), s -> Mono .empty ());
154
181
}
155
-
156
-
157
- private static class TestWebSessionIdResolver implements WebSessionIdResolver {
158
-
159
- private List <String > idsToResolve = new ArrayList <>();
160
-
161
- @ Nullable
162
- private String id = null ;
163
-
164
-
165
- public void setIdsToResolve (List <String > idsToResolve ) {
166
- this .idsToResolve = idsToResolve ;
167
- }
168
-
169
- @ Nullable
170
- public String getSavedId () {
171
- return this .id ;
172
- }
173
-
174
- @ Override
175
- public List <String > resolveSessionIds (ServerWebExchange exchange ) {
176
- return this .idsToResolve ;
177
- }
178
-
179
- @ Override
180
- public void setSessionId (ServerWebExchange exchange , String sessionId ) {
181
- this .id = sessionId ;
182
- }
183
-
184
- @ Override
185
- public void expireSession (ServerWebExchange exchange ) {
186
- this .id = null ;
187
- }
188
- }
189
-
190
182
}
0 commit comments