Skip to content

Commit

Permalink
#18558 Fixing error (#18570)
Browse files Browse the repository at this point in the history
  • Loading branch information
freddyDOTCMS committed Jun 1, 2020
1 parent ddf4c1a commit 804bf0c
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 16 deletions.
@@ -1,5 +1,8 @@
package com.dotcms.rest.api.v1.page;

import com.dotcms.api.web.HttpServletRequestThreadLocal;
import com.dotmarketing.portlets.containers.business.FileAssetContainerUtil;
import com.dotmarketing.portlets.containers.model.FileAssetContainer;
import com.dotmarketing.portlets.htmlpageasset.business.render.*;
import com.dotcms.content.elasticsearch.business.ESSearchResults;
import com.dotcms.contenttype.business.ContentTypeAPI;
Expand Down Expand Up @@ -939,4 +942,82 @@ public void shouldKeepTheAParserContainerContentAfterLayoutSaved() throws DotDat
assertEquals(1, multiTrees.size());
assertEquals("1", multiTrees.get(0).getRelationType());
}

/**
* Method to test: {@link PageResource#saveLayout(HttpServletRequest, HttpServletResponse, PageForm)}
* Given Scenario: When a Template have a {@link FileAssetContainer}
* ExpectedResult: Should response with the absolute path
*
* @throws Exception
*/
@Test
public void shouldResponseWith() throws DotDataException, DotSecurityException, IOException {
HttpServletRequestThreadLocal.INSTANCE.setRequest(request);

final Folder folder = new FolderDataGen().site(host).nextPersisted();

final User systemUser = APILocator.systemUser();
final String modeParam = "PREVIEW_MODE";
when(request.getAttribute(WebKeys.PAGE_MODE_PARAMETER)).thenReturn(PageMode.get(modeParam));
when(request.getAttribute(com.liferay.portal.util.WebKeys.USER)).thenReturn(systemUser);

final Language defaultLang = APILocator.getLanguageAPI().getDefaultLanguage();
final long languageId = defaultLang.getId();
when(request.getAttribute(WebKeys.HTMLPAGE_LANGUAGE)).thenReturn(String.valueOf(languageId));
when(request.getParameter("host_id")).thenReturn(host.getIdentifier());

final String pageName = "testPage-"+System.currentTimeMillis();

final String testContainer = "/test-get-container" + System.currentTimeMillis();
Container container = new ContainerAsFileDataGen().host(host).folderName(testContainer).nextPersisted();

container = APILocator.getContainerAPI().find(container.getInode(), systemUser, true);

final Template template = new TemplateDataGen()
.withContainer(container.getIdentifier())
.nextPersisted();

final HTMLPageAsset page = new HTMLPageDataGen(folder, template)
.languageId(languageId)
.pageURL(pageName)
.title(pageName)
.nextPersisted();

page.setIndexPolicy(IndexPolicy.WAIT_FOR);
page.setIndexPolicyDependencies(IndexPolicy.WAIT_FOR);
page.setBoolProperty(Contentlet.IS_TEST_MODE, true);
APILocator.getContentletAPI().publish(page, systemUser, false);

when(initDataObject.getUser()).thenReturn(systemUser);

final Contentlet contentlet1 = new ContentletDataGen(contentGenericType.id())
.languageId(languageId)
.folder(folder)
.host(host)
.setProperty("title", "content1")
.setProperty("body", "content1")
.nextPersisted();

contentlet1.setIndexPolicy(IndexPolicy.WAIT_FOR);
contentlet1.setIndexPolicyDependencies(IndexPolicy.WAIT_FOR);
contentlet1.setBoolProperty(Contentlet.IS_TEST_MODE, true);
APILocator.getContentletAPI().publish(contentlet1, systemUser, false);

MultiTree multiTree = new MultiTree(page.getIdentifier(), ((FileAssetContainer) container).getPath(), contentlet1.getIdentifier(),"1",0);
APILocator.getMultiTreeAPI().saveMultiTree(multiTree);

final Response response = pageResource
.render(request, this.response, page.getURI(), modeParam, null,
String.valueOf(languageId), null);

final HTMLPageAssetRendered htmlPageAssetRendered = (HTMLPageAssetRendered) ((ResponseEntityView) response.getEntity()).getEntity();

assertEquals(1, htmlPageAssetRendered.getContainers().size());
final ContainerRaw containerRaw = htmlPageAssetRendered.getContainers().iterator().next();

assertEquals(
FileAssetContainerUtil.getInstance().getFullPath((FileAssetContainer) container),
containerRaw.getContainerView().getPath()
);
}
}
Expand Up @@ -22,6 +22,7 @@
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.factories.MultiTreeAPI;
import com.dotmarketing.factories.PersonalizedContentlet;
import com.dotmarketing.portlets.containers.business.FileAssetContainerUtil;
import com.dotmarketing.portlets.containers.model.Container;
import com.dotmarketing.portlets.containers.model.FileAssetContainer;
import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
Expand Down Expand Up @@ -285,7 +286,9 @@ private String getNewUUID(final PageForm pageForm, final String containerId,
final Container foundContainer = APILocator.getContainerAPI()
.getWorkingContainerById(containerId, userAPI.getSystemUser(), false);
if (foundContainer instanceof FileAssetContainer) {
containerPath = FileAssetContainer.class.cast(foundContainer).getPath();
containerPath = FileAssetContainerUtil.getInstance().getFullPath(
FileAssetContainer.class.cast(foundContainer)
);
}

if (ContainerUUID.UUID_DEFAULT_VALUE.equals(uniqueId)) {
Expand Down
Expand Up @@ -84,7 +84,7 @@ private PaginatedArrayList<ContainerView> createContainerView(
containerViews.setQuery(allContainers.getQuery());
containerViews.addAll(
allContainers.stream()
.map(container -> new ContainerView(container, currentHost))
.map(container -> new ContainerView(container))
.collect(Collectors.toList())
);
return containerViews;
Expand Down
Expand Up @@ -13,16 +13,13 @@
@JsonSerialize(using = ContainerViewSerializer.class)
public class ContainerView {
private final Container container;
private final Host host;

public ContainerView(final Container container, final Host host) {
public ContainerView(final Container container) {
this.container = container;
this.host = host;
}

/**
* If {@link ContainerView#container} is a {@link FileAssetContainer} then return the Container relative oath
* if the container is into {@link ContainerView#host} otherwise ir return the absolute path.
* If the container is not a {@link FileAssetContainer} then return null.
*
* @return
Expand All @@ -31,9 +28,7 @@ public String getPath(){
if (FileAssetContainerUtil.getInstance().isFileAssetContainer(container)) {
final FileAssetContainer fileAssetContainer = (FileAssetContainer) container;

return !host.getIdentifier().equals(container.getIdentifier()) ?
FileAssetContainerUtil.getInstance().getFullPath(fileAssetContainer) :
fileAssetContainer.getPath();
return FileAssetContainerUtil.getInstance().getFullPath(fileAssetContainer);
} else {
return null;
}
Expand All @@ -42,8 +37,4 @@ public String getPath(){
public Container getContainer() {
return container;
}

public Host getHost() {
return host;
}
}
Expand Up @@ -4,6 +4,11 @@
import java.util.List;
import java.util.Map;

import com.dotmarketing.beans.Host;
import com.dotmarketing.portlets.containers.model.ContainerView;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import com.dotmarketing.beans.ContainerStructure;
import com.dotmarketing.portlets.containers.model.Container;
Expand Down Expand Up @@ -31,7 +36,10 @@ public class ContainerRaw implements Serializable {
* @param container The {@link Container} in the HTML Page.
* @param containerStructures The list of {@link ContainerStructure} relationships. the browser.
*/
public ContainerRaw(final Container container, final List<ContainerStructure> containerStructures, final Map<String, List<Map<String,Object>>> contentlets) {
public ContainerRaw(
final Container container,
final List<ContainerStructure> containerStructures,
final Map<String, List<Map<String,Object>>> contentlets) {
this.container = container;
this.containerStructures = (containerStructures != null) ? ImmutableList.copyOf(containerStructures) : ImmutableList.of();
this.contentlets = contentlets;
Expand All @@ -46,10 +54,15 @@ public Map<String, List<Map<String,Object>>> getContentlets() {
*
* @return The {@link Container} in the page.
*/
@JsonIgnore
public Container getContainer() {
return container;
}

@JsonProperty("container")
public ContainerView getContainerView() {
return new ContainerView(container);
}

/**
* Returns the relationships that determine what Content Types can be added to a specific Container.
Expand Down
Expand Up @@ -47,8 +47,7 @@ public ContainerRendered(final Container container, final List<ContainerStructur
* the browser.
*/
public ContainerRendered(final ContainerRaw containerRaw, final Map<String, String> rendered) {
super(containerRaw.getContainer(), containerRaw.getContainerStructures(), containerRaw.getContentlets());
this.rendered = rendered;
this(containerRaw.getContainer(), containerRaw.getContainerStructures(), rendered, containerRaw.getContentlets());

}

Expand Down

0 comments on commit 804bf0c

Please sign in to comment.