diff --git a/src/main/java/cz/cvut/kbss/study/model/RoleGroup.java b/src/main/java/cz/cvut/kbss/study/model/RoleGroup.java index eb7e5511..f058af50 100644 --- a/src/main/java/cz/cvut/kbss/study/model/RoleGroup.java +++ b/src/main/java/cz/cvut/kbss/study/model/RoleGroup.java @@ -7,6 +7,7 @@ import java.io.Serializable; import java.net.URI; import java.util.HashSet; +import java.util.Objects; import java.util.Set; @OWLClass(iri = Vocabulary.s_c_role_group) @@ -54,4 +55,17 @@ public void setRoles(Set roles) { public void generateUri() { this.uri = URI.create(Constants.BASE_URI + "sdfsf"); } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + RoleGroup roleGroup = (RoleGroup) o; + return Objects.equals(name, roleGroup.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } } diff --git a/src/main/java/cz/cvut/kbss/study/rest/RoleGroupController.java b/src/main/java/cz/cvut/kbss/study/rest/RoleGroupController.java new file mode 100644 index 00000000..1cbf4d1a --- /dev/null +++ b/src/main/java/cz/cvut/kbss/study/rest/RoleGroupController.java @@ -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 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 roleGroupService.findByName(name); + } + +} diff --git a/src/main/java/cz/cvut/kbss/study/service/RoleGroupService.java b/src/main/java/cz/cvut/kbss/study/service/RoleGroupService.java index b18f421f..1f373f74 100644 --- a/src/main/java/cz/cvut/kbss/study/service/RoleGroupService.java +++ b/src/main/java/cz/cvut/kbss/study/service/RoleGroupService.java @@ -8,5 +8,4 @@ public interface RoleGroupService extends BaseService { RoleGroup findByName(String name); - } diff --git a/src/test/java/cz/cvut/kbss/study/rest/RoleGroupControllerTest.java b/src/test/java/cz/cvut/kbss/study/rest/RoleGroupControllerTest.java new file mode 100644 index 00000000..d27c1ca7 --- /dev/null +++ b/src/test/java/cz/cvut/kbss/study/rest/RoleGroupControllerTest.java @@ -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 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()); + } + +}