Search in sources :

Example 1 with RoleRestData

use of org.codenergic.theskeleton.role.RoleRestData 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)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Date (java.util.Date)1 HashSet (java.util.HashSet)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 RoleEntity (org.codenergic.theskeleton.role.RoleEntity)1 RoleRestData (org.codenergic.theskeleton.role.RoleRestData)1 TokenStoreService (org.codenergic.theskeleton.tokenstore.TokenStoreService)1 Test (org.junit.Test)1 RunWith (org.junit.runner.RunWith)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)1 Mockito.verify (org.mockito.Mockito.verify)1 Mockito.when (org.mockito.Mockito.when)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)1