Skip to content

Commit

Permalink
[kbss-cvut/record-manager-ui#202] Implement RoleGroup controller
Browse files Browse the repository at this point in the history
  • Loading branch information
palagdan committed Sep 25, 2024
1 parent 69b4db4 commit 65fd7fb
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/main/java/cz/cvut/kbss/study/model/RoleGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -54,4 +55,17 @@ public void setRoles(Set<Role> 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);
}
}
45 changes: 45 additions & 0 deletions src/main/java/cz/cvut/kbss/study/rest/RoleGroupController.java
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ public interface RoleGroupService extends BaseService<RoleGroup> {

RoleGroup findByName(String name);


}
89 changes: 89 additions & 0 deletions src/test/java/cz/cvut/kbss/study/rest/RoleGroupControllerTest.java
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());
}

}

0 comments on commit 65fd7fb

Please sign in to comment.