24
24
import com .github .tomakehurst .wiremock .matching .MatchResult ;
25
25
import com .github .tomakehurst .wiremock .matching .RequestMatcherExtension ;
26
26
import com .github .tomakehurst .wiremock .matching .StringValuePattern ;
27
- import org .wiremock .extensions .state .internal .model .Context ;
28
27
import org .wiremock .extensions .state .internal .ContextManager ;
29
- import org .wiremock .extensions .state .internal .model .ContextTemplateModel ;
30
28
import org .wiremock .extensions .state .internal .StateExtensionMixin ;
29
+ import org .wiremock .extensions .state .internal .model .Context ;
30
+ import org .wiremock .extensions .state .internal .model .ContextTemplateModel ;
31
31
32
32
import java .util .Arrays ;
33
33
import java .util .Collection ;
@@ -67,23 +67,23 @@ private static List<Map.Entry<ContextMatcher, Object>> getMatchers(Parameters pa
67
67
.collect (Collectors .toUnmodifiableList ());
68
68
}
69
69
70
- private static <T > T cast ( Object object ) {
70
+ private static <T > T mapToObject ( Map < String , Object > map , Class < T > klass ) {
71
71
try {
72
- //noinspection unchecked
73
- return (T ) object ;
74
- } catch (ClassCastException ex ) {
75
- var msg = String .format ("Configuration has invalid type: %s" , ex .getMessage ());
72
+ return Json .mapToObject (map , klass );
73
+ } catch (Exception ex ) {
74
+ var msg = String .format ("Cannot create pattern matcher: %s" , ex .getMessage ());
76
75
var prefixed = String .format ("%s: %s" , "StateRequestMatcher" , msg );
77
76
notifier ().error (prefixed );
78
77
throw new ConfigurationException (prefixed );
79
78
}
80
79
}
81
80
82
- private static <T > T mapToObject ( Map < String , Object > map , Class <T > klass ) {
81
+ private static <T > T cast ( Object object , Class <T > target ) {
83
82
try {
84
- return Json .mapToObject (map , klass );
85
- } catch (Exception ex ) {
86
- var msg = String .format ("Cannot create pattern matcher: %s" , ex .getMessage ());
83
+ //noinspection unchecked
84
+ return target .cast (object );
85
+ } catch (ClassCastException ex ) {
86
+ var msg = String .format ("Configuration has invalid type: %s" , ex .getMessage ());
87
87
var prefixed = String .format ("%s: %s" , "StateRequestMatcher" , msg );
88
88
notifier ().error (prefixed );
89
89
throw new ConfigurationException (prefixed );
@@ -144,11 +144,11 @@ String renderTemplate(Object context, String value) {
144
144
145
145
Object renderTemplateRecursively (Object context , Object value ) {
146
146
if (value instanceof Collection ) {
147
- Collection <Object > castedCollection = cast (value );
147
+ Collection <Object > castedCollection = cast (value , Collection . class );
148
148
return castedCollection .stream ().map (it -> renderTemplateRecursively (context , it )).collect (Collectors .toList ());
149
149
} else if (value instanceof Map ) {
150
150
var newMap = new HashMap <String , Object >();
151
- Map <String , Object > castedMap = cast (value );
151
+ Map <String , Object > castedMap = cast (value , Map . class );
152
152
castedMap .forEach ((k , v ) -> newMap .put (
153
153
renderTemplate (context , k ),
154
154
renderTemplateRecursively (context , v )
@@ -162,7 +162,7 @@ Object renderTemplateRecursively(Object context, Object value) {
162
162
private enum ContextMatcher {
163
163
164
164
property ((Context c , Object object ) -> {
165
- Map <String , Map <String , Object >> mapValue = cast (object );
165
+ Map <String , Map <String , Object >> mapValue = cast (object , Map . class );
166
166
var results = mapValue .entrySet ().stream ().map (entry -> {
167
167
var patterns = mapToObject (entry .getValue (), StringValuePattern .class );
168
168
var propertyValue = c .getProperties ().get (entry .getKey ());
@@ -177,7 +177,7 @@ private enum ContextMatcher {
177
177
}),
178
178
179
179
list ((Context c , Object object ) -> {
180
- Map <String , Map <String , Map <String , Object >>> mapValue = cast (object );
180
+ Map <String , Map <String , Map <String , Object >>> mapValue = cast (object , Map . class );
181
181
var allResults = mapValue .entrySet ().stream ().map (listIndexEntry -> {
182
182
Map <String , String > listEntry ;
183
183
switch (listIndexEntry .getKey ()) {
@@ -194,7 +194,7 @@ private enum ContextMatcher {
194
194
if (listEntry == null ) {
195
195
return MatchResult .noMatch ();
196
196
} else {
197
- var results = listIndexEntry .getValue ().entrySet ().stream ().map (entry -> {
197
+ List < MatchResult > results = listIndexEntry .getValue ().entrySet ().stream ().map (entry -> {
198
198
var patterns = mapToObject (entry .getValue (), StringValuePattern .class );
199
199
var propertyValue = listEntry .get (entry .getKey ());
200
200
return patterns .match (propertyValue );
@@ -210,35 +210,35 @@ private enum ContextMatcher {
210
210
return MatchResult .aggregate (allResults );
211
211
}),
212
212
hasProperty ((Context c , Object object ) -> {
213
- String stringValue = cast (object );
213
+ String stringValue = cast (object , String . class );
214
214
return toMatchResult (c .getProperties ().containsKey (stringValue ));
215
215
}),
216
216
hasNotProperty ((Context c , Object object ) -> {
217
- String stringValue = cast (object );
217
+ String stringValue = cast (object , String . class );
218
218
return toMatchResult (!c .getProperties ().containsKey (stringValue ));
219
219
}),
220
220
updateCountEqualTo ((Context c , Object object ) -> {
221
- String stringValue = cast (object );
221
+ String stringValue = cast (object , String . class );
222
222
return toMatchResult (withConvertedNumber (c , stringValue , (context , value ) -> context .getUpdateCount ().equals (value )));
223
223
}),
224
224
updateCountLessThan ((Context c , Object object ) -> {
225
- String stringValue = cast (object );
225
+ String stringValue = cast (object , String . class );
226
226
return toMatchResult (withConvertedNumber (c , stringValue , (context , value ) -> context .getUpdateCount () < value ));
227
227
}),
228
228
updateCountMoreThan ((Context c , Object object ) -> {
229
- String stringValue = cast (object );
229
+ String stringValue = cast (object , String . class );
230
230
return toMatchResult (withConvertedNumber (c , stringValue , (context , value ) -> context .getUpdateCount () > value ));
231
231
}),
232
232
listSizeEqualTo ((Context c , Object object ) -> {
233
- String stringValue = cast (object );
233
+ String stringValue = cast (object , String . class );
234
234
return toMatchResult (withConvertedNumber (c , stringValue , (context , value ) -> context .getList ().size () == value ));
235
235
}),
236
236
listSizeLessThan ((Context c , Object object ) -> {
237
- String stringValue = cast (object );
237
+ String stringValue = cast (object , String . class );
238
238
return toMatchResult (withConvertedNumber (c , stringValue , (context , value ) -> context .getList ().size () < value ));
239
239
}),
240
240
listSizeMoreThan ((Context c , Object object ) -> {
241
- String stringValue = cast (object );
241
+ String stringValue = cast (object , String . class );
242
242
return toMatchResult (withConvertedNumber (c , stringValue , (context , value ) -> context .getList ().size () > value ));
243
243
});
244
244
0 commit comments