Search in sources :

Example 6 with RoleEntity

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)));
}
Also used : RoleEntity(org.codenergic.theskeleton.role.RoleEntity) ResultActions(org.springframework.test.web.servlet.ResultActions) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 7 with RoleEntity

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)));
}
Also used : RoleEntity(org.codenergic.theskeleton.role.RoleEntity) ResultActions(org.springframework.test.web.servlet.ResultActions) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 8 with RoleEntity

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();
}
Also used : RoleEntity(org.codenergic.theskeleton.role.RoleEntity) UserRoleEntity(org.codenergic.theskeleton.role.UserRoleEntity) RoleNotFoundException(org.codenergic.theskeleton.role.RoleNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with RoleEntity

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");
}
Also used : RoleEntity(org.codenergic.theskeleton.role.RoleEntity) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) RoleEntity(org.codenergic.theskeleton.role.RoleEntity) Date(java.util.Date) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockMvc(org.springframework.test.web.servlet.MockMvc) ResultActions(org.springframework.test.web.servlet.ResultActions) HashSet(java.util.HashSet) RoleRestData(org.codenergic.theskeleton.role.RoleRestData) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) TokenStoreService(org.codenergic.theskeleton.tokenstore.TokenStoreService) SpringRunner(org.springframework.test.context.junit4.SpringRunner) MockBean(org.springframework.boot.test.mock.mockito.MockBean) MockMvcRestDocumentation.document(org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document) MediaType(org.springframework.http.MediaType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Page(org.springframework.data.domain.Page) EnableSpringDataWebSupport(org.springframework.data.web.config.EnableSpringDataWebSupport) Collectors(java.util.stream.Collectors) MockMvcRequestBuilders(org.springframework.test.web.servlet.request.MockMvcRequestBuilders) Mockito.verify(org.mockito.Mockito.verify) EnableRestDocs(org.codenergic.theskeleton.core.test.EnableRestDocs) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest) PageImpl(org.springframework.data.domain.PageImpl) RoleRestData(org.codenergic.theskeleton.role.RoleRestData) ResultActions(org.springframework.test.web.servlet.ResultActions) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HashSet(java.util.HashSet) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 10 with RoleEntity

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();
}
Also used : RoleEntity(org.codenergic.theskeleton.role.RoleEntity) RolePrivilegeEntity(org.codenergic.theskeleton.role.RolePrivilegeEntity) RolePrivilegeEntity(org.codenergic.theskeleton.role.RolePrivilegeEntity) PrivilegeEntity(org.codenergic.theskeleton.privilege.PrivilegeEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

RoleEntity (org.codenergic.theskeleton.role.RoleEntity)11 Test (org.junit.Test)6 Transactional (org.springframework.transaction.annotation.Transactional)5 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 ResultActions (org.springframework.test.web.servlet.ResultActions)3 HashSet (java.util.HashSet)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Date (java.util.Date)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 EnableRestDocs (org.codenergic.theskeleton.core.test.EnableRestDocs)1 PrivilegeEntity (org.codenergic.theskeleton.privilege.PrivilegeEntity)1 RoleNotFoundException (org.codenergic.theskeleton.role.RoleNotFoundException)1 RolePrivilegeEntity (org.codenergic.theskeleton.role.RolePrivilegeEntity)1 RoleRestData (org.codenergic.theskeleton.role.RoleRestData)1 UserRoleEntity (org.codenergic.theskeleton.role.UserRoleEntity)1