From 207d6e210e81e7dfa3fb7931aecbfe1d251f6c7c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 10 Jul 2015 13:19:43 +0200 Subject: [PATCH] =?utf8?q?Add=20=E2=80=9Cwith=20metadata=E2=80=9D=20and=20?= =?utf8?q?=E2=80=9Cwith=20volatile=E2=80=9D=20flags=20to=20ListPeer=20comm?= =?utf8?q?and?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../fcp/quelaton/ListPeersCommand.java | 3 + .../fcp/quelaton/ListPeersCommandImpl.java | 16 ++++- .../fcp/quelaton/DefaultFcpClientTest.java | 72 ++++++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommand.java index 56a28c2..eb366d7 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommand.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommand.java @@ -12,6 +12,9 @@ import net.pterodactylus.fcp.Peer; */ public interface ListPeersCommand { + ListPeersCommand includeMetadata(); + ListPeersCommand includeVolatile(); + Future> execute(); } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java index 5638b98..97f3842 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java @@ -23,6 +23,8 @@ public class ListPeersCommandImpl implements ListPeersCommand { private final ListeningExecutorService threadPool; private final ConnectionSupplier connectionSupplier; + private final AtomicBoolean includeMetadata = new AtomicBoolean(false); + private final AtomicBoolean includeVolatile = new AtomicBoolean(false); public ListPeersCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) { this.threadPool = MoreExecutors.listeningDecorator(threadPool); @@ -30,9 +32,21 @@ public class ListPeersCommandImpl implements ListPeersCommand { } @Override + public ListPeersCommand includeMetadata() { + includeMetadata.set(true); + return this; + } + + @Override + public ListPeersCommand includeVolatile() { + includeVolatile.set(true); + return this; + } + + @Override public Future> execute() { String identifier = new RandomIdentifierGenerator().generate(); - ListPeers listPeers = new ListPeers(identifier); + ListPeers listPeers = new ListPeers(identifier, includeMetadata.get(), includeVolatile.get()); return threadPool.submit(() -> new ListPeersReplySequence().send(listPeers).get()); } diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index 4c49f9f..575c188 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -622,4 +622,76 @@ public class DefaultFcpClientTest { containsInAnyOrder("id1", "id2")); } + @Test + public void clientCanListPeersWithMetadata() throws IOException, ExecutionException, InterruptedException { + Future> peers = fcpClient.listPeers().includeMetadata().execute(); + connectNode(); + List lines = fcpServer.collectUntil(is("EndMessage")); + assertThat(lines, matchesFcpMessage( + "ListPeers", + "WithVolatile=false", + "WithMetadata=true", + "EndMessage" + )); + String identifier = extractIdentifier(lines); + fcpServer.writeLine( + "Peer", + "Identifier=" + identifier, + "identity=id1", + "metadata.foo=bar1", + "EndMessage" + ); + fcpServer.writeLine( + "Peer", + "Identifier=" + identifier, + "identity=id2", + "metadata.foo=bar2", + "EndMessage" + ); + fcpServer.writeLine( + "EndListPeers", + "Identifier=" + identifier, + "EndMessage" + ); + assertThat(peers.get(), hasSize(2)); + assertThat(peers.get().stream().map(peer -> peer.getMetadata("foo")).collect(Collectors.toList()), + containsInAnyOrder("bar1", "bar2")); + } + + @Test + public void clientCanListPeersWithVolatiles() throws IOException, ExecutionException, InterruptedException { + Future> peers = fcpClient.listPeers().includeVolatile().execute(); + connectNode(); + List lines = fcpServer.collectUntil(is("EndMessage")); + assertThat(lines, matchesFcpMessage( + "ListPeers", + "WithVolatile=true", + "WithMetadata=false", + "EndMessage" + )); + String identifier = extractIdentifier(lines); + fcpServer.writeLine( + "Peer", + "Identifier=" + identifier, + "identity=id1", + "volatile.foo=bar1", + "EndMessage" + ); + fcpServer.writeLine( + "Peer", + "Identifier=" + identifier, + "identity=id2", + "volatile.foo=bar2", + "EndMessage" + ); + fcpServer.writeLine( + "EndListPeers", + "Identifier=" + identifier, + "EndMessage" + ); + assertThat(peers.get(), hasSize(2)); + assertThat(peers.get().stream().map(peer -> peer.getVolatile("foo")).collect(Collectors.toList()), + containsInAnyOrder("bar1", "bar2")); + } + } -- 2.7.4