From 95687f924ca6676965aaf990c08a48547a2bc5ae Mon Sep 17 00:00:00 2001 From: Ian Young Date: Tue, 3 Oct 2017 15:01:32 +0100 Subject: [PATCH] Add always-accept and always-reject validators --- .../mda/validate/AlwaysAcceptValidator.java | 35 +++++++++ .../mda/validate/AlwaysRejectValidator.java | 75 +++++++++++++++++++ .../incommon/mda/validate/package-info.java | 18 +++++ .../uk/org/iay/incommon/mda/beans.xml | 10 +++ .../validate/AlwaysAcceptValidatorTest.java | 31 ++++++++ .../validate/AlwaysRejectValidatorTest.java | 54 +++++++++++++ 6 files changed, 223 insertions(+) create mode 100644 src/main/java/uk/org/iay/incommon/mda/validate/AlwaysAcceptValidator.java create mode 100644 src/main/java/uk/org/iay/incommon/mda/validate/AlwaysRejectValidator.java create mode 100644 src/main/java/uk/org/iay/incommon/mda/validate/package-info.java create mode 100644 src/test/java/uk/org/iay/incommon/mda/validate/AlwaysAcceptValidatorTest.java create mode 100644 src/test/java/uk/org/iay/incommon/mda/validate/AlwaysRejectValidatorTest.java diff --git a/src/main/java/uk/org/iay/incommon/mda/validate/AlwaysAcceptValidator.java b/src/main/java/uk/org/iay/incommon/mda/validate/AlwaysAcceptValidator.java new file mode 100644 index 0000000..a4f71a2 --- /dev/null +++ b/src/main/java/uk/org/iay/incommon/mda/validate/AlwaysAcceptValidator.java @@ -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 { + + @Override + public Action validate(@Nonnull final Object e, @Nonnull final Item item, @Nonnull final String stageId) { + return Action.DONE; + } + +} diff --git a/src/main/java/uk/org/iay/incommon/mda/validate/AlwaysRejectValidator.java b/src/main/java/uk/org/iay/incommon/mda/validate/AlwaysRejectValidator.java new file mode 100644 index 0000000..d644c5e --- /dev/null +++ b/src/main/java/uk/org/iay/incommon/mda/validate/AlwaysRejectValidator.java @@ -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 { + + /** + * Message format string. + * + * The generated message is formatted using this with the object being validated passed + * as an argument. + * + * Defaults to "value rejected: '%s'". + */ + @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; + } + +} diff --git a/src/main/java/uk/org/iay/incommon/mda/validate/package-info.java b/src/main/java/uk/org/iay/incommon/mda/validate/package-info.java new file mode 100644 index 0000000..cb23f1e --- /dev/null +++ b/src/main/java/uk/org/iay/incommon/mda/validate/package-info.java @@ -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; diff --git a/src/main/resources/uk/org/iay/incommon/mda/beans.xml b/src/main/resources/uk/org/iay/incommon/mda/beans.xml index 51b9699..2318eb1 100644 --- a/src/main/resources/uk/org/iay/incommon/mda/beans.xml +++ b/src/main/resources/uk/org/iay/incommon/mda/beans.xml @@ -31,4 +31,14 @@ + + + + + + diff --git a/src/test/java/uk/org/iay/incommon/mda/validate/AlwaysAcceptValidatorTest.java b/src/test/java/uk/org/iay/incommon/mda/validate/AlwaysAcceptValidatorTest.java new file mode 100644 index 0000000..762add7 --- /dev/null +++ b/src/test/java/uk/org/iay/incommon/mda/validate/AlwaysAcceptValidatorTest.java @@ -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 item = new MockItem("test"); + final Validator.Action action = v.validate("foo", item, "stage"); + Assert.assertEquals(Action.DONE, action); + + final List errs = item.getItemMetadata().get(ErrorStatus.class); + Assert.assertEquals(0, errs.size()); + } + +} diff --git a/src/test/java/uk/org/iay/incommon/mda/validate/AlwaysRejectValidatorTest.java b/src/test/java/uk/org/iay/incommon/mda/validate/AlwaysRejectValidatorTest.java new file mode 100644 index 0000000..583269a --- /dev/null +++ b/src/test/java/uk/org/iay/incommon/mda/validate/AlwaysRejectValidatorTest.java @@ -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 item = new MockItem("test"); + final Validator.Action action = v.validate("foo", item, "stage"); + Assert.assertEquals(Action.DONE, action); + + final List 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 item = new MockItem("test"); + final Validator.Action action = v.validate(new Double(1.25), item, "stage"); + Assert.assertEquals(Action.DONE, action); + + final List 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()); + } + +}