-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add always-accept and always-reject validators
- Loading branch information
Showing
6 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/main/java/uk/org/iay/incommon/mda/validate/AlwaysAcceptValidator.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,35 @@ | ||
| /* | ||
| * 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 javax.annotation.Nonnull; | ||
|
|
||
| import net.shibboleth.metadata.Item; | ||
| import net.shibboleth.metadata.validate.BaseValidator; | ||
| import net.shibboleth.metadata.validate.Validator; | ||
|
|
||
| /** | ||
| * A {@link Validator} which accepts any value, returning | ||
| * {@link net.shibboleth.metadata.validate.Validator.Action#DONE} | ||
| * to terminate any validator sequence. | ||
| */ | ||
| public class AlwaysAcceptValidator extends BaseValidator implements Validator<Object> { | ||
|
|
||
| @Override | ||
| public Action validate(@Nonnull final Object e, @Nonnull final Item<?> item, @Nonnull final String stageId) { | ||
| return Action.DONE; | ||
| } | ||
|
|
||
| } |
75 changes: 75 additions & 0 deletions
75
src/main/java/uk/org/iay/incommon/mda/validate/AlwaysRejectValidator.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,75 @@ | ||
| /* | ||
| * 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 javax.annotation.Nonnull; | ||
|
|
||
| import net.shibboleth.metadata.Item; | ||
| import net.shibboleth.metadata.validate.BaseValidator; | ||
| import net.shibboleth.metadata.validate.Validator; | ||
| import net.shibboleth.utilities.java.support.component.ComponentSupport; | ||
| import net.shibboleth.utilities.java.support.logic.Constraint; | ||
|
|
||
| /** | ||
| * A {@link Validator} which rejects any value, returning | ||
| * {@link net.shibboleth.metadata.validate.Validator.Action#DONE} | ||
| * to terminate any validator sequence. | ||
| * | ||
| * An error status is added using the value of the given property as a format string. | ||
| * The message defaults to a simple rejection message including the object's string value. | ||
| */ | ||
| public class AlwaysRejectValidator extends BaseValidator implements Validator<Object> { | ||
|
|
||
| /** | ||
| * Message format string. | ||
| * | ||
| * The generated message is formatted using this with the object being validated passed | ||
| * as an argument. | ||
| * | ||
| * Defaults to <code>"value rejected: '%s'"</code>. | ||
| */ | ||
| @Nonnull | ||
| private String message = "value rejected: '%s'"; | ||
|
|
||
| /** | ||
| * Returns the message format string. | ||
| * | ||
| * @return the message format string | ||
| */ | ||
| @Nonnull | ||
| public String getMessage() { | ||
| return message; | ||
| } | ||
|
|
||
| /** | ||
| * Set the message format string. | ||
| * | ||
| * @param newMessage the new message format string | ||
| */ | ||
| public void setMessage(@Nonnull final String newMessage) { | ||
| ComponentSupport.ifDestroyedThrowDestroyedComponentException(this); | ||
| ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this); | ||
|
|
||
| message = Constraint.isNotNull(newMessage, "message format string may not be null"); | ||
| } | ||
|
|
||
| @Override | ||
| public Action validate(@Nonnull final Object e, @Nonnull final Item<?> item, @Nonnull final String stageId) { | ||
| final String mess = String.format(message, e); | ||
| addError(mess, item, stageId); | ||
| return Action.DONE; | ||
| } | ||
|
|
||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/java/uk/org/iay/incommon/mda/validate/package-info.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,18 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * Classes for validation of specific object types. | ||
| */ | ||
| package uk.org.iay.incommon.mda.validate; |
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
31 changes: 31 additions & 0 deletions
31
src/test/java/uk/org/iay/incommon/mda/validate/AlwaysAcceptValidatorTest.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,31 @@ | ||
|
|
||
| package uk.org.iay.incommon.mda.validate; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.testng.annotations.Test; | ||
|
|
||
| import junit.framework.Assert; | ||
| 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 AlwaysAcceptValidatorTest { | ||
|
|
||
| @Test | ||
| public void validate() throws Exception { | ||
| final AlwaysAcceptValidator v = new AlwaysAcceptValidator(); | ||
| v.setId("comp"); | ||
| v.initialize(); | ||
|
|
||
| final Item<String> item = new MockItem("test"); | ||
| final Validator.Action action = v.validate("foo", item, "stage"); | ||
| Assert.assertEquals(Action.DONE, action); | ||
|
|
||
| final List<ErrorStatus> errs = item.getItemMetadata().get(ErrorStatus.class); | ||
| Assert.assertEquals(0, errs.size()); | ||
| } | ||
|
|
||
| } |
54 changes: 54 additions & 0 deletions
54
src/test/java/uk/org/iay/incommon/mda/validate/AlwaysRejectValidatorTest.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,54 @@ | ||
|
|
||
| package uk.org.iay.incommon.mda.validate; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.testng.annotations.Test; | ||
|
|
||
| import junit.framework.Assert; | ||
| 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 AlwaysRejectValidatorTest { | ||
|
|
||
| @Test | ||
| public void validate() throws Exception { | ||
| final AlwaysRejectValidator v = new AlwaysRejectValidator(); | ||
| v.setId("comp"); | ||
| v.initialize(); | ||
|
|
||
| final Item<String> item = new MockItem("test"); | ||
| final Validator.Action action = v.validate("foo", item, "stage"); | ||
| Assert.assertEquals(Action.DONE, action); | ||
|
|
||
| final List<ErrorStatus> errs = item.getItemMetadata().get(ErrorStatus.class); | ||
| Assert.assertEquals(1, errs.size()); | ||
|
|
||
| final ErrorStatus err = errs.get(0); | ||
| Assert.assertEquals("stage/comp", err.getComponentId()); | ||
| Assert.assertEquals("value rejected: 'foo'", err.getStatusMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void validateWithMessage() throws Exception { | ||
| final AlwaysRejectValidator v = new AlwaysRejectValidator(); | ||
| v.setId("comp"); | ||
| v.setMessage("decimal %.2f"); | ||
| v.initialize(); | ||
|
|
||
| final Item<String> item = new MockItem("test"); | ||
| final Validator.Action action = v.validate(new Double(1.25), item, "stage"); | ||
| Assert.assertEquals(Action.DONE, action); | ||
|
|
||
| final List<ErrorStatus> errs = item.getItemMetadata().get(ErrorStatus.class); | ||
| Assert.assertEquals(1, errs.size()); | ||
|
|
||
| final ErrorStatus err = errs.get(0); | ||
| Assert.assertEquals("stage/comp", err.getComponentId()); | ||
| Assert.assertEquals("decimal 1.25", err.getStatusMessage()); | ||
| } | ||
|
|
||
| } |