Skip to content

Commit

Permalink
Add always-accept and always-reject validators
Browse files Browse the repository at this point in the history
  • Loading branch information
iay committed Oct 3, 2017
1 parent 53aa345 commit 95687f9
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 0 deletions.
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;
}

}
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 src/main/java/uk/org/iay/incommon/mda/validate/package-info.java
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;
10 changes: 10 additions & 0 deletions src/main/resources/uk/org/iay/incommon/mda/beans.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@
<bean id="inc.InCommonEntityOrderingStrategy" abstract="true"
class="uk.org.iay.incommon.mda.dom.saml.InCommonEntityOrderingStrategy"/>

<!--
Validator beans.
-->

<bean id="inc.AlwaysAcceptValidator" abstract="true"
class="uk.org.iay.incommon.mda.validate.AlwaysAcceptValidator"/>

<bean id="inc.AlwaysRejectValidator" abstract="true"
class="uk.org.iay.incommon.mda.validate.AlwaysRejectValidator"/>

</beans>
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());
}

}
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());
}

}

0 comments on commit 95687f9

Please sign in to comment.