Skip to content

Commit

Permalink
Add DM support to Teaser component (adobe#2350)
Browse files Browse the repository at this point in the history
- provide servlet to include asset tab from image component into the the teaser dialog. In this case the cqDesign request attribute needs to be reset to the imageDelegate component
- move the DM settings from the metadata tab to the asset tap in the image dialog, so it is still only required to include the asset tab within the teaser dialog

relates to adobe#1473
  • Loading branch information
bpauli authored Nov 7, 2022
1 parent 9e09a40 commit d291222
Show file tree
Hide file tree
Showing 31 changed files with 5,373 additions and 401 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/maven-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,26 @@ jobs:
# Run code coverage check
- name: Run core coverage check
run: bash <(curl -s https://codecov.io/bash)
build-js:
runs-on: ubuntu-latest
defaults:
run:
working-directory: content
strategy:
matrix:
node-version: [ 14 ]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
cache-dependency-path: content/package-lock.json
- name: Install dependencies
run: npm install
- name: Run lint
run: npm run eslint
- name: Run tests
run: npm test -- --no-watch --no-progress --browsers=ChromeHeadlessCI --singleRun=true
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ jcr.log
atlassian-ide-plugin.xml
.vlt-sync.log
.vlt-sync-config.properties
node
node_modules

core.wcm.components.commons.datalayer.*

# JS items
**/coverage/**
node
**/node_modules/**
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ 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.wcm.core.components.internal.servlets;

import java.io.IOException;
import java.util.Objects;
import java.util.Optional;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.SyntheticResource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import com.adobe.cq.wcm.core.components.internal.models.v1.AbstractImageDelegatingModel;
import com.adobe.granite.ui.components.ExpressionResolver;
import com.day.cq.wcm.api.WCMFilteringResourceWrapper;
import com.day.cq.wcm.api.components.ComponentManager;

@Component(service = { Servlet.class })
@SlingServletResourceTypes(
resourceTypes = ImageDelegatePolicyServlet.RESOURCE_TYPE
)
public class ImageDelegatePolicyServlet extends SlingSafeMethodsServlet {

public static final String RESOURCE_TYPE = "core/wcm/components/include/imagedelegate";
private static final String PN_PATH = "path";

@Reference
ExpressionResolver expressionResolver;

@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response)
throws ServletException, IOException {
ResourceResolver resourceResolver = request.getResourceResolver();
ComponentManager componentManager = resourceResolver.adaptTo(ComponentManager.class);
RequestPathInfo requestPathInfo = request.getRequestPathInfo();
com.day.cq.wcm.api.components.Component component =
Optional.ofNullable(requestPathInfo.getSuffix())
.flatMap(s -> Optional.ofNullable(resourceResolver.getResource(s)))
.flatMap(r -> Optional.ofNullable(componentManager)
.map(c -> c.getComponentOfResource(r)))
.orElse(null);
if (Objects.nonNull(component)) {
ValueMap properties = component.getProperties();
String imageDelegate = properties.get(AbstractImageDelegatingModel.IMAGE_DELEGATE, String.class);
RequestDispatcher requestDispatcher = Optional.ofNullable(request.getResource().getValueMap().get(PN_PATH
, String.class))
.map(p -> new WCMFilteringResourceWrapper(resourceResolver.getResource(p), new SyntheticResource(resourceResolver,
requestPathInfo.getSuffix(),
imageDelegate), expressionResolver, request)).map(request::getRequestDispatcher).orElse(null);
if (Objects.nonNull(requestDispatcher)) {
requestDispatcher.include(request, response);
} else {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
} else {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ 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.wcm.core.components.internal.servlets;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import org.apache.http.HttpStatus;
import org.apache.sling.api.request.RequestDispatcherOptions;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.testing.mock.sling.servlet.MockRequestDispatcherFactory;
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.adobe.cq.wcm.core.components.context.CoreComponentTestContext;
import com.adobe.granite.ui.components.ExpressionCustomizer;
import com.day.cq.wcm.api.designer.Style;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.verify;

@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class ImageDelegatePolicyServletTest {

private static final String TEST_BASE = "/image-delegate-policy-servlet";
private static final String APPS_ROOT = "/apps/core/wcm/components";
private static final String CONTENT_ROOT = "/content";
private static final String SUFFIX = "/content/test/jcr:content/root/responsivegrid/teaser";

public final AemContext context = CoreComponentTestContext.newAemContext();
private ImageDelegatePolicyServlet underTest;

@Mock
private RequestDispatcher requestDispatcher;

@BeforeEach
void setUp() {
context.load().json(TEST_BASE + CoreComponentTestContext.TEST_CONTENT_JSON, CONTENT_ROOT);
context.load().json(TEST_BASE + CoreComponentTestContext.TEST_APPS_JSON, APPS_ROOT);
underTest = new ImageDelegatePolicyServlet();
}

@Test
void testCqDesignAttribute() throws ServletException, IOException {
context.requestPathInfo().setSuffix(SUFFIX);
context.currentResource("/apps/core/wcm/components/teaser/cq:design/content/items/tabs/items/image");
MockSlingHttpServletRequest request = context.request();
request.setRequestDispatcherFactory(new MockRequestDispatcherFactory() {
@Override
public RequestDispatcher getRequestDispatcher(String s, RequestDispatcherOptions requestDispatcherOptions) {
return requestDispatcher;
}

@Override
public RequestDispatcher getRequestDispatcher(Resource resource, RequestDispatcherOptions requestDispatcherOptions) {
return requestDispatcher;
}
});
underTest.doGet(request, context.response());
ExpressionCustomizer expressionCustomizer = (ExpressionCustomizer) request.getAttribute(ExpressionCustomizer.class.getName());
assertNotNull(expressionCustomizer);
Style cqDesign = (Style) expressionCustomizer.getVariable("cqDesign");
assertNotNull(cqDesign);
verify(requestDispatcher).include(context.request(), context.response());
}

@Test
void testComponentNotFound() throws ServletException, IOException {
context.requestPathInfo().setSuffix(SUFFIX + "non-existing");
context.currentResource("/apps/core/wcm/components/teaser/cq:design/content/items/tabs/items/image");
MockSlingHttpServletRequest request = context.request();
underTest.doGet(request, context.response());
assertEquals(HttpStatus.SC_NOT_FOUND, context.response().getStatus());
}

@Test
void testNoRequestDispatcher() throws ServletException, IOException {
MockSlingHttpServletRequest request = context.request();
context.currentResource("/apps/core/wcm/components/teaser/cq:design/content/items/tabs/items/image");
request.setRequestDispatcherFactory(new MockRequestDispatcherFactory() {
@Override
public RequestDispatcher getRequestDispatcher(String s, RequestDispatcherOptions requestDispatcherOptions) {
return null;
}

@Override
public RequestDispatcher getRequestDispatcher(Resource resource, RequestDispatcherOptions requestDispatcherOptions) {
return null;
}
});
context.requestPathInfo().setSuffix(SUFFIX);
underTest.doGet(request, context.response());
assertEquals(HttpStatus.SC_NOT_FOUND, context.response().getStatus());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"teaser": {
"jcr:primaryType" : "cq:Component",
"imageDelegate" : "core/wcm/components/image",
"jcr:createdBy" : "admin",
"jcr:title" : "Teaser (v2)",
"jcr:description" : "Displays an image together with a link that can act as a teaser for a site section.",
"cq:icon" : "image",
"componentGroup" : ".core-wcm",
"cq:design" : {
"jcr:primaryType": "nt:unstructured",
"content": {
"jcr:primaryType": "nt:unstructured",
"items": {
"jcr:primaryType": "nt:unstructured",
"tabs": {
"jcr:primaryType": "nt:unstructured",
"items": {
"jcr:primaryType": "nt:unstructured",
"image" :{
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "core/wcm/components/include/imagedelegate",
"path" : "core/wcm/components/image/cq:dialog/content/items/tabs/items/asset"
}
}
}
}
}
}
},
"image" : {
"jcr:primaryType" : "cq:Component",
"jcr:createdBy" : "admin",
"jcr:title" : "Image (v3)",
"sling:resourceSuperType": "core/wcm/components/image",
"jcr:description" : "Smart Adaptive Image",
"cq:icon" : "image",
"componentGroup" : ".core-wcm"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"test" : {
"jcr:primaryType": "cq:Page",
"jcr:content": {
"jcr:primaryType": "cq:PageContent",
"root": {
"jcr:primaryType": "nt:unstructured",
"responsivegrid": {
"jcr:primaryType": "nt:unstructured",
"teaser": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "core/wcm/components/teaser"
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions content/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/node_modules
/target
/src/content/jcr_root/apps/core/wcm/components/commons/datalayer
/test
/coverage
karma.conf.js
3 changes: 2 additions & 1 deletion content/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"globals": {
"Coral": false,
"Granite": false,
"CMP": false
"CMP": false,
"Promise": false
}
}
1 change: 1 addition & 0 deletions content/.stylelintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/coverage
Loading

0 comments on commit d291222

Please sign in to comment.