-
Notifications
You must be signed in to change notification settings - Fork 38k
/
JsonPathAssertionTests.java
145 lines (120 loc) · 5.55 KB
/
JsonPathAssertionTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.web.servlet.samples.standalone.resultmatchers;
import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.Person;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.in;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
/**
* Examples of defining expectations on JSON response content with
* <a href="https://github.com/jayway/JsonPath">JsonPath</a> expressions.
*
* @author Rossen Stoyanchev
* @see ContentAssertionTests
*/
public class JsonPathAssertionTests {
private MockMvc mockMvc;
@BeforeEach
public void setup() {
this.mockMvc = standaloneSetup(new MusicController())
.defaultRequest(get("/").accept(MediaType.APPLICATION_JSON))
.alwaysExpect(status().isOk())
.alwaysExpect(content().contentType("application/json"))
.build();
}
@Test
public void exists() throws Exception {
String composerByName = "$.composers[?(@.name == '%s')]";
String performerByName = "$.performers[?(@.name == '%s')]";
this.mockMvc.perform(get("/music/people"))
.andExpect(jsonPath(composerByName, "Johann Sebastian Bach").exists())
.andExpect(jsonPath(composerByName, "Johannes Brahms").exists())
.andExpect(jsonPath(composerByName, "Edvard Grieg").exists())
.andExpect(jsonPath(composerByName, "Robert Schumann").exists())
.andExpect(jsonPath(performerByName, "Vladimir Ashkenazy").exists())
.andExpect(jsonPath(performerByName, "Yehudi Menuhin").exists())
.andExpect(jsonPath("$.composers[0]").exists())
.andExpect(jsonPath("$.composers[1]").exists())
.andExpect(jsonPath("$.composers[2]").exists())
.andExpect(jsonPath("$.composers[3]").exists());
}
@Test
public void doesNotExist() throws Exception {
this.mockMvc.perform(get("/music/people"))
.andExpect(jsonPath("$.composers[?(@.name == 'Edvard Grieeeeeeg')]").doesNotExist())
.andExpect(jsonPath("$.composers[?(@.name == 'Robert Schuuuuuuman')]").doesNotExist())
.andExpect(jsonPath("$.composers[4]").doesNotExist());
}
@Test
public void equality() throws Exception {
this.mockMvc.perform(get("/music/people"))
.andExpect(jsonPath("$.composers[0].name").value("Johann Sebastian Bach"))
.andExpect(jsonPath("$.performers[1].name").value("Yehudi Menuhin"));
// Hamcrest matchers...
this.mockMvc.perform(get("/music/people"))
.andExpect(jsonPath("$.composers[0].name").value(equalTo("Johann Sebastian Bach")))
.andExpect(jsonPath("$.performers[1].name").value(equalTo("Yehudi Menuhin")));
}
@Test
public void hamcrestMatcher() throws Exception {
this.mockMvc.perform(get("/music/people"))
.andExpect(jsonPath("$.composers[0].name", startsWith("Johann")))
.andExpect(jsonPath("$.performers[0].name", endsWith("Ashkenazy")))
.andExpect(jsonPath("$.performers[1].name", containsString("di Me")))
.andExpect(jsonPath("$.composers[1].name", is(in(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms")))));
}
@Test
public void hamcrestMatcherWithParameterizedJsonPath() throws Exception {
String composerName = "$.composers[%s].name";
String performerName = "$.performers[%s].name";
this.mockMvc.perform(get("/music/people"))
.andExpect(jsonPath(composerName, 0).value(startsWith("Johann")))
.andExpect(jsonPath(performerName, 0).value(endsWith("Ashkenazy")))
.andExpect(jsonPath(performerName, 1).value(containsString("di Me")))
.andExpect(jsonPath(composerName, 1).value(is(in(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms")))));
}
@RestController
private static class MusicController {
@RequestMapping("/music/people")
public MultiValueMap<String, Person> get() {
MultiValueMap<String, Person> map = new LinkedMultiValueMap<>();
map.add("composers", new Person("Johann Sebastian Bach"));
map.add("composers", new Person("Johannes Brahms"));
map.add("composers", new Person("Edvard Grieg"));
map.add("composers", new Person("Robert Schumann"));
map.add("performers", new Person("Vladimir Ashkenazy"));
map.add("performers", new Person("Yehudi Menuhin"));
return map;
}
}
}