Skip to content

Commit

Permalink
#330 Added ability to send SPARQL queries via POST
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesalvo committed Jan 3, 2024
1 parent 3089665 commit bf1d3b4
Show file tree
Hide file tree
Showing 17 changed files with 1,169 additions and 220 deletions.
205 changes: 205 additions & 0 deletions RDFSharp.Test/Query/Mirella/Algebra/Queries/RDFAskQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,37 @@ public void ShouldApplyAskQueryToSPARQLEndpointAndHaveTrueResult()
Assert.IsTrue(result.AskResult);
}

[TestMethod]
public void ShouldApplyAskQueryToSPARQLEndpointAndHaveTrueResultViaPost()
{
server
.Given(
Request.Create()
.WithPath("/RDFAskQueryTest/ShouldApplyAskQueryToSPARQLEndpointAndHaveTrueResultViaPost/sparql")
.UsingPost())
.RespondWith(
Response.Create()
.WithBody(
@"<?xml version=""1.0""?>
<sparql xmlns='http://www.w3.org/2005/sparql-results#'>
<head />
<boolean>true</boolean>
</sparql>", encoding: Encoding.UTF8)
.WithHeader("Content-Type", "application/sparql-results+xml")
.WithStatusCode(HttpStatusCode.OK));

RDFAskQuery query = new RDFAskQuery()
.AddPrefix(RDFNamespaceRegister.GetByPrefix(RDFVocabulary.RDF.PREFIX))
.AddPatternGroup(new RDFPatternGroup()
.AddPattern(new RDFPattern(new RDFVariable("?S"), RDFVocabulary.RDF.TYPE, RDFVocabulary.RDFS.CLASS)));
RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFAskQueryTest/ShouldApplyAskQueryToSPARQLEndpointAndHaveTrueResultViaPost/sparql"));
RDFAskQueryResult result = query.ApplyToSPARQLEndpoint(endpoint, new RDFSPARQLEndpointQueryOptions() {
QueryMethod = RDFQueryEnums.RDFSPARQLEndpointQueryMethods.Post });

Assert.IsNotNull(result);
Assert.IsTrue(result.AskResult);
}

[TestMethod]
public void ShouldApplyAskQueryToSPARQLEndpointAndHaveFalseResult()
{
Expand Down Expand Up @@ -308,6 +339,37 @@ public void ShouldApplyAskQueryToSPARQLEndpointAndHaveFalseResult()
Assert.IsFalse(result.AskResult);
}

[TestMethod]
public void ShouldApplyAskQueryToSPARQLEndpointAndHaveFalseResultViaPost()
{
server
.Given(
Request.Create()
.WithPath("/RDFAskQueryTest/ShouldApplyAskQueryToSPARQLEndpointAndHaveFalseResultViaPost/sparql")
.UsingPost())
.RespondWith(
Response.Create()
.WithBody(
@"<?xml version=""1.0""?>
<sparql xmlns='http://www.w3.org/2005/sparql-results#'>
<head />
<boolean>false</boolean>
</sparql>", encoding: Encoding.UTF8)
.WithHeader("Content-Type", "application/sparql-results+xml")
.WithStatusCode(HttpStatusCode.OK));

RDFAskQuery query = new RDFAskQuery()
.AddPrefix(RDFNamespaceRegister.GetByPrefix(RDFVocabulary.RDF.PREFIX))
.AddPatternGroup(new RDFPatternGroup()
.AddPattern(new RDFPattern(new RDFVariable("?S"), RDFVocabulary.RDF.TYPE, RDFVocabulary.RDFS.CLASS)));
RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFAskQueryTest/ShouldApplyAskQueryToSPARQLEndpointAndHaveFalseResultViaPost/sparql"));
RDFAskQueryResult result = query.ApplyToSPARQLEndpoint(endpoint, new RDFSPARQLEndpointQueryOptions() {
QueryMethod = RDFQueryEnums.RDFSPARQLEndpointQueryMethods.Post });

Assert.IsNotNull(result);
Assert.IsFalse(result.AskResult);
}

[TestMethod]
public void ShouldApplyRawAskQueryToSPARQLEndpoint()
{
Expand Down Expand Up @@ -339,6 +401,37 @@ public void ShouldApplyRawAskQueryToSPARQLEndpoint()
Assert.IsTrue(result.AskResult);
}

[TestMethod]
public void ShouldApplyRawAskQueryToSPARQLEndpointViaPost()
{
server
.Given(
Request.Create()
.WithPath("/RDFAskQueryTest/ShouldApplyRawAskQueryToSPARQLEndpointViaPost/sparql")
.UsingPost())
.RespondWith(
Response.Create()
.WithBody(
@"<?xml version=""1.0""?>
<sparql xmlns='http://www.w3.org/2005/sparql-results#'>
<head />
<boolean>true</boolean>
</sparql>", encoding: Encoding.UTF8)
.WithHeader("Content-Type", "application/sparql-results+xml")
.WithStatusCode(HttpStatusCode.OK));

RDFAskQuery query = new RDFAskQuery()
.AddPrefix(RDFNamespaceRegister.GetByPrefix(RDFVocabulary.RDF.PREFIX))
.AddPatternGroup(new RDFPatternGroup()
.AddPattern(new RDFPattern(new RDFVariable("?S"), RDFVocabulary.RDF.TYPE, RDFVocabulary.RDFS.CLASS)));
RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFAskQueryTest/ShouldApplyRawAskQueryToSPARQLEndpointViaPost/sparql"));
RDFAskQueryResult result = RDFAskQuery.ApplyRawToSPARQLEndpoint(query.ToString(), endpoint, new RDFSPARQLEndpointQueryOptions() {
QueryMethod = RDFQueryEnums.RDFSPARQLEndpointQueryMethods.Post });

Assert.IsNotNull(result);
Assert.IsTrue(result.AskResult);
}

[TestMethod]
public void ShouldApplyAskQueryToNullSPARQLEndpointAndHaveFalseResult()
{
Expand Down Expand Up @@ -381,6 +474,35 @@ public void ShouldThrowExceptionWhenApplyingAskQueryToSPARQLEndpointAccordingToT
Assert.ThrowsException<RDFQueryException>(() => query.ApplyToSPARQLEndpoint(endpoint, new RDFSPARQLEndpointQueryOptions(250, RDFQueryEnums.RDFSPARQLEndpointQueryErrorBehaviors.ThrowException)));
}

[TestMethod]
public void ShouldThrowExceptionWhenApplyingAskQueryToSPARQLEndpointAccordingToTimingAndBehaviorViaPost()
{
server
.Given(
Request.Create()
.WithPath("/RDFAskQueryTest/ShouldThrowExceptionWhenApplyingAskQueryToSPARQLEndpointAccordingToTimingAndBehaviorViaPost/sparql")
.UsingPost())
.RespondWith(
Response.Create()
.WithBody(
@"<?xml version=""1.0""?>
<sparql xmlns='http://www.w3.org/2005/sparql-results#'>
<head />
<boolean>false</boolean>
</sparql>", encoding: Encoding.UTF8)
.WithStatusCode(HttpStatusCode.OK)
.WithDelay(750));

RDFAskQuery query = new RDFAskQuery()
.AddPrefix(RDFNamespaceRegister.GetByPrefix(RDFVocabulary.RDF.PREFIX))
.AddPatternGroup(new RDFPatternGroup()
.AddPattern(new RDFPattern(new RDFVariable("?S"), RDFVocabulary.RDF.TYPE, RDFVocabulary.RDFS.CLASS)));
RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFAskQueryTest/ShouldThrowExceptionWhenApplyingAskQueryToSPARQLEndpointAccordingToTimingAndBehaviorViaPost/sparql"));

Assert.ThrowsException<RDFQueryException>(() => query.ApplyToSPARQLEndpoint(endpoint, new RDFSPARQLEndpointQueryOptions(250,
RDFQueryEnums.RDFSPARQLEndpointQueryErrorBehaviors.ThrowException, RDFQueryEnums.RDFSPARQLEndpointQueryMethods.Post)));
}

[TestMethod]
public void ShouldGiveEmptyResultWhenApplyingAskQueryToSPARQLEndpointAccordingToTimingAndBehavior()
{
Expand Down Expand Up @@ -413,6 +535,38 @@ public void ShouldGiveEmptyResultWhenApplyingAskQueryToSPARQLEndpointAccordingTo
Assert.IsFalse(result.AskResult);
}

[TestMethod]
public void ShouldGiveEmptyResultWhenApplyingAskQueryToSPARQLEndpointAccordingToTimingAndBehaviorViaPost()
{
server
.Given(
Request.Create()
.WithPath("/RDFAskQueryTest/ShouldGiveEmptyResultWhenApplyingAskQueryToSPARQLEndpointAccordingToTimingAndBehaviorViaPost/sparql")
.UsingPost())
.RespondWith(
Response.Create()
.WithBody(
@"<?xml version=""1.0""?>
<sparql xmlns='http://www.w3.org/2005/sparql-results#'>
<head />
<boolean>false</boolean>
</sparql>", encoding: Encoding.UTF8)
.WithStatusCode(HttpStatusCode.OK)
.WithDelay(750));

RDFAskQuery query = new RDFAskQuery()
.AddPrefix(RDFNamespaceRegister.GetByPrefix(RDFVocabulary.RDF.PREFIX))
.AddPatternGroup(new RDFPatternGroup()
.AddPattern(new RDFPattern(new RDFVariable("?S"), RDFVocabulary.RDF.TYPE, RDFVocabulary.RDFS.CLASS)));
RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFAskQueryTest/ShouldGiveEmptyResultWhenApplyingAskQueryToSPARQLEndpointAccordingToTimingAndBehaviorViaPost/sparql"));

RDFAskQueryResult result = query.ApplyToSPARQLEndpoint(endpoint, new RDFSPARQLEndpointQueryOptions(250,
RDFQueryEnums.RDFSPARQLEndpointQueryErrorBehaviors.GiveEmptyResult, RDFQueryEnums.RDFSPARQLEndpointQueryMethods.Post));

Assert.IsNotNull(result);
Assert.IsFalse(result.AskResult);
}

[TestMethod]
public void ShouldThrowExceptionWhenApplyingAskQueryToSPARQLEndpointAccordingToBehavior()
{
Expand All @@ -437,6 +591,30 @@ public void ShouldThrowExceptionWhenApplyingAskQueryToSPARQLEndpointAccordingToB
Assert.ThrowsException<RDFQueryException>(() => query.ApplyToSPARQLEndpoint(endpoint, new RDFSPARQLEndpointQueryOptions(750, RDFQueryEnums.RDFSPARQLEndpointQueryErrorBehaviors.ThrowException)));
}

[TestMethod]
public void ShouldThrowExceptionWhenApplyingAskQueryToSPARQLEndpointAccordingToBehaviorViaPost()
{
server
.Given(
Request.Create()
.WithPath("/RDFAskQueryTest/ShouldThrowExceptionWhenApplyingAskQueryToSPARQLEndpointAccordingToBehaviorViaPost/sparql")
.UsingPost())
.RespondWith(
Response.Create()
.WithBody("Internal Server Error")
.WithStatusCode(HttpStatusCode.InternalServerError)
.WithFault(FaultType.NONE));

RDFAskQuery query = new RDFAskQuery()
.AddPrefix(RDFNamespaceRegister.GetByPrefix(RDFVocabulary.RDF.PREFIX))
.AddPatternGroup(new RDFPatternGroup()
.AddPattern(new RDFPattern(new RDFVariable("?S"), RDFVocabulary.RDF.TYPE, RDFVocabulary.RDFS.CLASS)));
RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFAskQueryTest/ShouldThrowExceptionWhenApplyingAskQueryToSPARQLEndpointAccordingToBehaviorViaPost/sparql"));

Assert.ThrowsException<RDFQueryException>(() => query.ApplyToSPARQLEndpoint(endpoint, new RDFSPARQLEndpointQueryOptions(750,
RDFQueryEnums.RDFSPARQLEndpointQueryErrorBehaviors.ThrowException, RDFQueryEnums.RDFSPARQLEndpointQueryMethods.Post)));
}

[TestMethod]
public void ShouldGiveEmptyResultWhenApplyingAskQueryToSPARQLEndpointAccordingToBehavior()
{
Expand Down Expand Up @@ -464,6 +642,33 @@ public void ShouldGiveEmptyResultWhenApplyingAskQueryToSPARQLEndpointAccordingTo
Assert.IsFalse(result.AskResult);
}

[TestMethod]
public void ShouldGiveEmptyResultWhenApplyingAskQueryToSPARQLEndpointAccordingToBehaviorViaPost()
{
server
.Given(
Request.Create()
.WithPath("/RDFAskQueryTest/ShouldGiveEmptyResultWhenApplyingAskQueryToSPARQLEndpointAccordingToBehaviorViaPost/sparql")
.UsingPost())
.RespondWith(
Response.Create()
.WithBody("Internal Server Error")
.WithStatusCode(HttpStatusCode.InternalServerError)
.WithFault(FaultType.NONE));

RDFAskQuery query = new RDFAskQuery()
.AddPrefix(RDFNamespaceRegister.GetByPrefix(RDFVocabulary.RDF.PREFIX))
.AddPatternGroup(new RDFPatternGroup()
.AddPattern(new RDFPattern(new RDFVariable("?S"), RDFVocabulary.RDF.TYPE, RDFVocabulary.RDFS.CLASS)));
RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFAskQueryTest/ShouldGiveEmptyResultWhenApplyingAskQueryToSPARQLEndpointAccordingToBehaviorViaPost/sparql"));

RDFAskQueryResult result = query.ApplyToSPARQLEndpoint(endpoint, new RDFSPARQLEndpointQueryOptions(1000,
RDFQueryEnums.RDFSPARQLEndpointQueryErrorBehaviors.GiveEmptyResult, RDFQueryEnums.RDFSPARQLEndpointQueryMethods.Post));

Assert.IsNotNull(result);
Assert.IsFalse(result.AskResult);
}

//ASYNC

[TestMethod]
Expand Down
Loading

0 comments on commit bf1d3b4

Please sign in to comment.