[java-metadata-aggregator] branch master updated: MDA-193 - refactor validator sequences for reuse
Ian Young
ian at iay.org.uk
Tue Jan 30 13:26:00 EST 2018
This is an automated email from the git hooks/post-receive script.
iay pushed a commit to branch master
in repository java-metadata-aggregator.
View the commit online:
http://git.shibboleth.net/view/?p=java-metadata-aggregator.git;a=commit;h=c48a11ce207b7792875a772e8bc0539fee3893e9
The following commit(s) were added to refs/heads/master by this push:
new c48a11c MDA-193 - refactor validator sequences for reuse
c48a11c is described below
commit c48a11ce207b7792875a772e8bc0539fee3893e9
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Tue Jan 30 18:25:54 2018 +0000
MDA-193 - refactor validator sequences for reuse
---
.../metadata/dom/AbstractDOMValidationStage.java | 38 +++------
.../ValidatorSequence.java} | 38 +++++----
.../metadata/validate/ValidatorSequenceTest.java | 89 ++++++++++++++++++++++
3 files changed, 118 insertions(+), 47 deletions(-)
diff --git a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/dom/AbstractDOMValidationStage.java b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/dom/AbstractDOMValidationStage.java
index 2bad57f..5f8a044 100644
--- a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/dom/AbstractDOMValidationStage.java
+++ b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/dom/AbstractDOMValidationStage.java
@@ -17,19 +17,14 @@
package net.shibboleth.metadata.dom;
-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.pipeline.StageProcessingException;
import net.shibboleth.metadata.validate.Validator;
+import net.shibboleth.metadata.validate.ValidatorSequence;
import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
-import net.shibboleth.utilities.java.support.component.ComponentSupport;
/**
* An abstract stage to apply a collection of validators to each object from each item.
@@ -40,22 +35,19 @@ import net.shibboleth.utilities.java.support.component.ComponentSupport;
public abstract class AbstractDOMValidationStage<V, C extends DOMTraversalContext>
extends AbstractDOMTraversalStage<C> {
- /** The list of validators to apply. */
+ /** The validator sequence to apply. */
@Nonnull
- private List<Validator<V>> validators = Collections.emptyList();
-
+ private ValidatorSequence<V> validators = new ValidatorSequence<>();
+
/**
* Set the list of validators to apply to each item.
*
* @param newValidators the list of validators to set
*/
public void setValidators(@Nonnull final List<Validator<V>> newValidators) {
- ComponentSupport.ifDestroyedThrowDestroyedComponentException(this);
- ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
-
- validators = ImmutableList.copyOf(Iterables.filter(newValidators, Predicates.notNull()));
+ validators.setValidators(newValidators);
}
-
+
/**
* Gets the list of validators being applied to each item.
*
@@ -63,7 +55,7 @@ public abstract class AbstractDOMValidationStage<V, C extends DOMTraversalContex
*/
@Nonnull
public List<Validator<V>> getValidators() {
- return Collections.unmodifiableList(validators);
+ return validators.getValidators();
}
/**
@@ -75,16 +67,12 @@ public abstract class AbstractDOMValidationStage<V, C extends DOMTraversalContex
*/
protected void applyValidators(@Nonnull final V obj, @Nonnull final C context)
throws StageProcessingException {
- for (final Validator<V> validator: validators) {
- final Validator.Action action = validator.validate(obj, context.getItem(), getId());
- if (action == Validator.Action.DONE) {
- return;
- }
- }
+ validators.validate(obj, context.getItem(), getId());
}
@Override
protected void doDestroy() {
+ validators.destroy();
validators = null;
super.doDestroy();
}
@@ -92,12 +80,8 @@ public abstract class AbstractDOMValidationStage<V, C extends DOMTraversalContex
@Override
protected void doInitialize() throws ComponentInitializationException {
super.doInitialize();
-
- for (final Validator<V> validator : validators) {
- if (!validator.isInitialized()) {
- validator.initialize();
- }
- }
+ validators.setId(getId());
+ validators.initialize();
}
}
diff --git a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/dom/AbstractDOMValidationStage.java b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/validate/ValidatorSequence.java
similarity index 75%
copy from aggregator-pipeline/src/main/java/net/shibboleth/metadata/dom/AbstractDOMValidationStage.java
copy to aggregator-pipeline/src/main/java/net/shibboleth/metadata/validate/ValidatorSequence.java
index 2bad57f..4ede415 100644
--- a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/dom/AbstractDOMValidationStage.java
+++ b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/validate/ValidatorSequence.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package net.shibboleth.metadata.dom;
+package net.shibboleth.metadata.validate;
import java.util.Collections;
import java.util.List;
@@ -26,24 +26,27 @@ 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.Validator;
import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
import net.shibboleth.utilities.java.support.component.ComponentSupport;
/**
- * An abstract stage to apply a collection of validators to each object from each item.
+ * 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 <V> type of the object to be validated
- * @param <C> the context to carry through the traversal
*/
-public abstract class AbstractDOMValidationStage<V, C extends DOMTraversalContext>
- extends AbstractDOMTraversalStage<C> {
+public class ValidatorSequence<V> extends BaseValidator implements Validator<V> {
/** The list of validators to apply. */
@Nonnull
private List<Validator<V>> validators = Collections.emptyList();
-
+
/**
* Set the list of validators to apply to each item.
*
@@ -55,7 +58,7 @@ public abstract class AbstractDOMValidationStage<V, C extends DOMTraversalContex
validators = ImmutableList.copyOf(Iterables.filter(newValidators, Predicates.notNull()));
}
-
+
/**
* Gets the list of validators being applied to each item.
*
@@ -66,23 +69,18 @@ public abstract class AbstractDOMValidationStage<V, C extends DOMTraversalContex
return Collections.unmodifiableList(validators);
}
- /**
- * Apply each of the configured validators in turn to the provided object.
- *
- * @param obj object to be validated
- * @param context context for the validation
- * @throws StageProcessingException if errors occur during processing
- */
- protected void applyValidators(@Nonnull final V obj, @Nonnull final C context)
+ @Override
+ public Action validate(@Nonnull final V value, @Nonnull final Item<?> item, @Nonnull final String stageId)
throws StageProcessingException {
for (final Validator<V> validator: validators) {
- final Validator.Action action = validator.validate(obj, context.getItem(), getId());
- if (action == Validator.Action.DONE) {
- return;
+ final Action action = validator.validate(value, item, stageId);
+ if (action == Action.DONE) {
+ return action;
}
}
+ return Action.CONTINUE;
}
-
+
@Override
protected void doDestroy() {
validators = null;
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/validate/ValidatorSequenceTest.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/validate/ValidatorSequenceTest.java
new file mode 100644
index 0000000..19e54bb
--- /dev/null
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/validate/ValidatorSequenceTest.java
@@ -0,0 +1,89 @@
+
+package net.shibboleth.metadata.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.MockItem;
+import net.shibboleth.metadata.validate.Validator;
+import net.shibboleth.metadata.validate.Validator.Action;
+
+public class ValidatorSequenceTest {
+
+ @Test
+ public void testNoValidators() throws Exception {
+ final ValidatorSequence<String> v = new ValidatorSequence<>();
+ v.setId("seq");
+ v.initialize();
+
+ final Item<String> item = new MockItem("content");
+ final Validator.Action action = v.validate("anything", item, "stage");
+ Assert.assertEquals(action, Action.CONTINUE);
+
+ final List<ErrorStatus> errs = item.getItemMetadata().get(ErrorStatus.class);
+ Assert.assertEquals(errs.size(), 0);
+ }
+
+ @Test
+ public void testAcceptReject() throws Exception {
+ final AcceptAllValidator<String> accept = new AcceptAllValidator<>();
+ accept.setId("accept");
+ accept.initialize();
+
+ final RejectAllValidator<String> reject = new RejectAllValidator<>();
+ reject.setId("reject");
+ reject.initialize();
+
+ final List<Validator<String>> vv = new ArrayList<>();
+ vv.add(accept);
+ vv.add(reject);
+
+ final ValidatorSequence<String> v = new ValidatorSequence<>();
+ v.setId("seq");
+ v.setValidators(vv);
+ v.initialize();
+
+ final Item<String> item = new MockItem("content");
+ final Validator.Action action = v.validate("anything", item, "stage");
+ Assert.assertEquals(action, Action.DONE);
+
+ final List<ErrorStatus> errs = item.getItemMetadata().get(ErrorStatus.class);
+ Assert.assertEquals(errs.size(), 0);
+ }
+
+ @Test
+ public void testRejectAccept() throws Exception {
+ final AcceptAllValidator<String> accept = new AcceptAllValidator<>();
+ accept.setId("accept");
+ accept.initialize();
+
+ final RejectAllValidator<String> reject = new RejectAllValidator<>();
+ reject.setId("reject");
+ reject.initialize();
+
+ final List<Validator<String>> vv = new ArrayList<>();
+ vv.add(reject);
+ vv.add(accept);
+
+ final ValidatorSequence<String> v = new ValidatorSequence<>();
+ v.setId("seq");
+ v.setValidators(vv);
+ v.initialize();
+
+ final Item<String> item = new MockItem("content");
+ final Validator.Action action = v.validate("anything", item, "stage");
+ Assert.assertEquals(action, Action.DONE);
+
+ final List<ErrorStatus> 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");
+ }
+
+}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list