Search in sources :

Example 1 with PrivilegeRestData

use of org.codenergic.theskeleton.privilege.PrivilegeRestData 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)

Aggregations

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 PrivilegeEntity (org.codenergic.theskeleton.privilege.PrivilegeEntity)1 PrivilegeRestData (org.codenergic.theskeleton.privilege.PrivilegeRestData)1 Test (org.junit.Test)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 Mockito.doNothing (org.mockito.Mockito.doNothing)1 Mockito.verify (org.mockito.Mockito.verify)1 Mockito.when (org.mockito.Mockito.when)1