Unit testing Sling Model that has similar code as of a core component (ContentFragmentListImpl)
I want to unit test the following Sling model, which has similar code as of the core component ContentFragmentListImpl. I tried to understand the unit test for ContentFragmentListImpl, but failed to do so. Any help is much appreciated.
Here is the code for the Sling model:
@Model( adaptables = {SlingHttpServletRequest.class, Resource.class},
adapter = {
CameraCardsList.class,
ComponentExporter.class
},
resourceType = {CameraCardsListImpl.RESOURCE_TYPE_V1, CameraCardsListImpl.RESOURCE_TYPE_V2}
)
@Exporter()
public class CameraCardsListImpl extends AbstractComponentImpl implements CameraCardsList {
public static final String RESOURCE_TYPE_V1 = "core/wcm/components/contentfragmentlist/v1/contentfragmentlist"
public static final String RESOURCE_TYPE_V2 = "core/wcm/components/contentfragmentlist/v2/contentfragmentlist"
@Self(injectionStrategy = InjectionStrategy.REQUIRED)
private SlingHttpServletRequest slingHttpServletRequest;
@SlingObject
private ResourceResolver resourceResolver;
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private String modelPath;
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private String[] elementNames;
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private String parentPath;
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private int maxItems;
@Self
private GetFragment getFragment;
private final List<DAMContentFragment> items = new ArrayList<>();
@PostConstruct
public void init() {
// some code
}
}
Here is the the core component ContentFragmentListImpl:
@Model(
adaptables = SlingHttpServletRequest.class,
adapters = {
ContentFragmentList.class,
ComponentExporter.class
},
resourceType = {ContentFragmentListImpl.RESOURCE_TYPE_V1,ContentFragmentListImpl.RESOURCE_TYPE_V2}
)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class ContentFragmentListImpl extends AbstractComponentImpl implements ContentFragmentList {
private static final Logger LOG = LoggerFactory.getLogger(ContentFragmentListImpl.class);
public static final String RESOURCE_TYPE_V1 = "core/wcm/components/contentfragmentlist/v1/contentfragmentlist";
public static final String RESOURCE_TYPE_V2 = "core/wcm/components/contentfragmentlist/v2/contentfragmentlist";
public static final String DEFAULT_DAM_PARENT_PATH = "/content/dam";
public static final int DEFAULT_MAX_ITEMS = -1;
@Self(injectionStrategy = InjectionStrategy.REQUIRED)
private SlingHttpServletRequest slingHttpServletRequest;
@OSGiService
private ContentTypeConverter contentTypeConverter;
@SlingObject
private ResourceResolver resourceResolver;
@ValueMapValue(name = ContentFragmentList.PN_MODEL_PATH, injectionStrategy = InjectionStrategy.OPTIONAL)
private String modelPath;
@ValueMapValue(name = ContentFragmentList.PN_ELEMENT_NAMES, injectionStrategy = InjectionStrategy.OPTIONAL)
private String[] elementNames;
@ValueMapValue(name = ContentFragmentList.PN_TAG_NAMES, injectionStrategy = InjectionStrategy.OPTIONAL)
private String[] tagNames;
@ValueMapValue(name = ContentFragmentList.PN_PARENT_PATH, injectionStrategy = InjectionStrategy.OPTIONAL)
private String parentPath;
@ValueMapValue(name = ContentFragmentList.PN_MAX_ITEMS, injectionStrategy = InjectionStrategy.OPTIONAL)
(intValues = DEFAULT_MAX_ITEMS)
private int maxItems;
@ValueMapValue(name = ContentFragmentList.PN_ORDER_BY, injectionStrategy = InjectionStrategy.OPTIONAL)
(values = JcrConstants.JCR_CREATED)
private String orderBy;
@ValueMapValue(name = ContentFragmentList.PN_SORT_ORDER, injectionStrategy = InjectionStrategy.OPTIONAL)
(values = Predicate.SORT_ASCENDING)
private String sortOrder;
private final List<DAMContentFragment> items = new ArrayList<>();
@PostConstruct
private void initModel() {
// some code
}
public Collection<DAMContentFragment> getListItems() {
return Collections.unmodifiableCollection(items);
}
public String getExportedType() {
return slingHttpServletRequest.getResource().getResourceType();
}
}
How would the unit test for CameraCardListImpl be like?