Skip to content

Commit

Permalink
developments for v3.9 (#325)
Browse files Browse the repository at this point in the history
* Add support for SPARQL SERVICE for pattern groups
  • Loading branch information
mdesalvo authored Nov 29, 2023
1 parent 58d444d commit 9819f21
Show file tree
Hide file tree
Showing 19 changed files with 1,920 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public class RDFClearOperationTest
[TestInitialize]
public void Initialize() { server = WireMockServer.Start(); }

[TestCleanup]
public void Cleanup() { server.Stop(); server.Dispose(); }

#region Tests
[TestMethod]
public void ShouldCreateClearNamedOperation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public class RDFDeleteDataOperationTest
[TestInitialize]
public void Initialize() { server = WireMockServer.Start(); }

[TestCleanup]
public void Cleanup() { server.Stop(); server.Dispose(); }

#region Tests
[TestMethod]
public void ShouldCreateDeleteDataOperation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public class RDFDeleteInsertWhereOperationTest
[TestInitialize]
public void Initialize() { server = WireMockServer.Start(); }

[TestCleanup]
public void Cleanup() { server.Stop(); server.Dispose(); }

#region Tests
[TestMethod]
public void ShouldCreateDeleteInsertWhereOperation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public class RDFDeleteWhereOperationTest
[TestInitialize]
public void Initialize() { server = WireMockServer.Start(); }

[TestCleanup]
public void Cleanup() { server.Stop(); server.Dispose(); }

#region Tests
[TestMethod]
public void ShouldCreateDeleteWhereOperation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public class RDFInsertDataOperationTest
[TestInitialize]
public void Initialize() { server = WireMockServer.Start(); }

[TestCleanup]
public void Cleanup() { server.Stop(); server.Dispose(); }

#region Tests
[TestMethod]
public void ShouldCreateInsertDataOperation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public class RDFInsertWhereOperationTest
[TestInitialize]
public void Initialize() { server = WireMockServer.Start(); }

[TestCleanup]
public void Cleanup() { server.Stop(); server.Dispose(); }

#region Tests
[TestMethod]
public void ShouldCreateInsertWhereOperation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public class RDFLoadOperationTest
[TestInitialize]
public void Initialize() { server = WireMockServer.Start(); }

[TestCleanup]
public void Cleanup() { server.Stop(); server.Dispose(); }

#region Tests
[TestMethod]
public void ShouldCreateLoadOperation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public class RDFAskQueryTest

[TestInitialize]
public void Initialize() { server = WireMockServer.Start(); }

[TestCleanup]
public void Cleanup() { server.Stop(); server.Dispose(); }

#region Tests
[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public class RDFConstructQueryTest

[TestInitialize]
public void Initialize() { server = WireMockServer.Start(); }

[TestCleanup]
public void Cleanup() { server.Stop(); server.Dispose(); }

#region Tests
[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public class RDFDescribeQueryTest

[TestInitialize]
public void Initialize() { server = WireMockServer.Start(); }

[TestCleanup]
public void Cleanup() { server.Stop(); server.Dispose(); }

#region Tests
[TestMethod]
Expand Down
136 changes: 130 additions & 6 deletions RDFSharp.Test/Query/Mirella/Algebra/Queries/RDFSelectQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ limitations under the License.
*/

using Microsoft.VisualStudio.TestTools.UnitTesting;
using RDFSharp.Model;
using RDFSharp.Query;
using RDFSharp.Store;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -24,9 +27,8 @@ limitations under the License.
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using RDFSharp.Model;
using RDFSharp.Query;
using RDFSharp.Store;
using WireMock.Types;
using WireMock.Util;

namespace RDFSharp.Test.Query
{
Expand All @@ -38,9 +40,6 @@ public class RDFSelectQueryTest
[TestInitialize]
public void Initialize() { server = WireMockServer.Start(); }

[TestCleanup]
public void Cleanup() { server.Stop(); server.Dispose(); }

#region Tests
[TestMethod]
public void ShouldCreateSelectQuery()
Expand Down Expand Up @@ -408,6 +407,131 @@ public void ShouldApplySelectQueryToNullGraphAndNotHaveResults()
Assert.IsTrue(result.SelectResults.Columns.Count == 0);
}

[TestMethod]
public void ShouldApplySelectQueryOnGraphWithServicePatternGroupAndThrowExceptionAccordingToTimingAndBehavior()
{
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/ShouldApplySelectQueryOnGraphWithServicePatternGroupAndThrowExceptionAccordingToTimingAndBehavior/sparql")
.UsingGet()
.WithParam(queryParams => queryParams.ContainsKey("query")))
.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/ShouldApplySelectQueryOnGraphWithServicePatternGroupAndThrowExceptionAccordingToTimingAndBehavior/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)));

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()
{
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/ShouldApplySelectQueryOnGraphWithServicePatternGroupAndGiveEmptyResultAccordingToTimingAndBehavior/sparql")
.UsingGet()
.WithParam(queryParams => queryParams.ContainsKey("query")))
.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/ShouldApplySelectQueryOnGraphWithServicePatternGroupAndGiveEmptyResultAccordingToTimingAndBehavior/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)));
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
28 changes: 28 additions & 0 deletions RDFSharp.Test/Query/Mirella/Algebra/RDFPatternGroupTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void ShouldCreatePatternGroup()
Assert.IsTrue(pGroup.IsEvaluable);
Assert.IsFalse(pGroup.IsOptional);
Assert.IsFalse(pGroup.JoinAsUnion);
Assert.IsNull(pGroup.EvaluateAsService);
Assert.IsNotNull(pGroup.GroupMembers);
Assert.IsTrue(pGroup.GroupMembers.Count == 0);
Assert.IsNotNull(pGroup.Variables);
Expand All @@ -58,6 +59,7 @@ public void ShouldCreateOptionalPatternGroup()
Assert.IsTrue(pGroup.IsEvaluable);
Assert.IsTrue(pGroup.IsOptional);
Assert.IsFalse(pGroup.JoinAsUnion);
Assert.IsFalse(pGroup.EvaluateAsService.HasValue);
Assert.IsNotNull(pGroup.GroupMembers);
Assert.IsTrue(pGroup.GroupMembers.Count == 0);
Assert.IsNotNull(pGroup.Variables);
Expand All @@ -80,6 +82,7 @@ public void ShouldCreateUnionWithNextPatternGroup()
Assert.IsTrue(pGroup.IsEvaluable);
Assert.IsFalse(pGroup.IsOptional);
Assert.IsTrue(pGroup.JoinAsUnion);
Assert.IsFalse(pGroup.EvaluateAsService.HasValue);
Assert.IsNotNull(pGroup.GroupMembers);
Assert.IsTrue(pGroup.GroupMembers.Count == 0);
Assert.IsNotNull(pGroup.Variables);
Expand All @@ -94,6 +97,31 @@ public void ShouldCreateUnionWithNextPatternGroup()
Assert.IsTrue(pGroup.GetEvaluablePatternGroupMembers().Count() == 0);
}

[TestMethod]
public void ShouldCreateServicePatternGroup()
{
RDFPatternGroup pGroup = new RDFPatternGroup().AsService(new RDFSPARQLEndpoint(new Uri("ex:org")));

Assert.IsNotNull(pGroup);
Assert.IsTrue(pGroup.IsEvaluable);
Assert.IsFalse(pGroup.IsOptional);
Assert.IsFalse(pGroup.JoinAsUnion);
Assert.IsTrue(pGroup.EvaluateAsService.HasValue);
Assert.IsTrue(string.Equals(pGroup.EvaluateAsService.Value.Item1.ToString(), "ex:org"));
Assert.IsNotNull(pGroup.GroupMembers);
Assert.IsTrue(pGroup.GroupMembers.Count == 0);
Assert.IsNotNull(pGroup.Variables);
Assert.IsTrue(pGroup.Variables.Count == 0);
Assert.IsTrue(pGroup.ToString().Equals(string.Concat(" SERVICE <ex:org> {", Environment.NewLine, " {", Environment.NewLine, " }", Environment.NewLine, " }", Environment.NewLine)));
Assert.IsTrue(pGroup.QueryMemberID.Equals(RDFModelUtilities.CreateHash(pGroup.QueryMemberStringID)));
Assert.IsTrue(pGroup.GetPatterns().Count() == 0);
Assert.IsTrue(pGroup.GetFilters().Count() == 0);
Assert.IsTrue(pGroup.GetPropertyPaths().Count() == 0);
Assert.IsTrue(pGroup.GetValues().Count() == 0);
Assert.IsTrue(pGroup.GetBinds().Count() == 0);
Assert.IsTrue(pGroup.GetEvaluablePatternGroupMembers().Count() == 0);
}

[TestMethod]
public void ShouldCreatePatternGroupWithPatterns()
{
Expand Down
Loading

0 comments on commit 9819f21

Please sign in to comment.