Skip to content

Commit

Permalink
Added a couple of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesalvo committed Jan 3, 2024
1 parent bf1d3b4 commit 2e92619
Showing 1 changed file with 168 additions and 0 deletions.
168 changes: 168 additions & 0 deletions RDFSharp.Test/Query/Mirella/Algebra/Queries/RDFSelectQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,69 @@ public void ShouldApplySelectQueryOnGraphWithServicePatternGroupAndThrowExceptio
}
}

[TestMethod]
public void ShouldApplySelectQueryOnGraphWithServicePatternGroupAndThrowExceptionAccordingToTimingAndBehaviorViaPost()
{
string receivedQuery = null;
string mockedResponseXml =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<sparql xmlns=""http://www.w3.org/2005/sparql-results#"">
<head>
<variable name=""?S"" />
</head>
<results>
<result>
<binding name=""?S"">
<uri>ex:flower</uri>
</binding>
</result>
</results>
</sparql>";
server
.Given(
Request.Create()
.WithPath("/RDFSelectQueryTest/ShouldApplySelectQueryOnGraphWithServicePatternGroupAndThrowExceptionAccordingToTimingAndBehaviorViaPost/sparql")
.UsingPost())
.RespondWith(
Response.Create()
.WithHeader("Content-Type", "application/sparql-results+xml")
.WithCallback(req =>
{
receivedQuery = req.RawQuery;
return new WireMock.ResponseMessage()
{
BodyData = new BodyData()
{
BodyAsString = mockedResponseXml,
Encoding = Encoding.UTF8,
DetectedBodyType = BodyType.String
}
};
})
.WithStatusCode(HttpStatusCode.OK)
.WithDelay(750));


RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFSelectQueryTest/ShouldApplySelectQueryOnGraphWithServicePatternGroupAndThrowExceptionAccordingToTimingAndBehaviorViaPost/sparql"));
RDFSelectQuery query = new RDFSelectQuery()
.AddPatternGroup(new RDFPatternGroup()
.AddBind(new RDFBind(new RDFConstantExpression(new RDFResource("ex:topolino")), new RDFVariable("?X")))
.UnionWithNext())
.AddPatternGroup(new RDFPatternGroup()
.AddPattern(new RDFPattern(new RDFVariable("?Y"), new RDFResource("ex:dogOf"), new RDFVariable("?X")))
.AsService(endpoint, new RDFSPARQLEndpointQueryOptions(250,
RDFQueryEnums.RDFSPARQLEndpointQueryErrorBehaviors.ThrowException, RDFQueryEnums.RDFSPARQLEndpointQueryMethods.Post)));

try
{
RDFSelectQueryResult result = query.ApplyToGraph(new RDFGraph());
}
catch (RDFQueryException qex)
{
Assert.IsTrue(string.Equals(qex.Message, "SELECT query on SPARQL endpoint failed because: The operation has timed out."));
}
}

[TestMethod]
public void ShouldApplySelectQueryOnGraphWithServicePatternGroupAndGiveEmptyResultAccordingToTimingAndBehavior()
{
Expand Down Expand Up @@ -610,6 +673,68 @@ public void ShouldApplySelectQueryOnGraphWithServicePatternGroupAndGiveEmptyResu
Assert.IsTrue(result.SelectResults.Rows[0]["?X"].Equals("ex:topolino"));
}

[TestMethod]
public void ShouldApplySelectQueryOnGraphWithServicePatternGroupAndGiveEmptyResultAccordingToTimingAndBehaviorViaPost()
{
string receivedQuery = null;
string mockedResponseXml =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<sparql xmlns=""http://www.w3.org/2005/sparql-results#"">
<head>
<variable name=""?S"" />
</head>
<results>
<result>
<binding name=""?S"">
<uri>ex:flower</uri>
</binding>
</result>
</results>
</sparql>";
server
.Given(
Request.Create()
.WithPath("/RDFSelectQueryTest/ShouldApplySelectQueryOnGraphWithServicePatternGroupAndGiveEmptyResultAccordingToTimingAndBehaviorViaPost/sparql")
.UsingPost())
.RespondWith(
Response.Create()
.WithHeader("Content-Type", "application/sparql-results+xml")
.WithCallback(req =>
{
receivedQuery = req.RawQuery;
return new WireMock.ResponseMessage()
{
BodyData = new BodyData()
{
BodyAsString = mockedResponseXml,
Encoding = Encoding.UTF8,
DetectedBodyType = BodyType.String
}
};
})
.WithStatusCode(HttpStatusCode.OK)
.WithDelay(750));


RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFSelectQueryTest/ShouldApplySelectQueryOnGraphWithServicePatternGroupAndGiveEmptyResultAccordingToTimingAndBehaviorViaPost/sparql"));
RDFSelectQuery query = new RDFSelectQuery()
.AddPatternGroup(new RDFPatternGroup()
.AddBind(new RDFBind(new RDFConstantExpression(new RDFResource("ex:topolino")), new RDFVariable("?X")))
.UnionWithNext())
.AddPatternGroup(new RDFPatternGroup()
.AddPattern(new RDFPattern(new RDFVariable("?Y"), new RDFResource("ex:dogOf"), new RDFVariable("?X")))
.AsService(endpoint, new RDFSPARQLEndpointQueryOptions(250,
RDFQueryEnums.RDFSPARQLEndpointQueryErrorBehaviors.GiveEmptyResult, RDFQueryEnums.RDFSPARQLEndpointQueryMethods.Post)));
RDFSelectQueryResult result = query.ApplyToGraph(new RDFGraph());

Assert.IsNotNull(result);
Assert.IsNotNull(result.SelectResults);
Assert.IsTrue(result.SelectResultsCount == 1);
Assert.IsTrue(result.SelectResults.Columns.Count == 1);
Assert.IsTrue(result.SelectResults.Columns[0].ColumnName.Equals("?X"));
Assert.IsTrue(result.SelectResults.Rows[0]["?X"].Equals("ex:topolino"));
}

[TestMethod]
public void ShouldApplySelectQueryToStoreAndHaveResults()
{
Expand Down Expand Up @@ -768,6 +893,49 @@ public void ShouldApplySelectQueryToSPARQLEndpointAndHaveResults()
Assert.IsTrue(result.SelectResults.Rows[0]["?S"].Equals("ex:flower"));
}

[TestMethod]
public void ShouldApplySelectQueryToSPARQLEndpointAndHaveResultsViaPost()
{
server
.Given(
Request.Create()
.WithPath("/RDFSelectQueryTest/ShouldApplySelectQueryToSPARQLEndpointAndHaveResultsViaPost/sparql")
.UsingPost())
.RespondWith(
Response.Create()
.WithBody(
@"<?xml version=""1.0"" encoding=""utf-8""?>
<sparql xmlns=""http://www.w3.org/2005/sparql-results#"">
<head>
<variable name=""?S"" />
</head>
<results>
<result>
<binding name=""?S"">
<uri>ex:flower</uri>
</binding>
</result>
</results>
</sparql>", encoding: Encoding.UTF8)
.WithHeader("Content-Type", "application/sparql-results+xml")
.WithStatusCode(HttpStatusCode.OK));

RDFSelectQuery query = new RDFSelectQuery()
.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 + "/RDFSelectQueryTest/ShouldApplySelectQueryToSPARQLEndpointAndHaveResultsViaPost/sparql"));
RDFSelectQueryResult result = query.ApplyToSPARQLEndpoint(endpoint, new RDFSPARQLEndpointQueryOptions() {
QueryMethod = RDFQueryEnums.RDFSPARQLEndpointQueryMethods.Post });

Assert.IsNotNull(result);
Assert.IsNotNull(result.SelectResults);
Assert.IsTrue(result.SelectResultsCount == 1);
Assert.IsTrue(result.SelectResults.Columns.Count == 1);
Assert.IsTrue(result.SelectResults.Columns[0].ColumnName.Equals("?S"));
Assert.IsTrue(result.SelectResults.Rows[0]["?S"].Equals("ex:flower"));
}

[TestMethod]
public void ShouldApplyRawSelectQueryToSPARQLEndpoint()
{
Expand Down

0 comments on commit 2e92619

Please sign in to comment.