Skip to content

Commit fe0becc

Browse files
admin@javaops.ruadmin@javaops.ru
authored andcommitted
6_03_add_tests
1 parent 0ab4285 commit fe0becc

File tree

7 files changed

+150
-13
lines changed

7 files changed

+150
-13
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@
8282
<artifactId>spring-boot-starter-test</artifactId>
8383
<scope>test</scope>
8484
</dependency>
85+
<!-- https://www.baeldung.com/spring-security-integration-tests -->
86+
<dependency>
87+
<groupId>org.springframework.security</groupId>
88+
<artifactId>spring-security-test</artifactId>
89+
<scope>test</scope>
90+
</dependency>
8591
</dependencies>
8692

8793
<build>

src/main/java/ru/javaops/bootjava/web/AccountController.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
* RequestMapping("/${spring.data.rest.basePath}/account") give "Not enough variable values"
3737
*/
3838
@RestController
39-
@RequestMapping("/api/account")
39+
@RequestMapping(AccountController.URL)
4040
@AllArgsConstructor
4141
@Slf4j
4242
@Tag(name = "Account Controller")
4343
public class AccountController implements RepresentationModelProcessor<RepositoryLinksResource> {
44+
static final String URL = "/api/account";
45+
4446
@SuppressWarnings("unchecked")
4547
private static final RepresentationModelAssemblerSupport<User, EntityModel<User>> ASSEMBLER =
4648
new RepresentationModelAssemblerSupport<>(AccountController.class, (Class<EntityModel<User>>) (Class<?>) EntityModel.class) {

src/test/java/ru/javaops/bootjava/RestaurantVotingApplicationTests.java

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ru.javaops.bootjava;
2+
3+
public class UserTestUtil {
4+
public static final int USER_ID = 1;
5+
public static final int ADMIN_ID = 2;
6+
public static final String USER_MAIL = "user@gmail.com";
7+
public static final String ADMIN_MAIL = "admin@javaops.ru";
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ru.javaops.bootjava.web;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.web.servlet.MockMvc;
7+
import org.springframework.test.web.servlet.ResultActions;
8+
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
9+
import org.springframework.transaction.annotation.Transactional;
10+
11+
//https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-testing-spring-boot-applications
12+
@SpringBootTest
13+
@Transactional
14+
@AutoConfigureMockMvc
15+
//https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-testing-spring-boot-applications-testing-with-mock-environment
16+
public abstract class AbstractControllerTest {
17+
18+
@Autowired
19+
protected MockMvc mockMvc;
20+
21+
protected ResultActions perform(MockHttpServletRequestBuilder builder) throws Exception {
22+
return mockMvc.perform(builder);
23+
}
24+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package ru.javaops.bootjava.web;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.hateoas.MediaTypes;
7+
import org.springframework.security.test.context.support.WithUserDetails;
8+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
9+
import ru.javaops.bootjava.repository.UserRepository;
10+
11+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
13+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
14+
import static ru.javaops.bootjava.UserTestUtil.*;
15+
import static ru.javaops.bootjava.web.AccountController.URL;
16+
17+
class AccountControllerTest extends AbstractControllerTest {
18+
19+
@Autowired
20+
private UserRepository userRepository;
21+
22+
@Test
23+
@WithUserDetails(value = USER_MAIL)
24+
void get() throws Exception {
25+
perform(MockMvcRequestBuilders.get(URL))
26+
.andExpect(status().isOk())
27+
.andDo(print())
28+
.andExpect(content().contentTypeCompatibleWith(MediaTypes.HAL_JSON_VALUE));
29+
}
30+
31+
@Test
32+
void getUnAuth() throws Exception {
33+
perform(MockMvcRequestBuilders.get(URL))
34+
.andExpect(status().isUnauthorized());
35+
}
36+
37+
@Test
38+
@WithUserDetails(value = USER_MAIL)
39+
void delete() throws Exception {
40+
perform(MockMvcRequestBuilders.delete(URL))
41+
.andExpect(status().isNoContent());
42+
Assertions.assertFalse(userRepository.findById(USER_ID).isPresent());
43+
Assertions.assertTrue(userRepository.findById(ADMIN_ID).isPresent());
44+
}
45+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ru.javaops.bootjava.web;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.hateoas.MediaTypes;
7+
import org.springframework.security.test.context.support.WithUserDetails;
8+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
9+
import ru.javaops.bootjava.repository.UserRepository;
10+
11+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
13+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
14+
import static ru.javaops.bootjava.UserTestUtil.*;
15+
16+
class UserControllerTest extends AbstractControllerTest {
17+
static final String URL = "/api/users/";
18+
19+
@Autowired
20+
private UserRepository userRepository;
21+
22+
@Test
23+
@WithUserDetails(value = ADMIN_MAIL)
24+
void get() throws Exception {
25+
perform(MockMvcRequestBuilders.get(URL + USER_ID))
26+
.andExpect(status().isOk())
27+
.andDo(print())
28+
.andExpect(content().contentTypeCompatibleWith(MediaTypes.HAL_JSON_VALUE));
29+
}
30+
31+
@Test
32+
@WithUserDetails(value = ADMIN_MAIL)
33+
void getAll() throws Exception {
34+
perform(MockMvcRequestBuilders.get(URL))
35+
.andExpect(status().isOk())
36+
.andDo(print())
37+
.andExpect(content().contentTypeCompatibleWith(MediaTypes.HAL_JSON_VALUE));
38+
}
39+
40+
@Test
41+
@WithUserDetails(value = ADMIN_MAIL)
42+
void getByEmail() throws Exception {
43+
perform(MockMvcRequestBuilders.get(URL + "search/by-email?email=" + ADMIN_MAIL))
44+
.andExpect(status().isOk())
45+
.andDo(print())
46+
.andExpect(content().contentTypeCompatibleWith(MediaTypes.HAL_JSON_VALUE));
47+
}
48+
49+
@Test
50+
@WithUserDetails(value = USER_MAIL)
51+
void getForbidden() throws Exception {
52+
perform(MockMvcRequestBuilders.get(URL))
53+
.andExpect(status().isForbidden());
54+
}
55+
56+
@Test
57+
@WithUserDetails(value = ADMIN_MAIL)
58+
void delete() throws Exception {
59+
perform(MockMvcRequestBuilders.delete(URL + USER_ID))
60+
.andExpect(status().isNoContent());
61+
Assertions.assertFalse(userRepository.findById(USER_ID).isPresent());
62+
Assertions.assertTrue(userRepository.findById(ADMIN_ID).isPresent());
63+
}
64+
}

0 commit comments

Comments
 (0)