Search in sources :

Example 1 with PrivilegeEntity

use of org.codenergic.theskeleton.privilege.PrivilegeEntity in project theskeleton by codenergic.

the class PrivilegeServiceTest method testFindPrivileges.

@Test
@SuppressWarnings("serial")
public void testFindPrivileges() {
    PrivilegeEntity result = new PrivilegeEntity() {

        {
            setId("123");
        }
    }.setName("user_list_read");
    Page<PrivilegeEntity> page = new PageImpl<>(Arrays.asList(result));
    when(privilegeRepository.findAll(any(Pageable.class))).thenReturn(page);
    assertThat(privilegeService.findPrivileges(new PageRequest(1, 10))).isEqualTo(page);
    verify(privilegeRepository).findAll(any(Pageable.class));
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) PrivilegeEntity(org.codenergic.theskeleton.privilege.PrivilegeEntity) Test(org.junit.Test)

Example 2 with PrivilegeEntity

use of org.codenergic.theskeleton.privilege.PrivilegeEntity in project theskeleton by codenergic.

the class RoleRestControllerTest method testFindPrivilegesByRoleCode.

@Test
public void testFindPrivilegesByRoleCode() throws Exception {
    final PrivilegeEntity privilege = new PrivilegeEntity().setId("privilege123").setName("user_list_read");
    final Set<PrivilegeEntity> privileges = new HashSet<>(Arrays.asList(privilege));
    final Set<PrivilegeRestData> expected = privileges.stream().map(p -> PrivilegeRestData.builder().fromPrivilegeEntity(p).build()).collect(Collectors.toSet());
    when(roleService.findPrivilegesByRoleCode("role123")).thenReturn(privileges);
    ResultActions resultActions = mockMvc.perform(get("/api/roles/role123/privileges").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andDo(document("role-privilege-read"));
    MockHttpServletResponse response = resultActions.andReturn().getResponse();
    verify(roleService).findPrivilegesByRoleCode("role123");
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(expected));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) PrivilegeRestData(org.codenergic.theskeleton.privilege.PrivilegeRestData) Arrays(java.util.Arrays) 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) HashSet(java.util.HashSet) MockMvc(org.springframework.test.web.servlet.MockMvc) ResultActions(org.springframework.test.web.servlet.ResultActions) MockMvcRequestBuilders.delete(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) MockMvcRequestBuilders.post(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post) PrivilegeEntity(org.codenergic.theskeleton.privilege.PrivilegeEntity) SpringRunner(org.springframework.test.context.junit4.SpringRunner) MockBean(org.springframework.boot.test.mock.mockito.MockBean) MockMvcRequestBuilders.put(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put) MockMvcRestDocumentation.document(org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document) MediaType(org.springframework.http.MediaType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) InjectUserDetailsService(org.codenergic.theskeleton.core.test.InjectUserDetailsService) IOException(java.io.IOException) Test(org.junit.Test) Mockito.doNothing(org.mockito.Mockito.doNothing) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) EnableSpringDataWebSupport(org.springframework.data.web.config.EnableSpringDataWebSupport) Mockito.verify(org.mockito.Mockito.verify) EnableRestDocs(org.codenergic.theskeleton.core.test.EnableRestDocs) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) PageImpl(org.springframework.data.domain.PageImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PrivilegeEntity(org.codenergic.theskeleton.privilege.PrivilegeEntity) ResultActions(org.springframework.test.web.servlet.ResultActions) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HashSet(java.util.HashSet) PrivilegeRestData(org.codenergic.theskeleton.privilege.PrivilegeRestData) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 3 with PrivilegeEntity

use of org.codenergic.theskeleton.privilege.PrivilegeEntity in project theskeleton by codenergic.

the class PrivilegeServiceTest method testFindPrivilegeById.

@Test
@SuppressWarnings("serial")
public void testFindPrivilegeById() {
    PrivilegeEntity result = new PrivilegeEntity() {

        {
            setId("123");
        }
    }.setName("user_list_read");
    when(privilegeRepository.findOne(eq("123"))).thenReturn(result);
    assertThat(privilegeService.findPrivilegeById("123")).isEqualTo(result);
    verify(privilegeRepository).findOne(eq("123"));
    when(privilegeRepository.findOne(eq("124"))).thenReturn(null);
    assertThat(privilegeService.findPrivilegeById("124")).isNull();
    verify(privilegeRepository).findOne(eq("124"));
}
Also used : PrivilegeEntity(org.codenergic.theskeleton.privilege.PrivilegeEntity) Test(org.junit.Test)

Example 4 with PrivilegeEntity

use of org.codenergic.theskeleton.privilege.PrivilegeEntity in project theskeleton by codenergic.

the class PrivilegeServiceTest method testFindPrivilegeByName.

@Test
public void testFindPrivilegeByName() {
    PrivilegeEntity result = new PrivilegeEntity();
    result.setName("user");
    when(privilegeRepository.findByName(eq("user"))).thenReturn(result);
    assertThat(privilegeService.findPrivilegeByName("user")).isEqualTo(result);
    verify(privilegeRepository).findByName(eq("user"));
    when(privilegeRepository.findByName(eq("admin"))).thenReturn(null);
    assertThat(privilegeService.findPrivilegeByName("admin")).isNull();
    verify(privilegeRepository).findByName(eq("admin"));
}
Also used : PrivilegeEntity(org.codenergic.theskeleton.privilege.PrivilegeEntity) Test(org.junit.Test)

Example 5 with PrivilegeEntity

use of org.codenergic.theskeleton.privilege.PrivilegeEntity in project theskeleton by codenergic.

the class RoleServiceTest method testAddPrivilegeToRole.

@Test
public void testAddPrivilegeToRole() {
    RoleEntity role = new RoleEntity().setId(UUID.randomUUID().toString()).setCode("role");
    PrivilegeEntity privilege = new PrivilegeEntity().setId(UUID.randomUUID().toString()).setName("privilege");
    RolePrivilegeEntity result = new RolePrivilegeEntity(role, privilege);
    result.setId(UUID.randomUUID().toString());
    when(roleRepository.findByCode("role")).thenReturn(role);
    when(privilegeRepository.findByName("privilege")).thenReturn(privilege);
    when(rolePrivilegeRepository.save(any(RolePrivilegeEntity.class))).thenReturn(result);
    assertThat(roleService.addPrivilegeToRole("role", "privilege")).isEqualTo(role);
    verify(roleRepository).findByCode("role");
    verify(privilegeRepository).findByName("privilege");
    verify(rolePrivilegeRepository).save(any(RolePrivilegeEntity.class));
}
Also used : PrivilegeEntity(org.codenergic.theskeleton.privilege.PrivilegeEntity) Test(org.junit.Test)

Aggregations

PrivilegeEntity (org.codenergic.theskeleton.privilege.PrivilegeEntity)6 Test (org.junit.Test)5 PageImpl (org.springframework.data.domain.PageImpl)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 EnableRestDocs (org.codenergic.theskeleton.core.test.EnableRestDocs)1 InjectUserDetailsService (org.codenergic.theskeleton.core.test.InjectUserDetailsService)1 PrivilegeRestData (org.codenergic.theskeleton.privilege.PrivilegeRestData)1 RoleEntity (org.codenergic.theskeleton.role.RoleEntity)1 RolePrivilegeEntity (org.codenergic.theskeleton.role.RolePrivilegeEntity)1 RunWith (org.junit.runner.RunWith)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)1