17
17
package org .springframework .test .web .client .samples .matchers ;
18
18
19
19
import java .net .URI ;
20
- import java .util . ArrayList ;
20
+ import java .net . URISyntaxException ;
21
21
import java .util .Arrays ;
22
- import java .util .List ;
23
22
24
- import org .junit .Before ;
23
+ import org .junit .After ;
25
24
import org .junit .Test ;
26
25
27
- import org .springframework .http .converter .HttpMessageConverter ;
28
26
import org .springframework .http .converter .json .MappingJackson2HttpMessageConverter ;
29
27
import org .springframework .test .web .Person ;
30
28
import org .springframework .test .web .client .MockRestServiceServer ;
41
39
* <a href="https://github.com/jayway/JsonPath">JsonPath</a> expressions.
42
40
*
43
41
* @author Rossen Stoyanchev
42
+ * @author Sam Brannen
44
43
* @see org.springframework.test.web.client.match.JsonPathRequestMatchers
44
+ * @see org.springframework.test.web.client.match.JsonPathRequestMatchersTests
45
45
*/
46
46
public class JsonPathRequestMatchersIntegrationTests {
47
47
48
- private MockRestServiceServer mockServer ;
48
+ private static final MultiValueMap < String , Person > people = new LinkedMultiValueMap < String , Person >() ;
49
49
50
- private RestTemplate restTemplate ;
51
-
52
- private MultiValueMap <String , Person > people ;
53
-
54
-
55
- @ Before
56
- public void setup () {
57
- this .people = new LinkedMultiValueMap <String , Person >();
58
- this .people .add ("composers" , new Person ("Johann Sebastian Bach" ));
59
- this .people .add ("composers" , new Person ("Johannes Brahms" ));
60
- this .people .add ("composers" , new Person ("Edvard Grieg" ));
61
- this .people .add ("composers" , new Person ("Robert Schumann" ));
62
- this .people .add ("performers" , new Person ("Vladimir Ashkenazy" ));
63
- this .people .add ("performers" , new Person ("Yehudi Menuhin" ));
50
+ static {
51
+ people .add ("composers" , new Person ("Johann Sebastian Bach" ));
52
+ people .add ("composers" , new Person ("Johannes Brahms" ));
53
+ people .add ("composers" , new Person ("Edvard Grieg" ));
54
+ people .add ("composers" , new Person ("Robert Schumann" ));
55
+ people .add ("performers" , new Person ("Vladimir Ashkenazy" ));
56
+ people .add ("performers" , new Person ("Yehudi Menuhin" ));
57
+ }
64
58
65
- List <HttpMessageConverter <?>> converters = new ArrayList <HttpMessageConverter <?>>();
66
- converters .add (new MappingJackson2HttpMessageConverter ());
59
+ private final RestTemplate restTemplate = new RestTemplate (Arrays .asList (new MappingJackson2HttpMessageConverter ()));
67
60
68
- this .restTemplate = new RestTemplate ();
69
- this .restTemplate .setMessageConverters (converters );
61
+ private final MockRestServiceServer mockServer = MockRestServiceServer .createServer (this .restTemplate );
70
62
71
- this .mockServer = MockRestServiceServer .createServer (this .restTemplate );
72
- }
73
63
74
64
@ Test
75
- public void testExists () throws Exception {
65
+ public void exists () throws Exception {
76
66
this .mockServer .expect (requestTo ("/composers" ))
77
67
.andExpect (content ().contentType ("application/json;charset=UTF-8" ))
78
68
.andExpect (jsonPath ("$.composers[0]" ).exists ())
79
69
.andExpect (jsonPath ("$.composers[1]" ).exists ())
80
70
.andExpect (jsonPath ("$.composers[2]" ).exists ())
81
71
.andExpect (jsonPath ("$.composers[3]" ).exists ())
82
72
.andRespond (withSuccess ());
83
-
84
- this .restTemplate .put (new URI ("/composers" ), this .people );
85
- this .mockServer .verify ();
86
73
}
87
74
88
75
@ Test
89
- public void testDoesNotExist () throws Exception {
76
+ public void doesNotExist () throws Exception {
90
77
this .mockServer .expect (requestTo ("/composers" ))
91
78
.andExpect (content ().contentType ("application/json;charset=UTF-8" ))
92
79
.andExpect (jsonPath ("$.composers[?(@.name == 'Edvard Grieeeeeeg')]" ).doesNotExist ())
93
80
.andExpect (jsonPath ("$.composers[?(@.name == 'Robert Schuuuuuuman')]" ).doesNotExist ())
94
81
.andExpect (jsonPath ("$.composers[-1]" ).doesNotExist ())
95
82
.andExpect (jsonPath ("$.composers[4]" ).doesNotExist ())
96
83
.andRespond (withSuccess ());
97
-
98
- this .restTemplate .put (new URI ("/composers" ), this .people );
99
- this .mockServer .verify ();
100
84
}
101
85
102
86
@ Test
103
- public void testEqualTo () throws Exception {
87
+ public void value () throws Exception {
104
88
this .mockServer .expect (requestTo ("/composers" ))
105
89
.andExpect (content ().contentType ("application/json;charset=UTF-8" ))
106
90
.andExpect (jsonPath ("$.composers[0].name" ).value ("Johann Sebastian Bach" ))
107
91
.andExpect (jsonPath ("$.performers[1].name" ).value ("Yehudi Menuhin" ))
108
- .andExpect (jsonPath ("$.composers[0].name" ).value (equalTo ("Johann Sebastian Bach" ))) // Hamcrest
109
- .andExpect (jsonPath ("$.performers[1].name" ).value (equalTo ("Yehudi Menuhin" ))) // Hamcrest
110
92
.andRespond (withSuccess ());
111
-
112
- this .restTemplate .put (new URI ("/composers" ), this .people );
113
- this .mockServer .verify ();
114
93
}
115
94
116
95
@ Test
117
- public void testHamcrestMatcher () throws Exception {
96
+ public void hamcrestMatchers () throws Exception {
118
97
this .mockServer .expect (requestTo ("/composers" ))
119
98
.andExpect (content ().contentType ("application/json;charset=UTF-8" ))
99
+ .andExpect (jsonPath ("$.composers[0].name" ).value (equalTo ("Johann Sebastian Bach" )))
100
+ .andExpect (jsonPath ("$.performers[1].name" ).value (equalTo ("Yehudi Menuhin" )))
120
101
.andExpect (jsonPath ("$.composers[0].name" , startsWith ("Johann" )))
121
102
.andExpect (jsonPath ("$.performers[0].name" , endsWith ("Ashkenazy" )))
122
103
.andExpect (jsonPath ("$.performers[1].name" , containsString ("di Me" )))
123
104
.andExpect (jsonPath ("$.composers[1].name" , isIn (Arrays .asList ("Johann Sebastian Bach" , "Johannes Brahms" ))))
124
105
.andExpect (jsonPath ("$.composers[:3].name" , hasItem ("Johannes Brahms" )))
125
106
.andRespond (withSuccess ());
126
-
127
- this .restTemplate .put (new URI ("/composers" ), this .people );
128
- this .mockServer .verify ();
129
107
}
130
108
131
109
@ Test
132
- public void testHamcrestMatcherWithParameterizedJsonPath () throws Exception {
110
+ public void hamcrestMatchersWithParameterizedJsonPaths () throws Exception {
133
111
String composerName = "$.composers[%s].name" ;
134
112
String performerName = "$.performers[%s].name" ;
135
113
@@ -140,8 +118,43 @@ public void testHamcrestMatcherWithParameterizedJsonPath() throws Exception {
140
118
.andExpect (jsonPath (performerName , 1 ).value (containsString ("di Me" )))
141
119
.andExpect (jsonPath (composerName , 1 ).value (isIn (Arrays .asList ("Johann Sebastian Bach" , "Johannes Brahms" ))))
142
120
.andRespond (withSuccess ());
121
+ }
122
+
123
+ @ Test
124
+ public void isArray () throws Exception {
125
+ this .mockServer .expect (requestTo ("/composers" ))
126
+ .andExpect (content ().contentType ("application/json;charset=UTF-8" ))
127
+ .andExpect (jsonPath ("$.composers" ).isArray ())
128
+ .andRespond (withSuccess ());
129
+ }
130
+
131
+ @ Test
132
+ public void isString () throws Exception {
133
+ this .mockServer .expect (requestTo ("/composers" ))
134
+ .andExpect (content ().contentType ("application/json;charset=UTF-8" ))
135
+ .andExpect (jsonPath ("$.composers[0].name" ).isString ())
136
+ .andRespond (withSuccess ());
137
+ }
138
+
139
+ @ Test
140
+ public void isNumber () throws Exception {
141
+ this .mockServer .expect (requestTo ("/composers" ))
142
+ .andExpect (content ().contentType ("application/json;charset=UTF-8" ))
143
+ .andExpect (jsonPath ("$.composers[0].someDouble" ).isNumber ())
144
+ .andRespond (withSuccess ());
145
+ }
146
+
147
+ @ Test
148
+ public void isBoolean () throws Exception {
149
+ this .mockServer .expect (requestTo ("/composers" ))
150
+ .andExpect (content ().contentType ("application/json;charset=UTF-8" ))
151
+ .andExpect (jsonPath ("$.composers[0].someBoolean" ).isBoolean ())
152
+ .andRespond (withSuccess ());
153
+ }
143
154
144
- this .restTemplate .put (new URI ("/composers" ), this .people );
155
+ @ After
156
+ public void performRequestAndVerify () throws URISyntaxException {
157
+ this .restTemplate .put (new URI ("/composers" ), people );
145
158
this .mockServer .verify ();
146
159
}
147
160
0 commit comments