-
Notifications
You must be signed in to change notification settings - Fork 41
/
StudentEndpointTest.java
142 lines (129 loc) · 6.78 KB
/
StudentEndpointTest.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
package br.com.devdojo;
import br.com.devdojo.model.Student;
import br.com.devdojo.repository.StudentRepository;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.BDDMockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import java.util.List;
import static java.util.Arrays.asList;
import static org.springframework.http.HttpMethod.DELETE;
/**
* @author William Suane for DevDojo on 7/11/17.
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class StudentEndpointTest {
@Autowired
private TestRestTemplate restTemplate;
@LocalServerPort
private int port;
@MockBean
private StudentRepository studentRepository;
@Autowired
private MockMvc mockMvc;
@TestConfiguration
static class Config {
@Bean
public RestTemplateBuilder restTemplateBuilder() {
return new RestTemplateBuilder().basicAuthorization("toyo", "devdojo");
}
}
@Before
public void setup() {
Student student = new Student(1L, "Legolas", "legolas@lotr.com");
BDDMockito.when(studentRepository.findOne(student.getId())).thenReturn(student);
}
@Test
public void listStudentsWhenUsernameAndPasswordAreIncorrectShouldReturnStatusCode401() {
System.out.println(port);
restTemplate = restTemplate.withBasicAuth("1", "1");
ResponseEntity<String> response = restTemplate.getForEntity("/v1/protected/students/", String.class);
Assertions.assertThat(response.getStatusCodeValue()).isEqualTo(401);
}
@Test
public void getStudentsByIdWhenUsernameAndPasswordAreIncorrectShouldReturnStatusCode401() {
System.out.println(port);
restTemplate = restTemplate.withBasicAuth("1", "1");
ResponseEntity<String> response = restTemplate.getForEntity("/v1/protected/students/1", String.class);
Assertions.assertThat(response.getStatusCodeValue()).isEqualTo(401);
}
@Test
public void listStudentsWhenUsernameAndPasswordAreCorrectShouldReturnStatusCode200() {
List<Student> students = asList(new Student(1L, "Legolas", "legolas@lotr.com"),
new Student(2L, "Aragorn", "aragorn@lotr.com"));
BDDMockito.when(studentRepository.findAll()).thenReturn(students);
ResponseEntity<String> response = restTemplate.getForEntity("/v1/protected/students/", String.class);
Assertions.assertThat(response.getStatusCodeValue()).isEqualTo(200);
}
@Test
public void getStudentsByIdWhenUsernameAndPasswordAreCorrectShouldReturnStatusCode200() {
ResponseEntity<Student> response = restTemplate.getForEntity("/v1/protected/students/{id}", Student.class, 1L);
Assertions.assertThat(response.getStatusCodeValue()).isEqualTo(200);
}
@Test
public void getStudentsByIdWhenUsernameAndPasswordAreCorrectAndStudentDoesNotExistShouldReturnStatusCode404() {
ResponseEntity<Student> response = restTemplate.getForEntity("/v1/protected/students/{id}", Student.class, -1);
Assertions.assertThat(response.getStatusCodeValue()).isEqualTo(404);
}
@Test
public void deleteWhenUserHasRoleAdminAndStudentExistsShouldReturnStatusCode200() {
BDDMockito.doNothing().when(studentRepository).delete(1L);
ResponseEntity<String> exchange = restTemplate.exchange("/v1/admin/students/{id}", DELETE, null, String.class, 1L);
Assertions.assertThat(exchange.getStatusCodeValue()).isEqualTo(200);
}
@Test
@WithMockUser(username = "xx", password = "xx", roles = {"USER","ADMIN"})
public void deleteWhenUserHasRoleAdminAndStudentDoesNotExistShouldReturnStatusCode404() throws Exception {
BDDMockito.doNothing().when(studentRepository).delete(1L);
// ResponseEntity<String> exchange = restTemplate.exchange("/v1/admin/students/{id}", DELETE, null, String.class, -1L);
// Assertions.assertThat(exchange.getStatusCodeValue()).isEqualTo(404);
mockMvc.perform(MockMvcRequestBuilders
.delete("/v1/admin/students/{id}", -1L))
.andExpect(MockMvcResultMatchers.status().isNotFound());
}
@Test
@WithMockUser(username = "xx", password = "xx", roles = {"USER"})
public void deleteWhenUserDoesNotHaveRoleAdminShouldReturnStatusCode403() throws Exception {
BDDMockito.doNothing().when(studentRepository).delete(1L);
// ResponseEntity<String> exchange = restTemplate.exchange("/v1/admin/students/{id}", DELETE, null, String.class, -1L);
// Assertions.assertThat(exchange.getStatusCodeValue()).isEqualTo(404);
mockMvc.perform(MockMvcRequestBuilders
.delete("/v1/admin/students/{id}", -1L))
.andExpect(MockMvcResultMatchers.status().isForbidden());
}
@Test
public void createWhenNameIsNullShouldReturnStatusCode400BadRequest() throws Exception{
Student student = new Student(3L, null, "sam@lotr.com");
BDDMockito.when(studentRepository.save(student)).thenReturn(student);
ResponseEntity<String> response = restTemplate.postForEntity("/v1/admin/students/", student, String.class);
Assertions.assertThat(response.getStatusCodeValue()).isEqualTo(400);
Assertions.assertThat(response.getBody()).contains("fieldMessage","O campo nome do estudante é obrigatório");
}
@Test
public void createShouldPersistDataAndReturnStatusCode201() throws Exception{
Student student = new Student(3L, "Sam", "sam@lotr.com");
BDDMockito.when(studentRepository.save(student)).thenReturn(student);
ResponseEntity<Student> response = restTemplate.postForEntity("/v1/admin/students/", student, Student.class);
Assertions.assertThat(response.getStatusCodeValue()).isEqualTo(201);
Assertions.assertThat(response.getBody().getId()).isNotNull();
}
}