@@ -77,7 +77,22 @@ void test_unknownContext_fail() {
77
77
String .format ("[ERROR: No state for context %s, property stateValueOne found]" , context ),
78
78
String .format ("[ERROR: No state for context %s, property stateValueTwo found]" , context ),
79
79
String .format ("[ERROR: No state for context %s, property listSize found]" , context )
80
- );
80
+ );
81
+ }
82
+
83
+ @ Test
84
+ void test_unknownContext_useDefault () {
85
+ createPostStub ();
86
+ createGetStub ();
87
+
88
+ String context = RandomStringUtils .randomAlphabetic (5 );
89
+ getAndAssertContextValue (
90
+ "state/default" ,
91
+ context ,
92
+ "defaultStateValueOne" ,
93
+ "defaultStateValueTwo" ,
94
+ "defaultListSize"
95
+ );
81
96
}
82
97
83
98
@ Test
@@ -94,7 +109,8 @@ void test_unknownProperty_fail() throws JsonProcessingException {
94
109
mapper .readTree (
95
110
mapper .writeValueAsString (Map .of (
96
111
"valueOne" , "{{state context=request.pathSegments.[1] property='unknownValue'}}" ,
97
- "valueTwo" , "{{state context=request.pathSegments.[1] property='unknownValue'}}"
112
+ "valueTwo" , "{{state context=request.pathSegments.[1] property='unknownValue'}}" ,
113
+ "unknown" , "{{state context=request.pathSegments.[1] property='unknown' default='defaultUnknown'}}"
98
114
)))
99
115
)
100
116
)
@@ -110,6 +126,109 @@ void test_unknownProperty_fail() throws JsonProcessingException {
110
126
);
111
127
}
112
128
129
+ @ Nested
130
+ public class Property {
131
+
132
+ @ BeforeEach
133
+ void setup () {
134
+ createPostStub ();
135
+ createGetStub ();
136
+ }
137
+
138
+ @ Test
139
+ void test_returnsStateFromPreviousRequest_ok () {
140
+ var contextValue = RandomStringUtils .randomAlphabetic (5 );
141
+
142
+ postAndAssertContextValue ("state" , contextValue , "one" );
143
+ getAndAssertContextValue ("state" , contextValue , contextValue , "one" , "0" );
144
+ }
145
+
146
+ @ Test
147
+ void test_defaults_returnsStateFromPreviousRequest_ok () {
148
+ var contextValue = RandomStringUtils .randomAlphabetic (5 );
149
+
150
+ postAndAssertContextValue ("state" , contextValue , "one" );
151
+ getAndAssertContextValue ("state/default" , contextValue , contextValue , "one" , "0" );
152
+ }
153
+
154
+ @ Test
155
+ void test_returnsFullBodyFromPreviousRequest_ok () {
156
+ var contextValue = RandomStringUtils .randomAlphabetic (5 );
157
+
158
+ postAndAssertContextValue ("state" , contextValue , "one" );
159
+ getAndAssertFullBody (contextValue );
160
+ }
161
+
162
+ @ Test
163
+ void test_differentStatesSupported_ok () {
164
+ var contextValueOne = RandomStringUtils .randomAlphabetic (5 );
165
+ var contextValueTwo = RandomStringUtils .randomAlphabetic (5 );
166
+
167
+ postAndAssertContextValue ("state" , contextValueOne , "one" );
168
+ postAndAssertContextValue ("state" , contextValueTwo , "one" );
169
+ getAndAssertContextValue ("state" , contextValueOne , contextValueOne , "one" , "0" );
170
+ getAndAssertContextValue ("state" , contextValueTwo , contextValueTwo , "one" , "0" );
171
+ }
172
+
173
+
174
+ }
175
+ @ Nested
176
+ public class List {
177
+ @ BeforeEach
178
+ void setup () {
179
+ createPostStub ();
180
+ createGetStub ();
181
+ }
182
+
183
+ @ Test
184
+ void test_returnsListElement_oneItem_ok () {
185
+ var contextValue = RandomStringUtils .randomAlphabetic (5 );
186
+
187
+ postAndAssertContextValue ("list" , contextValue , "one" );
188
+
189
+ getAndAssertContextValue ("list/0" , contextValue , contextValue , "one" , "1" );
190
+ }
191
+
192
+ @ Test
193
+ void test_defaults_knownItem_ok () {
194
+ var contextValue = RandomStringUtils .randomAlphabetic (5 );
195
+
196
+ postAndAssertContextValue ("list" , contextValue , "one" );
197
+
198
+ getAndAssertContextValue ("list/default/0" , contextValue , contextValue , "one" , "1" );
199
+ }
200
+
201
+ @ Test
202
+ void test_defaults_unknownItem_ok () {
203
+ var contextValue = RandomStringUtils .randomAlphabetic (5 );
204
+
205
+ postAndAssertContextValue ("list" , contextValue , "one" );
206
+
207
+ getAndAssertContextValue ("list/default/1" , contextValue , "defaultStateValueOne" , "defaultStateValueTwo" , "1" );
208
+ }
209
+
210
+ @ Test
211
+ void test_returnsListElement_multipleItems_ok () {
212
+ var contextValue = RandomStringUtils .randomAlphabetic (5 );
213
+
214
+ postAndAssertContextValue ("list" , contextValue , "one" );
215
+ postAndAssertContextValue ("list" , contextValue , "two" );
216
+ postAndAssertContextValue ("list" , contextValue , "three" );
217
+
218
+ getAndAssertContextValue ("list/1" , contextValue , contextValue , "two" , "3" );
219
+ }
220
+ @ Test
221
+ void test_returnsSingleListElement_lastItem_ok () {
222
+ var contextValue = RandomStringUtils .randomAlphabetic (5 );
223
+
224
+ postAndAssertContextValue ("list" , contextValue , "one" );
225
+ postAndAssertContextValue ("list" , contextValue , "two" );
226
+ postAndAssertContextValue ("list" , contextValue , "three" );
227
+
228
+ getAndAssertContextValue ("list/-1" , contextValue , contextValue , "three" , "3" );
229
+ }
230
+
231
+ }
113
232
private void createGetStub () {
114
233
wm .stubFor (
115
234
get (urlPathMatching ("/state/[^/]+" ))
@@ -122,7 +241,27 @@ private void createGetStub() {
122
241
Map .of (
123
242
"valueOne" , "{{state context=request.pathSegments.[1] property='stateValueOne'}}" ,
124
243
"valueTwo" , "{{state context=request.pathSegments.[1] property='stateValueTwo'}}" ,
125
- "listSize" , "{{state context=request.pathSegments.[1] property='listSize'}}"
244
+ "listSize" , "{{state context=request.pathSegments.[1] property='listSize'}}" ,
245
+ "unknown" , "{{state context=request.pathSegments.[1] property='unknown' default='defaultUnknown'}}"
246
+ )
247
+ )
248
+ )
249
+ )
250
+ )
251
+ );
252
+ wm .stubFor (
253
+ get (urlPathMatching ("/state/default/[^/]+" ))
254
+ .willReturn (
255
+ WireMock .ok ()
256
+ .withHeader ("content-type" , "application/json" )
257
+ .withJsonBody (
258
+ Json .node (
259
+ Json .write (
260
+ Map .of (
261
+ "valueOne" , "{{state context=request.pathSegments.[2] property='stateValueOne' default='defaultStateValueOne'}}" ,
262
+ "valueTwo" , "{{state context=request.pathSegments.[2] property='stateValueTwo' default='defaultStateValueTwo'}}" ,
263
+ "listSize" , "{{state context=request.pathSegments.[2] property='listSize' default='defaultListSize'}}" ,
264
+ "unknown" , "{{state context=request.pathSegments.[2] property='unknown' default='defaultUnknown'}}"
126
265
)
127
266
)
128
267
)
@@ -148,7 +287,28 @@ private void createGetStub() {
148
287
Map .of (
149
288
"valueOne" , "{{state context=request.pathSegments.[2] list=(join '[' request.pathSegments.[1] '].stateValueOne' '')}}" ,
150
289
"valueTwo" , "{{state context=request.pathSegments.[2] list=(join '[' request.pathSegments.[1] '].stateValueTwo' '')}}" ,
151
- "listSize" , "{{state context=request.pathSegments.[2] property='listSize'}}"
290
+ "listSize" , "{{state context=request.pathSegments.[2] property='listSize'}}" ,
291
+ "unknown" , "{{state context=request.pathSegments.[1] property='unknown' default='defaultUnknown'}}"
292
+ )
293
+ )
294
+ )
295
+ )
296
+ )
297
+ );
298
+
299
+ wm .stubFor (
300
+ get (urlPathMatching ("/list/default/[^/]+/[^/]+" ))
301
+ .willReturn (
302
+ WireMock .ok ()
303
+ .withHeader ("content-type" , "application/json" )
304
+ .withJsonBody (
305
+ Json .node (
306
+ Json .write (
307
+ Map .of (
308
+ "valueOne" , "{{state context=request.pathSegments.[3] list=(join '[' request.pathSegments.[2] '].stateValueOne' '') default='defaultStateValueOne'}}" ,
309
+ "valueTwo" , "{{state context=request.pathSegments.[3] list=(join '[' request.pathSegments.[2] '].stateValueTwo' '') default='defaultStateValueTwo'}}" ,
310
+ "listSize" , "{{state context=request.pathSegments.[3] property='listSize' default='defaultListSize'}}" ,
311
+ "unknown" , "{{state context=request.pathSegments.[3] property='unknown' default='defaultUnknown'}}"
152
312
)
153
313
)
154
314
)
@@ -229,6 +389,7 @@ private void getAndAssertContextValue(String path, String context, String valueO
229
389
.body ("valueOne" , equalTo (valueOne ))
230
390
.body ("valueTwo" , equalTo (valueTwo ))
231
391
.body ("listSize" , equalTo (listSize ))
392
+ .body ("unknown" , equalTo ("defaultUnknown" ))
232
393
.body ("other" , nullValue ());
233
394
}
234
395
@@ -255,84 +416,4 @@ private void postAndAssertContextValue(String path, String contextValueOne, Stri
255
416
.then ()
256
417
.statusCode (HttpStatus .SC_OK );
257
418
}
258
-
259
- @ Nested
260
- public class Property {
261
-
262
- @ BeforeEach
263
- void setup () {
264
- createPostStub ();
265
- createGetStub ();
266
- }
267
-
268
- @ Test
269
- void test_returnsStateFromPreviousRequest_ok () {
270
- var contextValue = RandomStringUtils .randomAlphabetic (5 );
271
-
272
- postAndAssertContextValue ("state" , contextValue , "one" );
273
- getAndAssertContextValue ("state" , contextValue , contextValue , "one" , "0" );
274
- }
275
-
276
- @ Test
277
- void test_returnsFullBodyFromPreviousRequest_ok () {
278
- var contextValue = RandomStringUtils .randomAlphabetic (5 );
279
-
280
- postAndAssertContextValue ("state" , contextValue , "one" );
281
- getAndAssertFullBody (contextValue );
282
- }
283
-
284
- @ Test
285
- void test_differentStatesSupported_ok () {
286
- var contextValueOne = RandomStringUtils .randomAlphabetic (5 );
287
- var contextValueTwo = RandomStringUtils .randomAlphabetic (5 );
288
-
289
- postAndAssertContextValue ("state" , contextValueOne , "one" );
290
- postAndAssertContextValue ("state" , contextValueTwo , "one" );
291
- getAndAssertContextValue ("state" , contextValueOne , contextValueOne , "one" , "0" );
292
- getAndAssertContextValue ("state" , contextValueTwo , contextValueTwo , "one" , "0" );
293
- }
294
- }
295
-
296
- @ Nested
297
- public class List {
298
-
299
- @ BeforeEach
300
- void setup () {
301
- createPostStub ();
302
- createGetStub ();
303
- }
304
-
305
- @ Test
306
- void test_returnsListElement_oneItem_ok () {
307
- var contextValue = RandomStringUtils .randomAlphabetic (5 );
308
-
309
- postAndAssertContextValue ("list" , contextValue , "one" );
310
-
311
- getAndAssertContextValue ("list/0" , contextValue , contextValue , "one" , "1" );
312
- }
313
-
314
- @ Test
315
- void test_returnsListElement_multipleItems_ok () {
316
- var contextValue = RandomStringUtils .randomAlphabetic (5 );
317
-
318
- postAndAssertContextValue ("list" , contextValue , "one" );
319
- postAndAssertContextValue ("list" , contextValue , "two" );
320
- postAndAssertContextValue ("list" , contextValue , "three" );
321
-
322
- getAndAssertContextValue ("list/1" , contextValue , contextValue , "two" , "3" );
323
- }
324
-
325
- @ Test
326
- void test_returnsSingleListElement_lastItem_ok () {
327
- var contextValue = RandomStringUtils .randomAlphabetic (5 );
328
-
329
- postAndAssertContextValue ("list" , contextValue , "one" );
330
- postAndAssertContextValue ("list" , contextValue , "two" );
331
- postAndAssertContextValue ("list" , contextValue , "three" );
332
-
333
- getAndAssertContextValue ("list/-1" , contextValue , contextValue , "three" , "3" );
334
- }
335
-
336
-
337
- }
338
419
}
0 commit comments