Repository navigation
Expand file tree
/
Copy pathTopicControllerWebTest.java
More file actions
125 lines (108 loc) · 5.9 KB
/
Copy pathTopicControllerWebTest.java
File metadata and controls
125 lines (108 loc) · 5.9 KB
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
package hello.controller;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
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;
/**
* End-to-end coverage of the JSON contract of the topic endpoints.
*
* <p>{@code TopicService} is a singleton holding a mutable list, so anything that writes to it leaks
* into the next test. Rather than depending on test order, the two tests that mutate the list
* ({@code /topic} writes and the in-place sort performed by {@code /topic/sort}) drop the context
* afterwards with {@link DirtiesContext}; every other test can then assume the seeded state.
*
* <p>Assertions go through {@code jsonPath} rather than through model getters on purpose: the JSON
* field names are the actual contract and they survive the ongoing conversion of the models to records.
*/
@SpringBootTest
@AutoConfigureMockMvc
class TopicControllerWebTest {
@Autowired
private MockMvc mockMvc;
@Test
void getAllTopicsReturnsTheSeededTopics() throws Exception {
mockMvc.perform(get("/topic"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[*].id", containsInAnyOrder("spring", "java", "javascript")))
.andExpect(jsonPath("$[?(@.id=='spring')].subjectName", contains("Spring Framework")))
.andExpect(jsonPath("$[?(@.id=='spring')].subjectDescription", contains("Spring Framework Description")));
}
@Test
void aTopicSerialisesWithExactlyIdSubjectNameAndSubjectDescription() throws Exception {
mockMvc.perform(get("/topic/java"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.*", hasSize(3)))
.andExpect(jsonPath("$.id").value("java"))
.andExpect(jsonPath("$.subjectName").value("Core Java"))
.andExpect(jsonPath("$.subjectDescription").value("Java Description"));
}
@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
void supportsTheFullCreateReadUpdateDeleteLifecycle() throws Exception {
mockMvc.perform(post("/topic")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{"id":"kotlin","subjectName":"Kotlin","subjectDescription":"Kotlin Description"}"""))
.andExpect(status().isOk());
mockMvc.perform(get("/topic"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(4)))
.andExpect(jsonPath("$[*].id", containsInAnyOrder("spring", "java", "javascript", "kotlin")));
mockMvc.perform(get("/topic/kotlin"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.subjectName").value("Kotlin"))
.andExpect(jsonPath("$.subjectDescription").value("Kotlin Description"));
mockMvc.perform(put("/topic/kotlin")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{"id":"kotlin","subjectName":"Kotlin Updated","subjectDescription":"Updated Description"}"""))
.andExpect(status().isOk());
mockMvc.perform(get("/topic/kotlin"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.subjectName").value("Kotlin Updated"))
.andExpect(jsonPath("$.subjectDescription").value("Updated Description"));
mockMvc.perform(delete("/topic/kotlin"))
.andExpect(status().isOk());
mockMvc.perform(get("/topic"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[*].id", not(hasItem("kotlin"))));
}
@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
void sortEndpointReturnsTopicsOrderedById() throws Exception {
mockMvc.perform(get("/topic/sort"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[*].id", contains("java", "javascript", "spring")));
}
@Test
void minimumLengthEndpointFiltersOnIdLength() throws Exception {
mockMvc.perform(get("/topic/minimum/length/4"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[*].id", containsInAnyOrder("spring", "javascript")));
mockMvc.perform(get("/topic/minimum/length/6"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[*].id", contains("javascript")));
mockMvc.perform(get("/topic/minimum/length/20"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(0)));
}
}