Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 85 additions & 2 deletions Test/Test/ValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,76 @@ public void HostGetRouteCollisionIsReported()
diagnostic.Message.Contains("docker-test"));
}

[TestMethod]
[DataRow("value", "value")]
[DataRow("value", "Value")]
public void DuplicateParameterNameIsReported(string first, string second)
{
ShouldReportShapeViolation(
Manifest("dup", extras: $"""
:parameter [ :name "{first}" ; :description "d" ; :example "x" ], [ :name "{second}" ; :description "d2" ; :example "y" ] ;
"""),
"Parameter name must be unique within its endpoint");
}

[TestMethod]
public void LiteralParameterDatatypeIsReported()
{
ShouldReportShapeViolation(
Manifest("datatype", extras: """:parameter [ :name "value" ; :description "d" ; :example "x" ; :datatype "integer" ] ;"""),
"Parameter datatype must be an IRI");
}

[TestMethod]
public void MissingParameterNameIsReported()
{
ShouldReportShapeViolation(
Manifest("noname", extras: """:parameter [ :description "d" ; :example "x" ] ;"""),
"Parameter must have at least one name");
}

[TestMethod]
public void InvalidParameterNameIsReported()
{
ShouldReportShapeViolation(
Manifest("badname", extras: """:parameter [ :name "not valid" ; :description "d" ; :example "x" ] ;"""),
"Parameter name must be a valid SPARQL variable name");
}

[TestMethod]
public void LiteralFrameIsReported()
{
ShouldReportShapeViolation(
Manifest("frameliteral", extras: """:frame "./f.jsonld" ;"""),
"Endpoint frame must be xsd:anyURI literal");
}

[TestMethod]
public void RelativeFrameWithoutDotSlashIsReported()
{
ShouldReportShapeViolation(
Manifest("framed", "f.jsonld"),
"Endpoint frame must start with './'");
}

[TestMethod]
public void LeadingSlashPathIsReported()
{
ShouldReportShapeViolation(
Manifest("slash", path: "/slash"),
"Endpoint path must not start with '/'");
}

[TestMethod]
public void TypedParameterHasNoDiagnostics()
{
using var directory = new TempDefinitions(
Manifest("typed", extras: """:parameter [ :name "value_1" ; :description "d" ; :example "1" ; :datatype xsd:integer ] ;"""),
"SELECT * WHERE { ?s ?p ?value_1 }");

Validate(directory.Provider).Should().BeEmpty();
}

[TestMethod]
public void HostPostOnlyRouteDoesNotCollide()
{
Expand All @@ -150,22 +220,35 @@ public void HostPostOnlyRouteDoesNotCollide()

private static IFileProvider Fixture(string name) => new PhysicalFileProvider(Path.Combine(AppContext.BaseDirectory, "Fixtures", name));

private static string Manifest(string name, string? frame = null) => $$"""
private static string Manifest(string name, string? frame = null, string extras = "", string? path = null) => $$"""
PREFIX : <http://example.org/namedquery/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

[
:name "{{name}}" ;
:endpoint [
:path "{{name}}" ;
:path "{{path ?? name}}" ;
:sparql "./q.sparql"^^xsd:anyURI ;
{{(frame is null ? "" : $":frame \"{frame}\"^^xsd:anyURI ;")}}
:name "{{name}}" ;
:description "{{name}}" ;
{{extras}}
] ;
] .
""";

private static void ShouldReportShapeViolation(string manifest, string message)
{
using var directory = new TempDefinitions(manifest, "SELECT * WHERE { ?s ?p ?o }", "{}");

var diagnostics = Validate(directory.Provider);

diagnostics.Should().Contain(diagnostic =>
diagnostic.Code == DiagnosticCodes.ShapeViolation &&
diagnostic.Message.Contains(message));
diagnostics.Should().NotContain(diagnostic => diagnostic.Code == DiagnosticCodes.Internal);
}

private static IReadOnlyList<Diagnostic> Validate(IFileProvider definitions, Action<WebApplication>? map = null)
{
var builder = WebApplication.CreateBuilder();
Expand Down
6 changes: 3 additions & 3 deletions TheODI.NamedQuery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,17 @@ Each endpoint:
| `:sparql` | yes | Relative path of the query file, an `xsd:anyURI` starting with `./` |
| `:name` | yes | Title (home page, OpenAPI, MCP tool title) |
| `:description` | yes | Description (home page, OpenAPI, MCP tool description) |
| `:frame` | no | Relative path of a JSON-LD frame applied to graph results served as JSON-LD |
| `:frame` | no | Relative path of a JSON-LD frame applied to graph results served as JSON-LD, an `xsd:anyURI` starting with `./` |
| `:parameter` | no, repeatable | A query parameter |

Each parameter:

| Property | Required | Meaning |
|---|---|---|
| `:name` | yes | Query string parameter name and SPARQL variable name |
| `:name` | yes | Query string parameter name and SPARQL variable name; letters, digits and `_` only; unique per endpoint, case-insensitive |
| `:description` | yes | Description (OpenAPI, MCP) |
| `:example` | yes | Example value (home page links, OpenAPI, MCP) |
| `:datatype` | no | Datatype of the bound literal; default `xsd:string` |
| `:datatype` | no | Datatype of the bound literal, an IRI such as `xsd:integer`; default `xsd:string` |

Every parameter must be supplied exactly once; otherwise the response is `400 Bad Request`. `format` is reserved.

Expand Down
Loading
Loading