forked from blcham/record-manager
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kbss-cvut/record-manager-ui#202] Implement RoleGroup controller
- Loading branch information
Showing
4 changed files
with
148 additions
and
1 deletion.
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
45 changes: 45 additions & 0 deletions
45
src/main/java/cz/cvut/kbss/study/rest/RoleGroupController.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,45 @@ | ||
package cz.cvut.kbss.study.rest; | ||
|
||
|
||
import cz.cvut.kbss.study.exception.NotFoundException; | ||
import cz.cvut.kbss.study.model.RoleGroup; | ||
import cz.cvut.kbss.study.security.SecurityConstants; | ||
import cz.cvut.kbss.study.service.RoleGroupService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/roleGroup") | ||
public class RoleGroupController extends BaseController{ | ||
|
||
private final RoleGroupService roleGroupService; | ||
|
||
@Autowired | ||
public RoleGroupController(RoleGroupService roleGroupService) { | ||
this.roleGroupService = roleGroupService; | ||
} | ||
|
||
@PreAuthorize("hasAuthority('" + SecurityConstants.administrator + "')") | ||
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) | ||
public List<RoleGroup> getRoleGroups() { | ||
return roleGroupService.findAll(); | ||
} | ||
|
||
@PreAuthorize("hasAuthority('" + SecurityConstants.administrator + "')") | ||
@GetMapping(value = "/{name}",produces = MediaType.APPLICATION_JSON_VALUE) | ||
public RoleGroup findByName(@PathVariable("name") String name) { | ||
RoleGroup result = roleGroupService.findByName(name); | ||
if(result == null){ | ||
throw NotFoundException.create("RoleGroup", name); | ||
} | ||
return result; | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -8,5 +8,4 @@ public interface RoleGroupService extends BaseService<RoleGroup> { | |
|
||
RoleGroup findByName(String name); | ||
|
||
|
||
} |
89 changes: 89 additions & 0 deletions
89
src/test/java/cz/cvut/kbss/study/rest/RoleGroupControllerTest.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,89 @@ | ||
package cz.cvut.kbss.study.rest; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import cz.cvut.kbss.study.model.Institution; | ||
import cz.cvut.kbss.study.model.RoleGroup; | ||
import cz.cvut.kbss.study.service.RoleGroupService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class RoleGroupControllerTest extends BaseControllerTestRunner { | ||
|
||
@Mock | ||
private RoleGroupService roleGroupServiceMock; | ||
|
||
|
||
@InjectMocks | ||
private RoleGroupController controller; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
super.setUp(controller); | ||
} | ||
|
||
@Test | ||
public void testGetRoleGroups() throws Exception { | ||
RoleGroup roleGroup = new RoleGroup(); | ||
roleGroup.setName("admin-role-group"); | ||
|
||
when(roleGroupServiceMock.findAll()).thenReturn(List.of(roleGroup)); | ||
|
||
final MvcResult result = mockMvc.perform(get("/roleGroup/")).andReturn(); | ||
|
||
final List<RoleGroup> body = objectMapper.readValue(result.getResponse().getContentAsString(), | ||
new TypeReference<>() { | ||
}); | ||
|
||
assertEquals(HttpStatus.OK, HttpStatus.valueOf(result.getResponse().getStatus())); | ||
assertEquals(body, List.of(roleGroup)); | ||
} | ||
|
||
@Test | ||
public void testFindByName() throws Exception { | ||
String roleName = "admin-role-group"; | ||
RoleGroup roleGroup = new RoleGroup(); | ||
roleGroup.setName(roleName); | ||
|
||
when(roleGroupServiceMock.findByName(roleName)).thenReturn(roleGroup); | ||
|
||
final MvcResult result = mockMvc.perform(get("/roleGroup/" + roleName)).andReturn(); | ||
|
||
final RoleGroup body = objectMapper.readValue(result.getResponse().getContentAsString(), | ||
new TypeReference<>() { | ||
}); | ||
|
||
assertEquals(HttpStatus.OK, HttpStatus.valueOf(result.getResponse().getStatus())); | ||
assertEquals(body, roleGroup); | ||
} | ||
|
||
@Test | ||
public void testFindByName_NotFound() throws Exception { | ||
String roleName = "NonExistentRole"; | ||
|
||
when(roleGroupServiceMock.findByName(roleName)).thenReturn(null); | ||
|
||
mockMvc.perform(get("/roleGroup/{name}", roleName) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
} |