Skip to content

Commit

Permalink
eclipse-rdf4jGH-5058: added W3C test suite (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
barthanssens committed Aug 1, 2024
1 parent 542503f commit ac24667
Show file tree
Hide file tree
Showing 974 changed files with 76,032 additions and 10 deletions.
47 changes: 37 additions & 10 deletions core/rio/csvw/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>rdf4j-rio-jsonld-legacy</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>rdf4j-rio-api</artifactId>
Expand All @@ -48,20 +43,52 @@
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</exclusion>
<exclusion>
<groupId>no.hasmac</groupId>
<artifactId>hasmac-json-ld</artifactId>
</exclusion>
<exclusion>
<groupId>com.github.jsonld-java</groupId>
<artifactId>jsonld-java</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-common-xml</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>rdf4j-sail-api</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>rdf4j-repository-sail</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>rdf4j-sail-memory</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>rdf4j-rio-trig</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-junit-jupiter-no-dependencies</artifactId>
<version>5.14.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*******************************************************************************
* Copyright (c) 2019 Eclipse RDF4J contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*******************************************************************************/

package org.eclipse.rdf4j.rio.csvw;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayDeque;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.RDF4J;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.rio.WriterConfig;
import org.eclipse.rdf4j.rio.helpers.BasicWriterSettings;
import org.eclipse.rdf4j.sail.memory.MemoryStore;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Based upon the SHACL W3C Compliance Test
*
*/
public class W3cComplianceTest {

public static Stream<Arguments> data() {
return getTestFiles().stream()
.sorted(Comparator.comparing(URL::toString))
.map(Arguments::of);
}

@ParameterizedTest
@MethodSource("data")
public void test(URL testCasePath) throws IOException {
boolean testPassed = false;

try {
runTest(testCasePath);
testPassed = true;
} catch (AssertionError e) {
switch (e.toString()) {
case "org.opentest4j.AssertionFailedError: expected: <false> but was: <true>":
testPassed = false;
break;
case "org.opentest4j.AssertionFailedError: expected: <true> but was: <false>":
testPassed = false;
break;
default:
throw e;
}
}
}

@ParameterizedTest
@MethodSource("data")
public void parsingTest(URL testCasePath) throws IOException, InterruptedException {
runParsingTest(testCasePath);
}


private static Set<URL> getTestFiles() {
Set<URL> testFiles = new HashSet<>();

Deque<URL> manifests = new ArrayDeque<>();
manifests.add(W3cComplianceTest.class.getClassLoader().getResource("w3c/manifest.ttl"));

while (!manifests.isEmpty()) {
URL pop = manifests.pop();
Manifest manifest = new Manifest(pop);

if (manifest.include.isEmpty()) {
testFiles.add(pop);
} else {
manifests.addAll(manifest.include);
}
}
return testFiles;
}

// Test Manifest
static class Manifest {
List<URL> include;

public Manifest(URL filename) {
SailRepository sailRepository = new SailRepository(new MemoryStore());
try (SailRepositoryConnection connection = sailRepository.getConnection()) {
connection.add(filename, filename.toString(), RDFFormat.TRIG);
} catch (IOException e) {
throw new RuntimeException(e);
}

try (SailRepositoryConnection connection = sailRepository.getConnection()) {
try (Stream<Statement> stream = connection
.getStatements(null,
connection.getValueFactory()
.createIRI("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#include"),
null)
.stream()) {
include = stream
.map(Statement::getObject)
.map(Value::stringValue)
.map(v -> {
try {
return new URL(v);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toList());
}
}
}
}

private void runTest(URL resourceName) throws IOException {
W3C_shaclTestValidate expected = new W3C_shaclTestValidate(resourceName);

SailRepository data = new SailRepository(new MemoryStore());

try (SailRepositoryConnection connection = data.getConnection()) {
connection.begin();
connection.add(resourceName, "http://example.org/", RDFFormat.TRIG);
connection.commit();
}

SailRepository shapes = new SailRepository(new MemoryStore());

try (RepositoryConnection conn = shapes.getConnection()) {
conn.add(resourceName, resourceName.toString(), RDFFormat.TURTLE);
conn.commit();
}

assertEquals(expected.conforms, validate.conforms());
}

static class W3C_shaclTestValidate {

W3C_shaclTestValidate(URL filename) {
this.filename = filename.getPath();
SailRepository sailRepository = Utils.getSailRepository(filename, RDFFormat.TURTLE);
try (SailRepositoryConnection connection = sailRepository.getConnection()) {
try (Stream<Statement> stream = connection.getStatements(null, SHACL.CONFORMS, null).stream()) {
conforms = stream
.map(Statement::getObject)
.map(o -> (Literal) o)
.map(Literal::booleanValue)
.findFirst()
.orElseThrow();
}
}
}

String filename;

boolean conforms;
}

}

42 changes: 42 additions & 0 deletions core/rio/csvw/src/test/resources/w3c/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<FilesMatch "(\.jsonld)$">
ForceType application/ld+json
SetHandler default_handler
Header set Access-Control-Allow-Origin "*"
</FilesMatch>

# Turn off MultiViews
Options -MultiViews

# Directive to ensure *.ttl and .jsonld files served appropriately
AddType text/turtle .ttl
AddType application/ld+json .jsonld

# Rewrite engine setup
RewriteEngine On
RewriteBase /tests

# Add link header
<FilesMatch "test014.csv">
Header set Link '<test014-linked-metadata.json>; rel="describedby"'
</FilesMatch>

# Rewrite rule to serve HTML content from the vocabulary URI if requested
RewriteCond %{HTTP_ACCEPT} !application/rdf\+xml.*(text/html|application/xhtml\+xml)
RewriteCond %{HTTP_ACCEPT} text/html [OR]
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/.*
RewriteRule ^vocab$ vocab.html [R=303]

# Rewrite rule to serve Turtle content from the vocabulary URI if requested
RewriteCond %{HTTP_ACCEPT} text/turtle
RewriteRule ^vocab$ vocab.ttl [R=303]

# Rewrite rule to serve JSON-LD content from the vocabulary URI if requested
RewriteCond %{HTTP_ACCEPT} application/ld+json
RewriteRule ^vocab$ vocab.jsonld [R=303]

# Choose the default response
# ---------------------------

# Rewrite rule to serve the HTML content from the vocabulary URI by default
RewriteRule ^vocab$ vocab.html [R=303]
33 changes: 33 additions & 0 deletions core/rio/csvw/src/test/resources/w3c/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
This README is for the W3C CSV Working Group's test suite.
This test suite contains xxx kinds of tests:

* CSV to JSON Test (`csvt:JsonToRdfTest`) - the result of processing
a CSV to JSON.

* CSV to RDF Test (`csvt:CsvToRdfTest`) - the result of processing
a CSV to RDF (Turtle) and testing results using RDF isomorphism.

* Metadata to JSON Test (`csvt:JsonToRdfTest`) - the result of processing
Metadata to JSON with one or more referenced CSV files.

* Metadata to RDF Test (`csvt:CsvToRdfTest`) - the result of processing
Metadata to RDF (Turtle) with one or more referenced CSV files and testing results using RDF isomorphism.

The manifest.ttl file in this directory lists all of the tests in the
CSV WG's test suite. Each test is one of the above tests. All
tests have a name (`mf:name`) and an input (`mf:action`). Evaluation
tests have an expected result (`mf:result`).

* An implementation passes a Mapping test if it parses the input
into a form which can be directly compared with the expected result.

Tests may have options which should be used to change processor behavior:

* noProv - Do not output provenance triples for RDF tests; these triples are optional and make comparison using RDF isomorphism impossible.

Tests may also have a `csvt:link` property indicating an HTTP Link header which should be returned when serving the `mf:action` file; processors should act as if this value was received in an HTTP request if it is not otherwise set.

The home of the test suite is <http://www.w3.org/2013/csvw/tests/>.
The base IRI for parsing each file is `mf:action`. For example, the test test001j and
test001r require relative IRI resolution against a base of
<http://www.w3.org/2013/csvw/tests/test001.csv>.
4 changes: 4 additions & 0 deletions core/rio/csvw/src/test/resources/w3c/countries.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
countryCode,latitude,longitude,name
AD,42.546245,1.601554,Andorra
AE,23.424076,53.847818,"United Arab Emirates"
AF,33.93911,67.709953,Afghanistan
53 changes: 53 additions & 0 deletions core/rio/csvw/src/test/resources/w3c/countries.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"@context": "http://www.w3.org/ns/csvw",
"tables": [{
"url": "countries.csv",
"tableSchema": {
"columns": [{
"name": "countryCode",
"titles": "countryCode",
"datatype": "string",
"propertyUrl": "http://www.geonames.org/ontology{#_name}"
}, {
"name": "latitude",
"titles": "latitude",
"datatype": "number"
}, {
"name": "longitude",
"titles": "longitude",
"datatype": "number"
}, {
"name": "name",
"titles": "name",
"datatype": "string"
}],
"aboutUrl": "http://example.org/countries.csv{#countryCode}",
"propertyUrl": "http://schema.org/{_name}",
"primaryKey": "countryCode"
}
}, {
"url": "country_slice.csv",
"tableSchema": {
"columns": [{
"name": "countryRef",
"titles": "countryRef",
"valueUrl": "http://example.org/countries.csv{#countryRef}"
}, {
"name": "year",
"titles": "year",
"datatype": "gYear"
}, {
"name": "population",
"titles": "population",
"datatype": "integer"
}],
"foreignKeys": [{
"columnReference": "countryRef",
"reference": {
"resource": "countries.csv",
"columnReference": "countryCode"
}
}]
}
}]
}
4 changes: 4 additions & 0 deletions core/rio/csvw/src/test/resources/w3c/country_slice.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
countryRef,year,population
AF,1960,9616353
AF,1961,9799379
AF,1962,9989846
Loading

0 comments on commit ac24667

Please sign in to comment.