You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by ja...@apache.org on 2014/02/28 18:51:28 UTC
[2/3] git commit: Extended LDP WebService tests - started with some
RDF/Sesame Matchers for unit-test
Extended LDP WebService tests
- started with some RDF/Sesame Matchers for unit-test
Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/77eae226
Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/77eae226
Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/77eae226
Branch: refs/heads/ldp
Commit: 77eae2266c17430715b69e6d16546b256db7ac0f
Parents: 2295d3c
Author: Jakob Frank <ja...@apache.org>
Authored: Fri Feb 28 14:17:11 2014 +0100
Committer: Jakob Frank <ja...@apache.org>
Committed: Fri Feb 28 14:17:11 2014 +0100
----------------------------------------------------------------------
.../ldp/webservices/LdpWebServiceTest.java | 44 +++++--
.../ldp/webservices/util/BaseRdfMatcher.java | 123 +++++++++++++++++++
.../webservices/util/HasStatementMatcher.java | 67 ++++++++++
.../ldp/webservices/util/HeaderMatchers.java | 86 +++++++++++++
.../ldp/webservices/util/SparqlAskMatcher.java | 53 ++++++++
.../util/SparqlGraphQueryMatcher.java | 74 +++++++++++
.../ldp/webservices/util/SparqlMatcher.java | 39 ++++++
.../util/SparqlTupleQueryMatcher.java | 63 ++++++++++
8 files changed, 542 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/marmotta/blob/77eae226/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java
index 5ba9a25..86937bf 100644
--- a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java
+++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java
@@ -19,11 +19,18 @@ package org.apache.marmotta.platform.ldp.webservices;
import com.jayway.restassured.RestAssured;
import org.apache.commons.io.IOUtils;
+import org.apache.marmotta.commons.vocabulary.LDP;
import org.apache.marmotta.platform.core.exception.io.MarmottaImportException;
import org.apache.marmotta.platform.core.test.base.JettyMarmotta;
+import org.apache.marmotta.platform.ldp.webservices.util.HasStatementMatcher;
+import org.apache.marmotta.platform.ldp.webservices.util.HeaderMatchers;
+import org.hamcrest.CoreMatchers;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
+import org.openrdf.model.impl.URIImpl;
+import org.openrdf.model.vocabulary.DCTERMS;
+import org.openrdf.model.vocabulary.RDF;
import org.openrdf.rio.RDFFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -32,6 +39,8 @@ import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URISyntaxException;
+import static org.apache.marmotta.platform.ldp.webservices.util.HasStatementMatcher.hasStatement;
+
/**
* Testing LDP web services
*
@@ -76,6 +85,7 @@ public class LdpWebServiceTest {
// The container
final String container = UriBuilder.fromPath(LdpWebService.PATH).path(testBase).path(containerName).build().toString();
final String newResource = UriBuilder.fromUri(container).path(resourceName).build().toString();
+ final String mimeType = RDFFormat.TURTLE.getDefaultMIMEType();
RestAssured.expect().statusCode(404).get(container);
@@ -84,7 +94,7 @@ public class LdpWebServiceTest {
.given()
.header("Slug", resourceName)
.body(testResourceTTL.getBytes())
- .contentType(RDFFormat.TURTLE.getDefaultMIMEType())
+ .contentType(mimeType)
.expect()
.statusCode(201)
.header("Location", baseUrl + newResource)
@@ -94,31 +104,51 @@ public class LdpWebServiceTest {
log.info("200 - container");
RestAssured
.given()
- .header("Accept", RDFFormat.TURTLE.getDefaultMIMEType())
+ .header("Accept", mimeType)
.expect()
.statusCode(200)
- .contentType(RDFFormat.TURTLE.getDefaultMIMEType())
- .get(container);
+ .header("Link", CoreMatchers.anyOf( //TODO: RestAssured only checks the FIST header...
+ HeaderMatchers.isLink("http://wiki.apache.org/marmotta/LDPImplementationReport", "describedby"),
+ HeaderMatchers.isLink(LDP.BasicContainer.stringValue(), "type"))
+ )
+ .header("ETag", HeaderMatchers.hasEntityTag(true)) // FIXME: be more specific here
+ .contentType(mimeType)
+ .body(
+ hasStatement(mimeType, baseUrl + container, new URIImpl(baseUrl + container), DCTERMS.MODIFIED, null),
+ hasStatement(mimeType, baseUrl + container, new URIImpl(baseUrl + container), RDF.TYPE, LDP.BasicContainer)
+ )
+ .get(container);
// also the new resource exists
RestAssured
.given()
- .header("Accept", RDFFormat.TURTLE.getDefaultMIMEType())
+ .header("Accept", mimeType)
.expect()
.statusCode(200)
- .contentType(RDFFormat.TURTLE.getDefaultMIMEType())
+ .header("Link", CoreMatchers.anyOf( //TODO: RestAssured only checks the FIST header...
+ HeaderMatchers.isLink("http://wiki.apache.org/marmotta/LDPImplementationReport", "describedby"),
+ HeaderMatchers.isLink(LDP.Resource.stringValue(), "type"))
+ )
+ .header("ETag", HeaderMatchers.hasEntityTag(true)) // FIXME: be more specific here
+ .contentType(mimeType)
+ .body(
+ hasStatement(mimeType, baseUrl + newResource, new URIImpl(baseUrl + newResource), DCTERMS.MODIFIED, null),
+ hasStatement(mimeType, baseUrl + newResource, new URIImpl(baseUrl + newResource), RDF.TYPE, LDP.Resource)
+ )
.get(newResource);
// delete
RestAssured
.expect()
.statusCode(204)
+ .header("Link", HeaderMatchers.isLink("http://wiki.apache.org/marmotta/LDPImplementationReport", "describedby"))
+ .header("ETag", HeaderMatchers.headerNotPresent())
.delete(newResource);
// now the new resource does not exist.
RestAssured
.given()
- .header("Accept", RDFFormat.TURTLE.getDefaultMIMEType())
+ .header("Accept", mimeType)
.expect()
.statusCode(404)
.get(newResource);
http://git-wip-us.apache.org/repos/asf/marmotta/blob/77eae226/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/BaseRdfMatcher.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/BaseRdfMatcher.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/BaseRdfMatcher.java
new file mode 100644
index 0000000..0b55d2d
--- /dev/null
+++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/BaseRdfMatcher.java
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.marmotta.platform.ldp.webservices.util;
+
+import com.google.common.base.Preconditions;
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeMatcher;
+import org.openrdf.repository.Repository;
+import org.openrdf.repository.RepositoryConnection;
+import org.openrdf.repository.RepositoryException;
+import org.openrdf.repository.sail.SailRepository;
+import org.openrdf.rio.RDFFormat;
+import org.openrdf.rio.RDFParseException;
+import org.openrdf.rio.Rio;
+import org.openrdf.sail.memory.MemoryStore;
+
+import java.io.IOException;
+import java.io.StringReader;
+
+/**
+ * Base class for RDF Matchers to be used in Unit-Tests.
+ */
+public abstract class BaseRdfMatcher extends TypeSafeMatcher<String> {
+
+ protected final RDFFormat format;
+ protected final String baseUri;
+
+ protected BaseRdfMatcher(String baseUri, RDFFormat format) {
+ this.baseUri = baseUri;
+ this.format = format;
+ }
+
+ protected BaseRdfMatcher(String mimeType, String baseUri) {
+ this(baseUri, Rio.getParserFormatForMIMEType(mimeType));
+ }
+
+ /**
+ * Create an initialized Repository from the provided String data representation using the default RDFFormat
+ *
+ * @param data the rdf data
+ * @param baseUri the baseUri
+ * @return an initialized Repository containing the data
+ */
+ protected Repository createRepository(String data, String baseUri) throws RepositoryException, RDFParseException, IOException {
+ return createRepository(data, baseUri, format);
+ }
+
+ /**
+ * Create an initialized Repository from the provided String data representation using the default RDFFormat
+ *
+ * @param data the rdf data
+ * @param baseUri the baseUri
+ * @param format the RDFFormat
+ * @return an initialized Repository containing the data
+ */
+ protected Repository createRepository(String data, String baseUri, RDFFormat format) throws RepositoryException, RDFParseException, IOException {
+ Preconditions.checkArgument(format != null, "RDFFormat must not be null");
+
+ final Repository repository = new SailRepository(new MemoryStore());
+ repository.initialize();
+
+ final RepositoryConnection con = repository.getConnection();
+ try (final StringReader r = new StringReader(data)) {
+ con.begin();
+ con.add(r, baseUri, format);
+ con.commit();
+ } catch (final Throwable t) {
+ con.rollback();
+ throw t;
+ } finally {
+ con.close();
+ }
+
+ return repository;
+ }
+
+ @Override
+ public final boolean matchesSafely(String item) {
+ boolean isMatching = false;
+ try {
+ final Repository rep = createRepository(item, baseUri);
+ final RepositoryConnection con = rep.getConnection();
+ try {
+ con.begin();
+ isMatching = matches(con);
+ con.commit();
+ } catch (final Throwable t) {
+ con.rollback();
+ throw t;
+ } finally {
+ con.close();
+ rep.shutDown();
+ }
+ } catch (Throwable t) {
+ isMatching = false;
+ }
+
+ return isMatching;
+ }
+
+ protected abstract boolean matches(RepositoryConnection con) throws Exception;
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("a RDF ").appendText(format.getName()).appendText(" String");
+ }
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/77eae226/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/HasStatementMatcher.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/HasStatementMatcher.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/HasStatementMatcher.java
new file mode 100644
index 0000000..1cbcb1f
--- /dev/null
+++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/HasStatementMatcher.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.marmotta.platform.ldp.webservices.util;
+
+import org.hamcrest.Description;
+import org.openrdf.model.Resource;
+import org.openrdf.model.URI;
+import org.openrdf.model.Value;
+import org.openrdf.repository.RepositoryConnection;
+import org.openrdf.repository.RepositoryException;
+
+/**
+ * Has Statement Matcher
+ */
+public class HasStatementMatcher extends BaseRdfMatcher {
+
+ private final Resource subject;
+ private final URI predicate;
+ private final Value object;
+ private final Resource[] contexts;
+
+ protected HasStatementMatcher(String mimeType, String baseUri, Resource subject, URI predicate, Value object, Resource... contexts) {
+ super(mimeType, baseUri);
+ this.subject = subject;
+ this.predicate = predicate;
+ this.object = object;
+ this.contexts = contexts;
+ }
+
+ @Override
+ protected boolean matches(RepositoryConnection con) throws RepositoryException {
+ return con.hasStatement(subject, predicate, object, true, contexts);
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ super.describeTo(description);
+ description.appendText(" containing a Statement(")
+ .appendValue(subject).appendText(" ")
+ .appendValue(predicate).appendText(" ")
+ .appendValue(object).appendText(")");
+ }
+
+
+ public static HasStatementMatcher hasStatement(String mimeType, Resource subject, URI predicate, Value object) {
+ return hasStatement(mimeType, "", subject, predicate, object);
+ }
+
+ public static HasStatementMatcher hasStatement(String mimeType, String baseUri, Resource subject, URI predicate, Value object) {
+ return new HasStatementMatcher(mimeType, baseUri, subject, predicate, object);
+ }
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/77eae226/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/HeaderMatchers.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/HeaderMatchers.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/HeaderMatchers.java
new file mode 100644
index 0000000..e578d1b
--- /dev/null
+++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/HeaderMatchers.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.marmotta.platform.ldp.webservices.util;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.marmotta.platform.ldp.util.EntityTagUtils;
+import org.hamcrest.CoreMatchers;
+import org.hamcrest.CustomTypeSafeMatcher;
+import org.hamcrest.Matcher;
+import org.jboss.resteasy.plugins.delegates.EntityTagDelegate;
+import org.jboss.resteasy.plugins.delegates.LinkDelegate;
+
+import javax.ws.rs.core.EntityTag;
+import javax.ws.rs.core.Link;
+
+/**
+ * Matcher collection to work with HttpHeaders.
+ */
+public class HeaderMatchers {
+
+ public static Matcher<String> headerPresent() {
+ return new CustomTypeSafeMatcher<String>("not to be empty") {
+ @Override
+ protected boolean matchesSafely(String item) {
+ return StringUtils.isNotBlank(item);
+ }
+ };
+ }
+
+ public static Matcher<String> headerNotPresent() {
+ return new CustomTypeSafeMatcher<String>("to be emtpy") {
+ @Override
+ protected boolean matchesSafely(String item) {
+ return StringUtils.isBlank(item);
+ }
+ };
+ }
+
+ public static Matcher<String> isLink(String uri, String rel) {
+ final Link expected = Link.fromUri(uri).rel(rel).build();
+ return new CustomTypeSafeMatcher<String>(String.format("a Link-Header to <%s> with rel='%s'", uri, rel)) {
+ @Override
+ protected boolean matchesSafely(String item) {
+ return expected.equals(new LinkDelegate().fromString(item));
+ }
+ };
+ }
+
+ public static Matcher<String> hasEntityTag(final boolean weakTag) {
+ return new CustomTypeSafeMatcher<String>(String.format("a %s EntityTag", weakTag?"weak":"strong")) {
+ @Override
+ protected boolean matchesSafely(String item) {
+ return (new EntityTagDelegate().fromString(item).isWeak() == weakTag);
+ }
+ };
+ }
+
+ public static Matcher<String> hasEntityTag(String value, boolean weakTag) {
+ final EntityTag expected = new EntityTag(value, weakTag);
+ return new CustomTypeSafeMatcher<String>(String.format("an EntityTag %s", expected)) {
+ @Override
+ protected boolean matchesSafely(String item) {
+ return EntityTagUtils.equals(expected, new EntityTagDelegate().fromString(item));
+ }
+ };
+ }
+
+ public static Matcher<String> hasEntityTag(String value) {
+ return hasEntityTag(value, false);
+ }
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/77eae226/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlAskMatcher.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlAskMatcher.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlAskMatcher.java
new file mode 100644
index 0000000..a8f1559
--- /dev/null
+++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlAskMatcher.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.marmotta.platform.ldp.webservices.util;
+
+import org.openrdf.query.BooleanQuery;
+import org.openrdf.query.MalformedQueryException;
+import org.openrdf.query.QueryLanguage;
+import org.openrdf.repository.RepositoryConnection;
+
+/**
+ * Sparql ASK-Query Matcher
+ */
+public class SparqlAskMatcher extends SparqlMatcher {
+ protected SparqlAskMatcher(String baseUri, String mimeType, String query) {
+ super(baseUri, mimeType, query);
+ }
+
+ @Override
+ protected boolean matches(RepositoryConnection con) throws Exception {
+ try {
+ final BooleanQuery booleanQuery = con.prepareBooleanQuery(QueryLanguage.SPARQL, query, baseUri);
+
+ return booleanQuery.evaluate();
+ } catch (MalformedQueryException e) {
+ throw new IllegalArgumentException("Invalid SPARQL Query: " + query, e);
+ }
+ }
+
+ public static SparqlAskMatcher sparqlAsk(String mimeType, String baseUri, String askQuery) {
+ return new SparqlAskMatcher(baseUri, mimeType, askQuery);
+ }
+
+ public static SparqlAskMatcher sparqlAsk(String mimeType, String askQuery) {
+ return sparqlAsk(mimeType, "", askQuery);
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/77eae226/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlGraphQueryMatcher.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlGraphQueryMatcher.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlGraphQueryMatcher.java
new file mode 100644
index 0000000..dd29bbe
--- /dev/null
+++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlGraphQueryMatcher.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.marmotta.platform.ldp.webservices.util;
+
+import org.openrdf.query.GraphQuery;
+import org.openrdf.query.GraphQueryResult;
+import org.openrdf.query.MalformedQueryException;
+import org.openrdf.query.QueryLanguage;
+import org.openrdf.repository.RepositoryConnection;
+import org.openrdf.rio.RDFWriter;
+import org.openrdf.rio.Rio;
+
+import java.io.StringWriter;
+import java.util.Map;
+
+/**
+ *
+ */
+public class SparqlGraphQueryMatcher extends SparqlMatcher {
+ private final BaseRdfMatcher matcher;
+
+ protected SparqlGraphQueryMatcher(String baseUri, String mimeType, String query, BaseRdfMatcher matcher) {
+ super(baseUri, mimeType, query);
+ this.matcher = matcher;
+ }
+
+ @Override
+ protected boolean matches(RepositoryConnection con) throws Exception {
+ try {
+ final GraphQuery graphQuery = con.prepareGraphQuery(QueryLanguage.SPARQL, query, baseUri);
+
+ final GraphQueryResult graph = graphQuery.evaluate();
+ // FIXME: this is not very efficient!
+ try {
+ final StringWriter stringWriter = new StringWriter();
+ final RDFWriter writer = Rio.createWriter(matcher.format, stringWriter);
+
+ writer.startRDF();
+ for (Map.Entry<String, String> namespace : graph.getNamespaces().entrySet()) {
+ writer.handleNamespace(namespace.getKey(), namespace.getValue());
+ }
+ while (graph.hasNext()) {
+ writer.handleStatement(graph.next());
+ }
+ writer.endRDF();
+
+ return matcher.matches(stringWriter.toString());
+ } finally {
+ graph.close();
+ }
+ } catch (MalformedQueryException e) {
+ throw new IllegalArgumentException("Invalid SPARQL Query: " + query, e);
+ }
+ }
+
+ public static SparqlGraphQueryMatcher sparqlGraphQuery(String mimeType, String baseUri, String query, BaseRdfMatcher matcher) {
+ return new SparqlGraphQueryMatcher(baseUri, mimeType, query, matcher);
+ }
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/77eae226/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlMatcher.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlMatcher.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlMatcher.java
new file mode 100644
index 0000000..7bc46e3
--- /dev/null
+++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlMatcher.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.marmotta.platform.ldp.webservices.util;
+
+import org.openrdf.repository.Repository;
+import org.openrdf.repository.RepositoryConnection;
+import org.openrdf.repository.RepositoryException;
+import org.openrdf.rio.RDFParseException;
+
+import java.io.IOException;
+
+/**
+ * SPARQL Matcher
+ */
+public abstract class SparqlMatcher extends BaseRdfMatcher {
+
+ protected final String query;
+
+ protected SparqlMatcher(String baseUri, String mimeType, String query) {
+ super(baseUri, mimeType);
+ this.query = query;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/77eae226/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlTupleQueryMatcher.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlTupleQueryMatcher.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlTupleQueryMatcher.java
new file mode 100644
index 0000000..3eb9f77
--- /dev/null
+++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/util/SparqlTupleQueryMatcher.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.marmotta.platform.ldp.webservices.util;
+
+import org.hamcrest.CoreMatchers;
+import org.hamcrest.Matcher;
+import org.openrdf.query.*;
+import org.openrdf.repository.RepositoryConnection;
+import org.openrdf.repository.RepositoryException;
+
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Match against Sparql Tuple Query.
+ */
+public class SparqlTupleQueryMatcher extends SparqlMatcher {
+ private final Matcher<Collection<BindingSet>> matcher;
+
+ protected SparqlTupleQueryMatcher(String baseUri, String mimeType, String query, Matcher<Collection<BindingSet>> matcher) {
+ super(baseUri, mimeType, query);
+ this.matcher = matcher;
+ }
+
+ @Override
+ protected boolean matches(RepositoryConnection con) throws RepositoryException, QueryEvaluationException {
+ try {
+ final TupleQuery tupleQuery;
+ tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, query, baseUri);
+ final TupleQueryResult result = tupleQuery.evaluate();
+ final List<BindingSet> bindingSets = QueryResults.asList(result);
+
+ return matcher.matches(bindingSets);
+ } catch (MalformedQueryException e) {
+ throw new IllegalArgumentException("Invalid SPARQL Query: " + query, e);
+ }
+ }
+
+ public static SparqlTupleQueryMatcher sparqlQuery(String mime, String baseUri, String query, Matcher<Collection<BindingSet>> matcher) {
+ return new SparqlTupleQueryMatcher(mime, baseUri, query, matcher);
+ }
+
+ @SafeVarargs
+ public static SparqlTupleQueryMatcher sparqlQuery(String mime, String baseUri, String query, Matcher<Collection<BindingSet>>... matchers) {
+ return new SparqlTupleQueryMatcher(mime, baseUri, query, CoreMatchers.allOf(matchers));
+ }
+
+}