[java-opensaml] branch main updated: OSJ-363 - Add activationCondition support to MetadataResolvers

Scott Cantor cantor.2 at osu.edu
Wed Oct 19 16:10:25 UTC 2022


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch main
in repository java-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=ae50698afb246c05a39515f600cf769e4d9bc39b

The following commit(s) were added to refs/heads/main by this push:
     new ae50698af OSJ-363 - Add activationCondition support to MetadataResolvers
ae50698af is described below

commit ae50698afb246c05a39515f600cf769e4d9bc39b
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Wed Oct 19 12:10:22 2022 -0400

    OSJ-363 - Add activationCondition support to MetadataResolvers
    
    https://shibboleth.atlassian.net/browse/OSJ-363
---
 .../impl/AbstractBatchMetadataResolver.java        |  4 +-
 .../impl/AbstractDynamicMetadataResolver.java      |  4 +-
 .../resolver/impl/AbstractMetadataResolver.java    | 75 ++++++++++++++++++----
 .../AbstractDynamicHTTPMetadataResolverTest.java   | 20 ++++++
 .../impl/AbstractDynamicMetadataResolverTest.java  | 17 +++++
 .../resolver/impl/DOMMetadataResolverTest.java     | 10 +++
 .../impl/FileBackedHTTPMetadataResolverTest.java   | 23 +++++++
 .../impl/FilesystemMetadataResolverTest.java       | 22 +++++++
 ...ctionDrivenDynamicHTTPMetadataResolverTest.java | 29 +++++++++
 .../resolver/impl/HTTPMetadataResolverTest.java    | 26 ++++++++
 .../impl/LocalDynamicMetadataResolverTest.java     | 15 +++++
 .../impl/ResourceBackedMetadataResolverTest.java   | 29 +++++++--
 12 files changed, 252 insertions(+), 22 deletions(-)

diff --git a/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/AbstractBatchMetadataResolver.java b/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/AbstractBatchMetadataResolver.java
index 4d907cce2..e530f6ff6 100644
--- a/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/AbstractBatchMetadataResolver.java
+++ b/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/AbstractBatchMetadataResolver.java
@@ -70,8 +70,6 @@ public abstract class AbstractBatchMetadataResolver extends AbstractMetadataReso
     
     /** Constructor. */
     public AbstractBatchMetadataResolver() {
-        super();
-        
         indexes = Collections.emptySet();
         
         setCacheSourceMetadata(true);
@@ -165,7 +163,7 @@ public abstract class AbstractBatchMetadataResolver extends AbstractMetadataReso
     }
 
     /** {@inheritDoc} */
-    @Override @Nonnull public Iterable<EntityDescriptor> resolve(final CriteriaSet criteria) throws ResolverException {
+    @Override @Nonnull protected Iterable<EntityDescriptor> doResolve(final CriteriaSet criteria) throws ResolverException {
         checkComponentActive();
         
         final EntityIdCriterion entityIdCriterion = criteria.get(EntityIdCriterion.class);
diff --git a/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/AbstractDynamicMetadataResolver.java b/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/AbstractDynamicMetadataResolver.java
index 0a9356750..9f2b514bf 100644
--- a/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/AbstractDynamicMetadataResolver.java
+++ b/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/AbstractDynamicMetadataResolver.java
@@ -191,8 +191,6 @@ public abstract class AbstractDynamicMetadataResolver extends AbstractMetadataRe
      * @param backgroundTaskTimer the {@link Timer} instance used to run resolver background management tasks
      */
     public AbstractDynamicMetadataResolver(@Nullable final Timer backgroundTaskTimer) {
-        super();
-        
         indexes = Collections.emptySet();
         
         if (backgroundTaskTimer == null) {
@@ -640,7 +638,7 @@ public abstract class AbstractDynamicMetadataResolver extends AbstractMetadataRe
 
     /** {@inheritDoc} */
     @Override
-    @Nonnull public Iterable<EntityDescriptor> resolve(@Nonnull final CriteriaSet criteria) throws ResolverException {
+    @Nonnull protected Iterable<EntityDescriptor> doResolve(@Nonnull final CriteriaSet criteria) throws ResolverException {
         checkComponentActive();
         
         final Context contextResolve = MetricsSupport.startTimer(timerResolve);
diff --git a/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/AbstractMetadataResolver.java b/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/AbstractMetadataResolver.java
index 3980a0ef5..3e39587b4 100644
--- a/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/AbstractMetadataResolver.java
+++ b/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/AbstractMetadataResolver.java
@@ -37,6 +37,8 @@ import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
 import org.opensaml.core.xml.io.Unmarshaller;
 import org.opensaml.core.xml.io.UnmarshallerFactory;
 import org.opensaml.core.xml.io.UnmarshallingException;
+import org.opensaml.profile.context.ProfileRequestContext;
+import org.opensaml.profile.criterion.ProfileRequestContextCriterion;
 import org.opensaml.saml.metadata.criteria.entity.EvaluableEntityDescriptorCriterion;
 import org.opensaml.saml.metadata.criteria.entity.impl.EntityDescriptorCriterionPredicateRegistry;
 import org.opensaml.saml.metadata.resolver.MetadataResolver;
@@ -73,19 +75,19 @@ public abstract class AbstractMetadataResolver extends AbstractIdentifiableIniti
         MetadataResolver {
 
     /** Class logger. */
-    private final Logger log = LoggerFactory.getLogger(AbstractMetadataResolver.class);
+    @Nonnull private final Logger log = LoggerFactory.getLogger(AbstractMetadataResolver.class);
 
     /** Unmarshaller factory used to get an unmarshaller for the metadata DOM. */
-    private UnmarshallerFactory unmarshallerFactory;
+    @Nonnull private UnmarshallerFactory unmarshallerFactory;
 
     /** Whether metadata is required to be valid. */
     private boolean requireValidMetadata;
 
     /** Filter applied to all metadata. */
-    private MetadataFilter mdFilter;
+    @Nullable private MetadataFilter mdFilter;
     
     /** Logging prefix. */
-    private String logPrefix;
+    @Nullable @NotEmpty private String logPrefix;
     
     /**
      * Whether problems during initialization should cause the provider to fail or go on without metadata. The
@@ -94,22 +96,25 @@ public abstract class AbstractMetadataResolver extends AbstractIdentifiableIniti
     private boolean failFastInitialization;
 
     /** Backing store for runtime EntityDescriptor data. */
-    private EntityBackingStore entityBackingStore;
+    @Nullable private EntityBackingStore entityBackingStore;
 
     /** Pool of parsers used to process XML. */
-    private ParserPool parser;
+    @NonnullAfterInit private ParserPool parser;
     
     /** Flag which determines whether predicates used in filtering are connected by 
      * a logical 'OR' (true) or by logical 'AND' (false). Defaults to false. */
     private boolean satisfyAnyPredicates;
     
     /** Registry used in resolving predicates from criteria. */
-    private CriterionPredicateRegistry<EntityDescriptor> criterionPredicateRegistry;
+    @Nullable private CriterionPredicateRegistry<EntityDescriptor> criterionPredicateRegistry;
     
     /** Flag which determines whether the default predicate registry will be used if one is not supplied explicitly.
      * Defaults to true. */
     private boolean useDefaultPredicateRegistry;
     
+    /** Activation condition. */
+    @Nullable private Predicate<ProfileRequestContext> activationCondition;
+    
     /** Constructor. */
     public AbstractMetadataResolver() {
         failFastInitialization = true;
@@ -166,7 +171,7 @@ public abstract class AbstractMetadataResolver extends AbstractIdentifiableIniti
      * 
      * @return pool of parsers to use to parse XML
      */
-    @Nonnull public ParserPool getParserPool() {
+    @NonnullAfterInit public ParserPool getParserPool() {
         return parser;
     }
 
@@ -249,8 +254,27 @@ public abstract class AbstractMetadataResolver extends AbstractIdentifiableIniti
         useDefaultPredicateRegistry = flag;
     }
 
+    /**
+     * Get an activation condition for this resolver.
+     * 
+     * @return activation condition
+     */
+    @Nullable public Predicate<ProfileRequestContext> getActivationCondition() {
+        return activationCondition;
+    }
+    
+    /**
+     * Set an activation condition for this resolver.
+     * 
+     * @param condition condition to set
+     */
+    public void setActivationCondition(@Nullable final Predicate<ProfileRequestContext> condition) {
+        checkSetterPreconditions();
+        activationCondition = condition;
+    }
+
     /** {@inheritDoc} */
-    @Override @Nullable public EntityDescriptor resolveSingle(final CriteriaSet criteria) throws ResolverException {
+    @Nullable public EntityDescriptor resolveSingle(@Nullable final CriteriaSet criteria) throws ResolverException {
         checkComponentActive();
         final Iterable<EntityDescriptor> iterable = resolve(criteria);
         if (iterable != null) {
@@ -262,12 +286,38 @@ public abstract class AbstractMetadataResolver extends AbstractIdentifiableIniti
         return null;
     }
     
+    /** {@inheritDoc} */
+    @Nullable public Iterable<EntityDescriptor> resolve(@Nullable final CriteriaSet criteria) throws ResolverException {
+        checkComponentActive();
+        if (activationCondition != null) {
+            final ProfileRequestContextCriterion prc = criteria.get(ProfileRequestContextCriterion.class);
+            if (!activationCondition.test(prc != null ? prc.getProfileRequestContext() : null)) {
+                log.info("{} Metadata resolver bypassed due to failed activation condition", getLogPrefix());
+                return null; 
+            }
+        }
+        
+        return doResolve(criteria);
+    }
+    
+    /**
+     * Subclasses should override this method.
+     * 
+     * @param criteria input criteria
+     * 
+     * @return resolution outcome
+     * 
+     * @throws ResolverException if an error occurs
+     */
+    @Nullable protected abstract Iterable<EntityDescriptor> doResolve(@Nullable final CriteriaSet criteria)
+            throws ResolverException;
+    
     /**
      * Get the XMLObject unmarshaller factory to use.
      * 
      * @return the unmarshaller factory instance to use
      */
-    protected UnmarshallerFactory getUnmarshallerFactory() {
+    @Nonnull protected UnmarshallerFactory getUnmarshallerFactory() {
         return unmarshallerFactory;
     }
 
@@ -279,11 +329,11 @@ public abstract class AbstractMetadataResolver extends AbstractIdentifiableIniti
             initMetadataResolver();
         } catch (final ComponentInitializationException e) {
             if (isFailFastInitialization()) {
-                log.error("{} Metadata provider failed to properly initialize, fail-fast=true, halting", 
+                log.error("{} Metadata resolver failed to properly initialize, fail-fast=true, halting", 
                         getLogPrefix());
                 throw e;
             }
-            log.error("{} Metadata provider failed to properly initialize, fail-fast=false, "
+            log.error("{} Metadata resolver failed to properly initialize, fail-fast=false, "
                     + "continuing on in a degraded state", getLogPrefix(), e);
         }
     }
@@ -295,6 +345,7 @@ public abstract class AbstractMetadataResolver extends AbstractIdentifiableIniti
         entityBackingStore = null;
         parser = null;
         criterionPredicateRegistry = null;
+        activationCondition = null;
 
         super.doDestroy();
     }
diff --git a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/AbstractDynamicHTTPMetadataResolverTest.java b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/AbstractDynamicHTTPMetadataResolverTest.java
index 7de904c3b..098199fe9 100644
--- a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/AbstractDynamicHTTPMetadataResolverTest.java
+++ b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/AbstractDynamicHTTPMetadataResolverTest.java
@@ -53,6 +53,8 @@ public class AbstractDynamicHTTPMetadataResolverTest extends XMLObjectBaseTestCa
     
     private byte[] entityDescriptorBytes;
     
+    private boolean allowActivation;
+    
     @BeforeMethod
     public void setUp() throws Exception {
         httpClientBuilder = new HttpClientBuilder();
@@ -70,7 +72,10 @@ public class AbstractDynamicHTTPMetadataResolverTest extends XMLObjectBaseTestCa
         resolver = new MockDynamicHTTPMetadataResolver(httpClient);
         resolver.setId("myDynamicResolver");
         resolver.setParserPool(parserPool);
+        resolver.setActivationCondition(prc -> {return allowActivation;});
         resolver.initialize();
+        
+        allowActivation = true;
     }
     
     @AfterMethod
@@ -80,6 +85,21 @@ public class AbstractDynamicHTTPMetadataResolverTest extends XMLObjectBaseTestCa
         }
     }
     
+    @Test
+    public void testInactive() throws ResolverException {
+        allowActivation = false;
+        
+        // Test uses MDQ protocol
+        String baseURL = "https://mdq.incommon.org";
+        String entityID = "urn:mace:incommon:osu.edu";
+        String requestURL = new MetadataQueryProtocolRequestURLBuilder(baseURL).apply(new CriteriaSet(new EntityIdCriterion(entityID)));
+        
+        CriteriaSet criteriaSet = new CriteriaSet(new RequestURLCriterion(requestURL));
+        
+        EntityDescriptor ed = resolver.resolveSingle(criteriaSet);
+        Assert.assertNull(ed);
+    }
+    
     @Test
     public void testBasicRequest() throws ResolverException {
         // Test uses MDQ protocol
diff --git a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/AbstractDynamicMetadataResolverTest.java b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/AbstractDynamicMetadataResolverTest.java
index 8d5ea14fb..116e7ebff 100644
--- a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/AbstractDynamicMetadataResolverTest.java
+++ b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/AbstractDynamicMetadataResolverTest.java
@@ -98,6 +98,8 @@ public class AbstractDynamicMetadataResolverTest extends XMLObjectBaseTestCase {
     private SignatureTrustEngine signatureTrustEngine;
     private SignatureValidationFilter signatureValidationFilter;
     
+    private boolean allowActivation;
+    
     @BeforeClass
     protected void setUpSigningSupport() throws NoSuchAlgorithmException, NoSuchProviderException {
         KeyPair kp = KeySupport.generateKeyPair(JCAConstants.KEY_ALGO_RSA, 1024, null);
@@ -165,7 +167,10 @@ public class AbstractDynamicMetadataResolverTest extends XMLObjectBaseTestCase {
         
         resolver = new MockDynamicResolver(sourceMap);
         resolver.setId("test123");
+        resolver.setActivationCondition(prc -> {return allowActivation;});
         resolver.setParserPool(XMLObjectProviderRegistrySupport.getParserPool());
+        
+        allowActivation = true;
     }
     
     @AfterMethod
@@ -174,7 +179,19 @@ public class AbstractDynamicMetadataResolverTest extends XMLObjectBaseTestCase {
             resolver.destroy();
         }
     }
+
     
+    @Test
+    public void testInactive() throws ComponentInitializationException, ResolverException {
+        allowActivation = false;
+        
+        resolver.initialize();
+        
+        DynamicEntityBackingStore backingStore = resolver.getBackingStore();
+        
+        Assert.assertNull(resolver.resolveSingle(new CriteriaSet(new EntityIdCriterion(id1))));
+    }
+
     @Test
     public void testNoEntities() throws ComponentInitializationException, ResolverException {
         resolver.initialize();
diff --git a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/DOMMetadataResolverTest.java b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/DOMMetadataResolverTest.java
index 44710c7d4..549b7853e 100644
--- a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/DOMMetadataResolverTest.java
+++ b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/DOMMetadataResolverTest.java
@@ -45,6 +45,8 @@ public class DOMMetadataResolverTest extends XMLObjectBaseTestCase {
     private String entityID;
 
     private CriteriaSet criteriaSet;
+    
+    private boolean allowActivation;
 
     @BeforeMethod
     protected void setUp() throws Exception {
@@ -60,6 +62,7 @@ public class DOMMetadataResolverTest extends XMLObjectBaseTestCase {
         }
         
         metadataProvider.setId("test");
+        metadataProvider.setActivationCondition(prc -> {return allowActivation;});
         metadataProvider.initialize();
         
         criteriaSet = new CriteriaSet(new EntityIdCriterion(entityID));
@@ -67,9 +70,16 @@ public class DOMMetadataResolverTest extends XMLObjectBaseTestCase {
 
     @Test
     public void testGetEntityDescriptor() throws ResolverException {
+        allowActivation = true;
+        
         EntityDescriptor descriptor = metadataProvider.resolveSingle(criteriaSet);
         Assert.assertNotNull(descriptor, "Retrieved entity descriptor was null");
         Assert.assertEquals(descriptor.getEntityID(), entityID, "Entity's ID does not match requested ID");
+
+        allowActivation = false;
+        
+        descriptor = metadataProvider.resolveSingle(criteriaSet);
+        Assert.assertNull(descriptor, "Retrieved entity descriptor was not null");
     }
     
     @Test
diff --git a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FileBackedHTTPMetadataResolverTest.java b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FileBackedHTTPMetadataResolverTest.java
index f1b811ca3..2a387bcab 100644
--- a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FileBackedHTTPMetadataResolverTest.java
+++ b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FileBackedHTTPMetadataResolverTest.java
@@ -92,6 +92,29 @@ public class FileBackedHTTPMetadataResolverTest extends XMLObjectBaseTestCase {
         Files.deleteIfExists(nioBackupFilePath);
     }
     
+    @Test
+    public void testInactive() throws Exception {
+        final boolean allowActivation = false;
+
+        Assert.assertFalse(backupFile.exists());
+        
+        metadataProvider = new FileBackedHTTPMetadataResolver(httpClientBuilder.buildClient(), metadataURLHttp, backupFilePath);
+        metadataProvider.setParserPool(parserPool);
+        metadataProvider.setId("test");
+        metadataProvider.setActivationCondition(prc -> {return allowActivation;});
+        metadataProvider.initialize();
+
+        Assert.assertNotNull(metadataProvider.wasLastRefreshSuccess());
+        Assert.assertTrue(metadataProvider.wasLastRefreshSuccess());
+        Assert.assertNull(metadataProvider.getLastFailureCause());
+        
+        Assert.assertFalse(metadataProvider.isInitializedFromBackupFile());
+        Assert.assertTrue(backupFile.exists());
+        
+        EntityDescriptor descriptor = metadataProvider.resolveSingle(criteriaSet);
+        Assert.assertNull(descriptor, "Retrieved entity descriptor was not null");
+    }
+    
     /**
      * Tests the basic success case.
      * 
diff --git a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FilesystemMetadataResolverTest.java b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FilesystemMetadataResolverTest.java
index d2769be03..51d753e82 100644
--- a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FilesystemMetadataResolverTest.java
+++ b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FilesystemMetadataResolverTest.java
@@ -33,6 +33,7 @@ import org.testng.Assert;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import com.google.common.base.Predicates;
 import com.google.common.io.Files;
 
 public class FilesystemMetadataResolverTest extends XMLObjectBaseTestCase {
@@ -56,6 +57,27 @@ public class FilesystemMetadataResolverTest extends XMLObjectBaseTestCase {
         criteriaSet = new CriteriaSet(new EntityIdCriterion(entityID));
     }
 
+    @Test
+    public void testInactive() throws Exception {
+        
+        metadataProvider = new FilesystemMetadataResolver(mdFile);
+        metadataProvider.setParserPool(parserPool);
+        metadataProvider.setId("test");
+        metadataProvider.setActivationCondition(Predicates.alwaysFalse());
+        metadataProvider.initialize();
+
+        Assert.assertNotNull(metadataProvider.wasLastRefreshSuccess());
+        Assert.assertTrue(metadataProvider.wasLastRefreshSuccess());
+        Assert.assertNull(metadataProvider.getLastFailureCause());
+        
+        Assert.assertNotNull(metadataProvider.wasLastRefreshSuccess());
+        Assert.assertTrue(metadataProvider.wasLastRefreshSuccess());
+        Assert.assertNull(metadataProvider.getLastFailureCause());
+        
+        EntityDescriptor descriptor = metadataProvider.resolveSingle(criteriaSet);
+        Assert.assertNull(descriptor, "Retrieved entity descriptor was not null");
+    }
+    
     /**
      * Tests the {@link HTTPMetadataResolver#lookupEntityID(String)} method.
      * 
diff --git a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FunctionDrivenDynamicHTTPMetadataResolverTest.java b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FunctionDrivenDynamicHTTPMetadataResolverTest.java
index 0a4027397..791b1ea34 100644
--- a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FunctionDrivenDynamicHTTPMetadataResolverTest.java
+++ b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/FunctionDrivenDynamicHTTPMetadataResolverTest.java
@@ -36,6 +36,8 @@ import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import com.google.common.base.Predicates;
+
 import net.shibboleth.shared.codec.StringDigester;
 import net.shibboleth.shared.codec.StringDigester.OutputFormat;
 import net.shibboleth.shared.httpclient.HttpClientBuilder;
@@ -61,6 +63,33 @@ public class FunctionDrivenDynamicHTTPMetadataResolverTest extends XMLObjectBase
         }
     }
     
+    
+    @Test
+    public void testInactive() throws Exception {
+        // Repo should return 'text/xml', which is supported by default.
+        String template = RepositorySupport.buildHTTPResourceURL("java-opensaml", "opensaml-saml-impl/src/test/resources/org/opensaml/saml/metadata/resolver/impl/${entityID}.xml", false);
+        String entityID = "https://www.example.org/sp";
+        
+        // Digesting the entityID is a little artificial for the test, but means we can test more easily against a path in the repo.
+        TemplateRequestURLBuilder requestURLBuilder = new TemplateRequestURLBuilder(
+                VelocityEngine.newVelocityEngine(), 
+                template, 
+                EncodingStyle.path, 
+                new StringDigester("SHA-1", OutputFormat.HEX_LOWER));
+        
+        resolver = new FunctionDrivenDynamicHTTPMetadataResolver(httpClientBuilder.buildClient());
+        resolver.setId("myDynamicResolver");
+        resolver.setParserPool(parserPool);
+        resolver.setRequestURLBuilder(requestURLBuilder);
+        resolver.setActivationCondition(Predicates.alwaysFalse());
+        resolver.initialize();
+        
+        CriteriaSet criteriaSet = new CriteriaSet( new EntityIdCriterion(entityID));
+        
+        EntityDescriptor ed = resolver.resolveSingle(criteriaSet);
+        Assert.assertNull(ed);
+    }
+    
     @Test
     public void testTemplateFromRepoDefaultContentTypes() throws Exception {
         // Repo should return 'text/xml', which is supported by default.
diff --git a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/HTTPMetadataResolverTest.java b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/HTTPMetadataResolverTest.java
index f99b096a4..0c3afa9d4 100644
--- a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/HTTPMetadataResolverTest.java
+++ b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/HTTPMetadataResolverTest.java
@@ -48,6 +48,7 @@ import org.testng.annotations.BeforeClass;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import com.google.common.base.Predicates;
 import com.google.common.io.ByteStreams;
 
 import net.shibboleth.shared.component.ComponentInitializationException;
@@ -88,6 +89,31 @@ public class HTTPMetadataResolverTest extends XMLObjectBaseTestCase {
         criteriaSet = new CriteriaSet(new EntityIdCriterion(entityID));
     }
     
+    /**
+     * Tests failed condition.
+     * 
+     * @throws Exception 
+     */
+    @Test
+    public void testInactive() throws Exception {
+        try {
+            metadataProvider = new HTTPMetadataResolver(httpClientBuilder.buildClient(), metadataURLHttp);
+            metadataProvider.setParserPool(parserPool);
+            metadataProvider.setId("test");
+            metadataProvider.setActivationCondition(Predicates.alwaysFalse());
+            metadataProvider.initialize();
+            
+            Assert.assertNotNull(metadataProvider.wasLastRefreshSuccess());
+            Assert.assertTrue(metadataProvider.wasLastRefreshSuccess());
+            Assert.assertNull(metadataProvider.getLastFailureCause());
+        } catch (ComponentInitializationException e) {
+            Assert.fail("Valid metadata failed init");
+        }
+        
+        EntityDescriptor descriptor = metadataProvider.resolveSingle(criteriaSet);
+        Assert.assertNull(descriptor, "Retrieved entity descriptor was not null");
+    }
+    
     /**
      * Tests the {@link HTTPMetadataResolver#lookupEntityID(String)} method.
      * @throws Exception 
diff --git a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/LocalDynamicMetadataResolverTest.java b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/LocalDynamicMetadataResolverTest.java
index 1cb54fd93..2d50ae87c 100644
--- a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/LocalDynamicMetadataResolverTest.java
+++ b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/LocalDynamicMetadataResolverTest.java
@@ -33,6 +33,7 @@ import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import com.google.common.base.Predicates;
 import com.google.common.util.concurrent.Uninterruptibles;
 
 import net.shibboleth.shared.codec.StringDigester;
@@ -86,6 +87,20 @@ public class LocalDynamicMetadataResolverTest extends XMLObjectBaseTestCase {
         }
     }
     
+    @Test
+    public void testInactive() throws ComponentInitializationException, IOException, ResolverException {
+        
+        resolver = new LocalDynamicMetadataResolver(null, sourceManager, new IdentityEntityIDGenerator());
+        resolver.setId("abc123");
+        resolver.setParserPool(parserPool);
+        resolver.setActivationCondition(Predicates.alwaysFalse());
+        resolver.initialize();
+        
+        sourceManager.save(entityID1, entity1);
+        
+        Assert.assertNull(resolver.resolveSingle(new CriteriaSet(new EntityIdCriterion(entityID1))));
+    }
+    
     @Test
     public void testEmptySource() throws ResolverException {
         Assert.assertNull(resolver.resolveSingle(new CriteriaSet(new EntityIdCriterion(entityID1))));
diff --git a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/ResourceBackedMetadataResolverTest.java b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/ResourceBackedMetadataResolverTest.java
index 31a666ea9..54ecb96a4 100644
--- a/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/ResourceBackedMetadataResolverTest.java
+++ b/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/ResourceBackedMetadataResolverTest.java
@@ -35,6 +35,8 @@ import org.testng.Assert;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import com.google.common.base.Predicates;
+
 /** Unit test for {@link ResourceBackedMetadataResolver}. */
 public class ResourceBackedMetadataResolverTest extends XMLObjectBaseTestCase {
 
@@ -43,22 +45,41 @@ public class ResourceBackedMetadataResolverTest extends XMLObjectBaseTestCase {
     private String entityID;
 
     private CriteriaSet criteriaSet;
-
+    
     @BeforeMethod
     protected void setUp() throws Exception {
         entityID = "urn:mace:incommon:washington.edu";
 
-        URL mdURL = ResourceBackedMetadataResolverTest.class
+        final URL mdURL = ResourceBackedMetadataResolverTest.class
+                .getResource("/org/opensaml/saml/saml2/metadata/InCommon-metadata.xml");
+        final Resource mdResource = ResourceHelper.of(new FileSystemResource(new File(mdURL.toURI()).getAbsolutePath()));
+
+        metadataProvider = new ResourceBackedMetadataResolver(new Timer(true), mdResource);
+        metadataProvider.setParserPool(parserPool);
+        metadataProvider.setMaxRefreshDelay(Duration.ofSeconds(500));
+        metadataProvider.setId("test");
+        metadataProvider.initialize();
+        
+        criteriaSet = new CriteriaSet(new EntityIdCriterion(entityID));
+    }
+
+    @Test
+    protected void testInactive() throws Exception {
+        entityID = "urn:mace:incommon:washington.edu";
+
+        final URL mdURL = ResourceBackedMetadataResolverTest.class
                 .getResource("/org/opensaml/saml/saml2/metadata/InCommon-metadata.xml");
-        Resource mdResource = ResourceHelper.of(new FileSystemResource(new File(mdURL.toURI()).getAbsolutePath()));
+        final Resource mdResource = ResourceHelper.of(new FileSystemResource(new File(mdURL.toURI()).getAbsolutePath()));
 
         metadataProvider = new ResourceBackedMetadataResolver(new Timer(true), mdResource);
         metadataProvider.setParserPool(parserPool);
         metadataProvider.setMaxRefreshDelay(Duration.ofSeconds(500));
         metadataProvider.setId("test");
+        metadataProvider.setActivationCondition(Predicates.alwaysFalse());
         metadataProvider.initialize();
         
         criteriaSet = new CriteriaSet(new EntityIdCriterion(entityID));
+        Assert.assertNull(metadataProvider.resolveSingle(criteriaSet));
     }
 
     /**
@@ -68,7 +89,7 @@ public class ResourceBackedMetadataResolverTest extends XMLObjectBaseTestCase {
      */
     @Test
     public void testGetEntityDescriptor() throws ResolverException {
-        EntityDescriptor descriptor = metadataProvider.resolveSingle(criteriaSet);
+        final EntityDescriptor descriptor = metadataProvider.resolveSingle(criteriaSet);
         Assert.assertNotNull(descriptor, "Retrieved entity descriptor was null");
         Assert.assertEquals(descriptor.getEntityID(), entityID, "Entity's ID does not match requested ID");
     }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list