Search in sources :

Example 1 with MockPropertyIterator

use of org.apache.sling.commons.testing.jcr.MockPropertyIterator in project acs-aem-commons by Adobe-Consulting-Services.

the class RootNodeMockFactory method generateBucketNodeChain.

private Node[] generateBucketNodeChain(Node rootNode, boolean isEmpty) throws RepositoryException {
    final Node[] bucketNodeChain = new Node[settings.bucketDepth];
    Node currentParentNode = rootNode;
    for (int i = 0; i < settings.bucketDepth; i++) {
        final Node node = mockStandardNode("bucketnode" + (isEmpty ? "-empty" : "") + "-level-" + (i + 1));
        bucketNodeChain[i] = node;
        when(node.getParent()).thenReturn(currentParentNode);
        when(node.getProperties()).thenReturn(new MockPropertyIterator(IteratorUtils.EMPTY_ITERATOR));
        when(node.hasProperty(JCRHttpCacheStoreConstants.PN_ISCACHEENTRYNODE)).thenReturn(false);
        when(node.hasProperty(JCRHttpCacheStoreConstants.PN_ISBUCKETNODE)).thenReturn(true);
        currentParentNode = node;
    }
    for (int i = 0; i < settings.bucketDepth; i++) {
        if (i < settings.bucketDepth) {
            final Node node = bucketNodeChain[i];
            final Node childNode = bucketNodeChain[i];
            final AtomicInteger deleteCounter = new AtomicInteger();
            doAnswer(new Answer<Object>() {

                @Override
                public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                    deleteCounter.getAndIncrement();
                    return null;
                }
            }).when(node).remove();
            when(node.getParent().getNodes()).thenAnswer(new Answer<NodeIterator>() {

                @Override
                public NodeIterator answer(InvocationOnMock invocationOnMock) throws Throwable {
                    if (deleteCounter.get() > 0) {
                        return new MockNodeIterator();
                    } else {
                        return new MockNodeIterator(new Node[] { childNode });
                    }
                }
            });
        }
    }
    return bucketNodeChain;
}
Also used : MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) NodeIterator(javax.jcr.NodeIterator) MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) AbstractNode(org.apache.jackrabbit.commons.AbstractNode) Node(javax.jcr.Node) MockPropertyIterator(org.apache.sling.commons.testing.jcr.MockPropertyIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 2 with MockPropertyIterator

use of org.apache.sling.commons.testing.jcr.MockPropertyIterator in project acs-aem-commons by Adobe-Consulting-Services.

the class RootNodeMockFactory method build.

public Node build() throws RepositoryException, IOException {
    final Node rootNode = mockStandardNode("rootnode");
    when(rootNode.getPath()).thenReturn(ROOT_PATH);
    Node[] bucketNodeChain = generateBucketNodeChain(rootNode, false);
    Node parentNode = bucketNodeChain[bucketNodeChain.length - 1];
    final NodeIterator entryNodes = generateEntryNodes(parentNode);
    when(parentNode.getNodes()).thenReturn(entryNodes);
    Node[] level1nodes = new Node[1 + settings.emptyBucketNodeChainCount];
    level1nodes[0] = bucketNodeChain[0];
    for (int i = 0; i < settings.emptyBucketNodeChainCount; i++) {
        Node[] emptyBucketNodeChain = generateBucketNodeChain(rootNode, true);
        when(emptyBucketNodeChain[emptyBucketNodeChain.length - 1].getNodes()).thenReturn(new MockNodeIterator());
        level1nodes[i + 1] = emptyBucketNodeChain[0];
    }
    when(rootNode.getNodes()).thenReturn(new MockNodeIterator(level1nodes));
    when(rootNode.getProperties()).thenReturn(new MockPropertyIterator(IteratorUtils.EMPTY_ITERATOR));
    return rootNode;
}
Also used : MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) NodeIterator(javax.jcr.NodeIterator) MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) AbstractNode(org.apache.jackrabbit.commons.AbstractNode) Node(javax.jcr.Node) MockPropertyIterator(org.apache.sling.commons.testing.jcr.MockPropertyIterator)

Example 3 with MockPropertyIterator

use of org.apache.sling.commons.testing.jcr.MockPropertyIterator in project acs-aem-commons by Adobe-Consulting-Services.

the class RootNodeMockFactory method mockEntryNode.

private Node mockEntryNode(Node parentNode, int i, boolean isExpired) throws RepositoryException, IOException {
    final String nodeName = (isExpired) ? "expired-entrynode-" : "entrynode-";
    final Node entryNode = mockStandardNode(nodeName + (i + 1));
    if (settings.enableCacheEntryBinaryContent) {
        mockEntryContentNode(entryNode);
    }
    when(entryNode.hasProperty(JCRHttpCacheStoreConstants.PN_ISCACHEENTRYNODE)).thenReturn(true);
    when(entryNode.hasProperty(JCRHttpCacheStoreConstants.PN_ISBUCKETNODE)).thenReturn(false);
    when(entryNode.hasProperty(JCRHttpCacheStoreConstants.PN_EXPIRES_ON)).thenReturn(true);
    when(entryNode.getNodes()).thenReturn(new MockNodeIterator());
    when(entryNode.getProperties()).thenReturn(new MockPropertyIterator(IteratorUtils.EMPTY_ITERATOR));
    when(entryNode.getParent()).thenReturn(parentNode);
    final MockProperty expiresMockProperty = new MockProperty(JCRHttpCacheStoreConstants.PN_EXPIRES_ON);
    int seconds;
    if (isExpired) {
        seconds = -9000;
    } else {
        seconds = 9000;
    }
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.SECOND, seconds);
    expiresMockProperty.setValue(calendar);
    when(entryNode.getProperty(JCRHttpCacheStoreConstants.PN_EXPIRES_ON)).thenReturn(expiresMockProperty);
    return entryNode;
}
Also used : MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) AbstractNode(org.apache.jackrabbit.commons.AbstractNode) Node(javax.jcr.Node) Calendar(java.util.Calendar) MockPropertyIterator(org.apache.sling.commons.testing.jcr.MockPropertyIterator) MockProperty(org.apache.sling.commons.testing.jcr.MockProperty)

Aggregations

Node (javax.jcr.Node)3 AbstractNode (org.apache.jackrabbit.commons.AbstractNode)3 MockNodeIterator (org.apache.sling.commons.testing.jcr.MockNodeIterator)3 MockPropertyIterator (org.apache.sling.commons.testing.jcr.MockPropertyIterator)3 NodeIterator (javax.jcr.NodeIterator)2 Calendar (java.util.Calendar)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 MockProperty (org.apache.sling.commons.testing.jcr.MockProperty)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1