SLING-8120 - pass the ResourceResolver to CapabilitiesSource
authorBertrand Delacretaz <bdelacretaz@apache.org>
Tue, 20 Nov 2018 14:14:49 +0000 (15:14 +0100)
committerBertrand Delacretaz <bdelacretaz@apache.org>
Tue, 20 Nov 2018 14:14:49 +0000 (15:14 +0100)
pom.xml
src/main/java/org/apache/sling/capabilities/jcr/SearchSource.java
src/test/java/org/apache/sling/capabilities/jcr/SearchSourceTest.java

diff --git a/pom.xml b/pom.xml
index 0f5afd7..ac924c3 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <version>${org.ops4j.pax.exam.version}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <version>2.23.0</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 </project>
index 74ee64b..aafe20b 100644 (file)
@@ -25,6 +25,7 @@ import javax.jcr.Session;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
 import javax.jcr.query.QueryResult;
+import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.capabilities.CapabilitiesSource;
 import org.apache.sling.jcr.api.SlingRepository;
 import org.apache.sling.serviceusermapping.ServiceUserMapped;
@@ -89,8 +90,8 @@ public class SearchSource implements CapabilitiesSource {
     }
 
     @Override
-    public Map<String, Object> getCapabilities() throws Exception {
-        refreshCachedValues();
+    public Map<String, Object> getCapabilities(ResourceResolver rr) throws Exception {
+        refreshCachedValues(rr);
         final Map<String, Object> result = new HashMap<>();
         result.put("similarity.search.active", similaritySearchActiveResult);
         return result;
@@ -100,7 +101,8 @@ public class SearchSource implements CapabilitiesSource {
      *  searching for any index definition that has useInSimilarity=true.
      *  Cache the result to avoid making too many searches.
      */
-    private void refreshCachedValues() {
+    private void refreshCachedValues(ResourceResolver unused) {
+
         if(System.currentTimeMillis() < similaritySearchCacheExpires) {
             log.debug("Using cached similaritySearchActive value");
             return;
@@ -111,6 +113,11 @@ public class SearchSource implements CapabilitiesSource {
         synchronized(this) {
             Session session = null;
             try {
+                // TODO for now we need a service user as in general users do not have
+                // access to /oak:index where the information that we need is found.
+                // This information should be provided by Oak in a different way such as
+                // JCR Repository descriptors or OSGi service properties on the Repository
+                // object.
                 session = repository.loginService(SUBSERVICE_NAME, null);
                 final QueryManager qm = session.getWorkspace().getQueryManager();
                 final QueryResult qr = qm.createQuery(similarityIndexQuery, Query.XPATH).execute();
index 7f72acf..f7fb275 100644 (file)
 package org.apache.sling.capabilities.jcr;
 
 import java.io.IOException;
-import java.math.BigDecimal;
 import java.util.Dictionary;
 import java.util.Hashtable;
-import java.util.Map;
 import java.util.UUID;
-import javax.inject.Inject;
 import javax.jcr.Node;
 import javax.jcr.Session;
+import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.capabilities.CapabilitiesSource;
 import org.apache.sling.jcr.api.SlingRepository;
 import org.apache.sling.serviceusermapping.ServiceUserMapped;
@@ -36,9 +34,9 @@ import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
-import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
+import org.mockito.Mockito;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
 
@@ -50,6 +48,7 @@ public class SearchSourceTest {
     private static final String SIMILARITY_ACTIVE_CAP = "similarity.search.active";
 
     private CapabilitiesSource searchSource;
+    private ResourceResolver resolver = Mockito.mock(ResourceResolver.class);
 
     private Dictionary<String, Object> props(Object ... nameValuePairs) {
         final Dictionary<String, Object> props = new Hashtable<>();
@@ -95,8 +94,8 @@ public class SearchSourceTest {
     @Test
     public void testNoSimilarity() throws Exception {
         registerSearchSource(true);
-        assertNotNull(searchSource.getCapabilities());
-        assertEquals("false", searchSource.getCapabilities().get(SIMILARITY_ACTIVE_CAP));
+        assertNotNull(searchSource.getCapabilities(resolver));
+        assertEquals("false", searchSource.getCapabilities(resolver).get(SIMILARITY_ACTIVE_CAP));
     }
 
     @Test
@@ -104,8 +103,8 @@ public class SearchSourceTest {
         registerSearchSource(true);
         createMockIndexNode("/oak:index", "foo", "useInSimilarity", true);
 
-        assertNotNull(searchSource.getCapabilities());
-        assertEquals("true", searchSource.getCapabilities().get(SIMILARITY_ACTIVE_CAP));
+        assertNotNull(searchSource.getCapabilities(resolver));
+        assertEquals("true", searchSource.getCapabilities(resolver).get(SIMILARITY_ACTIVE_CAP));
     }
 
     @Test(expected=ReferenceViolationException.class)
@@ -123,8 +122,8 @@ public class SearchSourceTest {
                 "cacheLifetimeSeconds", lifetimeSeconds);
 
         // With our custom query we get false first
-        assertNotNull(searchSource.getCapabilities());
-        assertEquals("false", searchSource.getCapabilities().get(SIMILARITY_ACTIVE_CAP));
+        assertNotNull(searchSource.getCapabilities(resolver));
+        assertEquals("false", searchSource.getCapabilities(resolver).get(SIMILARITY_ACTIVE_CAP));
 
         // Create a node that causes the query to return something
         // The capability value will change after some time, once its cache expires
@@ -136,7 +135,7 @@ public class SearchSourceTest {
         int nTrue = 0;
         final long testEnd = System.currentTimeMillis() + lifetimeSeconds * 1000L + 1500L;
         while(System.currentTimeMillis() < testEnd) {
-            final Object value = searchSource.getCapabilities().get(SIMILARITY_ACTIVE_CAP);
+            final Object value = searchSource.getCapabilities(resolver).get(SIMILARITY_ACTIVE_CAP);
             if("false".equals(value)) {
                 assertEquals("Expecting true value to come after all false values", 0, nTrue);
                 nFalse++;