use of org.codenergic.theskeleton.role.RoleEntity in project theskeleton by codenergic.
the class RolePrivilegeRestControllerTest method testAddPrivilegeToRole.
@Test
public void testAddPrivilegeToRole() throws Exception {
final RoleEntity role = new RoleEntity().setId("role123").setCode("role123");
when(privilegeService.addPrivilegeToRole("role123", "privilege123")).thenReturn(role);
ResultActions resultActions = mockMvc.perform(put("/api/roles/role123/privileges").content("{\"privilege\": \"privilege123\"}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andDo(document("role-privilege-create"));
MockHttpServletResponse response = resultActions.andReturn().getResponse();
verify(privilegeService).addPrivilegeToRole("role123", "privilege123");
assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(roleMapper.toRoleData(role)));
}
use of org.codenergic.theskeleton.role.RoleEntity in project theskeleton by codenergic.
the class RolePrivilegeRestControllerTest method testRemovePrivilegeFromRole.
@Test
public void testRemovePrivilegeFromRole() throws Exception {
final RoleEntity role = new RoleEntity().setId("role123").setCode("role123");
when(privilegeService.removePrivilegeFromRole("role123", "privilege123")).thenReturn(role);
ResultActions resultActions = mockMvc.perform(delete("/api/roles/role123/privileges").content("{\"privilege\": \"privilege123\"}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andDo(document("role-privilege-delete"));
MockHttpServletResponse response = resultActions.andReturn().getResponse();
verify(privilegeService).removePrivilegeFromRole("role123", "privilege123");
assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(roleMapper.toRoleData(role)));
}
use of org.codenergic.theskeleton.role.RoleEntity in project theskeleton by codenergic.
the class PrivilegeServiceImpl method addPrivilegeToRole.
@Override
@Transactional
public RoleEntity addPrivilegeToRole(String code, String privilegeName) {
RoleEntity role = roleRepository.findByCode(code).orElseThrow(() -> new RoleNotFoundException(code));
PrivilegeEntity privilege = privilegeRepository.findByName(privilegeName).orElseThrow(() -> new PrivilegeNotFoundException(privilegeName));
return rolePrivilegeRepository.save(new RolePrivilegeEntity(role, privilege)).getRole();
}
use of org.codenergic.theskeleton.role.RoleEntity in project theskeleton by codenergic.
the class UserRestControllerTest method testFindRolesByUserUsername.
@Test
public void testFindRolesByUserUsername() throws Exception {
final RoleEntity role = new RoleEntity().setId("role123").setCode("role123");
final Set<RoleEntity> roles = new HashSet<>(Arrays.asList(role));
final Set<RoleRestData> expected = roles.stream().map(r -> RoleRestData.builder().fromRoleEntity(r).build()).collect(Collectors.toSet());
when(userAdminService.findRolesByUserUsername("user123")).thenReturn(roles);
ResultActions resultActions = mockMvc.perform(get("/api/users/user123/roles").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andDo(document("user-role-read"));
MockHttpServletResponse response = resultActions.andReturn().getResponse();
assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(expected));
verify(userAdminService).findRolesByUserUsername("user123");
}
use of org.codenergic.theskeleton.role.RoleEntity in project theskeleton by codenergic.
the class RoleServiceImpl method addPrivilegeToRole.
@Override
@Transactional
public RoleEntity addPrivilegeToRole(String code, String privilegeName) {
RoleEntity role = findRoleByCode(code);
PrivilegeEntity privilege = privilegeRepository.findByName(privilegeName);
return rolePrivilegeRepository.save(new RolePrivilegeEntity(role, privilege)).getRole();
}
Aggregations