Expand my Community achievements bar.

How to register resource mapping configuration to aem context?

Avatar

Level 2

I have  a model where i am obtaining resource resolver from resourceresolverFactory . This model is a generic model referenced in multiple classes 

In one such test case , i want resolver.map("path") to give me the resolved paths according to my configurations. Is there a way to register these configurations to the aem context so that when resolver.map() is called it maps url correctly

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

4 Replies

Avatar

Community Advisor

Hi, 

You could try to mock the resourceResolver object so you can return the expected value when resolver.map() is invoked. Try something like this:

    
    @Mock
    private ResourceResolverFactory resourceResolverFactory;
    
    private ResourceResolver mockResolver;
    
    private AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);

    @BeforeEach
    void setUp() throws LoginException {
        MockitoAnnotations.openMocks(this);
        context.registerService(ResourceResolverFactory.class, resourceResolverFactory);
        mockResolver = context.resourceResolver().clone(null);
    
when(resourceResolverFactory.getServiceResourceResolver(any())).thenReturn(mockResolver);
    }

 

Hope this helps



Esteban Bustamante

Avatar

Level 2

I mainly need help in adding mappings to my resource resolver factory / resolver for my context,

I have code where using resourceResolverFactory.getServiceResourceResolver(map);
link=resolver.map(link);

resolver is getting mocked but due to no mappings its not mapping my link , i need the link to be mapped for accurate test anyway to set this mapping to the context

Avatar

Level 2

Hi @Zendarkke 

you can use AEM's ResourceResolverFactory to create a ResourceResolver instance and then register your custom configurations with the ResourceResolver using the ResourceResolver#setMapping method.

Here's an example:

// assuming you have a ResourceResolverFactory instance
ResourceResolverFactory resourceResolverFactory = ...;

// create a new ResourceResolver instance
ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);

// define your custom mapping configurations
Map<String, String> mappings = new HashMap<>();
mappings.put("/content/myapp", "/content/myapp/en");
mappings.put("/content/myapp/en/home", "/content/myapp/en/home.html");

// register the mappings with the ResourceResolver
resourceResolver.setMapping(mappings);

// now, when you call resolver.map("path"), it should use your custom mappings
String resolvedPath = resourceResolver.map("/content/myapp/en/home");
// resolvedPath should be "/content/myapp/en/home.html"

However, if you want to register these configurations at the AEM context level, so that all ResourceResolver instances created by the ResourceResolverFactory use these mappings, you can use the ResourceResolverFactory#setMapping method:

// assuming you have a ResourceResolverFactory instance
ResourceResolverFactory resourceResolverFactory = ...;

// define your custom mapping configurations
Map<String, String> mappings = new HashMap<>();
mappings.put("/content/myapp", "/content/myapp/en");
mappings.put("/content/myapp/en/home", "/content/myapp/en/home.html");

// register the mappings with the ResourceResolverFactory
resourceResolverFactory.setMapping(mappings);

// now, when you create a new ResourceResolver instance, it will use the custom mappings
ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
String resolvedPath = resourceResolver.map("/content/myapp/en/home");
// resolvedPath should be "/content/myapp/en/home.html"

 

Note that the setMapping method on ResourceResolverFactory sets the mappings for all ResourceResolver instances created by that factory. If you want to set mappings only for a specific ResourceResolver instance, use the setMapping method on the ResourceResolver instance itself.

Also, keep in mind that these mappings are only applied when using the map method of the ResourceResolver. If you're using other methods, such as resolve or getResource, these mappings will not be applied.

Avatar

Administrator

@Zendarkke Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni