From e7d9173955218d211f6e47868921fd2ab70598c0 Mon Sep 17 00:00:00 2001 From: Ian Young Date: Tue, 3 Oct 2017 18:10:51 +0100 Subject: [PATCH] Add ValidatorSequence --- .../mda/validate/ValidatorSequence.java | 100 ++++++++++++++++++ .../mda/validate/ValidatorSequenceTest.java | 89 ++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 src/main/java/uk/org/iay/incommon/mda/validate/ValidatorSequence.java create mode 100644 src/test/java/uk/org/iay/incommon/mda/validate/ValidatorSequenceTest.java diff --git a/src/main/java/uk/org/iay/incommon/mda/validate/ValidatorSequence.java b/src/main/java/uk/org/iay/incommon/mda/validate/ValidatorSequence.java new file mode 100644 index 0000000..db944e3 --- /dev/null +++ b/src/main/java/uk/org/iay/incommon/mda/validate/ValidatorSequence.java @@ -0,0 +1,100 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.org.iay.incommon.mda.validate; + +import java.util.Collections; +import java.util.List; + +import javax.annotation.Nonnull; + +import com.google.common.base.Predicates; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; + +import net.shibboleth.metadata.Item; +import net.shibboleth.metadata.pipeline.StageProcessingException; +import net.shibboleth.metadata.validate.BaseValidator; +import net.shibboleth.metadata.validate.Validator; +import net.shibboleth.utilities.java.support.component.ComponentInitializationException; +import net.shibboleth.utilities.java.support.component.ComponentSupport; + +/** + * A {@link Validator} implementation which encapsulates the functionality of stepping + * through a sequence of other validators. + * + * The {@link #validate} method of this class returns the + * {@link net.shibboleth.metadata.validate.Validator.Action#DONE} value + * to indicate that one of the called validators returned that value. + * + * @param type of the object to be validated + */ +public class ValidatorSequence extends BaseValidator implements Validator { + + /** The list of validators to apply. */ + @Nonnull + private List> validators = Collections.emptyList(); + + /** + * Set the list of validators to apply to each item. + * + * @param newValidators the list of validators to set + */ + public void setValidators(@Nonnull final List> newValidators) { + ComponentSupport.ifDestroyedThrowDestroyedComponentException(this); + ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this); + + validators = ImmutableList.copyOf(Iterables.filter(newValidators, Predicates.notNull())); + } + + /** + * Gets the list of validators being applied to each item. + * + * @return list of validators + */ + @Nonnull + public List> getValidators() { + return Collections.unmodifiableList(validators); + } + + @Override + public Action validate(@Nonnull final V value, @Nonnull final Item item, @Nonnull final String stageId) + throws StageProcessingException { + for (final Validator validator: validators) { + final Action action = validator.validate(value, item, stageId); + if (action == Action.DONE) { + return action; + } + } + return Action.CONTINUE; + } + + @Override + protected void doDestroy() { + validators = null; + super.doDestroy(); + } + + @Override + protected void doInitialize() throws ComponentInitializationException { + super.doInitialize(); + + for (final Validator validator : validators) { + if (!validator.isInitialized()) { + validator.initialize(); + } + } + } + +} diff --git a/src/test/java/uk/org/iay/incommon/mda/validate/ValidatorSequenceTest.java b/src/test/java/uk/org/iay/incommon/mda/validate/ValidatorSequenceTest.java new file mode 100644 index 0000000..4bf8c47 --- /dev/null +++ b/src/test/java/uk/org/iay/incommon/mda/validate/ValidatorSequenceTest.java @@ -0,0 +1,89 @@ + +package uk.org.iay.incommon.mda.validate; + +import java.util.ArrayList; +import java.util.List; + +import org.testng.Assert; +import org.testng.annotations.Test; + +import net.shibboleth.metadata.ErrorStatus; +import net.shibboleth.metadata.Item; +import net.shibboleth.metadata.validate.Validator; +import net.shibboleth.metadata.validate.Validator.Action; +import uk.org.ukfederation.mda.MockItem; + +public class ValidatorSequenceTest { + + @Test + public void testNoValidators() throws Exception { + final ValidatorSequence v = new ValidatorSequence<>(); + v.setId("seq"); + v.initialize(); + + final Item item = new MockItem("content"); + final Validator.Action action = v.validate("anything", item, "stage"); + Assert.assertEquals(action, Action.CONTINUE); + + final List errs = item.getItemMetadata().get(ErrorStatus.class); + Assert.assertEquals(errs.size(), 0); + } + + @Test + public void testAcceptReject() throws Exception { + final AcceptAllValidator accept = new AcceptAllValidator<>(); + accept.setId("accept"); + accept.initialize(); + + final RejectAllValidator reject = new RejectAllValidator<>(); + reject.setId("reject"); + reject.initialize(); + + final List> vv = new ArrayList<>(); + vv.add(accept); + vv.add(reject); + + final ValidatorSequence v = new ValidatorSequence<>(); + v.setId("seq"); + v.setValidators(vv); + v.initialize(); + + final Item item = new MockItem("content"); + final Validator.Action action = v.validate("anything", item, "stage"); + Assert.assertEquals(action, Action.DONE); + + final List errs = item.getItemMetadata().get(ErrorStatus.class); + Assert.assertEquals(errs.size(), 0); + } + + @Test + public void testRejectAccept() throws Exception { + final AcceptAllValidator accept = new AcceptAllValidator<>(); + accept.setId("accept"); + accept.initialize(); + + final RejectAllValidator reject = new RejectAllValidator<>(); + reject.setId("reject"); + reject.initialize(); + + final List> vv = new ArrayList<>(); + vv.add(reject); + vv.add(accept); + + final ValidatorSequence v = new ValidatorSequence<>(); + v.setId("seq"); + v.setValidators(vv); + v.initialize(); + + final Item item = new MockItem("content"); + final Validator.Action action = v.validate("anything", item, "stage"); + Assert.assertEquals(action, Action.DONE); + + final List errs = item.getItemMetadata().get(ErrorStatus.class); + Assert.assertEquals(errs.size(), 1); + final ErrorStatus err = errs.get(0); + Assert.assertEquals(err.getStatusMessage(), "value rejected: 'anything'"); + Assert.assertEquals(err.getComponentId(), "stage/reject"); + } + +}