-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathChatRestControllerUnitTests.java
187 lines (167 loc) · 7.4 KB
/
ChatRestControllerUnitTests.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package cz.muni.chat.server.rest;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.chat.server.facade.BackgroundColor;
import cz.muni.chat.server.facade.NewChatMessageRequest;
import cz.muni.chat.server.service.ChatService;
import cz.muni.chat.server.service.StoredMessage;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO;
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.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Unit tests.
* <p>
* Unit tests are run by Maven's surefire plugin in the "mvn test" phase,
* the class name must end with "Test" or "Tests".
* <p>
* The unit tests test the RestController as a separated unit, with mock (fake)
* implementation of ChatService which provides exactly the expected data only
* and with mock implementation of Spring MVC that calls the RestController.
*/
@WebMvcTest(ChatRestController.class)
@EnableSpringDataWebSupport(pageSerializationMode = VIA_DTO)
class ChatRestControllerUnitTests {
private static final Logger log = LoggerFactory.getLogger(ChatRestControllerUnitTests.class);
// injected mock implementation of Spring MVC
@Autowired
private MockMvc mockMvc;
// injected mock implementation of the ChatService interface
@MockitoBean
private ChatService mockChatService;
// injected mapper for converting classes into JSON strings
@Autowired
private ObjectMapper objectMapper;
static ZonedDateTime now = ZonedDateTime.of(2023, 1, 5, 10, 0, 0, 0, ZoneId.systemDefault());
static StoredMessage message01 = new StoredMessage("01", now, "hello", null, null, null, null);
static List<StoredMessage> allMessages = List.of(message01);
@BeforeAll
static void beforeAll() {
}
@AfterAll
static void afterAll() {
}
@BeforeEach
void beforeEach() {
}
@AfterEach
void afterEach() {
}
@Test
void getAllMessages() throws Exception {
log.debug("getAllMessages");
// define what mock service returns when called
given(mockChatService.getAllMessages()).willReturn(allMessages);
// call controller and check the result
mockMvc.perform(get("/api/messages").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.[?(@.id=='01')].text").value("hello"))
;
}
@Test
void getExistingMessage() throws Exception {
log.debug("getMessage existing");
// define what mock service returns when called
given(mockChatService.getMessage(anyString())).willReturn(message01);
// call controller and check the result
mockMvc.perform(get("/api/message/01").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.[?(@.id=='01')].text").value("hello"))
//.andDo(print())
;
}
@Test
void getNonExistingMessage() throws Exception {
log.debug("getMessage non-existing");
// define what mock service returns when called
given(mockChatService.getMessage("-1")).willReturn(null);
// call controller and check the result
mockMvc.perform(get("/api/message/-1"))
.andExpect(status().isNotFound())
;
}
@Test
void createMessage() throws Exception {
log.debug("createMessage");
// define what mock service returns when called
String id = "X";
String text = "To be or not to be";
String author = "Shakespeare";
String textColor = "black";
BackgroundColor backgroundColor = BackgroundColor.WHITE;
StoredMessage msg = new StoredMessage(id, now, text, author, textColor, backgroundColor.getValue(), null);
given(mockChatService.createNewMessage(text, author, textColor, backgroundColor.getValue())).willReturn(msg);
// call controller and check the result
NewChatMessageRequest n = new NewChatMessageRequest();
n.setText(text);
n.setTextColor(textColor);
n.setBackgroundColor(BackgroundColor.WHITE);
mockMvc.perform(post("/api/messages?author=" + author)
.header("User-Agent", "007")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(n))
)
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").value(id))
.andExpect(jsonPath("$.text").value(text))
.andExpect(jsonPath("$.author").value(author))
.andExpect(jsonPath("$.textColor").value(textColor))
.andExpect(jsonPath("$.backgroundColor").value(backgroundColor.getValue()))
;
}
@Test
void createMessageWithoutText() throws Exception {
log.debug("createMessageWithoutText");
// mock service is not needed in this test
// call controller and check the result
NewChatMessageRequest n = new NewChatMessageRequest();
n.setText(null);
n.setTextColor("black");
mockMvc.perform(post("/api/messages")
.header("User-Agent", "007")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(n))
)
.andExpect(status().isBadRequest())
//.andDo(print())
;
}
@Test
void paged() throws Exception {
log.debug("paged");
// define what mock service returns when called
Pageable p = PageRequest.of(0, 2);
Page<StoredMessage> page = new PageImpl<>(allMessages, p, allMessages.size());
given(mockChatService.getPageOfMessages(any())).willReturn(page);
// call controller and check the result
mockMvc.perform(get("/api/paged?page={page}&size={size}", p.getPageNumber(), p.getPageSize()).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.content[0].id").value("01"))
.andExpect(jsonPath("$.content.length()").value(1))
//.andDo(print())
;
}
}