Search in sources :

Example 31 with ResultActions

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

the class UserRestControllerTest method testRemoveRoleFromUser.

@Test
public void testRemoveRoleFromUser() throws Exception {
    final UserEntity user = new UserEntity().setId("user123");
    when(userAdminService.removeRoleFromUser("user123", "role123")).thenReturn(user);
    ResultActions resultActions = mockMvc.perform(delete("/api/users/user123/roles").content("{\"role\": \"role123\"}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andDo(document("user-role-delete"));
    MockHttpServletResponse response = resultActions.andReturn().getResponse();
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(UserRestData.builder().fromUserEntity(user).build()));
    verify(userAdminService).removeRoleFromUser("user123", "role123");
}
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 32 with ResultActions

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

the class UserRestControllerTest method testUpdateUserPassword.

@Test
public void testUpdateUserPassword() throws Exception {
    final UserEntity user = new UserEntity().setId("user123");
    when(userAdminService.updateUserPassword(eq("user123"), any())).thenReturn(user);
    ResultActions resultActions = mockMvc.perform(put("/api/users/user123/password").content("{\"username\": \"user123\"}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andDo(document("user-update-password"));
    MockHttpServletResponse response = resultActions.andReturn().getResponse();
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(UserRestData.builder().fromUserEntity(user).build()));
    verify(userAdminService).updateUserPassword(eq("user123"), any());
}
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 33 with ResultActions

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

the class UserRestControllerTest method testFindUsersByUsernameStartingWith.

@Test
public void testFindUsersByUsernameStartingWith() throws Exception {
    final UserEntity user = new UserEntity().setId("user123").setEmail("user@server");
    final Page<UserEntity> users = new PageImpl<>(Arrays.asList(user));
    when(userAdminService.findUsersByUsernameStartingWith(eq("user123"), any())).thenReturn(users);
    ResultActions resultActions = mockMvc.perform(get("/api/users?username=user123").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andDo(document("user-read-username-startingwith"));
    MockHttpServletResponse response = resultActions.andReturn().getResponse();
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(users.map(u -> UserRestData.builder().fromUserEntity(u).build())));
    verify(userAdminService).findUsersByUsernameStartingWith(eq("user123"), any());
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) 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 34 with ResultActions

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

the class UserRestControllerTest method testExtendsUserExpiration.

@Test
public void testExtendsUserExpiration() throws Exception {
    final UserEntity user = new UserEntity().setExpiredAt(new Date());
    when(userAdminService.extendsUserExpiration("user123", 60)).thenReturn(user);
    ResultActions resultActions = mockMvc.perform(put("/api/users/user123/exp").content("{\"amount\": 60}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andDo(document("user-extend-exp"));
    MockHttpServletResponse response = resultActions.andReturn().getResponse();
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(UserRestData.builder().fromUserEntity(user).build()));
    verify(userAdminService).extendsUserExpiration("user123", 60);
}
Also used : ResultActions(org.springframework.test.web.servlet.ResultActions) Date(java.util.Date) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 35 with ResultActions

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

the class UserRestControllerTest method testEnableOrDisableUser.

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