Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
src/main/java/uk/org/iay/incommon/mda/validate/ValidatorSequence.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <V> type of the object to be validated | ||
*/ | ||
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. | ||
* | ||
* @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())); | ||
} | ||
|
||
/** | ||
* Gets the list of validators being applied to each item. | ||
* | ||
* @return list of validators | ||
*/ | ||
@Nonnull | ||
public List<Validator<V>> 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<V> 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<V> validator : validators) { | ||
if (!validator.isInitialized()) { | ||
validator.initialize(); | ||
} | ||
} | ||
} | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
src/test/java/uk/org/iay/incommon/mda/validate/ValidatorSequenceTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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"); | ||
} | ||
|
||
} |