Skip to content

Commit

Permalink
fix TestUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
markusgraube committed Feb 12, 2015
1 parent b4f8522 commit 9154681
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static void executeDeleteQuery(String user, String message, String graphN
String query = String.format(
"USER \"%s\" %n"
+ "MESSAGE \"%s\" %n"
+ "DELETE { GRAPH <%s> REVISION \"%s\" %n"
+ "DELETE DATA { GRAPH <%s> REVISION \"%s\" %n"
+ " { %n"
+ " %s %n"
+ " } %n"
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/de/tud/plt/r43ples/webservice/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class Endpoint {
"(?<action>INSERT|DELETE).*<(?<graph>[^>]*)>",
patternModifier);
private final Pattern patternUpdateRevision = Pattern.compile(
"(?<action>INSERT|DELETE|WHERE)(\\s*DATA){0,1}\\s*\\{\\s*GRAPH\\s*<(?<graph>[^>]*)>\\s*REVISION\\s*\"(?<revision>[^\"]*)\"",
"(?<action>INSERT|DELETE|WHERE)(?<data>\\s*DATA){0,1}\\s*\\{\\s*GRAPH\\s*<(?<graph>[^>]*)>\\s*REVISION\\s*\"(?<revision>[^\"]*)\"",
patternModifier);
private final Pattern patternEmptyGraphPattern = Pattern.compile(
"GRAPH\\s*<(?<graph>[^>]*)>\\s*\\{\\s*\\}",
Expand Down Expand Up @@ -574,16 +574,20 @@ private Response getUpdateResponse(final String query, final String user, final
// numbers or reference
// names
String action = m.group("action");
String data = m.group("data");
if (data == null)
data = "";

String newRevisionNumber = RevisionManagement.getNextRevisionNumber(graphName, revisionName);
String addSetGraphUri = graphName + "-delta-added-" + newRevisionNumber;
String removeSetGraphUri = graphName + "-delta-removed-" + newRevisionNumber;
if (!RevisionManagement.isBranch(graphName, revisionName)) {
throw new InternalServerErrorException("Revision is not referenced by a branch");
}
if (action.equalsIgnoreCase("INSERT")) {
queryM = m.replaceFirst(String.format("INSERT DATA { GRAPH <%s>", addSetGraphUri));
queryM = m.replaceFirst(String.format("INSERT %s { GRAPH <%s>", data, addSetGraphUri));
} else if (action.equalsIgnoreCase("DELETE")) {
queryM = m.replaceFirst(String.format("INSERT DATA { GRAPH <%s>", removeSetGraphUri));
queryM = m.replaceFirst(String.format("INSERT %s { GRAPH <%s>", data, removeSetGraphUri));
} else if (action.equalsIgnoreCase("WHERE")) {
// TODO ersetze mit SPARQL JOIN
String tempGraphName = graphName + "-temp";
Expand All @@ -596,7 +600,6 @@ private Response getUpdateResponse(final String query, final String user, final
// Remove empty insert clauses which otherwise will lead to errors
m= patternEmptyGraphPattern.matcher(queryM);
queryM = m.replaceAll("");
// queryM = queryM.replaceAll("}\\s*INSERT", "}; INSERT");

TripleStoreInterface.executeUpdateQuery(queryM);

Expand Down Expand Up @@ -643,8 +646,7 @@ private Response getUpdateResponse(final String query, final String user, final
// Respond with next revision number
responseBuilder.header(graphNameHeader + "-revision-number", newRevisionNumber);
responseBuilder.header(graphNameHeader + "-revision-number-of-MASTER", RevisionManagement.getMasterRevisionNumber(graphName));
logger.info("Respond with new revision number " + newRevisionNumber + ".");
logger.info("Respond with new revision number " + newRevisionNumber);
logger.debug("Respond with new revision number " + newRevisionNumber + ".");
queryM = m.replaceAll(String.format("GRAPH <%s> ", graphName));
m = patternGraphWithRevision.matcher(queryM);
}
Expand Down Expand Up @@ -925,7 +927,7 @@ private Response getMergeResponse(final String sparqlQuery, final String user, f
// Respond with next revision number
responseBuilder.header(graphNameHeader + "-revision-number", newRevisionNumber);
responseBuilder.header(graphNameHeader + "-revision-number-of-MASTER", RevisionManagement.getMasterRevisionNumber(graphName));
logger.info("Respond with new revision number " + newRevisionNumber + ".");
logger.debug("Respond with new revision number " + newRevisionNumber + ".");
}
}
if (!foundEntry)
Expand Down
32 changes: 22 additions & 10 deletions src/test/java/de/tud/plt/r43ples/test/TestUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public void test_insert_existing_triples() throws SAXException, IOException {
String insert_template = ""
+ "USER \"test_user\" %n"
+ "MESSAGE \"test commit message 6 (same as 5)\" %n"
+ "INSERT { GRAPH <%s> REVISION \"5\" { %s } } %n"
+ "DELETE { GRAPH <%s> REVISION \"5\" { %s } } ";
+ "INSERT DATA { GRAPH <%s> REVISION \"5\" { %s } }; %n"
+ "DELETE DATA { GRAPH <%s> REVISION \"5\" { %s } } ";
ep.sparql(format, String.format(insert_template,
graph_test, ResourceManagement.getContentFromResource("samples/dataset1/added-5.nt"),
graph_test, ResourceManagement.getContentFromResource("samples/dataset1/removed-5.nt")));
Expand All @@ -90,27 +90,39 @@ public void test_insert_existing_triples() throws SAXException, IOException {
}

@Test
public void testRestructuring() {
public void testRestructuring() throws SAXException, IOException {
String query = "SELECT ?s ?p ?o FROM <"+graphName+"> REVISION \"B2\"\n"
+ "WHERE {?s ?p ?o} ORDER By ?s ?p ?o";
String result = ep.sparql(format, query).getEntity().toString();
String expected = ResourceManagement.getContentFromResource("dataset-merge/response-B2.xml");
assertXMLEqual(expected, result);

// restructure commit to B2
logger.info("Restructure commit to B2");
String query = String.format(""
query = String.format(""
+ "USER \"shensel\" %n"
+ "MESSAGE \"restructure commit to B2.\" %n"
+ "DELETE { GRAPH <%s> REVISION \"B2\" {"
+ " <http://example.com/testS> <http://example.com/testP> ?o."
+ "INSERT { GRAPH <%s> REVISION \"B2\" {"
+ " <http://example.com/newTestS> <http://example.com/newTestP> ?o."
+ "} } %n"
+ "WHERE { GRAPH <%s> REVISION \"B2\" {"
+ " <http://example.com/testS> <http://example.com/testP> ?o"
+ "} } %n"
+ "INSERT { GRAPH <%s> REVISION \"B2\" {"
+ " <http://example.com/newTestS> <http://example.com/newTestP> ?o."
+ "} };"
+ "DELETE { GRAPH <%s> REVISION \"B2\" {"
+ " <http://example.com/testS> <http://example.com/testP> ?o."
+ "} } %n"
+ "WHERE { GRAPH <%s> REVISION \"B2\" {"
+ " <http://example.com/testS> <http://example.com/testP> ?o"
+ "} }",
graphName, graphName, graphName, graphName);
logger.debug("Execute query: \n" + query);
logger.debug("Response: \n" + ep.sparql(format, query));
result = ep.sparql(format, query).toString();

query = "SELECT ?s ?p ?o FROM <"+graphName+"> REVISION \"B2\"\n"
+ "WHERE {?s ?p ?o} ORDER By ?s ?p ?o";
result = ep.sparql(format, query).getEntity().toString();
expected = ResourceManagement.getContentFromResource("dataset-merge/response-B2-restructured.xml");
assertXMLEqual(expected, result);
}

@Test
Expand Down
76 changes: 76 additions & 0 deletions src/test/resources/dataset-merge/response-B2-restructured.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="s"/>
<variable name="p"/>
<variable name="o"/>
</head>
<results>
<result>
<binding name="s">
<uri>http://example.com/newTestS</uri>
</binding>
<binding name="p">
<uri>http://example.com/newTestP</uri>
</binding>
<binding name="o">
<literal>A</literal>
</binding>
</result>
<result>
<binding name="s">
<uri>http://example.com/newTestS</uri>
</binding>
<binding name="p">
<uri>http://example.com/newTestP</uri>
</binding>
<binding name="o">
<literal>B</literal>
</binding>
</result>
<result>
<binding name="s">
<uri>http://example.com/newTestS</uri>
</binding>
<binding name="p">
<uri>http://example.com/newTestP</uri>
</binding>
<binding name="o">
<literal>D</literal>
</binding>
</result>
<result>
<binding name="s">
<uri>http://example.com/newTestS</uri>
</binding>
<binding name="p">
<uri>http://example.com/newTestP</uri>
</binding>
<binding name="o">
<literal>H</literal>
</binding>
</result>
<result>
<binding name="s">
<uri>http://example.com/newTestS</uri>
</binding>
<binding name="p">
<uri>http://example.com/newTestP</uri>
</binding>
<binding name="o">
<literal>I</literal>
</binding>
</result>
<result>
<binding name="s">
<uri>http://example.com/newTestS</uri>
</binding>
<binding name="p">
<uri>http://example.com/newTestP</uri>
</binding>
<binding name="o">
<literal>J</literal>
</binding>
</result>
</results>
</sparql>
76 changes: 76 additions & 0 deletions src/test/resources/dataset-merge/response-B2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="s"/>
<variable name="p"/>
<variable name="o"/>
</head>
<results>
<result>
<binding name="s">
<uri>http://example.com/testS</uri>
</binding>
<binding name="p">
<uri>http://example.com/testP</uri>
</binding>
<binding name="o">
<literal>A</literal>
</binding>
</result>
<result>
<binding name="s">
<uri>http://example.com/testS</uri>
</binding>
<binding name="p">
<uri>http://example.com/testP</uri>
</binding>
<binding name="o">
<literal>B</literal>
</binding>
</result>
<result>
<binding name="s">
<uri>http://example.com/testS</uri>
</binding>
<binding name="p">
<uri>http://example.com/testP</uri>
</binding>
<binding name="o">
<literal>D</literal>
</binding>
</result>
<result>
<binding name="s">
<uri>http://example.com/testS</uri>
</binding>
<binding name="p">
<uri>http://example.com/testP</uri>
</binding>
<binding name="o">
<literal>H</literal>
</binding>
</result>
<result>
<binding name="s">
<uri>http://example.com/testS</uri>
</binding>
<binding name="p">
<uri>http://example.com/testP</uri>
</binding>
<binding name="o">
<literal>I</literal>
</binding>
</result>
<result>
<binding name="s">
<uri>http://example.com/testS</uri>
</binding>
<binding name="p">
<uri>http://example.com/testP</uri>
</binding>
<binding name="o">
<literal>J</literal>
</binding>
</result>
</results>
</sparql>

0 comments on commit 9154681

Please sign in to comment.