Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed May 19, 2023
2 parents 22daa95 + bee9146 commit b4a6a28
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 40 deletions.
15 changes: 15 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/plugins/maven-changes-plugin/xsd/changes-1.0.0.xsd">
<body>

<release version="5.2.2" date="2023-05-19">
<action type="update" dev="sseifert" issue="12">
MockAemBindingsValuesProvider: Support LazyBindings.
</action>
<action type="update" dev="sseifert" issue="12">
MockAemBindingsValuesProvider: Add support for Sling Bindings properties xssAPI, currentContentPolicy, currentContentPolicyProperties.
</action>
<action type="update" dev="sseifert" issue="12">
MockAemBindingsValuesProvider: Use ContentPolicyStyle for resolving currentStyle if content policies are used.
</action>
<action type="update" dev="sseifert">
Update to latest JCR Mock, OSGI Mock, ResourceResolver Mock, Sling Mock, Servlet Helpers.
</action>
</release>

<release version="5.2.0" date="2023-03-27">
<action type="add" dev="royteeuwen" issue="4">
Implement all mock methods that were added since AEM 6.5.6+ for LanguageManager.
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.parent</artifactId>
<version>5.2.0</version>
<version>5.2.2</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
*/
package io.wcm.testing.mock.aem.context;

import static io.wcm.testing.mock.aem.context.MockAemSlingBindings.resolveSlingBindingProperty;

import java.util.Map;

import javax.script.Bindings;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.scripting.LazyBindings;
import org.apache.sling.api.scripting.SlingBindings;
import org.apache.sling.scripting.api.BindingsValuesProvider;
import org.osgi.service.component.annotations.Activate;
Expand Down Expand Up @@ -66,9 +69,14 @@ public void addBindings(Bindings bindings) {
}

private void putProperty(Bindings bindings, String key, SlingHttpServletRequest request) {
Object value = MockAemSlingBindings.resolveSlingBindingProperty(context, key, request);
if (value != null) {
bindings.put(key, value);
if (bindings instanceof LazyBindings) {
bindings.put(key, (LazyBindings.Supplier)() -> resolveSlingBindingProperty(context, key, request));
}
else {
Object value = resolveSlingBindingProperty(context, key, request);
if (value != null) {
bindings.put(key, value);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.xss.XSSAPI;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -35,7 +36,10 @@
import com.day.cq.wcm.api.designer.Design;
import com.day.cq.wcm.api.designer.Designer;
import com.day.cq.wcm.api.designer.Style;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyManager;
import com.day.cq.wcm.commons.WCMUtils;
import com.day.cq.wcm.commons.policy.ContentPolicyStyle;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

Expand Down Expand Up @@ -68,7 +72,13 @@ enum SlingBindingsProperty {

RESOURCE_DESIGN("resourceDesign"),

CURRENT_STYLE("currentStyle");
CURRENT_STYLE("currentStyle"),

XSSAPI("xssAPI"),

CURRENT_CONTENT_POLICY("currentContentPolicy"),

CURRENT_CONTENT_POLICY_PROPS("currentContentPolicyProperties");

private final String key;

Expand Down Expand Up @@ -131,6 +141,15 @@ private MockAemSlingBindings() {
if (StringUtils.equals(property, SlingBindingsProperty.CURRENT_STYLE.key())) {
return getStyle(request);
}
if (StringUtils.equals(property, SlingBindingsProperty.XSSAPI.key())) {
return getXssApi(context);
}
if (StringUtils.equals(property, SlingBindingsProperty.CURRENT_CONTENT_POLICY.key())) {
return getCurrentContentPolicy(request);
}
if (StringUtils.equals(property, SlingBindingsProperty.CURRENT_CONTENT_POLICY_PROPS.key())) {
return getCurrentContentPolicyProperties(request);
}

return null;
}
Expand Down Expand Up @@ -202,9 +221,40 @@ private static Design getResourceDesign(SlingHttpServletRequest request, AemCont

private static Style getStyle(SlingHttpServletRequest request) {
ComponentContext wcmComponentContext = getWcmComponentContext(request);
Design currentDesign = getCurrentDesign(request);
if (wcmComponentContext != null && currentDesign != null) {
return currentDesign.getStyle(wcmComponentContext.getCell());
if (wcmComponentContext != null) {
ContentPolicy contentPolicy = getCurrentContentPolicy(request);
if (contentPolicy != null) {
return new ContentPolicyStyle(contentPolicy, wcmComponentContext.getCell());
}
else {
Design currentDesign = getCurrentDesign(request);
if (currentDesign != null) {
return currentDesign.getStyle(wcmComponentContext.getCell());
}
}
}
return null;
}

private static XSSAPI getXssApi(AemContextImpl context) {
return context.getService(XSSAPI.class);
}

private static ContentPolicy getCurrentContentPolicy(SlingHttpServletRequest request) {
ComponentContext wcmComponentContext = getWcmComponentContext(request);
if (wcmComponentContext != null) {
ContentPolicyManager policyManager = request.getResourceResolver().adaptTo(ContentPolicyManager.class);
if (policyManager != null) {
return policyManager.getPolicy(request.getResource(), request);
}
}
return null;
}

private static ValueMap getCurrentContentPolicyProperties(SlingHttpServletRequest request) {
ContentPolicy contentPolicy = getCurrentContentPolicy(request);
if (contentPolicy != null) {
return wrap(contentPolicy.getProperties());
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import static org.junit.Assert.assertNull;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.adapter.AdapterManager;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.factory.ModelFactory;
import org.apache.sling.scripting.api.BindingsValuesProvidersByContext;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -44,6 +46,7 @@ public class MockAemSlingBindingsTest {

private static final String COMPONENT_RESOURCE_SUPER_TYPE = "app1/components/component2";
private static final String COMPONENT_RESOURCE_TYPE = "app1/components/component1";
private static final String CHILD_COMPOSITE_COMPONENT_RESOURCE_TYPE = COMPONENT_RESOURCE_TYPE + "/child1";

@Rule
public AemContext context = TestAemContext.newAemContext();
Expand Down Expand Up @@ -101,6 +104,9 @@ public void testBindings() {
assertNotNull(model.getCurrentDesign());
assertNotNull(model.getResourceDesign());
assertNotNull(model.getCurrentStyle());
assertNotNull(model.getXssAPI());
assertNull(model.getCurrentContentPolicy());
assertNull(model.getCurrentContentPolicyProperties());
}

@Test
Expand Down Expand Up @@ -170,6 +176,9 @@ public void testBindings_EditMode() {
assertNotNull(model.getCurrentDesign());
assertNotNull(model.getResourceDesign());
assertNotNull(model.getCurrentStyle());
assertNotNull(model.getXssAPI());
assertNull(model.getCurrentContentPolicy());
assertNull(model.getCurrentContentPolicyProperties());
}

@Test
Expand Down Expand Up @@ -207,6 +216,9 @@ public void testBindingsModelFactory() throws Exception {
assertNotNull(model.getCurrentDesign());
assertNotNull(model.getResourceDesign());
assertNotNull(model.getCurrentStyle());
assertNotNull(model.getXssAPI());
assertNull(model.getCurrentContentPolicy());
assertNull(model.getCurrentContentPolicyProperties());
}

@Test
Expand All @@ -218,6 +230,8 @@ public void testContentPolicy() {

SlingBindingsModel model = context.request().adaptTo(SlingBindingsModel.class);
assertEquals("value1", model.getCurrentStyle().get("policyProp1", String.class));
assertEquals("value1", model.getCurrentContentPolicy().getProperties().get("policyProp1", String.class));
assertEquals("value1", model.getCurrentContentPolicyProperties().get("policyProp1", String.class));
}

@Test
Expand All @@ -231,6 +245,28 @@ public void testContentPolicy_SlingModelDelegation() {

SlingBindingsModel model = context.request().adaptTo(SlingBindingsModel.class);
assertEquals("value1", model.getCurrentStyle().get("policyProp1", String.class));
assertNull(model.getCurrentContentPolicy());
assertNull(model.getCurrentContentPolicyProperties());
}

@Test
public void testContentPolicy_CompositeSlingModelDelegationViaRequestWrapper() {
context.currentResource(currentResource);
context.create().resource("/apps/" + CHILD_COMPOSITE_COMPONENT_RESOURCE_TYPE,
JcrConstants.JCR_PRIMARYTYPE, NameConstants.NT_COMPONENT);
context.contentPolicyMapping(CHILD_COMPOSITE_COMPONENT_RESOURCE_TYPE,
"policyProp1", "value1");
AdapterManager adapterManager = context.getService(AdapterManager.class);
BindingsValuesProvidersByContext bindingsValuesProvidersByContext = context.getService(BindingsValuesProvidersByContext.class);
Resource testResource = context.create().resource(currentPage.getContentResource().getPath() + "/testResource/child1",
"sling:resourceType", CHILD_COMPOSITE_COMPONENT_RESOURCE_TYPE);

// wrap current request with resource
SlingHttpServletRequest request = new MockOverrideSlingPathRequestWrapper(adapterManager,
context.request(), testResource.getPath(), bindingsValuesProvidersByContext);

SlingBindingsModel model = request.adaptTo(SlingBindingsModel.class);
assertEquals("value1", model.getCurrentStyle().get("policyProp1", String.class));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package io.wcm.testing.mock.aem.context;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.SimpleBindings;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.adapter.AdapterManager;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.scripting.SlingBindings;
import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
import org.apache.sling.scripting.api.AbstractScriptEngineFactory;
import org.apache.sling.scripting.api.BindingsValuesProvider;
import org.apache.sling.scripting.api.BindingsValuesProvidersByContext;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;

class MockOverrideSlingPathRequestWrapper extends SlingHttpServletRequestWrapper {

private static final String ATTR_SLING_BINDINGS = SlingBindings.class.getName();
private final AdapterManager adapterManager;
private final SlingBindings myBindings;
private final Resource resource;
private final Map<Class<?>, Object> adaptersCache;

MockOverrideSlingPathRequestWrapper(AdapterManager adapterManager, SlingHttpServletRequest request, String path) {
super(request);
this.adapterManager = adapterManager;
this.myBindings = new SlingBindings();
this.adaptersCache = new HashMap();
SlingBindings slingBindings = (SlingBindings)this.getSlingRequest().getAttribute(ATTR_SLING_BINDINGS);
this.resource = this.getSlingRequest().getResourceResolver().resolve(this.getSlingRequest(), path);
if (slingBindings != null) {
this.myBindings.putAll(slingBindings);
}

this.myBindings.put("properties", this.resource.getValueMap());
this.myBindings.put("resource", this.resource);
this.myBindings.put("request", this);
this.myBindings.put("resolver", this.resource.getResourceResolver());
Page currentPage = null;
PageManager pageManager = this.getSlingRequest().getResourceResolver().adaptTo(PageManager.class);
if (pageManager != null) {
currentPage = pageManager.getContainingPage(this.resource);
}

this.myBindings.put("currentPage", currentPage);
}

MockOverrideSlingPathRequestWrapper(AdapterManager adapterManager, SlingHttpServletRequest request, String path,
BindingsValuesProvidersByContext bindingsValuesProvidersByContext) {
this(adapterManager, request, path);
SimpleBindings additionalBindings = new SimpleBindings();
additionalBindings.putAll(this.myBindings);
Collection<BindingsValuesProvider> bindingsValuesProviders = bindingsValuesProvidersByContext
.getBindingsValuesProviders(new SlingModelsScriptEngineFactory(), "request");
Iterator<BindingsValuesProvider> bindingsValuesProviderIterator = bindingsValuesProviders.iterator();

while (bindingsValuesProviderIterator.hasNext()) {
BindingsValuesProvider provider = bindingsValuesProviderIterator.next();
provider.addBindings(additionalBindings);
}

this.myBindings.putAll(additionalBindings);
}

@Override
public Object getAttribute(String name) {
return ATTR_SLING_BINDINGS.equals(name) ? this.myBindings : super.getAttribute(name);
}

@Override
public Resource getResource() {
return this.resource;
}

/**
* Overriding `adaptTo` to avoid using the original request as the adaptable.
*/
@Override
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
AdapterType result = null;
synchronized (this) {
result = (AdapterType)this.adaptersCache.get(type);

if (result == null) {
result = adapterManager.getAdapter(this, type);

if (result != null) {
this.adaptersCache.put(type, result);
}
}

return result;
}
}

private static class SlingModelsScriptEngineFactory extends AbstractScriptEngineFactory implements ScriptEngineFactory {

SlingModelsScriptEngineFactory() {
this.setNames(new String[] { "sling-models-exporter", "sling-models" });
}

@Override
public String getLanguageName() {
return null;
}

@Override
public String getLanguageVersion() {
return null;
}

@Override
public ScriptEngine getScriptEngine() {
return null;
}
}
}

Loading

0 comments on commit b4a6a28

Please sign in to comment.