Skip to content

Commit 2bf898e

Browse files
authored
Adding support for sling mappings (#1670) (#1671)
2 parents ac41e82 + 5327426 commit 2bf898e

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

bundles/af-core/src/main/java/com/adobe/cq/forms/core/components/internal/models/v2/form/FormContainerImpl.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
import java.util.function.Consumer;
2323

2424
import javax.annotation.PostConstruct;
25+
import javax.inject.Inject;
2526

2627
import org.apache.commons.lang3.StringUtils;
2728
import org.apache.http.osgi.services.HttpClientBuilderFactory;
2829
import org.apache.sling.api.SlingHttpServletRequest;
2930
import org.apache.sling.api.resource.Resource;
31+
import org.apache.sling.api.resource.ResourceResolver;
3032
import org.apache.sling.api.resource.ValueMap;
3133
import org.apache.sling.models.annotations.Default;
3234
import org.apache.sling.models.annotations.Exporter;
@@ -152,6 +154,9 @@ public class FormContainerImpl extends AbstractContainerImpl implements FormCont
152154
@Self(injectionStrategy = InjectionStrategy.OPTIONAL)
153155
private AutoSaveConfiguration autoSaveConfig;
154156

157+
@Inject
158+
private ResourceResolver resourceResolver;
159+
155160
@Override
156161
public String getFieldType() {
157162
return super.getFieldType(FieldType.FORM);
@@ -316,13 +321,13 @@ public String getAction() {
316321
ComponentUtils.getEncodedPath(resource.getPath() + ".model.json");
317322
}
318323
}
319-
return getContextPath() + ADOBE_GLOBAL_API_ROOT + FORMS_RUNTIME_API_GLOBAL_ROOT + "/submit/" + getId();
324+
return getContextPath() + resourceResolver.map(ADOBE_GLOBAL_API_ROOT + FORMS_RUNTIME_API_GLOBAL_ROOT + "/submit/" + getId());
320325
}
321326

322327
@Override
323328
@JsonIgnore
324329
public String getDataUrl() {
325-
return getContextPath() + ADOBE_GLOBAL_API_ROOT + FORMS_RUNTIME_API_GLOBAL_ROOT + "/data/" + getId();
330+
return getContextPath() + resourceResolver.map(ADOBE_GLOBAL_API_ROOT + FORMS_RUNTIME_API_GLOBAL_ROOT + "/data/" + getId());
326331
}
327332

328333
@Override
@@ -460,7 +465,8 @@ public String getName() {
460465

461466
@Override
462467
public String getCustomFunctionUrl() {
463-
return getContextPath() + ADOBE_GLOBAL_API_ROOT + FORMS_RUNTIME_API_GLOBAL_ROOT + "/customfunctions/" + getId();
468+
return getContextPath() + resourceResolver.map(ADOBE_GLOBAL_API_ROOT + FORMS_RUNTIME_API_GLOBAL_ROOT + "/customfunctions/"
469+
+ getId());
464470
}
465471

466472
@JsonIgnore

bundles/af-core/src/test/java/com/adobe/cq/forms/core/components/internal/models/v2/form/FormContainerImplTest.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,66 @@ void testGetDataUrl() throws Exception {
227227
.getDataUrl());
228228
}
229229

230+
@Test
231+
void testGetActionWithResourceResolverMapping() throws Exception {
232+
// Create a spy of the resource resolver to mock the map method
233+
org.apache.sling.api.resource.ResourceResolver resourceResolver = Mockito.spy(context.resourceResolver());
234+
235+
// Mock the map method to return a mapped path
236+
String originalPath = "/adobe/forms/af/submit/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==";
237+
String mappedPath = "/content/adobe/forms/af/submit/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==";
238+
Mockito.when(resourceResolver.map("/adobe/forms/af/submit/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==")).thenReturn(mappedPath);
239+
240+
// Set the mocked resource resolver in the context
241+
context.registerService(org.apache.sling.api.resource.ResourceResolver.class, resourceResolver);
242+
243+
FormContainer formContainer = Utils.getComponentUnderTest(PATH_FORM_1, FormContainer.class, context);
244+
String action = formContainer.getAction();
245+
246+
// Verify that the mapped path is used in the action URL
247+
assertTrue(action.contains(mappedPath));
248+
249+
// Verify that the map method was called with the correct path (called during mock setup + actual execution)
250+
Mockito.verify(resourceResolver, times(2)).map("/adobe/forms/af/submit/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==");
251+
}
252+
253+
@Test
254+
void testGetDataUrlWithResourceResolverMapping() throws Exception {
255+
// Create a spy of the resource resolver to mock the map method
256+
org.apache.sling.api.resource.ResourceResolver resourceResolver = Mockito.spy(context.resourceResolver());
257+
258+
// Mock the map method to return a mapped path
259+
String originalPath = "/adobe/forms/af/data/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==";
260+
String mappedPath = "/content/adobe/forms/af/data/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==";
261+
Mockito.when(resourceResolver.map("/adobe/forms/af/data/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==")).thenReturn(mappedPath);
262+
263+
// Set the mocked resource resolver in the context
264+
context.registerService(org.apache.sling.api.resource.ResourceResolver.class, resourceResolver);
265+
266+
FormContainer formContainer = Utils.getComponentUnderTest(PATH_FORM_1, FormContainer.class, context);
267+
String dataUrl = formContainer.getDataUrl();
268+
269+
// Verify that the mapped path is used in the data URL
270+
assertTrue(dataUrl.contains(mappedPath));
271+
assertEquals(mappedPath, dataUrl);
272+
273+
// Verify that the map method was called with the correct path (called during mock setup + actual execution)
274+
Mockito.verify(resourceResolver, times(2)).map("/adobe/forms/af/data/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==");
275+
}
276+
277+
@Test
278+
void testResourceResolverMappingIdentityWhenNoMapping() throws Exception {
279+
// Test the case where resourceResolver.map() returns the same path (no mapping configured)
280+
FormContainer formContainer = Utils.getComponentUnderTest(PATH_FORM_1, FormContainer.class, context);
281+
282+
String action = formContainer.getAction();
283+
String dataUrl = formContainer.getDataUrl();
284+
285+
// These should match the original expected values when no mapping is applied
286+
assertEquals("/adobe/forms/af/submit/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==", action);
287+
assertEquals("/adobe/forms/af/data/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==", dataUrl);
288+
}
289+
230290
@Test
231291
void testGetDorProperties() throws Exception {
232292
FormContainer formContainer = Utils.getComponentUnderTest(PATH_FORM_1, FormContainer.class, context);
@@ -597,6 +657,30 @@ void testCustomFunctionUrl() throws Exception {
597657
assertEquals("/adobe/forms/af/customfunctions/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==", formContainer.getCustomFunctionUrl());
598658
}
599659

660+
@Test
661+
void testGetCustomFunctionUrlWithResourceResolverMapping() throws Exception {
662+
// Create a spy of the resource resolver to mock the map method
663+
org.apache.sling.api.resource.ResourceResolver resourceResolver = Mockito.spy(context.resourceResolver());
664+
665+
// Mock the map method to return a mapped path
666+
String originalPath = "/adobe/forms/af/customfunctions/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==";
667+
String mappedPath = "/content/adobe/forms/af/customfunctions/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==";
668+
Mockito.when(resourceResolver.map("/adobe/forms/af/customfunctions/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==")).thenReturn(mappedPath);
669+
670+
// Set the mocked resource resolver in the context
671+
context.registerService(org.apache.sling.api.resource.ResourceResolver.class, resourceResolver);
672+
673+
FormContainer formContainer = Utils.getComponentUnderTest(PATH_FORM_1, FormContainer.class, context);
674+
String customFunctionUrl = formContainer.getCustomFunctionUrl();
675+
676+
// Verify that the mapped path is used in the custom function URL
677+
assertTrue(customFunctionUrl.contains(mappedPath));
678+
assertEquals(mappedPath, customFunctionUrl);
679+
680+
// Verify that the map method was called with the correct path (called during mock setup + actual execution)
681+
Mockito.verify(resourceResolver, times(2)).map("/adobe/forms/af/customfunctions/L2NvbnRlbnQvZm9ybXMvYWYvZGVtbw==");
682+
}
683+
600684
@Test
601685
public void testGetAutoSaveProperties() throws Exception {
602686
context.load().json(BASE + "/test-content-auto-save.json", PATH_FORM_WITH_AUTO_SAVE);

0 commit comments

Comments
 (0)