Created
June 10, 2019 23:30
-
-
Save dfparker2002/87df3ddd26020399b3946b2560a0f07a to your computer and use it in GitHub Desktop.
JCR entry node visitor + test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//src: https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/43ae3e3f13f32674d8d44c6cfff556ba1d55d26d/bundle/src/main/java/com/adobe/acs/commons/httpcache/store/jcr/impl/visitor/EntryNodeByStringKeyVisitor.java | |
import javax.jcr.Node; | |
import javax.jcr.RepositoryException; | |
import org.apache.commons.lang.StringUtils; | |
import org.apache.sling.commons.classloader.DynamicClassLoaderManager; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.adobe.acs.commons.httpcache.engine.CacheContent; | |
import com.adobe.acs.commons.httpcache.keys.CacheKey; | |
import com.adobe.acs.commons.httpcache.store.jcr.impl.handler.EntryNodeToCacheContentHandler; | |
import com.adobe.acs.commons.httpcache.store.jcr.impl.handler.EntryNodeToCacheKeyHandler; | |
import java.io.IOException; | |
public class EntryNodeByStringKeyVisitor extends AbstractNodeVisitor | |
{ | |
private static final Logger log = LoggerFactory.getLogger(EntryNodeByStringKeyVisitor.class); | |
private final DynamicClassLoaderManager dclm; | |
private final String cacheKeyStr; | |
private CacheContent cacheContent; | |
public EntryNodeByStringKeyVisitor(int maxLevel, DynamicClassLoaderManager dclm, String cacheKeyStr) { | |
super( maxLevel, -1); | |
this.dclm = dclm; | |
this.cacheKeyStr = cacheKeyStr; | |
} | |
public CacheContent getCacheContentIfPresent() | |
{ | |
return cacheContent; | |
} | |
protected void entering(final Node node, int level) throws RepositoryException | |
{ | |
super.entering(node, level); | |
if(isCacheEntryNode(node)){ | |
try { | |
final CacheKey cacheKey = getCacheKey(node); | |
if(StringUtils.equals(cacheKey.toString(), cacheKeyStr)) { | |
cacheContent = new EntryNodeToCacheContentHandler(node).get(); | |
} | |
} catch (Exception e) { | |
log.error("Exception occured in retrieving the cacheKey from the entryNode", e); | |
throw new RepositoryException(e); | |
} | |
} | |
} | |
protected CacheKey getCacheKey(final Node node) throws RepositoryException, IOException, ClassNotFoundException { | |
return new EntryNodeToCacheKeyHandler(node, dclm).get(); | |
} | |
public void visit(Node node) throws RepositoryException { | |
if(cacheContent == null){ | |
//only continue visiting | |
super.visit(node); | |
} | |
} | |
} | |
import static org.junit.Assert.assertNotNull; | |
import static org.junit.Assert.assertNull; | |
import static org.mockito.Matchers.any; | |
import static org.mockito.Mockito.mock; | |
import static org.mockito.Mockito.spy; | |
import static org.mockito.Mockito.when; | |
import java.io.IOException; | |
import javax.jcr.Node; | |
import javax.jcr.RepositoryException; | |
import org.apache.commons.lang.RandomStringUtils; | |
import org.apache.sling.commons.classloader.DynamicClassLoaderManager; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.runners.MockitoJUnitRunner; | |
import com.adobe.acs.commons.httpcache.keys.CacheKey; | |
import com.adobe.acs.commons.httpcache.store.jcr.impl.visitor.mock.RootNodeMockFactory; | |
@RunWith(MockitoJUnitRunner.class) | |
public final class EntryNodeByStringKeyVisitorTest { | |
@Test | |
public void testPresent() throws RepositoryException, IOException, ClassNotFoundException { | |
final RootNodeMockFactory.Settings settings = new RootNodeMockFactory.Settings(); | |
settings.setEntryNodeCount(10); | |
final Node rootNode = new RootNodeMockFactory(settings).build(); | |
final EntryNodeByStringKeyVisitor visitor = getMockedEntryNodeByStringKeyVisitor("[resourcePath: /content/some/path]", true); | |
visitor.visit(rootNode); | |
assertNotNull(visitor.getCacheContentIfPresent()); | |
} | |
@Test | |
public void testNotPresent() throws RepositoryException, IOException, ClassNotFoundException { | |
final RootNodeMockFactory.Settings settings = new RootNodeMockFactory.Settings(); | |
settings.setEntryNodeCount(10); | |
final Node rootNode = new RootNodeMockFactory(settings).build(); | |
final EntryNodeByStringKeyVisitor visitor = getMockedEntryNodeByStringKeyVisitor("[resourcePath: /content/some/path]", false); | |
visitor.visit(rootNode); | |
assertNull(visitor.getCacheContentIfPresent()); | |
} | |
private EntryNodeByStringKeyVisitor getMockedEntryNodeByStringKeyVisitor(String cacheKeyStr, boolean match) throws ClassNotFoundException, RepositoryException, IOException { | |
final DynamicClassLoaderManager dclm = mock(DynamicClassLoaderManager.class); | |
final EntryNodeByStringKeyVisitor visitor = spy(new EntryNodeByStringKeyVisitor(11, dclm, cacheKeyStr)); | |
final CacheKey cacheKey = mock(CacheKey.class); | |
if (match) { | |
when(cacheKey.toString()).thenReturn(cacheKeyStr); | |
} else { | |
when(cacheKey.toString()).thenReturn(RandomStringUtils.random(10000)); | |
} | |
when(visitor.getCacheKey(any(Node.class))).thenReturn(cacheKey); | |
return visitor; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment