Search in sources :

Example 1 with ModelFactory

use of org.apache.sling.models.factory.ModelFactory in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ClientLibrariesImplTest method testUtilsGetXFResourceTypes.

// 
// Below are tests for the specific methods in com.adobe.cq.wcm.core.components.internal.Utils
// 
@Test
void testUtilsGetXFResourceTypes() {
    Resource xfResource = context.currentResource(EXPERIENCE_FRAGMENT_PATH);
    ModelFactory modelFactory = mock(ModelFactory.class);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        Resource resource = (Resource) args[1];
        String fragmentPath = resource.getValueMap().get("fragmentVariationPath", String.class);
        ExperienceFragment experienceFragment = mock(ExperienceFragment.class);
        when(experienceFragment.getLocalizedFragmentVariationPath()).thenReturn(fragmentPath);
        return experienceFragment;
    }).when(modelFactory).getModelFromWrappedRequest(any(SlingHttpServletRequest.class), any(Resource.class), eq(ExperienceFragment.class));
    Set<String> resourceTypes = Utils.getXFResourceTypes(xfResource, context.request(), modelFactory);
    Set<String> expectedResourceTypes = new HashSet<>(Arrays.asList("core/wcm/components/page/v2/page", "cq:Page", "core/wcm/components/teaser/v1/teaser", "wcm/foundation/components/responsivegrid"));
    assertEquals(expectedResourceTypes, resourceTypes);
}
Also used : Resource(org.apache.sling.api.resource.Resource) ModelFactory(org.apache.sling.models.factory.ModelFactory) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ExperienceFragment(com.adobe.cq.wcm.core.components.models.ExperienceFragment) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 2 with ModelFactory

use of org.apache.sling.models.factory.ModelFactory in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class YouTubeImplTest method getYouTubeUnderTest.

private YouTubeImpl getYouTubeUnderTest(String resourcePath, Page page) {
    Resource resource = context.resourceResolver().getResource(resourcePath);
    if (resource == null) {
        throw new IllegalStateException("Did you forget to define test resource " + resourcePath + "?");
    }
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver(), context.bundleContext());
    SlingBindings bindings = new SlingBindings();
    bindings.put(SlingBindings.RESOURCE, resource);
    bindings.put(SlingBindings.REQUEST, request);
    bindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, page);
    bindings.put(WCMBindingsConstants.NAME_COMPONENT_CONTEXT, componentContext);
    bindings.put(WCMBindingsConstants.NAME_PROPERTIES, resource.getValueMap());
    ((MockRequestPathInfo) request.getRequestPathInfo()).setResourcePath(resourcePath);
    request.setResource(resource);
    request.setAttribute(SlingBindings.class.getName(), bindings);
    ModelFactory modelFactory = context.getService(ModelFactory.class);
    YouTubeImpl youTubeImpl = (YouTubeImpl) modelFactory.createModel(request, YouTube.class);
    return youTubeImpl;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) MockRequestPathInfo(org.apache.sling.servlethelpers.MockRequestPathInfo) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Resource(org.apache.sling.api.resource.Resource) ModelFactory(org.apache.sling.models.factory.ModelFactory) YouTube(com.adobe.cq.wcm.core.components.models.embeddable.YouTube)

Example 3 with ModelFactory

use of org.apache.sling.models.factory.ModelFactory in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ContentFragmentUtilsTest method getComponentExport.

@Test
public void getComponentExport() {
    // GIVEN
    AemContext slingContext = CoreComponentTestContext.newAemContext();
    slingContext.load().json(this.getClass().getResourceAsStream("foo.json"), "/foo");
    MockSlingHttpServletRequest slingHttpServletRequest = new MockSlingHttpServletRequest(slingContext.bundleContext());
    ComponentExporter componentExporter = new TestComponentExporter();
    ModelFactory modelFactory = Mockito.mock(ModelFactory.class);
    Mockito.when(modelFactory.getModelFromWrappedRequest(Mockito.any(), Mockito.any(), Mockito.eq(ComponentExporter.class))).thenReturn(componentExporter);
    // WHEN
    Map<String, ComponentExporter> exporterMap = ContentFragmentUtils.getComponentExporters(slingContext.resourceResolver().getResource("/foo").listChildren(), modelFactory, slingHttpServletRequest, null);
    // THEN
    Assertions.assertEquals(componentExporter, exporterMap.get("bar"));
    Assertions.assertEquals(componentExporter, exporterMap.get("qux"));
}
Also used : AemContext(io.wcm.testing.mock.aem.junit5.AemContext) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ComponentExporter(com.adobe.cq.export.json.ComponentExporter) ModelFactory(org.apache.sling.models.factory.ModelFactory) Test(org.junit.jupiter.api.Test)

Example 4 with ModelFactory

use of org.apache.sling.models.factory.ModelFactory in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ClientLibrariesImplTest method testUtilsGetTemplateResourceTypes.

@Test
void testUtilsGetTemplateResourceTypes() {
    Page page = context.currentPage(PAGE_WITH_TEMPLATE);
    ModelFactory modelFactory = mock(ModelFactory.class);
    Set<String> resourceTypes = Utils.getTemplateResourceTypes(page, context.request(), modelFactory);
    Set<String> expectedResourceTypes = new HashSet<>(Arrays.asList("core/wcm/components/page/v2/page", "wcm/foundation/components/responsivegrid", "core/wcm/components/text/v1/text"));
    assertEquals(expectedResourceTypes, resourceTypes);
}
Also used : Page(com.day.cq.wcm.api.Page) ModelFactory(org.apache.sling.models.factory.ModelFactory) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

ModelFactory (org.apache.sling.models.factory.ModelFactory)4 Test (org.junit.jupiter.api.Test)3 HashSet (java.util.HashSet)2 Resource (org.apache.sling.api.resource.Resource)2 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)2 ComponentExporter (com.adobe.cq.export.json.ComponentExporter)1 ExperienceFragment (com.adobe.cq.wcm.core.components.models.ExperienceFragment)1 YouTube (com.adobe.cq.wcm.core.components.models.embeddable.YouTube)1 Page (com.day.cq.wcm.api.Page)1 AemContext (io.wcm.testing.mock.aem.junit5.AemContext)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 SlingBindings (org.apache.sling.api.scripting.SlingBindings)1 MockRequestPathInfo (org.apache.sling.servlethelpers.MockRequestPathInfo)1