17
17
package org .springframework .web .service .invoker ;
18
18
19
19
import java .util .List ;
20
- import java .util .Map ;
21
- import java .util .Optional ;
22
20
23
- import org .apache .groovy .util .Maps ;
24
21
import org .junit .jupiter .api .Test ;
25
22
26
- import org .springframework .lang .Nullable ;
27
23
import org .springframework .util .ObjectUtils ;
28
24
import org .springframework .web .bind .annotation .CookieValue ;
29
25
import org .springframework .web .service .annotation .GetExchange ;
30
26
31
27
import static org .assertj .core .api .Assertions .assertThat ;
32
- import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
33
28
34
29
35
30
/**
36
31
* Unit tests for {@link RequestHeaderArgumentResolver}.
32
+ * <p>For base class functionality, see {@link NamedValueArgumentResolverTests}.
37
33
*
38
34
* @author Rossen Stoyanchev
39
35
*/
40
36
class CookieValueArgumentResolverTests {
41
37
42
- private final TestHttpClientAdapter clientAdapter = new TestHttpClientAdapter ();
38
+ private final TestHttpClientAdapter client = new TestHttpClientAdapter ();
43
39
44
- private final Service service = this .clientAdapter . createService (Service .class );
40
+ private final Service service = HttpServiceProxyFactory . builder ( this .client ). build (). createClient (Service .class );
45
41
46
42
47
- @ Test
48
- void stringCookie () {
49
- this .service .executeString ("test" );
50
- assertCookie ("cookie" , "test" );
51
- }
52
-
53
- @ Test
54
- void objectCookie () {
55
- this .service .execute (Boolean .TRUE );
56
- assertCookie ("cookie" , "true" );
57
- }
58
-
59
- @ Test
60
- void listCookie () {
61
- this .service .executeList (List .of ("test1" , Boolean .TRUE , "test3" ));
62
- assertCookie ("multiValueCookie" , "test1" , "true" , "test3" );
63
- }
64
-
65
- @ Test
66
- void arrayCookie () {
67
- this .service .executeArray ("test1" , Boolean .FALSE , "test3" );
68
- assertCookie ("multiValueCookie" , "test1" , "false" , "test3" );
69
- }
70
-
71
- @ Test
72
- void namedCookie () {
73
- this .service .executeNamed ("test" );
74
- assertCookie ("cookieRenamed" , "test" );
75
- }
76
-
77
- @ SuppressWarnings ("ConstantConditions" )
78
- @ Test
79
- void nullCookieRequired () {
80
- assertThatIllegalArgumentException ().isThrownBy (() -> this .service .executeString (null ));
81
- }
82
-
83
- @ Test
84
- void nullCookieNotRequired () {
85
- this .service .executeNotRequired (null );
86
- assertCookie ("cookie" );
87
- }
88
-
89
- @ Test
90
- void nullCookieWithDefaultValue () {
91
- this .service .executeWithDefaultValue (null );
92
- assertCookie ("cookie" , "default" );
93
- }
43
+ // Base class functionality should be tested in NamedValueArgumentResolverTests.
94
44
95
45
@ Test
96
- void optionalStringCookie () {
97
- this .service .executeOptional (Optional .of ("test" ));
98
- assertCookie ("cookie" , "test" );
99
- }
100
-
101
- @ Test
102
- void optionalObjectCookie () {
103
- this .service .executeOptional (Optional .of (Boolean .TRUE ));
104
- assertCookie ("cookie" , "true" );
105
- }
106
-
107
- @ Test
108
- void optionalEmpty () {
109
- this .service .executeOptional (Optional .empty ());
110
- assertCookie ("cookie" );
111
- }
112
-
113
- @ Test
114
- void optionalEmpthyWithDefaultValue () {
115
- this .service .executeOptionalWithDefaultValue (Optional .empty ());
116
- assertCookie ("cookie" , "default" );
117
- }
118
-
119
- @ Test
120
- void mapOfCookies () {
121
- this .service .executeMap (Maps .of ("cookie1" , "true" , "cookie2" , "false" ));
122
- assertCookie ("cookie1" , "true" );
123
- assertCookie ("cookie2" , "false" );
124
- }
125
-
126
- @ Test
127
- void mapOfCookiesIsNull () {
128
- assertThatIllegalArgumentException ().isThrownBy (() -> this .service .executeMap (null ));
129
- }
130
-
131
- @ Test
132
- void mapOfCookiesHasOptionalValue () {
133
- this .service .executeMapWithOptionalValue (Map .of ("cookie" , Optional .of ("test" )));
46
+ void cookieValue () {
47
+ this .service .execute ("test" );
134
48
assertCookie ("cookie" , "test" );
135
49
}
136
50
137
51
private void assertCookie (String key , String ... values ) {
138
- List <String > actualValues = this .clientAdapter .getRequestValues ().getCookies ().get (key );
52
+ List <String > actualValues = this .client .getRequestValues ().getCookies ().get (key );
139
53
if (ObjectUtils .isEmpty (values )) {
140
54
assertThat (actualValues ).isNull ();
141
55
}
@@ -145,41 +59,10 @@ private void assertCookie(String key, String... values) {
145
59
}
146
60
147
61
148
- @ SuppressWarnings ("OptionalUsedAsFieldOrParameterType" )
149
62
private interface Service {
150
63
151
64
@ GetExchange
152
- void executeString (@ CookieValue String cookie );
153
-
154
- @ GetExchange
155
- void execute (@ CookieValue Object cookie );
156
-
157
- @ GetExchange
158
- void executeList (@ CookieValue List <Object > multiValueCookie );
159
-
160
- @ GetExchange
161
- void executeArray (@ CookieValue Object ... multiValueCookie );
162
-
163
- @ GetExchange
164
- void executeNamed (@ CookieValue (name = "cookieRenamed" ) String cookie );
165
-
166
- @ GetExchange
167
- void executeNotRequired (@ Nullable @ CookieValue (required = false ) String cookie );
168
-
169
- @ GetExchange
170
- void executeWithDefaultValue (@ Nullable @ CookieValue (defaultValue = "default" ) String cookie );
171
-
172
- @ GetExchange
173
- void executeOptional (@ CookieValue Optional <Object > cookie );
174
-
175
- @ GetExchange
176
- void executeOptionalWithDefaultValue (@ CookieValue (defaultValue = "default" ) Optional <Object > cookie );
177
-
178
- @ GetExchange
179
- void executeMap (@ Nullable @ CookieValue Map <String , String > cookie );
180
-
181
- @ GetExchange
182
- void executeMapWithOptionalValue (@ CookieValue Map <String , Optional <String >> cookies );
65
+ void execute (@ CookieValue String cookie );
183
66
184
67
}
185
68
0 commit comments