[java-metadata-aggregator] 02/02: WIP: extended formatting
Ian Young
ian at iay.org.uk
Wed Dec 20 18:07:35 UTC 2023
This is an automated email from the git hooks/post-receive script.
iay pushed a commit to branch dev/MDA-299
in repository java-metadata-aggregator.
View the commit online:
http://git.shibboleth.net/view/?p=java-metadata-aggregator.git;a=commit;h=b9159b07bcf693f4118a62137d3ee25738d78641
commit b9159b07bcf693f4118a62137d3ee25738d78641
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Wed Dec 20 18:07:15 2023 +0000
WIP: extended formatting
---
.../metadata/validate/BaseAsValidator.java | 2 +-
.../metadata/validate/BaseValidator.java | 62 +++++++++++-
.../validate/string/AsURLStringValidator.java | 11 +-
.../resources/net/shibboleth/metadata/beans.xml | 3 +
.../string/AsURLStringValidatorLitmusTest.java | 111 +++++++++++++++++++++
.../validate/string/AsURLStringValidatorTest.java | 107 ++++++++++++++++++--
.../AsURLStringValidatorLitmusTest-config.xml | 23 +++++
7 files changed, 307 insertions(+), 12 deletions(-)
diff --git a/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseAsValidator.java b/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseAsValidator.java
index fb8eeb7..62154b7 100644
--- a/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseAsValidator.java
+++ b/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseAsValidator.java
@@ -130,7 +130,7 @@ public abstract class BaseAsValidator<V, A> extends BaseValidator implements Val
return applyValidators(v, item);
} catch (final IllegalArgumentException e) {
if (isConversionRequired()) {
- addErrorMessage(t, item, stageId);
+ addErrorMessage(t, item, stageId, e);
return Action.DONE;
} else {
return Action.CONTINUE;
diff --git a/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseValidator.java b/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseValidator.java
index 3b8d35c..a142c8a 100644
--- a/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseValidator.java
+++ b/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseValidator.java
@@ -15,6 +15,7 @@
package net.shibboleth.metadata.validate;
import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
@@ -129,11 +130,49 @@ public abstract class BaseValidator extends AbstractIdentifiableInitializableCom
}
}
+ /**
+ * Formats the message with the given subject.
+ *
+ * @param subject subject of the validation, to be provided as an argument to the
+ * formatting string.
+ * @return formatted message
+ */
+ protected @Nonnull String formatMessage(final @Nonnull Object subject) {
+ final String mess = String.format(getMessage(), subject);
+ assert mess != null;
+ return mess;
+ }
+
+ /**
+ * Formats the message with the given subject.
+ *
+ * <p>
+ * If the supplied {@link Throwable} has a non-<code>null</code> detail
+ * message, this is added to the resulting formatted string.
+ * </p>
+ *
+ * @param subject subject of the validation, to be provided as an argument to the
+ * formatting string.
+ * @param cause a {@link Throwable} which is to be interpreted as the cause of the
+ * validation failure.
+ * @return formatted message
+ */
+ protected @Nonnull String formatMessage(final @Nonnull Object subject, final @Nonnull Throwable cause) {
+ final @Nullable var cmsg = cause.getMessage();
+ if (cmsg == null) {
+ return formatMessage(subject);
+ } else {
+ return formatMessage(subject) + " (" + cmsg + ")";
+ }
+ }
+
/**
* Add an {@link ErrorStatus} to the given {@link Item}.
*
+ * <p>
* The status message included in the {@link ErrorStatus} is generated
* by formatting the provided value with the {@link #message} field.
+ * </p>
*
* @param extra extra value to include in the status metadata
* @param item {@link Item} to add the status metadata to
@@ -143,9 +182,26 @@ public abstract class BaseValidator extends AbstractIdentifiableInitializableCom
*/
protected void addErrorMessage(@Nonnull final Object extra, @Nonnull final Item<?> item,
@Nonnull final String stageId) {
- final String mess = String.format(getMessage(), extra);
- assert mess != null;
- addError(mess, item, stageId);
+ addError(formatMessage(extra), item, stageId);
}
+ /**
+ * Add an {@link ErrorStatus} to the given {@link Item}.
+ *
+ * <p>
+ * The status message included in the {@link ErrorStatus} is generated
+ * by formatting the provided value with the {@link #message} field.
+ * </p>
+ *
+ * @param extra extra value to include in the status metadata
+ * @param item {@link Item} to add the status metadata to
+ * @param stageId component identifier for the calling stage
+ * @param cause reason for the validation failure, as a {@link Throwable}
+ *
+ * @since 0.10.0
+ */
+ protected void addErrorMessage(@Nonnull final Object extra, @Nonnull final Item<?> item,
+ @Nonnull final String stageId, @Nonnull final Throwable cause) {
+ addError(formatMessage(extra, cause), item, stageId);
+ }
}
diff --git a/mda-framework/src/main/java/net/shibboleth/metadata/validate/string/AsURLStringValidator.java b/mda-framework/src/main/java/net/shibboleth/metadata/validate/string/AsURLStringValidator.java
index 14d1a31..7340c0d 100644
--- a/mda-framework/src/main/java/net/shibboleth/metadata/validate/string/AsURLStringValidator.java
+++ b/mda-framework/src/main/java/net/shibboleth/metadata/validate/string/AsURLStringValidator.java
@@ -15,6 +15,8 @@
package net.shibboleth.metadata.validate.string;
import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
import javax.annotation.Nonnull;
@@ -44,11 +46,16 @@ public class AsURLStringValidator extends BaseAsValidator<String, URL>
@Override
protected @Nonnull URL convert(@Nonnull final String value) throws IllegalArgumentException {
try {
- final var result = new URL(value);
+ final var uri = new URI(value);
+ final var result = uri.toURL();
assert result != null;
return result;
} catch (final MalformedURLException e) {
- throw new IllegalArgumentException(e);
+ throw new IllegalArgumentException(e.getMessage(), e);
+ } catch (final URISyntaxException e) {
+ throw new IllegalArgumentException(e.getMessage(), e);
+ } catch (final IllegalArgumentException e) {
+ throw e;
}
}
diff --git a/mda-framework/src/main/resources/net/shibboleth/metadata/beans.xml b/mda-framework/src/main/resources/net/shibboleth/metadata/beans.xml
index 7577ce1..9e040c7 100644
--- a/mda-framework/src/main/resources/net/shibboleth/metadata/beans.xml
+++ b/mda-framework/src/main/resources/net/shibboleth/metadata/beans.xml
@@ -338,6 +338,9 @@
<bean id="mda.AsLiteralTailStringValidator" abstract="true" parent="mda.component_parent"
class="net.shibboleth.metadata.validate.string.AsLiteralTailStringValidator"/>
+ <bean id="mda.AsURLStringValidator" abstract="true" parent="mda.component_parent"
+ class="net.shibboleth.metadata.validate.string.AsURLStringValidator"/>
+
<bean id="mda.RejectStringRegexValidator" abstract="true" parent="mda.validator_parent"
class="net.shibboleth.metadata.validate.string.RejectStringRegexValidator"/>
diff --git a/mda-framework/src/test/java/net/shibboleth/metadata/validate/string/AsURLStringValidatorLitmusTest.java b/mda-framework/src/test/java/net/shibboleth/metadata/validate/string/AsURLStringValidatorLitmusTest.java
new file mode 100644
index 0000000..96e2115
--- /dev/null
+++ b/mda-framework/src/test/java/net/shibboleth/metadata/validate/string/AsURLStringValidatorLitmusTest.java
@@ -0,0 +1,111 @@
+
+package net.shibboleth.metadata.validate.string;
+
+import javax.annotation.Nonnull;
+
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
+import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import net.shibboleth.metadata.ErrorStatus;
+import net.shibboleth.metadata.testing.MockItem;
+import net.shibboleth.metadata.validate.Validator.Action;
+
+/**
+ * A litmus test for {@link AsURLStringValidator} involving a set of valid and invalid
+ * values.
+ *
+ * <p>
+ * The configuration for the stage is taken from a Spring XML configuration file.
+ * </p>
+ */
+ at ContextConfiguration("AsURLStringValidatorLitmusTest-config.xml")
+public class AsURLStringValidatorLitmusTest extends AbstractTestNGSpringContextTests {
+
+ /** {@link AsURLStringValidator} to run for each test. */
+ private AsURLStringValidator validator;
+
+ @BeforeClass
+ private void setUp() throws Exception {
+ validator = makeValidator();
+ }
+
+ /** Acquire the configured validator from the Spring context. */
+ private AsURLStringValidator makeValidator() throws Exception {
+ assert applicationContext != null;
+ final AsURLStringValidator validator = applicationContext.getBean("litmusTest", AsURLStringValidator.class);
+ validator.initialize();
+ return validator;
+ }
+
+ /**
+ * Test a value we expect to be accepted.
+ *
+ * @param value value to test
+ * @throws Exception if something goes wrong
+ */
+ private void good(@Nonnull final String value) throws Exception {
+ final var item = new MockItem("item");
+ var result = validator.validate(value, item, "stage");
+ Assert.assertEquals(result, Action.CONTINUE);
+ Assert.assertTrue(item.getItemMetadata().isEmpty());
+ }
+
+ /**
+ * Test a value we expect to be rejected.
+ *
+ * @param value value to test
+ * @throws Exception if something goes wrong
+ */
+ private void bad(@Nonnull final String value) throws Exception {
+ final var item = new MockItem("item");
+ var result = validator.validate(value, item, "stage");
+ Assert.assertEquals(result, Action.DONE);
+ final var errors = item.getItemMetadata().get(ErrorStatus.class);
+ Assert.assertEquals(errors.size(), 1);
+ }
+
+ /**
+ * Litmus tests from <code>URLCheckerTest</code>.
+ *
+ * <p>
+ * We want the <em>configured</em> {@link AsURLStringValidator} to have
+ * the same behaviour as the old Xalan extension.
+ * </p>
+ *
+ * @throws Exception if something goes wrong.
+ */
+ @Test
+ public void testsFromURLCheckerTest() throws Exception {
+ good("https://example.org:1234/example");
+
+ // Non-integer port number
+ bad("https://example.org:port/example");
+
+ /**
+ * Test the case where the authority's port field is present but empty.
+ *
+ * This is valid by the specification, but is regarded as invalid by
+ * libxml2's xs:anyURI checker.
+ */
+ //bad("http://example.org:/example/");
+
+ // Doubled scheme looks like empty port field
+ //bad("http://http://example.org/example/");
+
+ // Bare domain
+ bad("www.example.org");
+
+ // Empty value
+ bad("");
+
+ // Missing authority caused by extra slash
+ //bad("http:///foo/");
+
+ // Import transform artifact
+ bad("http://*** FILL IN ***/");
+
+ }
+}
diff --git a/mda-framework/src/test/java/net/shibboleth/metadata/validate/string/AsURLStringValidatorTest.java b/mda-framework/src/test/java/net/shibboleth/metadata/validate/string/AsURLStringValidatorTest.java
index 7f37329..f26515e 100644
--- a/mda-framework/src/test/java/net/shibboleth/metadata/validate/string/AsURLStringValidatorTest.java
+++ b/mda-framework/src/test/java/net/shibboleth/metadata/validate/string/AsURLStringValidatorTest.java
@@ -2,16 +2,111 @@ package net.shibboleth.metadata.validate.string;
import java.net.URL;
+import javax.annotation.Nonnull;
+
+import org.testng.Assert;
import org.testng.annotations.Test;
+import net.shibboleth.metadata.ErrorStatus;
+import net.shibboleth.metadata.testing.MockItem;
+import net.shibboleth.metadata.validate.Validator.Action;
+import net.shibboleth.metadata.validate.testing.CollectingValidator;
+import net.shibboleth.shared.collection.CollectionSupport;
+
public class AsURLStringValidatorTest {
- //@Test
- public void f() throws Exception {
- var v = new URL("http://wibble wobble");
- System.out.println(v);
- var v2 = new URL("data:asdadasd");
- System.out.println(v2);
+ /**
+ * Generic test for a good URL.
+ *
+ * @throws Exception if something goes wrong
+ */
+ private @Nonnull URL good(@Nonnull final String value) throws Exception {
+ final var cv = CollectingValidator.<URL>getInstance("collect");
+ final var item = new MockItem("item");
+ final var v = new AsURLStringValidator();
+ v.setId("test");
+ v.setValidators(CollectionSupport.singletonList(cv));
+ v.initialize();
+ var result = v.validate(value, item, "stage");
+ Assert.assertEquals(result, Action.CONTINUE);
+ Assert.assertTrue(item.getItemMetadata().isEmpty());
+ var collected = cv.getValues();
+ Assert.assertEquals(collected.size(), 1);
+ var url = collected.get(0);
+ cv.destroy();
+ v.destroy();
+ assert url != null;
+ return url;
+ }
+
+ @Test
+ public void testSuccess() throws Exception {
+ final var url = good("HTTPS://example.org:1234/example");
+ // Confirm lower-casing of protocol field.
+ Assert.assertEquals(url.getProtocol(), "https");
+ }
+
+ /**
+ * Generic test for a bad URL.
+ *
+ * @param bad bad URL to test
+ */
+ private @Nonnull ErrorStatus badURL(@Nonnull String value) throws Exception {
+ final var item = new MockItem("item");
+ final var v = new AsURLStringValidator();
+ v.setId("test");
+ v.initialize();
+ var result = v.validate(value, item, "stage");
+ Assert.assertEquals(result, Action.DONE);
+ final var errors = item.getItemMetadata().get(ErrorStatus.class);
+ Assert.assertEquals(errors.size(), 1);
+ final var error = errors.get(0);
+ assert error != null;
+ v.destroy();
+ return error;
+ }
+
+ @Test
+ public void testBadPort() throws Exception {
+ String bad = "https://example.org:port/example";
+ badURL(bad);
+ }
+
+
+ /**
+ * Test the case where the authority's port field is present but empty.
+ *
+ * This is valid by the specification, but is regarded as invalid by
+ * libxml2's xs:anyURI checker.
+ */
+// @Test
+// public void testEmptyPort() throws Exception {
+// badURL("http://example.org:/example/");
+// }
+
+// @Test
+// public void testDoubleScheme() throws Exception {
+// badURL("http://http://example.org/example/");
+// }
+
+ @Test
+ public void testBareDomain() throws Exception {
+ badURL("www.example.org");
+ }
+
+ @Test
+ public void testEmptyUrl() throws Exception {
+ badURL("");
+ }
+
+// @Test
+// public void testEmptyAuthority() throws Exception {
+// badURL("http:///foo/");
+// }
+
+ @Test
+ public void testFillInHostName() throws Exception {
+ badURL("http://*** FILL IN ***/");
}
}
diff --git a/mda-framework/src/test/resources/net/shibboleth/metadata/validate/string/AsURLStringValidatorLitmusTest-config.xml b/mda-framework/src/test/resources/net/shibboleth/metadata/validate/string/AsURLStringValidatorLitmusTest-config.xml
new file mode 100644
index 0000000..9d8f920
--- /dev/null
+++ b/mda-framework/src/test/resources/net/shibboleth/metadata/validate/string/AsURLStringValidatorLitmusTest-config.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:c="http://www.springframework.org/schema/c"
+ xmlns:p="http://www.springframework.org/schema/p"
+ xmlns:util="http://www.springframework.org/schema/util"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
+
+ <import resource="classpath:net/shibboleth/metadata/beans.xml"/>
+
+ <bean parent="mda.IdentifiableBeanPostProcessor"/>
+
+ <bean id="litmusTest" parent="mda.AsURLStringValidator">
+ <property name="validators">
+ <list>
+ <!-- URL validators go here -->
+ </list>
+ </property>
+ </bean>
+
+</beans>
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list