Skip to content

Commit

Permalink
Do not disable optimized Sling Alias resolution by default (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert authored Dec 7, 2023
1 parent eb1eb2a commit b35d383
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 19 deletions.
4 changes: 4 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<action type="update" dev="sseifert">
Update to latest Sling Mock.
</action>
<action type="update" dev="sseifert" issue="29">
Resource Resolver Factory: Set resource.resolver.optimize.alias.resolution=true and resource.resolver.enable.vanitypath=true to use the optimized sling:alias resolution.
As a result, sling:alias resolution is only supported with JCR_OAK resource resolver type.
</action>
</release>

<release version="5.4.2" date="2023-11-20">
Expand Down
7 changes: 7 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Map;
import java.util.Set;

import io.wcm.testing.mock.aem.xf.MockExperienceFragmentAdapterFactory;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolverFactory;
Expand Down Expand Up @@ -57,6 +56,7 @@
import io.wcm.testing.mock.aem.dam.MockAssetStore;
import io.wcm.testing.mock.aem.dam.MockPublishUtils;
import io.wcm.testing.mock.aem.granite.MockResourceCollectionManager;
import io.wcm.testing.mock.aem.xf.MockExperienceFragmentAdapterFactory;

/**
* Defines AEM context objects with lazy initialization.
Expand Down Expand Up @@ -138,10 +138,8 @@ protected final Map<String, Object> resourceResolverFactoryActivatorPropsMergeWi
});
props.put("resource.resolver.map.location", "/etc/map");
props.put("resource.resolver.default.vanity.redirect.status", "302");
props.put("resource.resolver.enable.vanitypath", false);
props.put("resource.resolver.vanitypath.maxEntries", -1);
props.put("resource.resolver.vanitypath.bloomfilter.maxBytes", 1024000);
props.put("resource.resolver.optimize.alias.resolution", false);
props.put(ResourceResolverFactoryConfigPropertyNames.getVanityPathAllowListPropertyName(), new String[] {
"/apps/",
"/libs/",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@
*/
package io.wcm.testing.mock.aem.context;

import static com.day.cq.commons.jcr.JcrConstants.JCR_PRIMARYTYPE;
import static com.day.cq.commons.jcr.JcrConstants.NT_UNSTRUCTURED;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.loader.ContentLoader;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -140,17 +137,4 @@ public void testSetCurrentPageViaSetResource_Resource() {
assertEquals(contentRoot + "/toolbar/jcr:content", context.currentResource().getPath());
}

@Test
public void testSlingAlias() {
if (context.resourceResolverType() == ResourceResolverType.RESOURCERESOLVER_MOCK) {
// sling:alias is not supported for RESOURCERESOLVER_MOCK
return;
}
Resource resource = context.create().resource(contentRoot + "/myresource",
JCR_PRIMARYTYPE, NT_UNSTRUCTURED,
"sling:alias", "myalias");
assertEquals(contentRoot + "/myresource", resource.getPath());
assertEquals(contentRoot + "/myalias", context.resourceResolver().map(resource.getPath()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2023 wcm.io
* %%
* 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.
* #L%
*/
package io.wcm.testing.mock.aem.context;

import static com.day.cq.commons.jcr.JcrConstants.JCR_PRIMARYTYPE;
import static com.day.cq.commons.jcr.JcrConstants.NT_UNSTRUCTURED;
import static org.junit.Assert.assertEquals;

import java.util.Map;

import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import io.wcm.testing.mock.aem.junit.AemContext;
import io.wcm.testing.mock.aem.junit.AemContextBuilder;

public class SlingAlias_JcrMockTest {

@Rule
public AemContext context = new AemContextBuilder(ResourceResolverType.JCR_MOCK)
/*
* The optimized alias resolution is based on ResourceChangeListener, which is not supported with JCR_MOCK,
* because JCR_MOCK does not support JCR Observation events. So, to use sling:alias with JCR_MOCK
* we have to disabled the optimized alias resolution.
* But be warned: This old code path is deprecated and likely to be removed in future Sling versions.
* See SLING-12054 and SLING-12025 as references.
*/
.resourceResolverFactoryActivatorProps(Map.of("resource.resolver.optimize.alias.resolution", false))
.build();

private String contentRoot;

@Before
public void setUp() throws Exception {
contentRoot = context.uniqueRoot().content() + "/sample";
}

@Test
public void testSlingAlias() throws PersistenceException {
Resource resource = context.create().resource(contentRoot + "/myresource",
JCR_PRIMARYTYPE, NT_UNSTRUCTURED,
"sling:alias", "myalias");
context.resourceResolver().commit();

assertEquals(contentRoot + "/myresource", resource.getPath());
assertEquals(contentRoot + "/myalias", context.resourceResolver().map(resource.getPath()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2023 wcm.io
* %%
* 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.
* #L%
*/
package io.wcm.testing.mock.aem.context;

import static com.day.cq.commons.jcr.JcrConstants.JCR_PRIMARYTYPE;
import static com.day.cq.commons.jcr.JcrConstants.NT_UNSTRUCTURED;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import io.wcm.testing.mock.aem.junit.AemContext;

public class SlingAlias_JcrOakTest {

@Rule
public AemContext context = new AemContext(ResourceResolverType.JCR_OAK);

private String contentRoot;

@Before
public void setUp() throws Exception {
contentRoot = context.uniqueRoot().content() + "/sample";
}

@Test
public void testSlingAlias() throws PersistenceException {
Resource resource = context.create().resource(contentRoot + "/myresource",
JCR_PRIMARYTYPE, NT_UNSTRUCTURED,
"sling:alias", "myalias");
context.resourceResolver().commit();

assertEquals(contentRoot + "/myresource", resource.getPath());

// alias processing happens asynchronously, so it may take a bit until the alias resolution works
await().atMost(2, SECONDS).until(() -> StringUtils.equals(contentRoot + "/myalias",
context.resourceResolver().map(resource.getPath())));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.wcm.testing.mock.aem.junit.AemContext;
import io.wcm.testing.mock.aem.junit.AemContextBuilder;
import io.wcm.testing.mock.aem.junit.AemContextCallback;

public final class TestAemContext {
Expand Down Expand Up @@ -59,6 +60,15 @@ private TestAemContext() {
return new AemContext(new SetUpCallback(), resourceResolverTypes);
}

public static @NotNull AemContextBuilder newAemContextBuilder() {
return newAemContextBuilder(ALL_TYPES);
}

public static @NotNull AemContextBuilder newAemContextBuilder(ResourceResolverType... resourceResolverTypes) {
return new AemContextBuilder(resourceResolverTypes)
.afterSetUp(new SetUpCallback());
}

/**
* Custom set up rules required in all unit tests.
*/
Expand Down

0 comments on commit b35d383

Please sign in to comment.