From 4c6612a61e2e5ce46d840bf6f0ae3771a6583446 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Sat, 12 Jul 2014 22:49:00 +0930 Subject: [PATCH] Rhizome Java API: list bundles new since token --- .../servaldna/ServalDClient.java | 7 +++ .../servaldna/rhizome/RhizomeBundleList.java | 12 ++++- java/org/servalproject/test/Rhizome.java | 28 ++++++++++ testdefs_java.sh | 15 ++++-- tests/rhizomejava | 54 +++++++++++++++++++ tests/rhizomerestful | 6 +-- 6 files changed, 115 insertions(+), 7 deletions(-) diff --git a/java/org/servalproject/servaldna/ServalDClient.java b/java/org/servalproject/servaldna/ServalDClient.java index dd7a895c..da459d4f 100644 --- a/java/org/servalproject/servaldna/ServalDClient.java +++ b/java/org/servalproject/servaldna/ServalDClient.java @@ -81,6 +81,13 @@ public class ServalDClient implements ServalDHttpConnectionFactory return list; } + public RhizomeBundleList rhizomeListBundlesSince(String token) throws ServalDInterfaceException, IOException + { + RhizomeBundleList list = new RhizomeBundleList(this, token); + list.connect(); + return list; + } + public RhizomeManifestBundle rhizomeManifest(BundleId bid) throws ServalDInterfaceException, IOException { return RhizomeCommon.rhizomeManifest(this, bid); diff --git a/java/org/servalproject/servaldna/rhizome/RhizomeBundleList.java b/java/org/servalproject/servaldna/rhizome/RhizomeBundleList.java index 47845bca..c1d14484 100644 --- a/java/org/servalproject/servaldna/rhizome/RhizomeBundleList.java +++ b/java/org/servalproject/servaldna/rhizome/RhizomeBundleList.java @@ -41,9 +41,15 @@ public class RhizomeBundleList { private HttpURLConnection httpConnection; private JSONTokeniser json; private JSONTableScanner table; + private String sinceToken; int rowCount; public RhizomeBundleList(ServalDHttpConnectionFactory connector) + { + this(connector, null); + } + + public RhizomeBundleList(ServalDHttpConnectionFactory connector, String since_token) { this.httpConnector = connector; this.table = new JSONTableScanner() @@ -61,6 +67,7 @@ public class RhizomeBundleList { .addColumn("sender", SubscriberId.class, JSONTokeniser.Narrow.ALLOW_NULL) .addColumn("recipient", SubscriberId.class, JSONTokeniser.Narrow.ALLOW_NULL) .addColumn("name", String.class); + this.sinceToken = since_token; } public boolean isConnected() @@ -72,7 +79,10 @@ public class RhizomeBundleList { { try { rowCount = 0; - httpConnection = httpConnector.newServalDHttpConnection("/restful/rhizome/bundlelist.json"); + if (this.sinceToken == null) + httpConnection = httpConnector.newServalDHttpConnection("/restful/rhizome/bundlelist.json"); + else + httpConnection = httpConnector.newServalDHttpConnection("/restful/rhizome/newsince/" + this.sinceToken + "/bundlelist.json"); httpConnection.connect(); json = RhizomeCommon.receiveRestfulResponse(httpConnection, HttpURLConnection.HTTP_OK); json.consume(JSONTokeniser.Token.START_OBJECT); diff --git a/java/org/servalproject/test/Rhizome.java b/java/org/servalproject/test/Rhizome.java index 9c713333..31425845 100644 --- a/java/org/servalproject/test/Rhizome.java +++ b/java/org/servalproject/test/Rhizome.java @@ -85,6 +85,32 @@ public class Rhizome { System.exit(0); } + static void rhizome_list_newsince(String token) throws ServalDInterfaceException, IOException, InterruptedException + { + System.err.println("token=" + token); + ServalDClient client = new ServerControl().getRestfulClient(); + RhizomeBundleList list = null; + try { + list = client.rhizomeListBundlesSince(token); + RhizomeListBundle bundle; + while ((bundle = list.nextBundle()) != null) { + System.out.println( + "_token=" + bundle.token + + ", _rowId=" + bundle.rowId + + ", _insertTime=" + bundle.insertTime + + ", _author=" + bundle.author + + ", _fromHere=" + bundle.fromHere + + ", " + manifestFields(bundle.manifest, ", ") + ); + } + } + finally { + if (list != null) + list.close(); + } + System.exit(0); + } + static void rhizome_manifest(BundleId bid, String dstpath) throws ServalDInterfaceException, IOException, InterruptedException { ServalDClient client = new ServerControl().getRestfulClient(); @@ -244,6 +270,8 @@ public class Rhizome { try { if (methodName.equals("rhizome-list")) rhizome_list(); + else if (methodName.equals("rhizome-list-newsince")) + rhizome_list_newsince(args[1]); else if (methodName.equals("rhizome-manifest")) rhizome_manifest(new BundleId(args[1]), args[2]); else if (methodName.equals("rhizome-payload-raw")) diff --git a/testdefs_java.sh b/testdefs_java.sh index d905fe91..60b46b94 100644 --- a/testdefs_java.sh +++ b/testdefs_java.sh @@ -45,14 +45,23 @@ _executeJava() { *) break;; esac done - "$func" "${opts[@]}" --core-backtrace java "-Djava.library.path=$LD_LIBRARY_PATH" -classpath "$PWD/classes" "$@" + "$func" "${opts[@]}" java "-Djava.library.path=$LD_LIBRARY_PATH" -classpath "$PWD/classes" "$@" +} + +_run() { + tfw_log "$@" + "$@" +} + +runJava() { + _executeJava _run "$@" } executeJava() { - _executeJava execute "$@" + _executeJava execute --core-backtrace "$@" } executeJavaOk() { - _executeJava executeOk "$@" + _executeJava executeOk --core-backtrace "$@" } diff --git a/tests/rhizomejava b/tests/rhizomejava index 8016f03f..25a233aa 100755 --- a/tests/rhizomejava +++ b/tests/rhizomejava @@ -146,6 +146,60 @@ test_RhizomeList() { done } +doc_RhizomeListNewSince="Java API list Rhizome bundles since token" +setup_RhizomeListNewSince() { + set_extra_config() { + executeOk_servald config set api.restful.newsince_timeout 60s \ + set api.restful.newsince_poll_ms 500 + } + setup + rhizome_add_bundles $SIDA1 0 5 + executeJavaOk org.servalproject.test.Rhizome rhizome-list + assertStdoutLineCount == 6 + unset_vars_with_prefix XX_ + unpack_vars XX_ "$(sed -n -e 1p "$TFWSTDOUT")" + token="$XX__token" + assert [ -n "$token" ] +} +test_RhizomeListNewSince() { + rhizome_list_newsince() { + runJava org.servalproject.test.Rhizome rhizome-list-newsince "$token" >newsince$i + } + for i in 1 2 3; do + fork %list$i rhizome_list_newsince + done + wait_until [ -e newsince1 -a -e newsince2 -a -e newsince3 ] + rhizome_add_bundles $SIDA1 6 10 + for i in 1 2 3; do + wait_until --timeout=10 grep "${BID[10]}" newsince$i + done + fork_terminate_all + fork_wait_all + for i in 1 2 3; do + tfw_preserve newsince$i + for ((n = 0; n <= 5; ++n)); do + assertGrep --matches=0 newsince$i "\" + done + for ((n = 6; n <= 10; ++n)); do + assertGrep --matches=1 newsince$i "\" + unset_vars_with_prefix XX_ + unpack_vars XX_ "$(sed -n -e "/\bid=${BID[$n]}\b/p" newsince$i)" + assert [ "$XX_id" = "${BID[$n]}" ] + assert [ "$XX_version" = "${VERSION[$n]}" ] + assert [ "$XX_filesize" = "${SIZE[$n]}" ] + assert [ "$XX_filehash" = "${HASH[$n]}" ] + assert [ "$XX_date" = "${DATE[$n]}" ] + assert [ "$XX_service" = "file" ] + assert [ "$XX_name" = "file$n" ] + assert [ "$XX__rowId" = "${ROWID[$n]}" ] + assert [ "$XX__fromHere" = "1" ] + assert [ "$XX__author" = "$SIDA1" ] + assert [ "$XX__insertTime" = "${INSERTTIME[$n]}" ] + assert [ -n "$XX__token" ] + done + done +} + assert_metadata() { local n=$1 assertStdoutGrep --matches=1 "^id=${BID[$n]}$CR\$" diff --git a/tests/rhizomerestful b/tests/rhizomerestful index 9aab4898..4d72997b 100755 --- a/tests/rhizomerestful +++ b/tests/rhizomerestful @@ -159,8 +159,8 @@ test_RhizomeList() { done } -doc_RhizomeNewSince="HTTP RESTful list Rhizome bundles since token as JSON" -setup_RhizomeNewSince() { +doc_RhizomeListNewSince="HTTP RESTful list Rhizome bundles since token as JSON" +setup_RhizomeListNewSince() { set_extra_config() { executeOk_servald config set api.restful.newsince_timeout 60s \ set api.restful.newsince_poll_ms 500 @@ -178,7 +178,7 @@ setup_RhizomeNewSince() { token=$(jq --raw-output '.[0][".token"]' array_of_objects.json) assert [ -n "$token" ] } -test_RhizomeNewSince() { +test_RhizomeListNewSince() { for i in 1 2 3; do fork %curl$i curl \ --silent --fail --show-error \