Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

due to @ChildResourceFromRequest my unit test cases are getting failed while writing unit testcases for AEM component

Avatar

Level 5

hi , all my testcases are getting failed showing runtime exception if I am including @ChildResourceFromRequest in my model class .

Model class : 

package comcore.models.components.content.collections.locations;

import com.adobe.acs.commons.models.injectors.annotation.ChildResourceFromRequest;
import com.core.configurations.GoogleMapAPIConfiguration;
import com.core.decorators.Validatable;
import com.core.models.components.ComponentModel;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ChildResource;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
import java.util.List;
import static java.util.Collections.emptyList;

@Model(
adaptables = SlingHttpServletRequest.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class LocationsModel extends ComponentModel implements Validatable {
private static final Logger log = LoggerFactory.getLogger(LocationsModel.class);
private static final String CSS_CLASS_NAME = "locations";

@OSGiService
private GoogleMapAPIConfiguration mapAPIConfiguration;

@ValueMapValue
private String title;

@ValueMapValue
private int zoomLevel;

//@ChildResourceFromRequest
@ChildResource // USING THIS AS ALTERNATIVE BUT NOT SURE IF IT WILL CHANGE THE FUNCTIONALITY
private final List<LocationItem> locations = emptyList();

private String locationsJson;
private String key;

@PostConstruct
protected void init() {
if (mapAPIConfiguration != null) {
this.key = mapAPIConfiguration.getKey();
}
/* I NEED TO COMMENT THIS CODE IN ORDER AVOID ERROR
try {
this.locationsJson = new ObjectMapper().writeValueAsString(this.locations);
} catch (Exception ex) {
log.error("Unable to generate locations to JSON string", ex);
}
*/


}

@Nonnull
@Override
public String getCssClassName() {
return CSS_CLASS_NAME;
}

@Override
public boolean isValid() {
return StringUtils.isNotEmpty(this.key) && !this.locations.isEmpty();
}

public String getTitle() {
return this.title;
}

@Nonnull
public List<LocationItem> getLocations() {
return this.locations;
}

public String getLocationsJson() {
return this.locationsJson;
}

public int getZoomLevel() {
return this.zoomLevel;
}

public String getKey() {
return this.key;
}
}


can you tell me why @ChildResourceFromRequest is not working 

Thank you

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

There is no difference in functionality, it is just direct adaption of child resource when adaptable is slingRequest.

 

a key difference in that it uses a mock request object pointed to the resource path as the adaptable, allowing the sling model to reference the request and other sling bindings not otherwise accessible when adapting a resource directly.



Arun Patidar

View solution in original post

8 Replies

Avatar

Community Advisor

Hi,

Could you share the details about @ChildResourceFromRequest annotations?

I am not able to find at sling official documentation

https://sling.apache.org/documentation/bundles/models.html#injector-specific-annotations-1

 



Arun Patidar

Avatar

Level 5

upon clicking on @ChildResourceFromRequest i am getting : 

package com.adobe.acs.commons.models.injectors.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.apache.sling.models.annotations.Source;
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
import org.apache.sling.models.spi.injectorspecific.InjectAnnotation;

@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@InjectAnnotation
@Source("child-resources-from-request")
public @interface ChildResourceFromRequest {
String name() default "";

/** @deprecated */
@Deprecated
boolean optional() default false;

InjectionStrategy injectionStrategy() default InjectionStrategy.DEFAULT;

String via() default "";
}

Avatar

Community Advisor

Ahh ok, this is from ACS common,

you can check the details at https://adobe-consulting-services.github.io/acs-aem-commons/features/sling-model-injectors/child-res...

 

I think you can continue using @ChildResource



Arun Patidar

Avatar

Level 5

will using @ChildResource instead of @ChildResourceFromRequest change any of my functionality?

Avatar

Correct answer by
Community Advisor

There is no difference in functionality, it is just direct adaption of child resource when adaptable is slingRequest.

 

a key difference in that it uses a mock request object pointed to the resource path as the adaptable, allowing the sling model to reference the request and other sling bindings not otherwise accessible when adapting a resource directly.



Arun Patidar

Avatar

Level 1

@arunpatidar Is there any solution to this issue when using ChildResourceFromRequest?

Avatar

Level 1

This doesn't answer this initial question and the accepted solution doesn't neither. 

I'm facing the same issue 

 

 

*** UPDATE ***

 

I found the solution:

add this to your setup function (@BeforeEach) in the unit tests 
       

 

 

context.registerInjectActivateService(new ChildResourceFromRequestAnnotationProcessorFactory());
context.registerInjectActivateService(new ChildResourceFromRequestInjector());

 

       

@elimccoy @rahul234dabas 

Avatar

Level 1

Thanks for clarifying this, I will apply this same change then for any customer annotation that is used