-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChannelRestControllerTest.java
95 lines (82 loc) · 3.41 KB
/
ChannelRestControllerTest.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
package be.kdg.programming5.controllers.api;
import be.kdg.programming5.model.Channel;
import be.kdg.programming5.model.Role;
import be.kdg.programming5.model.User;
import be.kdg.programming5.repository.ChannelRepository;
import be.kdg.programming5.repository.UserRepository;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
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.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance (TestInstance.Lifecycle.PER_CLASS)
class ChannelRestControllerTest {
private Channel c1;
private Channel c2;
private Channel c3;
@Autowired
private MockMvc mockMvc;
@Autowired
private ChannelRepository channelRepository;
@Autowired
private UserRepository userRepository;
@BeforeAll
void setup() {
final User u1 = userRepository.save(new User("user1", LocalDate.of(2000, 1, 1), Role.ADMIN, "password"));
final User u2 = userRepository.save(new User("user2", LocalDate.of(2000, 1, 1), Role.ADMIN, "password"));
final User u3 = userRepository.save(new User("user3", LocalDate.of(2000, 1, 1), Role.ADMIN, "password"));
final User u4 = userRepository.save(new User("user4", LocalDate.of(2000, 1, 1), Role.ADMIN, "password"));
c1 = channelRepository.save(new Channel("channel1", "description1"));
c2 = channelRepository.save(new Channel("channel2", "description2"));
c3 = channelRepository.save(new Channel("channel3", "description3"));
u1.joinChannel(c1);
u2.joinChannel(c1);
u1.joinChannel(c2);
u3.joinChannel(c2);
u4.joinChannel(c2);
userRepository.save(u1);
userRepository.save(u2);
userRepository.save(u3);
userRepository.save(u4);
}
@AfterAll
void tearDown() {
channelRepository.deleteAll();
userRepository.deleteAll();
}
@Test
void getUsersOfChannel() throws Exception {
mockMvc.perform(get("/api/channels/{id}/users", c1.getChannelID()))
.andExpect(status().isOk())
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[*].name", containsInAnyOrder("user1", "user2")));
mockMvc.perform(get("/api/channels/{id}/users", c2.getChannelID()))
.andExpect(status().isOk())
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[*].name", containsInAnyOrder("user1", "user3", "user4")));
}
@Test
void getUsersOfChannelNotFound() throws Exception {
mockMvc.perform(get("/api/channels/999/users"))
.andExpect(status().isNotFound());
}
@Test
void getUsersOfChannelNoContent() throws Exception {
mockMvc.perform(get("/api/channels/" + c3.getChannelID() + "/users"))
.andExpect(status().isNoContent());
}
}