diff --git a/pom.xml b/pom.xml index 4f1fcce1..e5b577a5 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.apicatalog titanium - 1.4.1 + 1.5.0-SNAPSHOT pom_parent.xml titanium-json-ld diff --git a/pom_jre8.xml b/pom_jre8.xml index d07357ce..73402153 100644 --- a/pom_jre8.xml +++ b/pom_jre8.xml @@ -6,7 +6,7 @@ com.apicatalog titanium - 1.4.1 + 1.5.0-SNAPSHOT pom_parent.xml titanium-json-ld-jre8 diff --git a/pom_parent.xml b/pom_parent.xml index 6b51dd00..ca938554 100644 --- a/pom_parent.xml +++ b/pom_parent.xml @@ -6,7 +6,7 @@ 4.0.0 com.apicatalog titanium - 1.4.1 + 1.5.0-SNAPSHOT pom Titanium JSON-LD 1.1 diff --git a/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java b/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java index 1eecc27d..a4d66d97 100644 --- a/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java +++ b/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java @@ -26,6 +26,7 @@ import com.apicatalog.jsonld.loader.DocumentLoader; import com.apicatalog.jsonld.loader.SchemeRouter; +import com.apicatalog.jsonld.uri.UriValidationPolicy; import jakarta.json.JsonObject; import jakarta.json.JsonValue; @@ -57,7 +58,7 @@ public enum ProcessingPolicy { /* default values */ public static final boolean DEFAULT_RDF_STAR = false; public static final boolean DEFAULT_NUMERIC_ID = false; - public static final boolean DEFAULT_URI_VALIDATION = true; + public static final UriValidationPolicy DEFAULT_URI_VALIDATION = UriValidationPolicy.Full; /** * The base IRI to use when expanding or compacting the document. If set, this @@ -132,7 +133,7 @@ public enum ProcessingPolicy { // document cache private Cache documentCache; - private boolean uriValidation; + private UriValidationPolicy uriValidation; private Duration timeout; @@ -488,9 +489,15 @@ public void setRdfStar(boolean rdfStar) { * Enabled by default. *

* - * @return true if validation is enabled + * @deprecated use JsonLdOptions#getUriValidation + * @return true if full validation is enabled */ + @Deprecated public boolean isUriValidation() { + return uriValidation == UriValidationPolicy.Full; + } + + public UriValidationPolicy getUriValidation() { return uriValidation; } @@ -505,10 +512,16 @@ public boolean isUriValidation() { * Enabled by default. *

* + * @deprecated use JsonLdOptions#setUriValidation(com.apicatalog.jsonld.uri.UriValidationPolicy) * @param enabled set true to enable validation */ + @Deprecated public void setUriValidation(boolean enabled) { - this.uriValidation = enabled; + this.uriValidation = UriValidationPolicy.of(enabled); + } + + public void setUriValidation(UriValidationPolicy uriValidation) { + this.uriValidation = uriValidation; } /** diff --git a/src/main/java/com/apicatalog/jsonld/context/ActiveContext.java b/src/main/java/com/apicatalog/jsonld/context/ActiveContext.java index d17018de..c15263b4 100644 --- a/src/main/java/com/apicatalog/jsonld/context/ActiveContext.java +++ b/src/main/java/com/apicatalog/jsonld/context/ActiveContext.java @@ -160,7 +160,7 @@ public ActiveContextBuilder newContext() { } public UriExpansion uriExpansion() { - return UriExpansion.with(this).uriValidation(runtime.isUriValidation()); + return UriExpansion.with(this).uriValidation(runtime.getUriValidation()); } public ValueExpansion valueExpansion() { diff --git a/src/main/java/com/apicatalog/jsonld/context/TermDefinitionBuilder.java b/src/main/java/com/apicatalog/jsonld/context/TermDefinitionBuilder.java index c75581cb..1f709366 100644 --- a/src/main/java/com/apicatalog/jsonld/context/TermDefinitionBuilder.java +++ b/src/main/java/com/apicatalog/jsonld/context/TermDefinitionBuilder.java @@ -35,6 +35,7 @@ import com.apicatalog.jsonld.lang.LanguageTag; import com.apicatalog.jsonld.uri.UriUtils; +import com.apicatalog.jsonld.uri.UriValidationPolicy; import jakarta.json.JsonArray; import jakarta.json.JsonObject; import jakarta.json.JsonString; @@ -270,7 +271,7 @@ public void create(final String term) throws JsonLdError { && activeContext.runtime().isV10()) // 12.4. || (Keywords.noneMatch(expandedTypeString, Keywords.ID, Keywords.JSON, Keywords.NONE, Keywords.VOCAB) - && UriUtils.isNotAbsoluteUri(expandedTypeString, true))) { + && UriUtils.isNotAbsoluteUri(expandedTypeString, UriValidationPolicy.Full))) { throw new JsonLdError(JsonLdErrorCode.INVALID_TYPE_MAPPING); } diff --git a/src/main/java/com/apicatalog/jsonld/deseralization/JsonLdToRdf.java b/src/main/java/com/apicatalog/jsonld/deseralization/JsonLdToRdf.java index f2153755..bbb1102b 100644 --- a/src/main/java/com/apicatalog/jsonld/deseralization/JsonLdToRdf.java +++ b/src/main/java/com/apicatalog/jsonld/deseralization/JsonLdToRdf.java @@ -29,6 +29,7 @@ import com.apicatalog.jsonld.lang.Keywords; import com.apicatalog.jsonld.lang.Utils; import com.apicatalog.jsonld.uri.UriUtils; +import com.apicatalog.jsonld.uri.UriValidationPolicy; import com.apicatalog.rdf.Rdf; import com.apicatalog.rdf.RdfDataset; import com.apicatalog.rdf.RdfResource; @@ -50,7 +51,7 @@ public final class JsonLdToRdf { // optional private boolean produceGeneralizedRdf; private RdfDirection rdfDirection; - private boolean uriValidation; + private UriValidationPolicy uriValidation; private JsonLdToRdf(NodeMap nodeMap, RdfDataset dataset) { this.nodeMap = nodeMap; @@ -202,7 +203,15 @@ public RdfDataset build() throws JsonLdError { return dataset; } + /** + * @deprecated use JsonLdToRdf#uriValidation(com.apicatalog.jsonld.uri.UriValidationPolicy) + */ + @Deprecated public JsonLdToRdf uriValidation(boolean uriValidation) { + return uriValidation(UriValidationPolicy.of(uriValidation)); + } + + public JsonLdToRdf uriValidation(UriValidationPolicy uriValidation) { this.uriValidation = uriValidation; return this; } diff --git a/src/main/java/com/apicatalog/jsonld/deseralization/ListToRdf.java b/src/main/java/com/apicatalog/jsonld/deseralization/ListToRdf.java index fffcb201..98930b47 100644 --- a/src/main/java/com/apicatalog/jsonld/deseralization/ListToRdf.java +++ b/src/main/java/com/apicatalog/jsonld/deseralization/ListToRdf.java @@ -24,6 +24,7 @@ import com.apicatalog.jsonld.JsonLdOptions.RdfDirection; import com.apicatalog.jsonld.flattening.NodeMap; import com.apicatalog.jsonld.json.JsonUtils; +import com.apicatalog.jsonld.uri.UriValidationPolicy; import com.apicatalog.rdf.Rdf; import com.apicatalog.rdf.RdfTriple; import com.apicatalog.rdf.RdfValue; @@ -46,7 +47,7 @@ final class ListToRdf { // optional private RdfDirection rdfDirection; - private boolean uriValidation; + private UriValidationPolicy uriValidation; private ListToRdf(final JsonArray list, final List triples, NodeMap nodeMap) { this.list = list; @@ -120,7 +121,15 @@ public RdfValue build() throws JsonLdError { return Rdf.createBlankNode(bnodes[0]); } + /** + * @deprecated use ListToRdf#uriValidation(com.apicatalog.jsonld.uri.UriValidationPolicy) + */ + @Deprecated public ListToRdf uriValidation(boolean uriValidation) { + return uriValidation(UriValidationPolicy.of(uriValidation)); + } + + public ListToRdf uriValidation(UriValidationPolicy uriValidation) { this.uriValidation = uriValidation; return this; } diff --git a/src/main/java/com/apicatalog/jsonld/deseralization/ObjectToRdf.java b/src/main/java/com/apicatalog/jsonld/deseralization/ObjectToRdf.java index 9964c9e4..75007bf6 100644 --- a/src/main/java/com/apicatalog/jsonld/deseralization/ObjectToRdf.java +++ b/src/main/java/com/apicatalog/jsonld/deseralization/ObjectToRdf.java @@ -37,6 +37,7 @@ import com.apicatalog.jsonld.lang.NodeObject; import com.apicatalog.jsonld.lang.ValueObject; import com.apicatalog.jsonld.uri.UriUtils; +import com.apicatalog.jsonld.uri.UriValidationPolicy; import com.apicatalog.rdf.Rdf; import com.apicatalog.rdf.RdfLiteral; import com.apicatalog.rdf.RdfResource; @@ -71,7 +72,7 @@ final class ObjectToRdf { // optional private RdfDirection rdfDirection; - private boolean uriValidation; + private UriValidationPolicy uriValidation; private ObjectToRdf(JsonObject item, List triples, NodeMap nodeMap) { this.item = item; @@ -295,7 +296,15 @@ private static final String toXsdDouble(BigDecimal bigDecimal) { return xsdNumberFormat.format(bigDecimal); } + /** + * @deprecated use Object#uriValidation(com.apicatalog.jsonld.uri.UriValidationPolicy) + */ + @Deprecated public ObjectToRdf uriValidation(boolean uriValidation) { + return uriValidation(UriValidationPolicy.of(uriValidation)); + } + + public ObjectToRdf uriValidation(UriValidationPolicy uriValidation) { this.uriValidation = uriValidation; return this; } diff --git a/src/main/java/com/apicatalog/jsonld/expansion/UriExpansion.java b/src/main/java/com/apicatalog/jsonld/expansion/UriExpansion.java index 3a6397bf..b844d4ab 100644 --- a/src/main/java/com/apicatalog/jsonld/expansion/UriExpansion.java +++ b/src/main/java/com/apicatalog/jsonld/expansion/UriExpansion.java @@ -30,6 +30,7 @@ import com.apicatalog.jsonld.uri.UriResolver; import com.apicatalog.jsonld.uri.UriUtils; +import com.apicatalog.jsonld.uri.UriValidationPolicy; import jakarta.json.JsonObject; import jakarta.json.JsonString; import jakarta.json.JsonValue; @@ -51,7 +52,7 @@ public final class UriExpansion { // optional private boolean documentRelative; private boolean vocab; - private boolean uriValidation; + private UriValidationPolicy uriValidation; private JsonObject localContext; private Map defined; @@ -206,7 +207,16 @@ private String expandResult(final String result) { return result; } + + /** + * @deprecated use UriExpansion#uriValidation(com.apicatalog.jsonld.uri.UriValidationPolicy) + */ + @Deprecated public UriExpansion uriValidation(boolean uriValidation) { + return uriValidation(UriValidationPolicy.of(uriValidation)); + } + + public UriExpansion uriValidation(UriValidationPolicy uriValidation) { this.uriValidation = uriValidation; return this; } diff --git a/src/main/java/com/apicatalog/jsonld/framing/Frame.java b/src/main/java/com/apicatalog/jsonld/framing/Frame.java index 79c76c25..a7cdc710 100644 --- a/src/main/java/com/apicatalog/jsonld/framing/Frame.java +++ b/src/main/java/com/apicatalog/jsonld/framing/Frame.java @@ -31,6 +31,7 @@ import com.apicatalog.jsonld.lang.ValueObject; import com.apicatalog.jsonld.uri.UriUtils; +import com.apicatalog.jsonld.uri.UriValidationPolicy; import jakarta.json.JsonArray; import jakarta.json.JsonObject; import jakarta.json.JsonString; @@ -172,9 +173,9 @@ private static final boolean validateFrameId(JsonObject frame) { || idArray .stream() .noneMatch(item -> JsonUtils.isNotString(item) - || UriUtils.isNotAbsoluteUri(((JsonString)item).getString(), true))); + || UriUtils.isNotAbsoluteUri(((JsonString)item).getString(), UriValidationPolicy.Full))); } - return JsonUtils.isString(id) && UriUtils.isAbsoluteUri(((JsonString)id).getString(), true); + return JsonUtils.isString(id) && UriUtils.isAbsoluteUri(((JsonString)id).getString(), UriValidationPolicy.Full); } private static final boolean validateFrameType(JsonObject frame) { @@ -193,12 +194,12 @@ private static final boolean validateFrameType(JsonObject frame) { || typeArray .stream() .noneMatch(item -> JsonUtils.isNotString(item) - || UriUtils.isNotAbsoluteUri(((JsonString)item).getString(), true))); + || UriUtils.isNotAbsoluteUri(((JsonString)item).getString(), UriValidationPolicy.Full))); } return JsonUtils.isEmptyArray(type) || JsonUtils.isEmptyObject(type) - || JsonUtils.isString(type) && UriUtils.isAbsoluteUri(((JsonString)type).getString(), true); + || JsonUtils.isString(type) && UriUtils.isAbsoluteUri(((JsonString)type).getString(), UriValidationPolicy.Full); } public Set keys() { diff --git a/src/main/java/com/apicatalog/jsonld/processor/FromRdfProcessor.java b/src/main/java/com/apicatalog/jsonld/processor/FromRdfProcessor.java index a0e80b0b..f47ddc49 100644 --- a/src/main/java/com/apicatalog/jsonld/processor/FromRdfProcessor.java +++ b/src/main/java/com/apicatalog/jsonld/processor/FromRdfProcessor.java @@ -40,7 +40,7 @@ public static final JsonArray fromRdf(final Document document, final JsonLdOptio .useNativeTypes(options.isUseNativeTypes()) .useRdfType(options.isUseRdfType()) .processingMode(options.getProcessingMode()) - .uriValidation(options.isUriValidation()) + .uriValidation(options.getUriValidation()) .build(); } diff --git a/src/main/java/com/apicatalog/jsonld/processor/ProcessingRuntime.java b/src/main/java/com/apicatalog/jsonld/processor/ProcessingRuntime.java index 0d92e7ee..d41d2515 100644 --- a/src/main/java/com/apicatalog/jsonld/processor/ProcessingRuntime.java +++ b/src/main/java/com/apicatalog/jsonld/processor/ProcessingRuntime.java @@ -8,6 +8,7 @@ import com.apicatalog.jsonld.document.Document; import com.apicatalog.jsonld.loader.DocumentLoader; +import com.apicatalog.jsonld.uri.UriValidationPolicy; import jakarta.json.JsonValue; /** @@ -49,10 +50,18 @@ public void tick() throws JsonLdError {/* NOP does nothing if timeout is not set */ public void resetTicker() {/* NOP does nothing if timeout is not set */} + /** + * @deprecated use ProcessingRuntime#getUriValidation() + */ + @Deprecated public boolean isUriValidation() { return options.isUriValidation(); } + public UriValidationPolicy getUriValidation() { + return options.getUriValidation(); + } + public boolean isV10() { return options.getProcessingMode() != null && options.getProcessingMode().equals(JsonLdVersion.V1_0); } diff --git a/src/main/java/com/apicatalog/jsonld/processor/ToRdfProcessor.java b/src/main/java/com/apicatalog/jsonld/processor/ToRdfProcessor.java index 55496dc9..863b9d8a 100644 --- a/src/main/java/com/apicatalog/jsonld/processor/ToRdfProcessor.java +++ b/src/main/java/com/apicatalog/jsonld/processor/ToRdfProcessor.java @@ -75,7 +75,7 @@ public static final RdfDataset toRdf(Document input, final JsonLdOptions options ) .produceGeneralizedRdf(options.isProduceGeneralizedRdf()) .rdfDirection(options.getRdfDirection()) - .uriValidation(options.isUriValidation()) + .uriValidation(options.getUriValidation()) .build(); } } diff --git a/src/main/java/com/apicatalog/jsonld/serialization/RdfToJsonld.java b/src/main/java/com/apicatalog/jsonld/serialization/RdfToJsonld.java index 18327cff..d4f79ee0 100644 --- a/src/main/java/com/apicatalog/jsonld/serialization/RdfToJsonld.java +++ b/src/main/java/com/apicatalog/jsonld/serialization/RdfToJsonld.java @@ -34,6 +34,7 @@ import com.apicatalog.jsonld.lang.LanguageTag; import com.apicatalog.jsonld.lang.Utils; import com.apicatalog.jsonld.uri.UriUtils; +import com.apicatalog.jsonld.uri.UriValidationPolicy; import com.apicatalog.rdf.RdfDataset; import com.apicatalog.rdf.RdfGraph; import com.apicatalog.rdf.RdfResource; @@ -57,7 +58,7 @@ public final class RdfToJsonld { private RdfDirection rdfDirection; private boolean useNativeTypes; private boolean useRdfType; - private boolean uriValidation; + private UriValidationPolicy uriValidation; private JsonLdVersion processingMode; @@ -434,7 +435,15 @@ protected static class Reference { private JsonObject value; } + /** + * @deprecated use RdfToJsonld#uriValidation(com.apicatalog.jsonld.uri.UriValidationPolicy) + */ + @Deprecated public RdfToJsonld uriValidation(boolean uriValidation) { + return uriValidation(UriValidationPolicy.of(uriValidation)); + } + + public RdfToJsonld uriValidation(UriValidationPolicy uriValidation) { this.uriValidation = uriValidation; return this; } diff --git a/src/main/java/com/apicatalog/jsonld/uri/UriUtils.java b/src/main/java/com/apicatalog/jsonld/uri/UriUtils.java index 851a3701..a15e02f1 100644 --- a/src/main/java/com/apicatalog/jsonld/uri/UriUtils.java +++ b/src/main/java/com/apicatalog/jsonld/uri/UriUtils.java @@ -94,15 +94,28 @@ public static final boolean isNotURI(final String uri) { */ @Deprecated public static final boolean isNotAbsoluteUri(final String uri) { - return isNotAbsoluteUri(uri, true); + return isNotAbsoluteUri(uri, UriValidationPolicy.of(true)); } + /** + * Deprecated in favor of {@link UriUtils#isNotAbsoluteUri(String, UriValidationPolicy)} + * + * @deprecated since 1.3.0 + * + * @param uri to check + * @return true if the given URI is absolute + */ + @Deprecated public static final boolean isNotAbsoluteUri(final String uri, final boolean validate) { - return !isAbsoluteUri(uri, validate); + return !isAbsoluteUri(uri, UriValidationPolicy.of(validate)); + } + + public static final boolean isNotAbsoluteUri(final String uri, UriValidationPolicy policy) { + return !isAbsoluteUri(uri, policy); } /** - * Deprecated in favor of {@link UriUtils#isAbsoluteUri(String, boolean)} + * Deprecated in favor of {@link UriUtils#isAbsoluteUri(String, UriValidationPolicy)} * * @deprecated since 1.3.0 * @@ -111,28 +124,44 @@ public static final boolean isNotAbsoluteUri(final String uri, final boolean val */ @Deprecated public static final boolean isAbsoluteUri(final String uri) { - return isAbsoluteUri(uri, true); + return isAbsoluteUri(uri, UriValidationPolicy.Full); } - public static final boolean isAbsoluteUri(final String uri, final boolean validate) { - - // if URI validation is disabled - if (!validate) { - // then validate just a scheme - return startsWithScheme(uri); - } + /** + * Deprecated in favor of {@link UriUtils#isAbsoluteUri(String, UriValidationPolicy)} + * + * @deprecated since 1.4.2 + * + * @param uri to check + * @return true if the given URI is absolute + */ + @Deprecated + public static final boolean isAbsoluteUri(final String uri, boolean validate) { + return isAbsoluteUri(uri, UriValidationPolicy.of(validate)); + } - if (uri == null - || uri.length() < 3 // minimal form s(1):ssp(1) + public static final boolean isAbsoluteUri(final String uri, final UriValidationPolicy policy) { + switch (policy) { + case None: + return true; + case SchemeOnly: + return startsWithScheme(uri); + case Full: + if (uri == null + || uri.length() < 3 // minimal form s(1):ssp(1) ) { - return false; + return false; + } else{ + try { + return URI.create(uri).isAbsolute(); + } catch (IllegalArgumentException e) { + return false; + } + } + default: + return false; } - try { - return URI.create(uri).isAbsolute(); - } catch (IllegalArgumentException e) { - return false; - } } private static final boolean startsWithScheme(final String uri) { diff --git a/src/main/java/com/apicatalog/jsonld/uri/UriValidationPolicy.java b/src/main/java/com/apicatalog/jsonld/uri/UriValidationPolicy.java new file mode 100644 index 00000000..abb64f37 --- /dev/null +++ b/src/main/java/com/apicatalog/jsonld/uri/UriValidationPolicy.java @@ -0,0 +1,26 @@ +package com.apicatalog.jsonld.uri; + +public enum UriValidationPolicy { + + /** + * No validation is performed + */ + None, + + /** + * The validation only targets the scheme + */ + SchemeOnly, + + /** + * The validation is be fully performed + */ + Full ; + + /** + * Method allowing to convert the legacy boolean to the matching policy + */ + public static UriValidationPolicy of(boolean value) { + return value ? Full : SchemeOnly ; + } +} diff --git a/src/main/java/com/apicatalog/rdf/Rdf.java b/src/main/java/com/apicatalog/rdf/Rdf.java index 24fbb65d..ce107913 100644 --- a/src/main/java/com/apicatalog/rdf/Rdf.java +++ b/src/main/java/com/apicatalog/rdf/Rdf.java @@ -27,6 +27,7 @@ import com.apicatalog.jsonld.http.media.MediaType; import com.apicatalog.jsonld.lang.BlankNode; import com.apicatalog.jsonld.uri.UriUtils; +import com.apicatalog.jsonld.uri.UriValidationPolicy; import com.apicatalog.rdf.io.RdfReader; import com.apicatalog.rdf.io.RdfWriter; import com.apicatalog.rdf.io.error.UnsupportedContentException; @@ -133,7 +134,7 @@ public static RdfValue createValue(String value) { return RdfProvider.provider().createBlankNode(value); } - if (UriUtils.isAbsoluteUri(value, true)) { + if (UriUtils.isAbsoluteUri(value, UriValidationPolicy.Full)) { return RdfProvider.provider().createIRI(value); } @@ -184,7 +185,7 @@ public static RdfResource createResource(String resource) { return RdfProvider.provider().createBlankNode(resource); } - if (UriUtils.isAbsoluteUri(resource, true)) { + if (UriUtils.isAbsoluteUri(resource, UriValidationPolicy.Full)) { return RdfProvider.provider().createIRI(resource); } diff --git a/src/main/java/com/apicatalog/rdf/io/nquad/NQuadsReader.java b/src/main/java/com/apicatalog/rdf/io/nquad/NQuadsReader.java index ab69d73d..8f1498eb 100644 --- a/src/main/java/com/apicatalog/rdf/io/nquad/NQuadsReader.java +++ b/src/main/java/com/apicatalog/rdf/io/nquad/NQuadsReader.java @@ -19,6 +19,7 @@ import java.util.Arrays; import com.apicatalog.jsonld.uri.UriUtils; +import com.apicatalog.jsonld.uri.UriValidationPolicy; import com.apicatalog.rdf.Rdf; import com.apicatalog.rdf.RdfDataset; import com.apicatalog.rdf.RdfLiteral; @@ -242,7 +243,7 @@ private void skipWhitespace(int min) throws RdfReaderException { } private static final void assertAbsoluteIri(final String iri, final String what) throws RdfReaderException { - if (UriUtils.isNotAbsoluteUri(iri, true)) { + if (UriUtils.isNotAbsoluteUri(iri, UriValidationPolicy.Full)) { throw new RdfReaderException(what + " must be an absolute IRI [" + iri + "]. "); } } diff --git a/src/test/java/com/apicatalog/jsonld/api/ToRdfApiTest.java b/src/test/java/com/apicatalog/jsonld/api/ToRdfApiTest.java index a5a4dcd2..3acbee88 100644 --- a/src/test/java/com/apicatalog/jsonld/api/ToRdfApiTest.java +++ b/src/test/java/com/apicatalog/jsonld/api/ToRdfApiTest.java @@ -15,22 +15,26 @@ */ package com.apicatalog.jsonld.api; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.io.ByteArrayInputStream; -import java.io.InputStreamReader; -import java.net.URI; - -import org.junit.jupiter.api.Test; - import com.apicatalog.jsonld.JsonLd; import com.apicatalog.jsonld.JsonLdError; +import com.apicatalog.jsonld.JsonLdOptions; +import com.apicatalog.jsonld.document.Document; import com.apicatalog.jsonld.document.JsonDocument; import com.apicatalog.jsonld.http.media.MediaType; +import com.apicatalog.jsonld.uri.UriValidationPolicy; import com.apicatalog.rdf.RdfDataset; - +import com.apicatalog.rdf.RdfNQuad; import jakarta.json.JsonValue; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URI; +import java.util.function.Predicate; + +import static org.junit.jupiter.api.Assertions.*; class ToRdfApiTest { @@ -112,4 +116,80 @@ void test11() throws JsonLdError { assertNotNull(result); assertEquals(0, result.size()); } + + private final String invalidUri = " http://example.com/invalid"; + private final String expandedInvalidSubject = "/com/apicatalog/jsonld/test/issue383-expanded-subject.json"; + + @Test + void test12() throws JsonLdError, IOException { + RdfDataset result = readRdfDataset(expandedInvalidSubject, UriValidationPolicy.None); + + boolean includeInvalidSubject = anyInDataset(result, rdfNQuad -> rdfNQuad.getSubject().getValue().equals(invalidUri)); + + assertTrue(includeInvalidSubject, "The resulting dataset without uri validation should include :" + invalidUri); + } + + @Test + void test13() throws JsonLdError, IOException { + RdfDataset result = readRdfDataset(expandedInvalidSubject, UriValidationPolicy.Full); + + boolean includeInvalidSubject = anyInDataset(result, rdfNQuad -> rdfNQuad.getSubject().getValue().equals(invalidUri)); + + assertFalse(includeInvalidSubject, "The resulting dataset with uri validation should NOT include :" + invalidUri); + } + + private final String expandedInvalidPredicate = "/com/apicatalog/jsonld/test/issue383-expanded-predicate.json"; + + @Test + void test14() throws JsonLdError, IOException { + RdfDataset result = readRdfDataset(expandedInvalidPredicate, UriValidationPolicy.None); + + boolean includeInvalidPredicate = anyInDataset(result, rdfNQuad -> rdfNQuad.getPredicate().getValue().equals(invalidUri)); + + assertTrue(includeInvalidPredicate, "The resulting dataset without uri validation should include :" + invalidUri); + } + + @Test + void test15() throws JsonLdError, IOException { + RdfDataset result = readRdfDataset(expandedInvalidPredicate, UriValidationPolicy.Full); + + boolean includeInvalidPredicate = anyInDataset(result, rdfNQuad -> rdfNQuad.getPredicate().getValue().equals(invalidUri)); + + assertFalse(includeInvalidPredicate, "The resulting dataset with uri validation should NOT include :" + invalidUri); + } + + private final String expandedInvalidObject = "/com/apicatalog/jsonld/test/issue383-expanded-object.json"; + + @Test + void test16() throws JsonLdError, IOException { + RdfDataset result = readRdfDataset(expandedInvalidObject, UriValidationPolicy.None); + + boolean includeInvalidObject = anyInDataset(result, rdfNQuad -> rdfNQuad.getObject().getValue().equals(invalidUri)); + + assertTrue(includeInvalidObject, "The resulting dataset without uri validation should include :" + invalidUri); + } + + @Test + void test17() throws JsonLdError, IOException { + RdfDataset result = readRdfDataset(expandedInvalidObject, UriValidationPolicy.Full); + + boolean includeInvalidObject = anyInDataset(result, rdfNQuad -> rdfNQuad.getObject().getValue().equals(invalidUri)); + + assertFalse(includeInvalidObject, "The resulting dataset with uri validation should NOT include :" + invalidUri); + } + + private boolean anyInDataset(final RdfDataset dataset, Predicate predicate) { + return dataset.toList().stream().anyMatch(predicate); + } + + private RdfDataset readRdfDataset(final String name, final UriValidationPolicy uriValidation) throws JsonLdError, IOException { + try (final InputStream is = getClass().getResourceAsStream(name)) { + JsonLdOptions options = new JsonLdOptions(); + options.setUriValidation(uriValidation); + Document document = JsonDocument.of(is); + + return JsonLd.toRdf(document) + .options(options).get(); + } + } } diff --git a/src/test/resources/com/apicatalog/jsonld/test/issue383-expanded-object.json b/src/test/resources/com/apicatalog/jsonld/test/issue383-expanded-object.json new file mode 100644 index 00000000..3aa719c3 --- /dev/null +++ b/src/test/resources/com/apicatalog/jsonld/test/issue383-expanded-object.json @@ -0,0 +1,27 @@ +[ + { + "@id": "http://example.com/john-doe", + "@type": [ + "http://schema.org/Person" + ], + "http://example.com/address": [ + { + "http://example.com/customid": [ + { + "@id": " http://example.com/invalid" + } + ], + "http://example.com/number": [ + { + "@value": 2 + } + ], + "http://example.com/street": [ + { + "@value": "my street" + } + ] + } + ] + } +] \ No newline at end of file diff --git a/src/test/resources/com/apicatalog/jsonld/test/issue383-expanded-predicate.json b/src/test/resources/com/apicatalog/jsonld/test/issue383-expanded-predicate.json new file mode 100644 index 00000000..94ac4019 --- /dev/null +++ b/src/test/resources/com/apicatalog/jsonld/test/issue383-expanded-predicate.json @@ -0,0 +1,22 @@ +[ + { + "@id": "http://example.com/john-doe", + "@type": [ + "http://schema.org/Person" + ], + "http://example.com/address": [ + { + " http://example.com/invalid": [ + { + "@value": 2 + } + ], + "http://example.com/street": [ + { + "@value": "my street" + } + ] + } + ] + } +] \ No newline at end of file diff --git a/src/test/resources/com/apicatalog/jsonld/test/issue383-expanded-subject.json b/src/test/resources/com/apicatalog/jsonld/test/issue383-expanded-subject.json new file mode 100644 index 00000000..c037e173 --- /dev/null +++ b/src/test/resources/com/apicatalog/jsonld/test/issue383-expanded-subject.json @@ -0,0 +1,22 @@ +[ + { + "@id": " http://example.com/invalid", + "@type": [ + "http://schema.org/Person" + ], + "http://example.com/address": [ + { + "http://example.com/number": [ + { + "@value": 2 + } + ], + "http://example.com/street": [ + { + "@value": "my street" + } + ] + } + ] + } +] \ No newline at end of file