Skip to main content
Level 2
Aug 27, 2024 12:03 PM UTC

How to register resource mapping configuration to aem context?

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

3 replies

EstebanBustamante
Adobe Champion and Community Advisor
Adobe Champion and Community Advisor
Aug 27, 2024 12:25 PM UTC

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

Translate
Esteban Bustamante
ZendarkkeAuthor
Level 2
Aug 27, 2024 2:37 PM UTC

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

Translate
kautuk_sahni
Level 9
Sep 13, 2024 7:25 AM UTC

@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!

Translate
Kautuk Sahni