Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

Avatar

Level 4

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.

View solution in original post

5 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

 

Avatar

Level 4

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

Level 4

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.