forked from JavaOPs/bootjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileControllerTest.java
More file actions
111 lines (97 loc) · 4.51 KB
/
ProfileControllerTest.java
File metadata and controls
111 lines (97 loc) · 4.51 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
package ru.javaops.bootjava.user.web;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import ru.javaops.bootjava.AbstractControllerTest;
import ru.javaops.bootjava.common.util.JsonUtil;
import ru.javaops.bootjava.user.UsersUtil;
import ru.javaops.bootjava.user.model.User;
import ru.javaops.bootjava.user.repository.UserRepository;
import ru.javaops.bootjava.user.to.UserTo;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static ru.javaops.bootjava.user.UserTestData.*;
import static ru.javaops.bootjava.user.web.ProfileController.REST_URL;
class ProfileControllerTest extends AbstractControllerTest {
@Autowired
private UserRepository repository;
@Test
@WithUserDetails(value = USER_MAIL)
void get() throws Exception {
perform(MockMvcRequestBuilders.get(REST_URL))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(USER_MATCHER.contentJson(user));
}
@Test
void getUnAuth() throws Exception {
perform(MockMvcRequestBuilders.get(REST_URL))
.andExpect(status().isUnauthorized());
}
@Test
@WithUserDetails(value = USER_MAIL)
void delete() throws Exception {
perform(MockMvcRequestBuilders.delete(REST_URL))
.andExpect(status().isNoContent());
USER_MATCHER.assertMatch(repository.findAll(), admin, guest);
}
@Test
void register() throws Exception {
UserTo newTo = new UserTo(null, "newName", "newemail@ya.ru", "newPassword");
User newUser = UsersUtil.createNewFromTo(newTo);
ResultActions action = perform(MockMvcRequestBuilders.post(REST_URL)
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.writeValue(newTo)))
.andDo(print())
.andExpect(status().isCreated());
User created = USER_MATCHER.readFromJson(action);
int newId = created.id();
newUser.setId(newId);
USER_MATCHER.assertMatch(created, newUser);
USER_MATCHER.assertMatch(repository.getExisted(newId), newUser);
}
@Test
@WithUserDetails(value = USER_MAIL)
void update() throws Exception {
UserTo updatedTo = new UserTo(null, "newName", USER_MAIL, "newPassword");
perform(MockMvcRequestBuilders.put(REST_URL).contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.writeValue(updatedTo)))
.andDo(print())
.andExpect(status().isNoContent());
USER_MATCHER.assertMatch(repository.getExisted(USER_ID), UsersUtil.updateFromTo(new User(user), updatedTo));
}
@Test
void registerInvalid() throws Exception {
UserTo newTo = new UserTo(null, null, null, null);
perform(MockMvcRequestBuilders.post(REST_URL)
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.writeValue(newTo)))
.andDo(print())
.andExpect(status().isUnprocessableEntity());
}
@Test
@WithUserDetails(value = USER_MAIL)
void updateInvalid() throws Exception {
UserTo updatedTo = new UserTo(null, null, "password", null);
perform(MockMvcRequestBuilders.put(REST_URL)
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.writeValue(updatedTo)))
.andDo(print())
.andExpect(status().isUnprocessableEntity());
}
@Test
@WithUserDetails(value = USER_MAIL)
void updateDuplicate() throws Exception {
UserTo updatedTo = new UserTo(null, "newName", ADMIN_MAIL, "newPassword");
perform(MockMvcRequestBuilders.put(REST_URL).contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.writeValue(updatedTo)))
.andDo(print())
.andExpect(status().isUnprocessableEntity())
.andExpect(content().string(containsString(UniqueMailValidator.EXCEPTION_DUPLICATE_EMAIL)));
}
}