Java Code Examples for org.springframework.test.web.servlet.ResultActions#andExpect()

The following examples show how to use org.springframework.test.web.servlet.ResultActions#andExpect() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContentControllerTest.java    From entando-components with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testUpdateContent() throws Exception {
    UserDetails user = this.createUser(true);
    when(this.httpSession.getAttribute("user")).thenReturn(user);
    when(this.contentService.updateContent(Mockito.any(ContentDto.class), Mockito.any(UserDetails.class), Mockito.any(BindingResult.class)))
            .thenReturn(Mockito.mock(ContentDto.class));
    String mockJson = "{\n"
            + "    \"id\": \"ART123\",\n"
            + "    \"typeCode\": \"ART\",\n"
            + "    \"attributes\": [\n"
            + "         {\"code\": \"code1\", \"value\": \"value1\"},\n"
            + "         {\"code\": \"code2\", \"value\": \"value2\"}\n"
            + "    ]}";
    ResultActions result = this.performPutContent("user", mockJson, user);
    result.andExpect(status().isOk());
}
 
Example 2
Source File: ContentModelControllerIntegrationTest.java    From entando-components with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testGetContentModelsSortId() throws Exception {

    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    ResultActions result = mockMvc
            .perform(get(BASE_URI)
                    .param("sort", "id")
                    .header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isOk());
    result.andExpect(jsonPath("$.payload[0].id", is(1)));

    result = mockMvc
            .perform(get(BASE_URI)
                    .param("direction", FieldSearchFilter.DESC_ORDER)
                    .param("sort", "id")
                    .header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isOk());
    result.andExpect(jsonPath("$.payload[0].id", is(11)));

}
 
Example 3
Source File: LanguageControllerIntegrationTest.java    From entando-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testDeactivateDefaultLangUnexistingCode() throws Exception {
    String langCode = "xx";

    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);

    String payload = "{\"isActive\": true}";
    ResultActions result = mockMvc
            .perform(put("/languages/{code}", new Object[]{langCode})
                    .content(payload)
                    .contentType(MediaType.APPLICATION_JSON_VALUE)
                    .header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isNotFound());

    result.andExpect(jsonPath("$.errors[0].code", is("2")));

}
 
Example 4
Source File: CategoryControllerIntegrationTest.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testGetInvalidCategoryTree() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    this.executeGet("invalid_code", accessToken, status().isNotFound());
    ResultActions result = mockMvc
            .perform(get("/categories")
                    .param("parentCode", "invalid_code")
                    .header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isNotFound());
}
 
Example 5
Source File: WardsTest.java    From vics with MIT License 5 votes vote down vote up
@Test
public void returnsStreetsByWard() throws Exception {
    String wardCode = "E05001221";
    String endpoint = "/ward/" + wardCode + "/street";
    pafApiStub.willReturnStreetsByWard(wardCode);
    when(sessionService.extractUserFromPrincipal(any(Principal.class)))
            .thenReturn(Try.success(earlsdon()));

    ResultActions resultActions = mockMvc.perform(get(endpoint)
            .accept(MediaType.APPLICATION_JSON))
            .andDo(print())
            .andExpect(status().isOk());

    resultActions.andExpect(status().isOk());
}
 
Example 6
Source File: ContentVersioningControllerTest.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testGetContentsVersioning() throws Exception {
    PagedMetadata<ContentVersionDTO> pagedMetadata = getContentVersionDTOPagedMetadata();
    UserDetails user = this.createUser(true);
    when(this.httpSession.getAttribute("user")).thenReturn(user);
    when(this.service.getLatestVersions(Mockito.any(RestListRequest.class))).thenReturn(pagedMetadata);
    ResultActions result = getListLatestVersions(user);
    result.andExpect(status().isOk());
}
 
Example 7
Source File: FileBrowserControllerIntegrationTest.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
private ResultActions executeDirectoryPost(String body, String accessToken, ResultMatcher expected) throws Exception {
    ResultActions result = mockMvc
            .perform(post("/fileBrowser/directory")
                    .content(body).contentType(MediaType.APPLICATION_JSON_VALUE)
                    .header("Authorization", "Bearer " + accessToken));
    result.andExpect(expected);
    return result;
}
 
Example 8
Source File: UserSettingsControllerUnitTest.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testUpdateSettingsWithEmptyParams() throws Throwable {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);

    UserSettingsRequest userSettingsRequest = new UserSettingsRequest();

    ResultActions result = mockMvc.perform(
            put("/userSettings")
                    .content(mapper.writeValueAsString(userSettingsRequest))
                    .contentType(MediaType.APPLICATION_JSON_VALUE)
                    .header("Authorization", "Bearer " + accessToken));

    result.andExpect(status().isBadRequest());
}
 
Example 9
Source File: ContentVersioningControllerTest.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testDeleteContentVersion() throws Exception {
    ContentDto content = new ContentDto();
    UserDetails user = this.createUser(true);
    when(this.httpSession.getAttribute("user")).thenReturn(user);
    when(this.validator.checkContentIdForVersion(CONTENT_ID, VERSION_ID)).thenReturn(true);
    when(this.service.getContent(Mockito.eq(VERSION_ID))).thenReturn(content);
    ResultActions result = deleteContentVersion(CONTENT_ID , VERSION_ID, user);
    result.andExpect(status().isOk());
}
 
Example 10
Source File: ContentModelControllerIntegrationTest.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testGetContentModelDictionary() throws Exception {

    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    ResultActions result = mockMvc
            .perform(get(BASE_URI + "/dictionary")
                    .header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isOk());

}
 
Example 11
Source File: ContentVersioningControllerTest.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testGetContentVersion() throws Exception {
    ContentDto content = new ContentDto();
    UserDetails user = this.createUser(true);
    when(this.httpSession.getAttribute("user")).thenReturn(user);
    when(this.validator.checkContentIdForVersion(CONTENT_ID, VERSION_ID)).thenReturn(true);
    when(this.service.getContent(Mockito.eq(VERSION_ID))).thenReturn(content);
    ResultActions result = getContentVersion(CONTENT_ID , VERSION_ID, user);
    result.andExpect(status().isOk());
}
 
Example 12
Source File: PageConfigurationControllerIntegrationTest.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Given: a page only in draft When: the user request the configuration for
 * status draft Then the result is ok
 *
 * Given: a page only in draft When: the user request the configuration for
 * status published Then an error with status code 400 is raised
 *
 * @throws Exception
 */
@Test
public void testGetPageConfigurationOnLineNotFound() throws Exception {
    String pageCode = "draft_page_100";
    try {
        Page mockPage = createPage(pageCode, null);
        this.pageManager.addPage(mockPage);
        IPage onlinePage = this.pageManager.getOnlinePage(pageCode);
        assertThat(onlinePage, is(nullValue()));
        IPage draftPage = this.pageManager.getDraftPage(pageCode);
        assertThat(draftPage, is(not(nullValue())));

        UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
        String accessToken = mockOAuthInterceptor(user);

        ResultActions result = mockMvc
                .perform(get("/pages/{pageCode}/configuration", new Object[]{pageCode})
                        .param("status", IPageService.STATUS_DRAFT)
                        .header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());

        result = mockMvc
                .perform(get("/pages/{pageCode}/configuration", new Object[]{pageCode})
                        .param("status", IPageService.STATUS_ONLINE)
                        .header("Authorization", "Bearer " + accessToken));

        result.andExpect(status().isBadRequest());
        result.andExpect(jsonPath("$.errors[0].code", is("3")));
    } finally {
        this.pageManager.deletePage(pageCode);
    }

}
 
Example 13
Source File: GuiFragmentControllerTest.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void should_be_unauthorized() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24")
            .withGroup(Group.FREE_GROUP_NAME).build();
    String accessToken = mockOAuthInterceptor(user);
    ResultActions result = mockMvc.perform(get("/fragments")
            .header("Authorization", "Bearer " + accessToken)
    );
    String response = result.andReturn().getResponse().getContentAsString();
    result.andExpect(status().isForbidden());
}
 
Example 14
Source File: GuiFragmentSettingsControllerIntegrationTest.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testUpdateConfiguration() throws Exception {
    String value = this.configManager.getParam(SystemConstants.CONFIG_PARAM_EDIT_EMPTY_FRAGMENT_ENABLED);
    Assert.assertTrue(null == value || value.equalsIgnoreCase("false"));
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    try {
        String payload = "{\"enableEditingWhenEmptyDefaultGui\":true}";
        ResultActions result = this.executePut(payload, accessToken, status().isOk());
        result.andExpect(jsonPath("$.errors", Matchers.hasSize(0)));
        result.andExpect(jsonPath("$.payload." + GuiFragmentSettingsController.RESULT_PARAM_NAME, is(true)));
        value = this.configManager.getParam(SystemConstants.CONFIG_PARAM_EDIT_EMPTY_FRAGMENT_ENABLED);
        Assert.assertEquals("true", value);

        payload = "{\"enableEditingWhenEmptyDefaultGui\":false}";
        result = this.executePut(payload, accessToken, status().isOk());
        result.andExpect(jsonPath("$.errors", Matchers.hasSize(0)));
        result.andExpect(jsonPath("$.payload." + GuiFragmentSettingsController.RESULT_PARAM_NAME, is(false)));
        value = this.configManager.getParam(SystemConstants.CONFIG_PARAM_EDIT_EMPTY_FRAGMENT_ENABLED);
        Assert.assertEquals("false", value);

    } catch (Exception e) {
        throw e;
    } finally {
        this.configManager.updateParam(SystemConstants.CONFIG_PARAM_EDIT_EMPTY_FRAGMENT_ENABLED, "false");
        value = this.configManager.getParam(SystemConstants.CONFIG_PARAM_EDIT_EMPTY_FRAGMENT_ENABLED);
        Assert.assertEquals("false", value);
    }
}
 
Example 15
Source File: ProfileTypeControllerIntegrationTest.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testGetUserProfileAttributeTypes_2() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    ResultActions result = mockMvc
            .perform(get("/profileTypeAttributes").param("pageSize", "5")
                    .param("sort", "code").param("direction", "DESC")
                    .header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isOk());
    result.andExpect(jsonPath("$.payload", Matchers.hasSize(5)));
    result.andExpect(jsonPath("$.metaData.pageSize", is(5)));
    result.andExpect(jsonPath("$.metaData.lastPage", is(3)));
    result.andExpect(jsonPath("$.metaData.totalItems", is(15)));
    result.andExpect(jsonPath("$.payload[0]", is("Timestamp")));
}
 
Example 16
Source File: DatabaseControllerTest.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void getReport_2() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    when(databaseManager.getBackupReport(ArgumentMatchers.anyString())).thenReturn(null);
    ResultActions result = mockMvc.perform(
            get("/database/report/{reportCode}", new Object[]{"develop"})
            .header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isNotFound());
}
 
Example 17
Source File: KisiControllerTest.java    From spring-examples with GNU General Public License v3.0 5 votes vote down vote up
@Test
void whenInvalidInput_thenReturns400() throws Exception {
    // given
    KisiDto kisi = KisiDto.builder().soyadi("temel").build();

    // when
    ResultActions actions = mockMvc.perform(post("/kisi")
            .contentType(CONTENT_TYPE)
            .content(objectMapper.writeValueAsString(kisi)));

    // then
    actions.andExpect(status().isBadRequest());
}
 
Example 18
Source File: GuiFragmentSettingsControllerTest.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
private ResultActions executePut(String body, String accessToken, ResultMatcher rm) throws Exception {
    ResultActions result = mockMvc
            .perform(put("/fragmentsSettings").content(body)
                    .contentType(MediaType.APPLICATION_JSON_VALUE)
                    .header("Authorization", "Bearer " + accessToken));
    result.andExpect(rm);
    return result;
}
 
Example 19
Source File: UserControllerIntegrationTest.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testAddUserAuthorities_2() throws Exception {
    Group group = createGroup(1);
    Role role = createRole(1);
    String username = "mockuser_1";
    UserDetails mockuser = this.createUser(username);
    try {
        this.groupManager.addGroup(group);
        this.roleManager.addRole(role);
        this.userManager.addUser(mockuser);
        UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
        String accessToken = mockOAuthInterceptor(user);
        String mockJson1 = "[{\"group\":\"group1\", \"role\":\"role1\"}]";
        ResultActions result1 = mockMvc.perform(
                post("/users/{target}/authorities", username)
                        .content(mockJson1).contentType(MediaType.APPLICATION_JSON)
                        .header("Authorization", "Bearer " + accessToken));
        result1.andExpect(status().isOk());
        result1.andExpect(jsonPath("$.payload[0].group", is("group1")));

        List<Authorization> auths = this.authorizationManager.getUserAuthorizations(username);
        Assert.assertEquals(1, auths.size());

        String mockJson2 = "[{\"group\":\"customers\", \"role\":\"supervisor\"}]";
        ResultActions result2 = mockMvc.perform(
                post("/users/{target}/authorities", username)
                        .content(mockJson2).contentType(MediaType.APPLICATION_JSON)
                        .header("Authorization", "Bearer " + accessToken));
        result2.andExpect(status().isOk());
        result2.andExpect(jsonPath("$.payload[0].group", is("customers")));

        auths = this.authorizationManager.getUserAuthorizations(username);
        Assert.assertEquals(2, auths.size());

        ResultActions result3 = mockMvc.perform(
                get("/users/{target}/authorities", username)
                        .contentType(MediaType.APPLICATION_JSON)
                        .header("Authorization", "Bearer " + accessToken));
        result3.andExpect(status().isOk());
        result3.andExpect(jsonPath("$.payload", Matchers.hasSize(2)));

        String mockJson4 = "[{\"group\":\"helpdesk\", \"role\":\"pageManager\"}]";

        ResultActions result4 = mockMvc.perform(
                put("/users/{target}/authorities", username)
                        .content(mockJson4).contentType(MediaType.APPLICATION_JSON)
                        .header("Authorization", "Bearer " + accessToken));
        result4.andExpect(status().isOk());
        result4.andExpect(jsonPath("$.payload[0].group", is("helpdesk")));

        auths = this.authorizationManager.getUserAuthorizations(username);
        Assert.assertEquals(1, auths.size());
        Assert.assertEquals("helpdesk", auths.get(0).getGroup().getName());

        String mockJson5 = "[{\"group\":\"wrong_group\", \"role\":\"pageManager\"}]";
        ResultActions result5 = mockMvc.perform(
                put("/users/{target}/authorities", username)
                        .content(mockJson5).contentType(MediaType.APPLICATION_JSON)
                        .header("Authorization", "Bearer " + accessToken));
        result5.andExpect(status().isBadRequest());
        result5.andExpect(jsonPath("$.errors.size()", is(1)));
        result5.andExpect(jsonPath("$.errors[0].code", is("2")));

        auths = this.authorizationManager.getUserAuthorizations(username);
        Assert.assertEquals(1, auths.size());
        Assert.assertEquals("helpdesk", auths.get(0).getGroup().getName());

    } finally {
        this.authorizationManager.deleteUserAuthorizations(username);
        this.groupManager.removeGroup(group);
        this.roleManager.removeRole(role);
        this.userManager.removeUser(username);
    }
}
 
Example 20
Source File: ProfileTypeControllerIntegrationTest.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testDeleteUserProfileAttribute() throws Exception {
    String typeCode = "TST";
    try {
        Assert.assertNull(this.userProfileManager.getEntityPrototype(typeCode));
        UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
        String accessToken = mockOAuthInterceptor(user);

        this.executeProfileTypePost("2_POST_valid.json", accessToken, status().isOk());
        IApsEntity profileType = this.userProfileManager.getEntityPrototype(typeCode);
        Assert.assertEquals(3, profileType.getAttributeList().size());
        Assert.assertNotNull(profileType.getAttribute("list"));

        ResultActions result1 = this.executeProfileAttributeDelete("wrongCode", "list_wrong", accessToken, status().isNotFound());
        result1.andExpect(jsonPath("$.payload", Matchers.hasSize(0)));
        result1.andExpect(jsonPath("$.errors", Matchers.hasSize(1)));
        result1.andExpect(jsonPath("$.errors[0].code", is("1")));
        result1.andExpect(jsonPath("$.metaData.size()", is(0)));

        ResultActions result2 = this.executeProfileAttributeDelete(typeCode, "list_wrong", accessToken, status().isOk());
        result2.andExpect(jsonPath("$.payload.profileTypeCode", is(typeCode)));
        result2.andExpect(jsonPath("$.payload.attributeCode", is("list_wrong")));
        result2.andExpect(jsonPath("$.errors", Matchers.hasSize(0)));
        result2.andExpect(jsonPath("$.metaData.size()", is(0)));

        profileType = this.userProfileManager.getEntityPrototype(typeCode);
        Assert.assertEquals(3, profileType.getAttributeList().size());

        ResultActions result3 = this.executeProfileAttributeDelete(typeCode, "list", accessToken, status().isOk());
        result3.andExpect(jsonPath("$.payload.profileTypeCode", is(typeCode)));
        result3.andExpect(jsonPath("$.payload.attributeCode", is("list")));
        result3.andExpect(jsonPath("$.errors", Matchers.hasSize(0)));
        result3.andExpect(jsonPath("$.metaData.size()", is(0)));

        profileType = this.userProfileManager.getEntityPrototype(typeCode);
        Assert.assertEquals(2, profileType.getAttributeList().size());
        Assert.assertNull(profileType.getAttribute("list"));
    } finally {
        if (null != this.userProfileManager.getEntityPrototype(typeCode)) {
            ((IEntityTypesConfigurer) this.userProfileManager).removeEntityPrototype(typeCode);
        }
    }
}