From 97da7d5e96b0117b391652d8c492bd8d2cf67bc3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 22 Nov 2024 23:15:15 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Add=20test=20for=20getGetRequests()?= =?utf8?q?=20and=20refactor=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/fcp/highlevel/FcpClient.java | 7 +----- .../pterodactylus/fcp/highlevel/FcpClientTest.java | 27 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java index 491a219..78985cc 100644 --- a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java @@ -919,12 +919,7 @@ public class FcpClient implements Closeable { * if an FCP error occurs */ public Collection getGetRequests(final boolean global) throws IOException, FcpException { - return from(getRequests(global)).filter(new Predicate() { - @Override - public boolean apply(Request request) { - return request instanceof GetRequest; - } - }).toList(); + return getRequests(global).stream().filter(request -> request instanceof GetRequest).collect(toList()); } /** diff --git a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java index 040575a..2c04f33 100644 --- a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java @@ -5,6 +5,7 @@ import net.pterodactylus.fcp.AddPeer.Visibility; import net.pterodactylus.fcp.AllData; import net.pterodactylus.fcp.EndListPeerNotes; import net.pterodactylus.fcp.EndListPeers; +import net.pterodactylus.fcp.EndListPersistentRequests; import net.pterodactylus.fcp.FcpConnection; import net.pterodactylus.fcp.FcpListener; import net.pterodactylus.fcp.FcpMessage; @@ -15,6 +16,9 @@ import net.pterodactylus.fcp.Peer; import net.pterodactylus.fcp.PeerNote; import net.pterodactylus.fcp.PeerNoteType; import net.pterodactylus.fcp.PeerRemoved; +import net.pterodactylus.fcp.PersistentGet; +import net.pterodactylus.fcp.PersistentPut; +import net.pterodactylus.fcp.PersistentPutDir; import net.pterodactylus.fcp.ProtocolError; import net.pterodactylus.fcp.SSKKeypair; import net.pterodactylus.fcp.UnknownNodeIdentifier; @@ -47,6 +51,7 @@ import static net.pterodactylus.fcp.test.Matchers.hasField; import static net.pterodactylus.fcp.test.NodeRefs.createNodeRef; import static net.pterodactylus.fcp.test.PeerMatchers.peerWithIdentity; import static net.pterodactylus.fcp.test.Peers.createPeer; +import static net.pterodactylus.fcp.test.Requests.isGetRequest; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.anything; @@ -634,6 +639,28 @@ public class FcpClientTest { } } + @Test + public void getGetRequestsReturnsGetRequests() throws IOException, FcpException { + FcpConnection fcpConnection = createFcpConnection(message -> { + if (message.getName().equals("ListPersistentRequests")) { + return (listener, connection) -> { + listener.receivedPersistentGet(connection, new PersistentGet(new FcpMessage("PersistentGet").put("Identifier", "get1"))); + listener.receivedPersistentPut(connection, new PersistentPut(new FcpMessage("PersistentPut").put("Identifier", "put1"))); + listener.receivedPersistentPut(connection, new PersistentPut(new FcpMessage("PersistentPut").put("Identifier", "put2"))); + listener.receivedPersistentPutDir(connection, new PersistentPutDir(new FcpMessage("PersistentPutDir").put("Identifier", "putdir1"))); + listener.receivedPersistentPutDir(connection, new PersistentPutDir(new FcpMessage("PersistentPutDir").put("Identifier", "putdir2"))); + listener.receivedPersistentPutDir(connection, new PersistentPutDir(new FcpMessage("PersistentPutDir").put("Identifier", "putdir3"))); + listener.receivedEndListPersistentRequests(connection, new EndListPersistentRequests(new FcpMessage("EndListPersistentRequests"))); + }; + } + return FcpClientTest::doNothing; + }); + try (FcpClient fcpClient = new FcpClient(fcpConnection)) { + Collection getRequests = fcpClient.getGetRequests(false); + assertThat(getRequests, contains(isGetRequest(equalTo("get1")))); + } + } + private static void doNothing(FcpListener listener, FcpConnection connection) { // do nothing. } -- 2.7.4