Skip to content

Commit

Permalink
Better JSON-LD support with Quarkus (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
acoburn authored Apr 18, 2020
1 parent 25097ed commit 045a7c7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
15 changes: 15 additions & 0 deletions components/test/src/main/java/org/trellisldp/test/LdpRdfTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public interface LdpRdfTests extends CommonTests {
String SIMPLE_RESOURCE = "/simpleResource.ttl";
String BASIC_CONTAINER = "/basicContainer.ttl";
String ANNOTATION_RESOURCE = "/annotation.ttl";
String SHEX_JSONLD = "/shex.jsonld";

/**
* Set the location of the test resource.
Expand Down Expand Up @@ -108,6 +109,7 @@ default Stream<Executable> runTests() throws Exception {
this::testGetNTriples,
this::testGetRDF,
this::testRdfContainment,
this::testPostJsonLd,
this::testInvalidRDF);
}

Expand Down Expand Up @@ -320,6 +322,19 @@ default void testRdfContainment() throws Exception {
}
}

/**
* Verify that POSTing JSON-LD is supported.
*/
default void testPostJsonLd() {
final String rdf = getResourceAsString(SHEX_JSONLD);

// POST an LDP-RS
try (final Response res = target().request().header(SLUG, generateRandomValue(getClass().getSimpleName()))
.post(entity(rdf, APPLICATION_LD_JSON_TYPE))) {
assertAll("Check POSTing an RDF resource", checkRdfResponse(res, LDP.RDFSource, null));
}
}

/**
* Test creating resource with invalid RDF.
*/
Expand Down
16 changes: 16 additions & 0 deletions components/test/src/main/resources/org/trellisldp/test/shex.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{ "@context": "http://www.w3.org/ns/shex.jsonld",
"type": "Schema",
"shapes": [
{ "id": "http://school.example/#enrolleeAge",
"type": "NodeConstraint",
"datatype": "http://www.w3.org/2001/XMLSchema#integer",
"mininclusive": 13, "maxinclusive": 20 },
{ "id": "http://school.example/#Enrollee",
"type": "Shape",
"expression": {
"type": "TripleConstraint", "min": 1, "max": 2,
"predicate": "http://ex.example/#hasGuardian",
"valueExpr": {
"type": "NodeConstraint",
"nodeKind": "iri" } } }
] }
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import static org.trellisldp.http.core.HttpConstants.CONFIG_HTTP_VERSIONING;
import static org.trellisldp.http.impl.HttpUtils.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -39,6 +41,7 @@
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.StreamingOutput;

import org.apache.commons.io.IOUtils;
import org.apache.commons.rdf.api.Dataset;
import org.apache.commons.rdf.api.Graph;
import org.apache.commons.rdf.api.IRI;
Expand Down Expand Up @@ -169,16 +172,17 @@ protected IRI getInternalId() {
* @param dataset the dataset
*/
protected void readEntityIntoDataset(final IRI graphName, final RDFSyntax syntax, final Dataset dataset) {
try {
getServices().getIOService().read(entity, syntax, getIdentifier())
try (final InputStream in = new ByteArrayInputStream(IOUtils.toByteArray(entity))) {
getServices().getIOService().read(in, syntax, getIdentifier())
.map(skolemizeTriples(getServices().getResourceService(), getBaseUrl()))
.filter(triple -> !RDF.type.equals(triple.getPredicate())
|| !triple.getObject().ntriplesString().startsWith("<" + LDP.getNamespace()))
.filter(triple -> !LDP.contains.equals(triple.getPredicate())).map(triple ->
rdf.createQuad(graphName, triple.getSubject(), triple.getPredicate(), triple.getObject()))
.forEachOrdered(dataset::add);
} catch (final IOException ex) {
throw new BadRequestException("Error handling input stream: " + ex.getMessage(), ex);
} catch (final RuntimeTrellisException ex) {
LOGGER.debug("Could not parse incoming RDF", ex);
throw new BadRequestException("Invalid RDF content: " + ex.getMessage(), ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,24 @@ void testBinaryEntityNoContentType() throws IOException {
}
}

@Test
void testRdfIOError() throws IOException {
final Triple triple = rdf.createTriple(rdf.createIRI(baseUrl + NEW_RESOURCE), DC.title,
rdf.createLiteral("A title"));

when(mockIoService.supportedWriteSyntaxes()).thenReturn(asList(TURTLE, JSONLD, NTRIPLES));
when(mockIoService.read(any(), eq(TURTLE), any())).thenAnswer(x -> Stream.of(triple));
when(mockTrellisRequest.getContentType()).thenReturn("text/turtle");

final InputStream ioStream = getClass().getResource(RESOURCE_TURTLE).openStream();
ioStream.close();
final PostHandler handler = new PostHandler(mockTrellisRequest, root, NEW_RESOURCE, ioStream, mockBundler,
extensions, null);
assertThrows(BadRequestException.class, () ->
unwrapAsyncError(handler.createResource(handler.initialize(mockParent, MISSING_RESOURCE))));
}


@Test
void testError() throws IOException {
when(mockResourceService.create(any(Metadata.class), any(Dataset.class))).thenReturn(asyncException());
Expand Down
1 change: 1 addition & 0 deletions platform/quarkus/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ dependencies {
implementation 'io.quarkus:quarkus-smallrye-openapi'
implementation 'io.quarkus:quarkus-smallrye-reactive-messaging'

implementation "com.github.jsonld-java:jsonld-java:$jsonldVersion"
implementation "com.github.spullara.mustache.java:compiler:$mustacheVersion"
implementation "com.google.guava:guava:$guavaVersion"
implementation "org.apache.jena:jena-arq:$jenaVersion"
Expand Down

0 comments on commit 045a7c7

Please sign in to comment.