Skip to content

Commit

Permalink
Mapper_WoT_Test: add iRail test case
Browse files Browse the repository at this point in the history
Test redirection
  • Loading branch information
DylanVanAssche committed Jun 1, 2021
1 parent ef4f6bf commit a1f99b2
Show file tree
Hide file tree
Showing 6 changed files with 2,854 additions and 1 deletion.
52 changes: 51 additions & 1 deletion src/test/java/be/ugent/rml/Mapper_WoT_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ public void evaluate_essence_wot_support() throws IOException {
server.stop(0);
}

@Test
public void evaluate_irail_stations_wot_support() throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/redirect", new Mapper_WoT_Test.IRailStationHandler1());
server.createContext("/stations", new Mapper_WoT_Test.IRailStationHandler2());
server.setExecutor(null); // creates a default executor
server.start();

HashMap<Term, String> outPaths = new HashMap<Term, String>();
outPaths.put(new NamedNode("rmlmapper://default.store"), "./web-of-things/irail-stations/out-default.nq");
doMapping("./web-of-things/irail-stations/mapping.ttl", outPaths);

server.stop(0);
}

static class TrashCansFileHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
Expand Down Expand Up @@ -64,4 +79,39 @@ public void handle(HttpExchange t) throws IOException {

}
}
}

static class IRailStationHandler1 implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {

// Redirect HTTP 302
List<String> newLocation = new ArrayList<>();
String response = "Redirected to /stations";
newLocation.add("http://" + t.getLocalAddress().getHostName() + ":" + t.getLocalAddress().getPort() + "/stations");
System.out.println(newLocation);
t.getResponseHeaders().put("Location", newLocation);
t.sendResponseHeaders(302, response.length());
}
}

static class IRailStationHandler2 implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
String response = "couldn't load iRail stations JSON file";
try {
response = Utils.fileToString(Utils.getFile("./web-of-things/irail-stations/stations.json"));
} catch (IOException ex) {
ex.printStackTrace();
}
List<String> contentType = new ArrayList<>();
contentType.add("application/json");

// Return stations if not redirected
t.getResponseHeaders().put("Content-Type", contentType);
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
19 changes: 19 additions & 0 deletions src/test/java/be/ugent/rml/TestCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public Executor doMapping(String mapPath, String outPath) {
* This method executes a mapping with Targets, compares it to the expected out, and returns the used Executor.
* @param mapPath The path of the mapping file.
* @param outPaths The paths of the files with the expected output.
* @param privateSecurityDataPath The path of the private security data file.
* @return The Executor used to execute the mapping.
*/
public Executor doMapping(String mapPath, HashMap<Term, String> outPaths, String privateSecurityDataPath) {
Expand All @@ -168,6 +169,24 @@ public Executor doMapping(String mapPath, HashMap<Term, String> outPaths, String
return null;
}

/**
* This method executes a mapping with Targets, compares it to the expected out, and returns the used Executor.
* @param mapPath The path of the mapping file.
* @param outPaths The paths of the files with the expected output.
* @return The Executor used to execute the mapping.
*/
public Executor doMapping(String mapPath, HashMap<Term, String> outPaths) {
try {
Executor executor = this.createExecutor(mapPath);
doMapping(executor, outPaths);
return executor;
} catch (Exception e) {
logger.error(e.getMessage(), e);
fail();
}
return null;
}

/**
* This method executes a mapping, compares it to the expected out, and returns the used Executor.
* @param mapPath The path of the mapping file.
Expand Down
65 changes: 65 additions & 0 deletions src/test/resources/web-of-things/irail-stations/mapping.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix td: <https://www.w3.org/2019/wot/td#> .
@prefix htv: <http://www.w3.org/2011/http#> .
@prefix hctl: <https://www.w3.org/2019/wot/hypermedia#> .
@prefix schema: <http://schema.org/> .
@prefix gtfs: <http://vocab.gtfs.org/terms#> .
@prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@base <http://example.org/rules/> .

<#WoTWebAPISource> a td:PropertyAffordance;
td:hasForm [
# URL and content type
hctl:hasTarget "http://localhost:8000/redirect";
hctl:forContentType "application/json";
# Read only
hctl:hasOperationType td:readproperty;
# Set HTTP method and headers
htv:methodName "GET";
htv:headers ([
htv:fieldName "User-Agent";
htv:fieldValue "RMLMapper";
]);
];
.

<#WoTWebAPI> a td:Thing;
td:hasPropertyAffordance <#WoTWebResource>;
.

<#TriplesMap> a rr:TriplesMap;
rml:logicalSource [ a rml:LogicalSource;
rml:source <#WoTWebAPISource>;
rml:referenceFormulation ql:JSONPath;
rml:iterator "$.station.[*]";
];
rr:subjectMap [
rml:reference "@id";
];
rr:predicateObjectMap [
rr:predicate rdf:type;
rr:object gtfs:Station;
];
rr:predicateObjectMap [
rr:predicate schema:name;
rr:objectMap [
rml:reference "name";
];
];
rr:predicateObjectMap [
rr:predicate geo:latitude;
rr:objectMap [
rml:reference "locationY";
];
];
rr:predicateObjectMap [
rr:predicate geo:longitude;
rr:objectMap [
rml:reference "locationX";
];
];
.
Loading

0 comments on commit a1f99b2

Please sign in to comment.