Search in sources :

Example 1 with PageStatusRequest

use of org.entando.entando.web.page.model.PageStatusRequest in project entando-core by entando.

the class PageControllerTest method shouldValidateStatusPutDraftRef.

@Test
public void shouldValidateStatusPutDraftRef() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    PageStatusRequest request = new PageStatusRequest();
    request.setStatus("draft");
    PageM pageToUnpublish = new PageM(true);
    pageToUnpublish.setCode("page_to_unpublish");
    pageToUnpublish.setParentCode("service");
    pageToUnpublish.setChildrenCodes(new String[] { "child_page" });
    PageM child = new PageM(true);
    child.setCode("child_page");
    child.setParentCode("page_to_unpublish");
    PageM root = new PageM(true);
    root.setCode("home");
    root.setParentCode("home");
    when(authorizationService.isAuth(any(UserDetails.class), any(String.class))).thenReturn(true);
    when(this.controller.getPageValidator().getPageManager().getDraftPage(any(String.class))).thenReturn(pageToUnpublish, child);
    when(this.controller.getPageValidator().getPageManager().getOnlineRoot()).thenReturn(root);
    when(this.controller.getPageValidator().getPageUtilizer().getPageUtilizers(any(String.class))).thenReturn(new ArrayList());
    ResultActions result = mockMvc.perform(put("/pages/{pageCode}/status", "page_to_publish").sessionAttr("user", user).content(convertObjectToJsonBytes(request)).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isBadRequest());
    String response = result.andReturn().getResponse().getContentAsString();
    System.out.println("RESPONSE: " + response);
    result.andExpect(jsonPath("$.errors", hasSize(1)));
    result.andExpect(jsonPath("$.errors[0].code", is(PageController.ERRCODE_REFERENCED_ONLINE_PAGE)));
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) ArrayList(java.util.ArrayList) ResultActions(org.springframework.test.web.servlet.ResultActions) PageStatusRequest(org.entando.entando.web.page.model.PageStatusRequest) AbstractControllerTest(org.entando.entando.web.AbstractControllerTest) Test(org.junit.Test)

Example 2 with PageStatusRequest

use of org.entando.entando.web.page.model.PageStatusRequest in project entando-core by entando.

the class PageValidator method validateReferences.

public void validateReferences(String pageCode, PageStatusRequest pageStatusRequest, Errors errors) {
    List<Content> contents = new ArrayList<>();
    List<String> invalidRefs = new ArrayList<>();
    IPage page = this.getPageManager().getDraftPage(pageCode);
    try {
        List<String> contentIds = this.getPageUtilizer().getPageUtilizers(pageCode);
        Optional.ofNullable(contentIds).ifPresent(ids -> ids.forEach(id -> {
            try {
                contents.add(this.getContentManager().loadContent(id, true));
            } catch (ApsSystemException e) {
                logger.error("Error loadingreferences for page {}", pageCode, e);
                throw new RestServerError("Error loadingreferences for page", e);
            }
        }));
    } catch (ApsSystemException e) {
        logger.error("Error loadingreferences for page {}", pageCode, e);
        throw new RestServerError("Error loadingreferences for page", e);
    }
    if (pageStatusRequest.getStatus().equals(IPageService.STATUS_ONLINE)) {
        contents.stream().forEach(content -> {
            if (!content.isOnLine()) {
                invalidRefs.add(content.getId());
            }
        });
        IPage parent = null;
        if (invalidRefs.isEmpty()) {
            parent = this.getPageManager().getDraftPage(page.getParentCode());
        }
        if (!invalidRefs.isEmpty() || !parent.isOnline()) {
            errors.reject(PageController.ERRCODE_REFERENCED_DRAFT_PAGE, new String[] { pageCode }, "page.status.invalid.draft.ref");
        }
    } else {
        boolean isRoot = this.getPageManager().getOnlineRoot().getCode().equals(pageCode);
        if (contents.isEmpty() && !isRoot) {
            Optional.ofNullable(page.getChildrenCodes()).ifPresent(children -> Arrays.asList(children).forEach(child -> {
                IPage childPage = this.getPageManager().getDraftPage(child);
                if (childPage.isOnline()) {
                    invalidRefs.add(child);
                }
            }));
        }
        if (isRoot || !contents.isEmpty() || !invalidRefs.isEmpty()) {
            errors.reject(PageController.ERRCODE_REFERENCED_ONLINE_PAGE, new String[] { pageCode }, "page.status.invalid.online.ref");
        }
    }
}
Also used : Arrays(java.util.Arrays) Errors(org.springframework.validation.Errors) Validator(org.springframework.validation.Validator) PageRequest(org.entando.entando.web.page.model.PageRequest) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) PageStatusRequest(org.entando.entando.web.page.model.PageStatusRequest) PageController(org.entando.entando.web.page.PageController) LoggerFactory(org.slf4j.LoggerFactory) Content(com.agiletec.plugins.jacms.aps.system.services.content.model.Content) Autowired(org.springframework.beans.factory.annotation.Autowired) IContentManager(com.agiletec.plugins.jacms.aps.system.services.content.IContentManager) PageUtilizer(com.agiletec.aps.system.services.page.PageUtilizer) StringUtils(org.apache.commons.lang3.StringUtils) Group(com.agiletec.aps.system.services.group.Group) ArrayList(java.util.ArrayList) IPage(com.agiletec.aps.system.services.page.IPage) IPageService(org.entando.entando.aps.system.services.page.IPageService) List(java.util.List) Component(org.springframework.stereotype.Component) RestServerError(org.entando.entando.aps.system.exception.RestServerError) IPageManager(com.agiletec.aps.system.services.page.IPageManager) PagePositionRequest(org.entando.entando.web.page.model.PagePositionRequest) Optional(java.util.Optional) IPage(com.agiletec.aps.system.services.page.IPage) Content(com.agiletec.plugins.jacms.aps.system.services.content.model.Content) RestServerError(org.entando.entando.aps.system.exception.RestServerError) ArrayList(java.util.ArrayList) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException)

Example 3 with PageStatusRequest

use of org.entando.entando.web.page.model.PageStatusRequest in project entando-core by entando.

the class PageServiceIntegrationTest method testUpdatePageStatus.

@Test
public void testUpdatePageStatus() {
    PageDto pageToClone = pageService.getPage("pagina_11", "draft");
    assertNotNull(pageToClone);
    PageRequest pageRequest = this.createRequestFromDto(pageToClone);
    pageRequest.setCode("pagina_13");
    PageDto addedPage = pageService.addPage(pageRequest);
    assertNotNull(addedPage);
    assertEquals("pagina_13", addedPage.getCode());
    assertEquals("pagina_1", addedPage.getParentCode());
    addedPage = pageService.getPage("pagina_13", "draft");
    assertEquals("draft", addedPage.getStatus());
    PageStatusRequest pageStatusRequest = new PageStatusRequest();
    pageStatusRequest.setStatus("published");
    PageDto modPage = pageService.updatePageStatus("pagina_13", pageStatusRequest.getStatus());
    assertNotNull(modPage);
    assertEquals("published", modPage.getStatus());
    addedPage = pageService.getPage("pagina_13", "published");
    assertNotNull(addedPage);
    assertEquals("published", addedPage.getStatus());
    pageService.removePage("pagina_13");
}
Also used : PageRequest(org.entando.entando.web.page.model.PageRequest) PageDto(org.entando.entando.aps.system.services.page.model.PageDto) PageStatusRequest(org.entando.entando.web.page.model.PageStatusRequest) Test(org.junit.Test)

Example 4 with PageStatusRequest

use of org.entando.entando.web.page.model.PageStatusRequest in project entando-core by entando.

the class PageControllerTest method shouldValidateStatusPutOnlineRef.

@Test
public void shouldValidateStatusPutOnlineRef() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    PageStatusRequest request = new PageStatusRequest();
    request.setStatus("published");
    PageM pageToPublish = new PageM(false);
    pageToPublish.setCode("page_to_publish");
    pageToPublish.setParentCode("unpublished");
    PageM unpublished = new PageM(false);
    unpublished.setCode("unpublished");
    unpublished.setParentCode("service");
    when(authorizationService.isAuth(any(UserDetails.class), any(String.class))).thenReturn(true);
    when(this.controller.getPageValidator().getPageManager().getDraftPage(any(String.class))).thenReturn(pageToPublish, unpublished);
    when(this.controller.getPageValidator().getPageUtilizer().getPageUtilizers(any(String.class))).thenReturn(new ArrayList());
    ResultActions result = mockMvc.perform(put("/pages/{pageCode}/status", "page_to_publish").sessionAttr("user", user).content(convertObjectToJsonBytes(request)).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isBadRequest());
    String response = result.andReturn().getResponse().getContentAsString();
    System.out.println("RESPONSE: " + response);
    result.andExpect(jsonPath("$.errors", hasSize(1)));
    result.andExpect(jsonPath("$.errors[0].code", is(PageController.ERRCODE_REFERENCED_DRAFT_PAGE)));
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) ArrayList(java.util.ArrayList) ResultActions(org.springframework.test.web.servlet.ResultActions) PageStatusRequest(org.entando.entando.web.page.model.PageStatusRequest) AbstractControllerTest(org.entando.entando.web.AbstractControllerTest) Test(org.junit.Test)

Aggregations

PageStatusRequest (org.entando.entando.web.page.model.PageStatusRequest)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 UserDetails (com.agiletec.aps.system.services.user.UserDetails)2 AbstractControllerTest (org.entando.entando.web.AbstractControllerTest)2 PageRequest (org.entando.entando.web.page.model.PageRequest)2 ResultActions (org.springframework.test.web.servlet.ResultActions)2 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)1 Group (com.agiletec.aps.system.services.group.Group)1 IPage (com.agiletec.aps.system.services.page.IPage)1 IPageManager (com.agiletec.aps.system.services.page.IPageManager)1 PageUtilizer (com.agiletec.aps.system.services.page.PageUtilizer)1 IContentManager (com.agiletec.plugins.jacms.aps.system.services.content.IContentManager)1 Content (com.agiletec.plugins.jacms.aps.system.services.content.model.Content)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 StringUtils (org.apache.commons.lang3.StringUtils)1 RestServerError (org.entando.entando.aps.system.exception.RestServerError)1 IPageService (org.entando.entando.aps.system.services.page.IPageService)1