-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add controller layer tests coverage
- Loading branch information
1 parent
129ac04
commit 7a89a2b
Showing
6 changed files
with
190 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
...-service/src/test/java/dev/earlspilner/users/rest/controller/UserRestControllerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package dev.earlspilner.users.rest.controller; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import dev.earlspilner.users.mapper.UserMapper; | ||
import dev.earlspilner.users.model.User; | ||
import dev.earlspilner.users.model.UserRole; | ||
import dev.earlspilner.users.service.ApplicationTestConfig; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.web.WebAppConfiguration; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
import java.util.List; | ||
|
||
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; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
@SpringBootTest | ||
@ContextConfiguration(classes = ApplicationTestConfig.class) | ||
@WebAppConfiguration | ||
class UserRestControllerTests { | ||
|
||
@Autowired | ||
private UserMapper userMapper; | ||
|
||
@Autowired | ||
private UserRestController userRestController; | ||
|
||
private MockMvc mockMvc; | ||
|
||
@BeforeEach | ||
void init() { | ||
this.mockMvc = MockMvcBuilders.standaloneSetup(userRestController).build(); | ||
} | ||
|
||
@Test | ||
void testCreateUserSuccess() throws Exception { | ||
User user = new User(); | ||
user.setName("Alex Tourette"); | ||
user.setUsername("tourette"); | ||
user.setEmail("[email protected]"); | ||
user.setPassword("password"); | ||
user.setRoles(List.of(UserRole.ROLE_VISITOR)); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
String newUserAsJSON = mapper.writeValueAsString(userMapper.toUserDto(user)); | ||
this.mockMvc.perform(post("/users") | ||
.content(newUserAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) | ||
.andExpect(status().isCreated()) | ||
.andExpect(jsonPath("$.id").value(3)) | ||
.andExpect(jsonPath("$.username").value("tourette")); | ||
} | ||
|
||
@Test | ||
@WithMockUser(roles = "ADMIN") | ||
void testGetUserSuccess() throws Exception { | ||
this.mockMvc.perform(get("/users/jbloch") | ||
.accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
@WithMockUser(roles = "ADMIN") | ||
void testGetUserFailure() throws Exception { | ||
this.mockMvc.perform(get("/users/unknown") | ||
.accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
@WithMockUser(roles = "ADMIN") | ||
void testCreateUserFailureFullName() throws Exception { | ||
User user = new User(); | ||
user.setName(""); | ||
user.setUsername("tourette"); | ||
user.setEmail("[email protected]"); | ||
user.setPassword("password"); | ||
user.setRoles(List.of(UserRole.ROLE_VISITOR)); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
String newUserAsJSON = mapper.writeValueAsString(userMapper.toUserDto(user)); | ||
this.mockMvc.perform(post("/users") | ||
.content(newUserAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) | ||
.andExpect(status().isBadRequest()); | ||
} | ||
|
||
@Test | ||
@WithMockUser(roles = "ADMIN") | ||
void testCreateUserFailureUsername() throws Exception { | ||
User user = new User(); | ||
user.setName("Alex Tourette"); | ||
user.setUsername(""); | ||
user.setEmail("[email protected]"); | ||
user.setPassword("password"); | ||
user.setRoles(List.of(UserRole.ROLE_VISITOR)); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
String newUserAsJSON = mapper.writeValueAsString(userMapper.toUserDto(user)); | ||
this.mockMvc.perform(post("/users") | ||
.content(newUserAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) | ||
.andExpect(status().isBadRequest()); | ||
} | ||
|
||
@Test | ||
@WithMockUser(roles = "ADMIN") | ||
void testCreateUserFailureEmail() throws Exception { | ||
User user = new User(); | ||
user.setName("Alex Tourette"); | ||
user.setUsername("tourette"); | ||
user.setEmail(""); | ||
user.setPassword("password"); | ||
user.setRoles(List.of(UserRole.ROLE_VISITOR)); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
String newUserAsJSON = mapper.writeValueAsString(userMapper.toUserDto(user)); | ||
this.mockMvc.perform(post("/users") | ||
.content(newUserAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) | ||
.andExpect(status().isBadRequest()); | ||
} | ||
|
||
@Test | ||
@WithMockUser(roles = "ADMIN") | ||
void testCreateUserFailurePassword() throws Exception { | ||
User user = new User(); | ||
user.setName("Alex Tourette"); | ||
user.setUsername("tourette"); | ||
user.setEmail("[email protected]"); | ||
user.setPassword(""); | ||
user.setRoles(List.of(UserRole.ROLE_VISITOR)); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
String newUserAsJSON = mapper.writeValueAsString(userMapper.toUserDto(user)); | ||
this.mockMvc.perform(post("/users") | ||
.content(newUserAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) | ||
.andExpect(status().isBadRequest()); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...-api-users-service/src/test/java/dev/earlspilner/users/service/ApplicationTestConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package dev.earlspilner.users.service; | ||
|
||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
@TestConfiguration | ||
public class ApplicationTestConfig { | ||
|
||
public ApplicationTestConfig() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters