-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into issue/CIF-2827
- Loading branch information
Showing
14 changed files
with
242 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...dobe/cq/commerce/core/components/internal/servlets/ShowNavRootRenderConditionServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
~ Copyright 2022 Adobe | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | ||
package com.adobe.cq.commerce.core.components.internal.servlets; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.Servlet; | ||
import javax.servlet.ServletException; | ||
|
||
import org.apache.sling.api.SlingHttpServletRequest; | ||
import org.apache.sling.api.SlingHttpServletResponse; | ||
import org.apache.sling.api.servlets.SlingSafeMethodsServlet; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
|
||
import com.adobe.cq.commerce.core.components.internal.services.site.SiteStructureFactory; | ||
import com.adobe.cq.commerce.core.components.models.common.SiteStructure; | ||
import com.adobe.granite.ui.components.rendercondition.RenderCondition; | ||
import com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition; | ||
import com.day.cq.wcm.api.Page; | ||
import com.day.cq.wcm.api.PageManager; | ||
|
||
/** | ||
* {@code ShowNavRootRenderConditionServlet} implements a {@code granite:rendercondition} used to determine if the navRoot option should be | ||
* displayed on the properties page of a page. | ||
*/ | ||
@Component( | ||
service = { Servlet.class }, | ||
property = { | ||
"sling.servlet.resourceTypes=" + ShowNavRootRenderConditionServlet.RESOURCE_TYPE, | ||
"sling.servlet.methods=GET", | ||
"sling.servlet.extensions=html" | ||
}) | ||
public class ShowNavRootRenderConditionServlet extends SlingSafeMethodsServlet { | ||
public final static String RESOURCE_TYPE = "core/cif/components/renderconditions/showNavRoot"; | ||
|
||
@Reference | ||
private SiteStructureFactory siteStructureFactory; | ||
|
||
@Override | ||
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) | ||
throws ServletException, IOException { | ||
request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(showNavRoot(request))); | ||
} | ||
|
||
private boolean showNavRoot(SlingHttpServletRequest request) { | ||
PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class); | ||
if (pageManager == null) { | ||
return false; | ||
} | ||
|
||
String pagePath = request.getParameter("item"); | ||
Page page = pageManager.getPage(pagePath); | ||
if (page == null) { | ||
return false; | ||
} | ||
|
||
SiteStructure siteStructure = siteStructureFactory.getSiteStructure(page); | ||
Page landingPage = siteStructure.getLandingPage(); | ||
|
||
// Show navRoot option if there is no landing page or if the current page is the landing page. | ||
return landingPage == null || landingPage.equals(page); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
.../cq/commerce/core/components/internal/servlets/ShowNavRootRenderConditionServletTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
~ Copyright 2022 Adobe | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | ||
package com.adobe.cq.commerce.core.components.internal.servlets; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.ServletException; | ||
|
||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.servlethelpers.MockSlingHttpServletRequest; | ||
import org.apache.sling.servlethelpers.MockSlingHttpServletResponse; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import com.adobe.granite.ui.components.rendercondition.RenderCondition; | ||
import com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition; | ||
import io.wcm.testing.mock.aem.junit.AemContext; | ||
|
||
import static com.adobe.cq.commerce.core.testing.TestContext.newAemContext; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class ShowNavRootRenderConditionServletTest { | ||
|
||
@Rule | ||
public final AemContext context = newAemContext("/context/SiteStructureImplTest/jcr-content.json"); | ||
|
||
private MockSlingHttpServletRequest request; | ||
private MockSlingHttpServletResponse response; | ||
private ShowNavRootRenderConditionServlet servlet; | ||
|
||
@Before | ||
public void setUp() { | ||
response = new MockSlingHttpServletResponse(); | ||
request = new MockSlingHttpServletRequest(context.resourceResolver()); | ||
Resource resource = mock(Resource.class); | ||
request.setResource(resource); | ||
request.setPathInfo("/mnt/overlay/wcm/core/content/sites/properties.html"); | ||
|
||
servlet = context.registerInjectActivateService(new ShowNavRootRenderConditionServlet()); | ||
} | ||
|
||
@Test | ||
public void testReturnsTrueForLandingPageEqualsPage() throws ServletException, IOException { | ||
request.setQueryString("item=/content/nav-root"); | ||
servlet.doGet(request, response); | ||
|
||
SimpleRenderCondition condition = (SimpleRenderCondition) request.getAttribute(RenderCondition.class.getName()); | ||
Assert.assertTrue(condition.check()); | ||
} | ||
|
||
@Test | ||
public void testReturnsTrueForNoLandingPage() throws ServletException, IOException { | ||
request.setQueryString("item=/content/no-nav-root"); | ||
servlet.doGet(request, response); | ||
|
||
SimpleRenderCondition condition = (SimpleRenderCondition) request.getAttribute(RenderCondition.class.getName()); | ||
Assert.assertTrue(condition.check()); | ||
} | ||
|
||
@Test | ||
public void testReturnsFalseForDefinedLandingPage() throws ServletException, IOException { | ||
request.setQueryString("item=/content/nav-root/content-page"); | ||
servlet.doGet(request, response); | ||
|
||
SimpleRenderCondition condition = (SimpleRenderCondition) request.getAttribute(RenderCondition.class.getName()); | ||
Assert.assertFalse(condition.check()); | ||
} | ||
|
||
@Test | ||
public void testReturnsFalseForInvalidPage() throws ServletException, IOException { | ||
request.setQueryString("item=/content/does-not-exist"); | ||
servlet.doGet(request, response); | ||
|
||
SimpleRenderCondition condition = (SimpleRenderCondition) request.getAttribute(RenderCondition.class.getName()); | ||
Assert.assertFalse(condition.check()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.