Hi All,
I coudln't able to adapt the resource to my Sling Model class,
getting the error
Could not yet find an adapter factory for the model class my_class from adaptable class org.apache.sling.testing.resourceresolver.MockResource.
Attaching the Sling models and junit files here.
Model Interface
package com.mycore.core.models;
import org.apache.commons.lang.StringUtils;
public interface ConfiguratorPromptModel {
default String getNameplateId() {
return StringUtils.EMPTY;
}
}
Model Implementation
package com.mycore.core.internal.models.v1;
import com.mycore.core.models.ConfiguratorPromptModel;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
@Model(adaptables = Resource.class, adapters = {ConfiguratorPromptModel.class})
public class ConfiguratorPromptModelImpl implements ConfiguratorPromptModel {
public String getNameplateId() {
return nameplateId;
}
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private String nameplateId;
}
Test Case
package com.mycore.core.internal.models.v1;
import com.mycore.core.models.ConfiguratorPromptModel;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.factory.ModelFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(AemContextExtension.class)
class ConfiguratorPromptModelImplTest {
private ConfiguratorPromptModel configuratorPromptModel;
@BeforeEach
public void setUp(AemContext context) throws Exception {
context.load().json("/content/configuratorprompt/configuratorprompt.json", "/content/configuratorprompt.html");
Resource resource = context.resourceResolver().getResource("/content/configuratorprompt.html");
this.configuratorPromptModel = context.getService(ModelFactory.class).createModel(resource, ConfiguratorPromptModelImpl.class);
}
void getNameplateId() {
assertEquals("nameplate", configuratorPromptModel.getNameplateId());
}
}
Thanks,
Gokulraj
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @rsGokulraj4 ,
{ "jcr:primaryType": "cq:Page", "jcr:content": { "jcr:primaryType": "cq:PageContent", "jcr:title": "en", "cq:template": "/conf/we-retail/settings/wcm/templates/homepage", "cq:contextHubSegmentsPath": "/etc/segmentation/contexthub", "jcr:isCheckedOut": true, "pageTitle": "Test page", "jcr:uuid": "d2d32722-0ce4-4663-8873-1b11a8e34ceb", "sling:resourceType": "we-retail/components/page", "cq:contextHubPath": "/etc/cloudsettings/default/contexthub", "root": { "jcr:primaryType": "nt:unstructured", "layout": "responsiveGrid", "sling:resourceType": "we-retail/components/container", "container": { "jcr:primaryType": "nt:unstructured", "sling:resourceType": "we-retail/components/container", "container": { "jcr:primaryType": "nt:unstructured", "layout": "responsiveGrid", "sling:resourceType": "we-retail/components/container", "banner": { "jcr:primaryType": "nt:unstructured", "id": "1234", "type": "callbutton", "bannerText":"Banner Text", "sling:resourceType": "we-retail/components/banner", "cq:styleIds": [ "1654672680483", "1654672784107" ] } } } } } }
Is this working now?
Thanks
Hi @rsGokulraj4 ,
PFB example
@ExtendWith(AemContextExtension.class) public class BannerTest { /** The Constant RESOURCE_CONTENT. */ private static final String RESOURCE_CONTENT = "/core/models/banner/banner.json"; /** The Constant TEST_CONTENT_ROOT. */ private static final String TEST_CONTENT_ROOT = "/content/we-retail/us/en"; /** The Constant RESOURCE. */ private static final String RESOURCE = TEST_CONTENT_ROOT + "/jcr:content/root/container/container/banner"; /** The model. */ private Banner model; /** The resource. */ private Resource resource; /** * Sets the up. * * @param aemContext the new up */ @BeforeEach public void setup(AemContext aemContext) { Class<Banner> modelClass = Banner.class; aemContext.load().json(RESOURCE_CONTENT, TEST_CONTENT_ROOT); aemContext.addModelsForClasses(modelClass); resource = aemContext.currentResource(RESOURCE); model = resource.adaptTo(modelClass); } /** * Simple load and getter test. */ @Test public void simpleLoadAndGetterTest() { assertEquals("1234", model.getId()); assertEquals("Banner Text", model.getBannerText()); } }
Hope this helps
Thanks
Hi @Bimmi_Soi
Thanks for the suggestion,
I tried the approach you have shared, but still getting the null exception on line.
model = resource.adaptTo(modelClass);
Check if you are using these imports and share the json file
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.sling.api.resource.Resource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
Hi @rsGokulraj4 ,
{ "jcr:primaryType": "cq:Page", "jcr:content": { "jcr:primaryType": "cq:PageContent", "jcr:title": "en", "cq:template": "/conf/we-retail/settings/wcm/templates/homepage", "cq:contextHubSegmentsPath": "/etc/segmentation/contexthub", "jcr:isCheckedOut": true, "pageTitle": "Test page", "jcr:uuid": "d2d32722-0ce4-4663-8873-1b11a8e34ceb", "sling:resourceType": "we-retail/components/page", "cq:contextHubPath": "/etc/cloudsettings/default/contexthub", "root": { "jcr:primaryType": "nt:unstructured", "layout": "responsiveGrid", "sling:resourceType": "we-retail/components/container", "container": { "jcr:primaryType": "nt:unstructured", "sling:resourceType": "we-retail/components/container", "container": { "jcr:primaryType": "nt:unstructured", "layout": "responsiveGrid", "sling:resourceType": "we-retail/components/container", "banner": { "jcr:primaryType": "nt:unstructured", "id": "1234", "type": "callbutton", "bannerText":"Banner Text", "sling:resourceType": "we-retail/components/banner", "cq:styleIds": [ "1654672680483", "1654672784107" ] } } } } } }
Is this working now?
Thanks
Views
Likes
Replies
Views
Likes
Replies