From a6768b693605f09a766c80df7e6232577cfbbc4a Mon Sep 17 00:00:00 2001 From: Aaron Birkland Date: Fri, 9 Mar 2018 02:50:52 -0500 Subject: [PATCH] Add jsonld deserialization filter, fix, test --- jsonld-addon-filters/pom.xml | 7 + .../jsonld/{compact => }/ConfigUtil.java | 4 +- .../jsonld/{compact => }/JsonldUtil.java | 6 +- .../jsonld/compact/CompactionFilter.java | 26 ++- .../jsonld/compact/CompactionWrapper.java | 2 +- .../deserialize/DeserializationFilter.java | 86 ++++++++++ .../deserialize/DeserializationWrapper.java | 149 ++++++++++++++++++ .../deserialize/JsonldNtriplesTranslator.java | 55 +++++++ .../jsonld/{compact => }/ConfigUtilTest.java | 12 +- .../jsonld/compact/CompactionFilterTest.java | 5 +- .../fcrepo/jsonld/compact/CompactorTest.java | 2 +- .../compact/JsonldNtriplesTranslatorTest.java | 106 +++++++++++++ .../src/test/resources/file.nt | 6 + .../src/test/resources/null-relative.json | 45 ++++++ .../src/test/resources/null-relative.ttl | 6 + .../src/test/resources/uri-context.json | 10 ++ .../jsonld/integration/CompactionIT.java | 2 +- .../jsonld/integration/DeserializationIT.java | 92 +++++++++++ .../src/test/resources/compact-uri.json | 10 ++ .../src/test/resources/web.xml | 12 ++ 20 files changed, 619 insertions(+), 24 deletions(-) rename jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/{compact => }/ConfigUtil.java (97%) rename jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/{compact => }/JsonldUtil.java (94%) create mode 100644 jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/deserialize/DeserializationFilter.java create mode 100644 jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/deserialize/DeserializationWrapper.java create mode 100644 jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/deserialize/JsonldNtriplesTranslator.java rename jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/{compact => }/ConfigUtilTest.java (83%) create mode 100644 jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/JsonldNtriplesTranslatorTest.java create mode 100644 jsonld-addon-filters/src/test/resources/file.nt create mode 100644 jsonld-addon-filters/src/test/resources/null-relative.json create mode 100644 jsonld-addon-filters/src/test/resources/null-relative.ttl create mode 100644 jsonld-addon-filters/src/test/resources/uri-context.json create mode 100644 jsonld-addon-integration/src/test/java/org/dataconservancy/fcrepo/jsonld/integration/DeserializationIT.java create mode 100644 jsonld-addon-integration/src/test/resources/compact-uri.json diff --git a/jsonld-addon-filters/pom.xml b/jsonld-addon-filters/pom.xml index bb51910..4badb45 100644 --- a/jsonld-addon-filters/pom.xml +++ b/jsonld-addon-filters/pom.xml @@ -98,6 +98,13 @@ test + + org.apache.jena + jena-core + 3.6.0 + test + + org.json json diff --git a/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/compact/ConfigUtil.java b/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/ConfigUtil.java similarity index 97% rename from jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/compact/ConfigUtil.java rename to jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/ConfigUtil.java index 091194a..6cb7bce 100644 --- a/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/compact/ConfigUtil.java +++ b/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/ConfigUtil.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.dataconservancy.fcrepo.jsonld.compact; +package org.dataconservancy.fcrepo.jsonld; import static java.util.stream.Stream.concat; @@ -30,7 +30,7 @@ * * @author apb@jhu.edu */ -class ConfigUtil { +public class ConfigUtil { static final Logger LOG = LoggerFactory.getLogger(ConfigUtil.class); diff --git a/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/compact/JsonldUtil.java b/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/JsonldUtil.java similarity index 94% rename from jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/compact/JsonldUtil.java rename to jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/JsonldUtil.java index 94bd97d..11c54ec 100644 --- a/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/compact/JsonldUtil.java +++ b/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/JsonldUtil.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package org.dataconservancy.fcrepo.jsonld.compact; +package org.dataconservancy.fcrepo.jsonld; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.dataconservancy.fcrepo.jsonld.compact.ConfigUtil.extract; -import static org.dataconservancy.fcrepo.jsonld.compact.ConfigUtil.props; +import static org.dataconservancy.fcrepo.jsonld.ConfigUtil.extract; +import static org.dataconservancy.fcrepo.jsonld.ConfigUtil.props; import java.io.FileInputStream; import java.io.FileNotFoundException; diff --git a/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/compact/CompactionFilter.java b/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/compact/CompactionFilter.java index bf185dd..a5b2797 100644 --- a/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/compact/CompactionFilter.java +++ b/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/compact/CompactionFilter.java @@ -16,8 +16,8 @@ package org.dataconservancy.fcrepo.jsonld.compact; -import static org.dataconservancy.fcrepo.jsonld.compact.ConfigUtil.getValue; -import static org.dataconservancy.fcrepo.jsonld.compact.JsonldUtil.loadContexts; +import static org.dataconservancy.fcrepo.jsonld.ConfigUtil.getValue; +import static org.dataconservancy.fcrepo.jsonld.JsonldUtil.loadContexts; import java.io.IOException; import java.net.MalformedURLException; @@ -30,6 +30,7 @@ import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; @@ -82,13 +83,22 @@ public void init(FilterConfig filterConfig) throws ServletException { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - LOG.debug("Servicing response"); + LOG.debug("Compaction filter is considering response"); - LOG.debug("Initial Output Stream: " + response.getOutputStream()); - final CompactionWrapper compactionWrapper = new CompactionWrapper((HttpServletResponse) response, compactor, - defaultContext); - chain.doFilter(request, compactionWrapper); - compactionWrapper.getOutputStream().close(); + final String method = ((HttpServletRequest) request).getMethod(); + + if (method.equalsIgnoreCase("GET")) { + + LOG.debug("Compaction filter is compacting"); + final CompactionWrapper compactionWrapper = new CompactionWrapper((HttpServletResponse) response, + compactor, + defaultContext); + chain.doFilter(request, compactionWrapper); + compactionWrapper.getOutputStream().close(); + } else { + LOG.debug("Compaction filter is doing nothing"); + chain.doFilter(request, response); + } } @Override diff --git a/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/compact/CompactionWrapper.java b/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/compact/CompactionWrapper.java index 871b678..085ac35 100644 --- a/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/compact/CompactionWrapper.java +++ b/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/compact/CompactionWrapper.java @@ -82,7 +82,7 @@ public void close() throws IOException { @Override public void setContentLength(int len) { - LOG.info("Ignoring content length of {}", len); + LOG.debug("Ignoring content length of {}", len); } @Override diff --git a/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/deserialize/DeserializationFilter.java b/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/deserialize/DeserializationFilter.java new file mode 100644 index 0000000..feb15d1 --- /dev/null +++ b/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/deserialize/DeserializationFilter.java @@ -0,0 +1,86 @@ +/* + * Copyright 2017 Johns Hopkins University + * + * 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 org.dataconservancy.fcrepo.jsonld.deserialize; + +import static org.dataconservancy.fcrepo.jsonld.JsonldUtil.loadContexts; + +import java.io.IOException; +import java.util.Optional; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.github.jsonldjava.core.JsonLdOptions; + +/** + * @author apb@jhu.edu + */ +public class DeserializationFilter implements Filter { + + JsonldNtriplesTranslator translator; + + private static final Logger LOG = LoggerFactory.getLogger(DeserializationFilter.class); + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + LOG.info("Initializing JSON-LD deserialiation"); + + final JsonLdOptions options = new JsonLdOptions(); + + loadContexts(options); + + translator = new JsonldNtriplesTranslator(); + translator.setOptions(options); + } + + /** + * {@inheritDoc} + */ + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, + ServletException { + + LOG.debug("Deserialization filter considering the request"); + + final String method = ((HttpServletRequest) request).getMethod(); + final String contentType = Optional.ofNullable( + request.getContentType()).orElse(Optional.ofNullable(((HttpServletRequest) request).getHeader( + "content-type")).orElse("")); + + if (("POST".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method)) && + contentType.contains("application/ld+json")) { + LOG.debug("Deserialization filter is deserializing JSON-LD"); + chain.doFilter(new DeserializationWrapper((HttpServletRequest) request, translator), response); + } else { + LOG.debug("Deserialization filter is doing nothing: " + method + ", " + contentType); + chain.doFilter(request, response); + } + } + + @Override + public void destroy() { + + } +} diff --git a/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/deserialize/DeserializationWrapper.java b/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/deserialize/DeserializationWrapper.java new file mode 100644 index 0000000..75073f9 --- /dev/null +++ b/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/deserialize/DeserializationWrapper.java @@ -0,0 +1,149 @@ +/* + * Copyright 2017 Johns Hopkins University + * + * 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 org.dataconservancy.fcrepo.jsonld.deserialize; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; + +import javax.servlet.ReadListener; +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; + +import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author apb@jhu.edu + */ +class DeserializationWrapper extends HttpServletRequestWrapper { + + private final ServletInputStream originalInputStream; + + private final JsonldNtriplesTranslator transltor; + + private static final Logger LOG = LoggerFactory.getLogger(DeserializationWrapper.class); + + /** + * @param request + */ + public DeserializationWrapper(HttpServletRequest request, JsonldNtriplesTranslator translator) { + super(request); + try { + this.originalInputStream = request.getInputStream(); + this.transltor = translator; + } catch (final IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public ServletInputStream getInputStream() { + + return new ServletInputStream() { + + final ByteArrayInputStream translatedOutputStream; + + boolean finished = false; + + { + try (InputStream in = originalInputStream) { + + final String originalBody = IOUtils.toString( + originalInputStream, UTF_8); + LOG.debug("Original content: " + originalBody); + + final String translatedBody = transltor.translate(originalBody); + LOG.debug("Translated content: " + translatedBody); + translatedOutputStream = new ByteArrayInputStream(translatedBody.getBytes(UTF_8)); + + } catch (final IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public int read() throws IOException { + final int byt = translatedOutputStream.read(); + if (byt == -1) { + finished = true; + } + return byt; + } + + @Override + public void setReadListener(ReadListener readListener) { + originalInputStream.setReadListener(readListener); + } + + @Override + public boolean isReady() { + return true; + } + + @Override + public boolean isFinished() { + return finished; + } + + @Override + public void close() throws IOException { + translatedOutputStream.close(); + } + }; + } + + @Override + public String getContentType() { + return "text/turtle"; + } + + @Override + public String getHeader(String name) { + if (name.equalsIgnoreCase("content-type")) { + return "text/turtle"; + } else { + return super.getHeader(name); + } + } + + @Override + public Enumeration getHeaders(String name) { + if (name.equalsIgnoreCase("Content-Type")) { + return Collections.enumeration(Arrays.asList("text/turtle")); + } else { + return super.getHeaders(name); + } + } + + @Override + public int getContentLength() { + return -1; + } + + @Override + public long getContentLengthLong() { + return -1; + } +} diff --git a/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/deserialize/JsonldNtriplesTranslator.java b/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/deserialize/JsonldNtriplesTranslator.java new file mode 100644 index 0000000..42c4c38 --- /dev/null +++ b/jsonld-addon-filters/src/main/java/org/dataconservancy/fcrepo/jsonld/deserialize/JsonldNtriplesTranslator.java @@ -0,0 +1,55 @@ +/* + * Copyright 2017 Johns Hopkins University + * + * 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 org.dataconservancy.fcrepo.jsonld.deserialize; + +import static com.github.jsonldjava.utils.JsonUtils.fromString; + +import java.io.IOException; +import java.net.URI; +import java.util.UUID; + +import com.github.jsonldjava.core.JsonLdError; +import com.github.jsonldjava.core.JsonLdOptions; +import com.github.jsonldjava.core.JsonLdProcessor; +import com.github.jsonldjava.core.RDFDatasetUtils; + +/** + * @author apb@jhu.edu + */ +public class JsonldNtriplesTranslator { + + static final String NULL_RELATIVE = "null::" + UUID.randomUUID() + "::"; + + private JsonLdOptions options = new JsonLdOptions(); + + public void setOptions(JsonLdOptions options) { + this.options = options; + options.format = "application/nquads"; + options.setBase(NULL_RELATIVE); + } + + public String translate(String jsonld) { + + URI.create(NULL_RELATIVE); + try { + return ((String) JsonLdProcessor.toRDF(fromString(jsonld), RDFDatasetUtils::toNQuads, options)) + .replaceAll(NULL_RELATIVE, ""); + } catch (JsonLdError | IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/ConfigUtilTest.java b/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/ConfigUtilTest.java similarity index 83% rename from jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/ConfigUtilTest.java rename to jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/ConfigUtilTest.java index 3e1c3f4..f26ae0e 100644 --- a/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/ConfigUtilTest.java +++ b/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/ConfigUtilTest.java @@ -14,14 +14,14 @@ * limitations under the License. */ -package org.dataconservancy.fcrepo.jsonld.compact; +package org.dataconservancy.fcrepo.jsonld; import static java.lang.String.join; -import static org.dataconservancy.fcrepo.jsonld.compact.ConfigUtil.extract; -import static org.dataconservancy.fcrepo.jsonld.compact.ConfigUtil.props; -import static org.dataconservancy.fcrepo.jsonld.compact.ConfigUtil.removePrefix; -import static org.dataconservancy.fcrepo.jsonld.compact.ConfigUtil.toEnvName; -import static org.dataconservancy.fcrepo.jsonld.compact.ConfigUtil.toPropName; +import static org.dataconservancy.fcrepo.jsonld.ConfigUtil.extract; +import static org.dataconservancy.fcrepo.jsonld.ConfigUtil.props; +import static org.dataconservancy.fcrepo.jsonld.ConfigUtil.removePrefix; +import static org.dataconservancy.fcrepo.jsonld.ConfigUtil.toEnvName; +import static org.dataconservancy.fcrepo.jsonld.ConfigUtil.toPropName; import static org.junit.Assert.assertEquals; import java.util.Map; diff --git a/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/CompactionFilterTest.java b/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/CompactionFilterTest.java index 24ed404..825de47 100644 --- a/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/CompactionFilterTest.java +++ b/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/CompactionFilterTest.java @@ -18,12 +18,12 @@ import static java.lang.String.join; import static java.nio.charset.StandardCharsets.UTF_8; +import static org.dataconservancy.fcrepo.jsonld.JsonldUtil.COMPACTION_PROP_PRELOAD_FILES; +import static org.dataconservancy.fcrepo.jsonld.JsonldUtil.COMPACTION_PROP_PRELOAD_URIS; import static org.dataconservancy.fcrepo.jsonld.compact.CompactionFilter.CONTEXT_COMPACTION_URI_PROP; import static org.dataconservancy.fcrepo.jsonld.compact.JsonldTestUtil.getContextFileLocation; import static org.dataconservancy.fcrepo.jsonld.compact.JsonldTestUtil.getUncompactedJsonld; import static org.dataconservancy.fcrepo.jsonld.compact.JsonldTestUtil.isCompact; -import static org.dataconservancy.fcrepo.jsonld.compact.JsonldUtil.COMPACTION_PROP_PRELOAD_FILES; -import static org.dataconservancy.fcrepo.jsonld.compact.JsonldUtil.COMPACTION_PROP_PRELOAD_URIS; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; @@ -103,6 +103,7 @@ public void close() { }; when(originalResponse.getOutputStream()).thenReturn(servletOut); + when(originalRequest.getMethod()).thenReturn("GET"); chain = new FilterChain() { diff --git a/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/CompactorTest.java b/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/CompactorTest.java index 706847d..11aaa24 100644 --- a/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/CompactorTest.java +++ b/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/CompactorTest.java @@ -16,9 +16,9 @@ package org.dataconservancy.fcrepo.jsonld.compact; +import static org.dataconservancy.fcrepo.jsonld.JsonldUtil.addStaticContext; import static org.dataconservancy.fcrepo.jsonld.compact.JsonldTestUtil.getUncompactedJsonld; import static org.dataconservancy.fcrepo.jsonld.compact.JsonldTestUtil.isCompact; -import static org.dataconservancy.fcrepo.jsonld.compact.JsonldUtil.addStaticContext; import static org.junit.Assert.assertTrue; import java.net.URL; diff --git a/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/JsonldNtriplesTranslatorTest.java b/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/JsonldNtriplesTranslatorTest.java new file mode 100644 index 0000000..dd7c6ea --- /dev/null +++ b/jsonld-addon-filters/src/test/java/org/dataconservancy/fcrepo/jsonld/compact/JsonldNtriplesTranslatorTest.java @@ -0,0 +1,106 @@ +/* + * Copyright 2017 Johns Hopkins University + * + * 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 org.dataconservancy.fcrepo.jsonld.compact; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.dataconservancy.fcrepo.jsonld.JsonldUtil.addStaticContext; +import static org.dataconservancy.fcrepo.jsonld.compact.JsonldTestUtil.getUncompactedJsonld; +import static org.junit.Assert.assertTrue; + +import java.io.StringReader; +import java.net.URL; + +import org.dataconservancy.fcrepo.jsonld.deserialize.JsonldNtriplesTranslator; + +import org.apache.commons.io.IOUtils; +import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.ModelFactory; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.github.jsonldjava.core.JsonLdOptions; + +/** + * @author apb@jhu.edu + */ +public class JsonldNtriplesTranslatorTest { + + static JsonLdOptions options; + + @BeforeClass + public static void setUp() throws Exception { + final URL CONTEXT_URL = new URL("http://example.org/farm.jsonld"); + + options = new JsonLdOptions(); + + addStaticContext(CONTEXT_URL, JsonldNtriplesTranslatorTest.class.getResourceAsStream("/context.jsonld"), + options); + } + + /* Basic json-ld with embedded context */ + @Test + public void embeddedContextTest() throws Exception { + + final JsonldNtriplesTranslator t = new JsonldNtriplesTranslator(); + t.setOptions(options); + + final String JSON = getUncompactedJsonld(); + + final Model desiredTriples = ModelFactory.createDefaultModel(); + desiredTriples.read(this.getClass().getResourceAsStream("/file.nt"), null, "N-Triples"); + + final Model actualTriples = ModelFactory.createDefaultModel(); + actualTriples.read(new StringReader(t.translate(JSON)), null, "N-Triples"); + + assertTrue(desiredTriples.isIsomorphicWith(actualTriples)); + } + + /* Verifies that the null relative URI is OK */ + @Test + public void nullRelativeIdTest() throws Exception { + + final JsonldNtriplesTranslator t = new JsonldNtriplesTranslator(); + t.setOptions(options); + + final String JSON = IOUtils.toString(this.getClass().getResourceAsStream("/null-relative.json"), UTF_8); + + final Model desiredTriples = ModelFactory.createDefaultModel(); + desiredTriples.read(this.getClass().getResourceAsStream("/null-relative.ttl"), null, "TTL"); + + final Model actualTriples = ModelFactory.createDefaultModel(); + actualTriples.read(new StringReader(t.translate(JSON)), null, "TTL"); + + assertTrue(desiredTriples.isIsomorphicWith(actualTriples)); + + } + + /* JSON-LD with URI context */ + @Test + public void linkedContextTest() throws Exception { + + final JsonldNtriplesTranslator t = new JsonldNtriplesTranslator(); + t.setOptions(options); + + final String JSON = IOUtils.toString(this.getClass().getResourceAsStream("/uri-context.json"), UTF_8); + + final Model desiredTriples = ModelFactory.createDefaultModel(); + desiredTriples.read(this.getClass().getResourceAsStream("/file.nt"), null, "N-Triples"); + + final Model actualTriples = ModelFactory.createDefaultModel(); + actualTriples.read(new StringReader(t.translate(JSON)), null, "N-Triples"); + } +} diff --git a/jsonld-addon-filters/src/test/resources/file.nt b/jsonld-addon-filters/src/test/resources/file.nt new file mode 100644 index 0000000..4a9d213 --- /dev/null +++ b/jsonld-addon-filters/src/test/resources/file.nt @@ -0,0 +1,6 @@ + "1980-12-01T00:00:00.000Z"^^ . + "false"^^ . + "3.05E1"^^ . + "yoda" . + "124"^^ . + . \ No newline at end of file diff --git a/jsonld-addon-filters/src/test/resources/null-relative.json b/jsonld-addon-filters/src/test/resources/null-relative.json new file mode 100644 index 0000000..1a4f982 --- /dev/null +++ b/jsonld-addon-filters/src/test/resources/null-relative.json @@ -0,0 +1,45 @@ +{ + "@id": "", + "@type": "ns001:Cow", + "birthDate": "1980-12-01T00:00:00.000Z", + "ns001:healthy": false, + "ns001:milkVolume": 30.5, + "name": "yoda", + "ns001:weight": 124, + "@context": { + "weight": { + "@id": "http://example.com/farm/weight", + "@type": "http://www.w3.org/2001/XMLSchema#integer" + }, + "healthy": { + "@id": "http://example.com/farm/healthy", + "@type": "http://www.w3.org/2001/XMLSchema#boolean" + }, + "milkVolume": { + "@id": "http://example.com/farm/milkVolume", + "@type": "http://www.w3.org/2001/XMLSchema#double" + }, + "name": { + "@id": "http://example.com/farm/name" + }, + "birthDate": { + "@id": "http://example.com/farm/birthDate", + "@type": "http://www.w3.org/2001/XMLSchema#dateTime" + }, + "premis": "http://www.loc.gov/premis/rdf/v1#", + "test": "info:fedora/test/", + "rdfs": "http://www.w3.org/2000/01/rdf-schema#", + "ns001": "http://example.com/farm/", + "xsi": "http://www.w3.org/2001/XMLSchema-instance", + "xmlns": "http://www.w3.org/2000/xmlns/", + "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", + "fedora": "http://fedora.info/definitions/v4/repository#", + "xml": "http://www.w3.org/XML/1998/namespace", + "ebucore": "http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#", + "ldp": "http://www.w3.org/ns/ldp#", + "xs": "http://www.w3.org/2001/XMLSchema", + "fedoraconfig": "http://fedora.info/definitions/v4/config#", + "foaf": "http://xmlns.com/foaf/0.1/", + "dc": "http://purl.org/dc/elements/1.1/" + } +} \ No newline at end of file diff --git a/jsonld-addon-filters/src/test/resources/null-relative.ttl b/jsonld-addon-filters/src/test/resources/null-relative.ttl new file mode 100644 index 0000000..f710b8f --- /dev/null +++ b/jsonld-addon-filters/src/test/resources/null-relative.ttl @@ -0,0 +1,6 @@ +<> "1980-12-01T00:00:00.000Z"^^ . +<> "false"^^ . +<> "3.05E1"^^ . +<> "yoda" . +<> "124"^^ . +<> . \ No newline at end of file diff --git a/jsonld-addon-filters/src/test/resources/uri-context.json b/jsonld-addon-filters/src/test/resources/uri-context.json new file mode 100644 index 0000000..6b717c8 --- /dev/null +++ b/jsonld-addon-filters/src/test/resources/uri-context.json @@ -0,0 +1,10 @@ +{ + "@id": "http://localhost:8080/rest/farm/kine/eb/5f/90/ff/eb5f90ff-80bb-4c3f-851b-086c4e85c0d8", + "@type": "ns001:Cow", + "birthDate": "1980-12-01T00:00:00.000Z", + "ns001:healthy": false, + "ns001:milkVolume": 30.5, + "name": "yoda", + "ns001:weight": 124, + "@context": "http://example.org/farm.jsonld" +} \ No newline at end of file diff --git a/jsonld-addon-integration/src/test/java/org/dataconservancy/fcrepo/jsonld/integration/CompactionIT.java b/jsonld-addon-integration/src/test/java/org/dataconservancy/fcrepo/jsonld/integration/CompactionIT.java index f40e9cb..06b9f72 100644 --- a/jsonld-addon-integration/src/test/java/org/dataconservancy/fcrepo/jsonld/integration/CompactionIT.java +++ b/jsonld-addon-integration/src/test/java/org/dataconservancy/fcrepo/jsonld/integration/CompactionIT.java @@ -46,7 +46,7 @@ public class CompactionIT { @Test public void CompactionTest() throws Exception { - final FcrepoClient client = new FcrepoClientBuilder().build(); + final FcrepoClient client = new FcrepoClientBuilder().throwExceptionOnFailure().build(); final URI jsonldResource = attempt(60, () -> { try (FcrepoResponse response = client diff --git a/jsonld-addon-integration/src/test/java/org/dataconservancy/fcrepo/jsonld/integration/DeserializationIT.java b/jsonld-addon-integration/src/test/java/org/dataconservancy/fcrepo/jsonld/integration/DeserializationIT.java new file mode 100644 index 0000000..65b40d3 --- /dev/null +++ b/jsonld-addon-integration/src/test/java/org/dataconservancy/fcrepo/jsonld/integration/DeserializationIT.java @@ -0,0 +1,92 @@ +/* + * Copyright 2017 Johns Hopkins University + * + * 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 org.dataconservancy.fcrepo.jsonld.integration; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Collections.emptyList; +import static org.dataconservancy.fcrepo.jsonld.compact.JsonldTestUtil.isCompact; +import static org.junit.Assert.assertTrue; + +import java.net.URI; +import java.util.Arrays; +import java.util.concurrent.Callable; + +import org.fcrepo.client.FcrepoClient; +import org.fcrepo.client.FcrepoClient.FcrepoClientBuilder; +import org.fcrepo.client.FcrepoResponse; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; + +/** + * @author apb@jhu.edu + */ +public class DeserializationIT { + + static final URI SERVER_MANAGED = URI.create("http://fedora.info/definitions/v4/repository#ServerManaged"); + + String fcrepoBaseURI = String.format("http://localhost:%s/%s/rest/", System.getProperty( + "fcrepo.dynamic.test.port", "8080"), System.getProperty("fcrepo.cxtPath", "fcrepo")); + + @Test + public void deserializationTest() throws Exception { + final FcrepoClient client = new FcrepoClientBuilder().throwExceptionOnFailure().build(); + + final URI jsonldResource = attempt(60, () -> { + + try (FcrepoResponse response = client + .post(URI.create(fcrepoBaseURI)) + .body(this.getClass().getResourceAsStream("/compact-uri.json"), "application/ld+json") + .perform()) { + return response.getLocation(); + } + }); + + try (FcrepoResponse response = client + .get(jsonldResource) + .preferRepresentation(emptyList(), Arrays.asList(SERVER_MANAGED)) + .accept("application/ld+json") + .perform()) { + + final String body = IOUtils.toString(response.getBody(), UTF_8); + + assertTrue(body, isCompact(body)); + } + + } + + static T attempt(final int times, final Callable it) { + + Throwable caught = null; + + for (int tries = 0; tries < times; tries++) { + try { + return it.call(); + } catch (final Throwable e) { + caught = e; + try { + Thread.sleep(1000); + System.out.println("."); + } catch (final InterruptedException i) { + Thread.currentThread().interrupt(); + return null; + } + } + } + throw new RuntimeException("Failed executing task", caught); + } +} diff --git a/jsonld-addon-integration/src/test/resources/compact-uri.json b/jsonld-addon-integration/src/test/resources/compact-uri.json new file mode 100644 index 0000000..2c8df84 --- /dev/null +++ b/jsonld-addon-integration/src/test/resources/compact-uri.json @@ -0,0 +1,10 @@ +{ + "@context": "http://example.org/farm", + "id": "", + "type": "Cow", + "birthDate": "1980-12-01T00:00:00.000Z", + "healthy": false, + "milkVolume": 30.5, + "name": "yoda", + "weight": 124 + } \ No newline at end of file diff --git a/jsonld-addon-integration/src/test/resources/web.xml b/jsonld-addon-integration/src/test/resources/web.xml index 6ac16ff..86304da 100644 --- a/jsonld-addon-integration/src/test/resources/web.xml +++ b/jsonld-addon-integration/src/test/resources/web.xml @@ -4702,9 +4702,21 @@ true + + jsonld-deserialization-filter + org.dataconservancy.fcrepo.jsonld.deserialize.DeserializationFilter + true + + + jsonld-compaction-filter /* + + + jsonld-deserialization-filter + /* +