Search in sources :

Example 1 with LiveRelationship

use of com.day.cq.wcm.msm.api.LiveRelationship in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ContainerServletTest method setUp.

@BeforeEach
public void setUp() throws WCMException, NoSuchFieldException {
    context.load().json(TEST_BASE + CoreComponentTestContext.TEST_CONTENT_JSON, CONTENT_ROOT);
    // make the carousel component the current resource
    context.currentResource(CAROUSEL_PATH);
    // set http method
    context.request().setMethod("POST");
    // live relationship manager
    LiveRelationshipManager liveRelationshipManager = mock(LiveRelationshipManager.class);
    when(liveRelationshipManager.getLiveRelationship(any(Resource.class), anyBoolean())).then(invocation -> {
        Object[] arguments = invocation.getArguments();
        Resource resource = (Resource) arguments[0];
        if (LIVE_COPY_PATH.equals(resource.getPath())) {
            LiveRelationship liveRelationship = mock(LiveRelationship.class);
            LiveStatus liveStatus = mock(LiveStatus.class);
            when(liveStatus.isSourceExisting()).thenReturn(true);
            when(liveRelationship.getStatus()).thenReturn(liveStatus);
            return liveRelationship;
        }
        return null;
    });
    FieldSetter.setField(servlet, servlet.getClass().getDeclaredField("liveRelationshipManager"), liveRelationshipManager);
}
Also used : LiveRelationshipManager(com.day.cq.wcm.msm.api.LiveRelationshipManager) Resource(org.apache.sling.api.resource.Resource) LiveRelationship(com.day.cq.wcm.msm.api.LiveRelationship) LiveStatus(com.day.cq.wcm.msm.api.LiveStatus) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with LiveRelationship

use of com.day.cq.wcm.msm.api.LiveRelationship in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class NavigationImplTest method internalSetup.

protected void internalSetup() throws WCMException {
    context.load().json(testBase + CoreComponentTestContext.TEST_CONTENT_JSON, "/content");
    context.load().json(testBase + "/test-conf.json", "/conf");
    context.registerService(LanguageManager.class, new MockLanguageManager());
    LiveRelationshipManager relationshipManager = mock(LiveRelationshipManager.class);
    when(relationshipManager.getLiveRelationships(any(Resource.class), isNull(), isNull())).then(invocation -> {
        Object[] arguments = invocation.getArguments();
        Resource resource = (Resource) arguments[0];
        if ("/content/navigation-blueprint".equals(resource.getPath())) {
            LiveRelationship liveRelationship = mock(LiveRelationship.class);
            when(liveRelationship.getTargetPath()).thenReturn("/content/navigation-livecopy");
            final ArrayList<LiveRelationship> relationships = new ArrayList<>();
            relationships.add(liveRelationship);
            final Iterator<LiveRelationship> iterator = relationships.iterator();
            return new RangeIterator() {

                int index = 0;

                @Override
                public void skip(long skipNum) {
                }

                @Override
                public long getSize() {
                    return relationships.size();
                }

                @Override
                public long getPosition() {
                    return index;
                }

                @Override
                public boolean hasNext() {
                    return iterator.hasNext();
                }

                @Override
                public Object next() {
                    index++;
                    return iterator.next();
                }
            };
        }
        return null;
    });
    context.registerService(LiveRelationshipManager.class, relationshipManager);
}
Also used : RangeIterator(javax.jcr.RangeIterator) LiveRelationshipManager(com.day.cq.wcm.msm.api.LiveRelationshipManager) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) LiveRelationship(com.day.cq.wcm.msm.api.LiveRelationship) MockLanguageManager(io.wcm.testing.mock.aem.MockLanguageManager)

Example 3 with LiveRelationship

use of com.day.cq.wcm.msm.api.LiveRelationship in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class LocalizationUtils method getLocalPage.

/**
 * Given the current requested page and a reference page, this method will determine a page belonging to the
 * current site and locale that can be used instead of the reference site.
 *
 * Specifically, if the reference page and the current page are both found under a language root, and that language
 * root is not the same, then the returned page is the page located under the current page's language root at the
 * same relative path as the reference page is located under it's own language root; or empty if that page does not
 * exist.
 *
 * If either the reference page or the current page are not located under a language root, or if they share the
 * same language root, and if the reference page has a live relationship where the target is the current page or
 * an ancestor of the current page, then the target of that live relationship is returned; or empty if that page
 * does not exist.
 *
 * All other conditions return empty.
 *
 * @param referencePage The referenced page.
 * @param currentPage The current page.
 * @param resourceResolver A resource resolver.
 * @param languageManager The language manager service.
 * @param relationshipManager The live relationship manager service.
 * @return A page, that belongs to the same language or live copy as the current page, and can be used as the local
 * alternative to the referenced page, or empty if no such page exists.
 */
public static Optional<Page> getLocalPage(@NotNull final Page referencePage, @NotNull final Page currentPage, @NotNull final ResourceResolver resourceResolver, @NotNull final LanguageManager languageManager, @NotNull final LiveRelationshipManager relationshipManager) {
    Page referencePageLanguageRoot = Optional.ofNullable(referencePage.getPath()).map(resourceResolver::getResource).map(languageManager::getLanguageRoot).orElse(null);
    Page currentPageLanguageRoot = languageManager.getLanguageRoot(currentPage.getContentResource());
    if (referencePageLanguageRoot != null && currentPageLanguageRoot != null && !referencePageLanguageRoot.equals(currentPageLanguageRoot)) {
        // check if there's a language copy of the navigation root
        return Optional.ofNullable(referencePage.getPageManager().getPage(ResourceUtil.normalize(String.join("/", currentPageLanguageRoot.getPath(), getRelativePath(referencePageLanguageRoot, referencePage)))));
    } else {
        try {
            String currentPagePath = currentPage.getPath() + "/";
            return Optional.of(Optional.ofNullable((Iterator<LiveRelationship>) relationshipManager.getLiveRelationships(referencePage.adaptTo(Resource.class), null, null)).map(liveRelationshipIterator -> StreamSupport.stream(((Iterable<LiveRelationship>) () -> liveRelationshipIterator).spliterator(), false)).orElseGet(Stream::empty).map(LiveRelationship::getTargetPath).filter(target -> currentPagePath.startsWith(target + "/")).map(referencePage.getPageManager()::getPage).findFirst().orElse(referencePage));
        } catch (WCMException e) {
        // ignore it
        }
    }
    return Optional.empty();
}
Also used : LiveRelationship(com.day.cq.wcm.msm.api.LiveRelationship) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ResourceUtil(org.apache.sling.api.resource.ResourceUtil) Iterator(java.util.Iterator) Resource(org.apache.sling.api.resource.Resource) LanguageManager(com.day.cq.wcm.api.LanguageManager) Page(com.day.cq.wcm.api.Page) LiveRelationshipManager(com.day.cq.wcm.msm.api.LiveRelationshipManager) Nullable(org.jetbrains.annotations.Nullable) WCMException(com.day.cq.wcm.api.WCMException) Stream(java.util.stream.Stream) Optional(java.util.Optional) StreamSupport(java.util.stream.StreamSupport) NotNull(org.jetbrains.annotations.NotNull) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) Stream(java.util.stream.Stream) LiveRelationship(com.day.cq.wcm.msm.api.LiveRelationship) WCMException(com.day.cq.wcm.api.WCMException)

Example 4 with LiveRelationship

use of com.day.cq.wcm.msm.api.LiveRelationship in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ExperienceFragmentImplTest method setUp.

@BeforeEach
void setUp() throws WCMException {
    context.load().json(TEST_BASE + CoreComponentTestContext.TEST_CONTENT_JSON, CONTENT_ROOT);
    context.load().json(TEST_BASE + CoreComponentTestContext.TEST_CONF_JSON, CONF_ROOT);
    context.load().json(TEST_BASE + CoreComponentTestContext.TEST_APPS_JSON, APPS_ROOT);
    LiveRelationshipManager relationshipManager = mock(LiveRelationshipManager.class);
    when(relationshipManager.isSource(any(Resource.class))).then(invocation -> {
        Object[] arguments = invocation.getArguments();
        Resource resource = (Resource) arguments[0];
        return BLUEPRINT_PAGE.equals(resource.getPath());
    });
    when(relationshipManager.getLiveRelationships(any(Resource.class), isNull(), isNull())).then(invocation -> {
        Object[] arguments = invocation.getArguments();
        Resource resource = (Resource) arguments[0];
        if (BLUEPRINT_PAGE.equals(resource.getPath())) {
            LiveRelationship liveRelationship = mock(LiveRelationship.class);
            LiveCopy liveCopy = mock(LiveCopy.class);
            when(liveCopy.getBlueprintPath()).thenReturn(BLUEPRINT_ROOT);
            when(liveRelationship.getLiveCopy()).thenReturn(liveCopy);
            final ArrayList<LiveRelationship> relationships = new ArrayList<>();
            relationships.add(liveRelationship);
            final Iterator<LiveRelationship> iterator = relationships.iterator();
            return new RangeIterator() {

                int index = 0;

                @Override
                public void skip(long skipNum) {
                }

                @Override
                public long getSize() {
                    return relationships.size();
                }

                @Override
                public long getPosition() {
                    return index;
                }

                @Override
                public boolean hasNext() {
                    return iterator.hasNext();
                }

                @Override
                public Object next() {
                    index++;
                    return iterator.next();
                }
            };
        }
        return null;
    });
    when(relationshipManager.hasLiveRelationship(any(Resource.class))).then(invocation -> {
        Object[] arguments = invocation.getArguments();
        Resource resource = (Resource) arguments[0];
        return LIVECOPY_PAGE.equals(resource.getPath());
    });
    when(relationshipManager.getLiveRelationship(any(Resource.class), anyBoolean())).then(invocation -> {
        Object[] arguments = invocation.getArguments();
        Resource resource = (Resource) arguments[0];
        if (LIVECOPY_PAGE.equals(resource.getPath())) {
            LiveRelationship liveRelationship = mock(LiveRelationship.class);
            LiveCopy liveCopy = mock(LiveCopy.class);
            when(liveCopy.getPath()).thenReturn(LIVECOPY_ROOT);
            when(liveRelationship.getLiveCopy()).thenReturn(liveCopy);
            return liveRelationship;
        }
        return null;
    });
    context.registerService(LiveRelationshipManager.class, relationshipManager);
}
Also used : LiveCopy(com.day.cq.wcm.msm.api.LiveCopy) RangeIterator(javax.jcr.RangeIterator) LiveRelationshipManager(com.day.cq.wcm.msm.api.LiveRelationshipManager) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) LiveRelationship(com.day.cq.wcm.msm.api.LiveRelationship) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with LiveRelationship

use of com.day.cq.wcm.msm.api.LiveRelationship in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class LocalizationUtils method getBlueprintPath.

/**
 * Returns the path of the blueprint of the resource.
 *
 * @param resource the resource
 * @param relationshipManager the live relationship manager service
 * @return the path of the blueprint of the resource if it exists, {@code null} otherwise
 */
@Nullable
public static String getBlueprintPath(@NotNull Resource resource, @NotNull LiveRelationshipManager relationshipManager) {
    try {
        if (relationshipManager.isSource(resource)) {
            // the resource is a blueprint
            RangeIterator liveCopiesIterator = relationshipManager.getLiveRelationships(resource, null, null);
            if (liveCopiesIterator != null) {
                LiveRelationship relationship = (LiveRelationship) liveCopiesIterator.next();
                LiveCopy liveCopy = relationship.getLiveCopy();
                if (liveCopy != null) {
                    return liveCopy.getBlueprintPath();
                }
            }
        }
    } catch (WCMException e) {
        LOGGER.error("Unable to get the blueprint: {}", e.getMessage());
    }
    return null;
}
Also used : LiveCopy(com.day.cq.wcm.msm.api.LiveCopy) RangeIterator(javax.jcr.RangeIterator) LiveRelationship(com.day.cq.wcm.msm.api.LiveRelationship) WCMException(com.day.cq.wcm.api.WCMException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

LiveRelationship (com.day.cq.wcm.msm.api.LiveRelationship)6 Resource (org.apache.sling.api.resource.Resource)5 LiveRelationshipManager (com.day.cq.wcm.msm.api.LiveRelationshipManager)4 WCMException (com.day.cq.wcm.api.WCMException)3 RangeIterator (javax.jcr.RangeIterator)3 LiveCopy (com.day.cq.wcm.msm.api.LiveCopy)2 ArrayList (java.util.ArrayList)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 Nullable (org.jetbrains.annotations.Nullable)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 LanguageManager (com.day.cq.wcm.api.LanguageManager)1 Page (com.day.cq.wcm.api.Page)1 LiveStatus (com.day.cq.wcm.msm.api.LiveStatus)1 MockLanguageManager (io.wcm.testing.mock.aem.MockLanguageManager)1 Iterator (java.util.Iterator)1 Optional (java.util.Optional)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 Node (javax.jcr.Node)1 RepositoryException (javax.jcr.RepositoryException)1