Search in sources :

Example 1 with LiveCopy

use of com.day.cq.wcm.msm.api.LiveCopy 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 2 with LiveCopy

use of com.day.cq.wcm.msm.api.LiveCopy 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

LiveCopy (com.day.cq.wcm.msm.api.LiveCopy)2 LiveRelationship (com.day.cq.wcm.msm.api.LiveRelationship)2 RangeIterator (javax.jcr.RangeIterator)2 WCMException (com.day.cq.wcm.api.WCMException)1 LiveRelationshipManager (com.day.cq.wcm.msm.api.LiveRelationshipManager)1 ArrayList (java.util.ArrayList)1 Resource (org.apache.sling.api.resource.Resource)1 Nullable (org.jetbrains.annotations.Nullable)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1