Search in sources :

Example 26 with ResultActions

use of org.springframework.test.web.servlet.ResultActions in project theskeleton by codenergic.

the class PrivilegeRestControllerTest method testFindPrivilegeByName.

@Test
public void testFindPrivilegeByName() throws Exception {
    PrivilegeEntity dbResult = new PrivilegeEntity().setId("123").setName("12345").setDescription("Description 12345");
    when(privilegeService.findPrivilegeByIdOrName("123")).thenReturn(dbResult);
    ResultActions resultActions = mockMvc.perform(get("/api/privileges/123").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andDo(document("privilege-read"));
    MockHttpServletResponse response = resultActions.andReturn().getResponse();
    verify(privilegeService).findPrivilegeByIdOrName("123");
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(PrivilegeRestData.builder().fromPrivilegeEntity(dbResult).build()));
}
Also used : 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 27 with ResultActions

use of org.springframework.test.web.servlet.ResultActions in project theskeleton by codenergic.

the class RoleRestControllerTest method testRemovePrivilegeFromRole.

@Test
public void testRemovePrivilegeFromRole() throws Exception {
    final RoleEntity role = new RoleEntity().setId("role123").setCode("role123");
    when(roleService.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(roleService).removePrivilegeFromRole("role123", "privilege123");
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(RoleRestData.builder().fromRoleEntity(role).build()));
}
Also used : 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 28 with ResultActions

use of org.springframework.test.web.servlet.ResultActions 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 29 with ResultActions

use of org.springframework.test.web.servlet.ResultActions in project theskeleton by codenergic.

the class RoleRestControllerTest method testUpdateRole.

@Test
public void testUpdateRole() throws Exception {
    RoleEntity input = new RoleEntity().setId("123").setCode("12345").setDescription("Description 12345");
    RoleEntity dbResult = new RoleEntity().setId(UUID.randomUUID().toString()).setCode("12345").setDescription("Description 12345");
    byte[] jsonInput = objectMapper.writeValueAsBytes(RoleRestData.builder().fromRoleEntity(input).build());
    when(roleService.updateRole(eq("123"), any())).thenReturn(dbResult);
    ResultActions resultActions = mockMvc.perform(put("/api/roles/123").contentType(MediaType.APPLICATION_JSON).content(jsonInput)).andExpect(status().isOk()).andDo(document("role-update"));
    MockHttpServletResponse response = resultActions.andReturn().getResponse();
    verify(roleService).updateRole(eq("123"), any());
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(RoleRestData.builder().fromRoleEntity(dbResult).build()));
}
Also used : 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 30 with ResultActions

use of org.springframework.test.web.servlet.ResultActions in project theskeleton by codenergic.

the class UserRestControllerTest method testLockOrUnlockUser.

@Test
public void testLockOrUnlockUser() throws Exception {
    final UserEntity user = new UserEntity().setAccountNonLocked(true);
    when(userAdminService.lockOrUnlockUser("user123", true)).thenReturn(user);
    ResultActions resultActions = mockMvc.perform(put("/api/users/user123/lock").content("{\"enabled\": true}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andDo(document("user-lock"));
    MockHttpServletResponse response = resultActions.andReturn().getResponse();
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(UserRestData.builder().fromUserEntity(user).build()));
    verify(userAdminService).lockOrUnlockUser("user123", true);
}
Also used : 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)

Aggregations

ResultActions (org.springframework.test.web.servlet.ResultActions)600 Test (org.junit.Test)525 UserDetails (com.agiletec.aps.system.services.user.UserDetails)297 AbstractControllerIntegrationTest (org.entando.entando.web.AbstractControllerIntegrationTest)221 AbstractControllerTest (org.entando.entando.web.AbstractControllerTest)101 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)43 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)39 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)38 MvcResult (org.springframework.test.web.servlet.MvcResult)27 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)26 UUID (java.util.UUID)22 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)22 InputStream (java.io.InputStream)18 IEntityTypesConfigurer (com.agiletec.aps.system.common.entity.IEntityTypesConfigurer)17 HashMap (java.util.HashMap)17 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)16 Test (org.junit.jupiter.api.Test)15 WebIntegrationBaseTest (org.nextprot.api.web.dbunit.base.mvc.WebIntegrationBaseTest)14 IPage (com.agiletec.aps.system.services.page.IPage)13 Page (com.agiletec.aps.system.services.page.Page)12