From: David ‘Bombe’ Roden Date: Fri, 22 Nov 2024 22:15:15 +0000 (+0100) Subject: ✅ Add test for getGetRequests() and refactor method X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=97da7d5e96b0117b391652d8c492bd8d2cf67bc3;p=jFCPlib.git ✅ Add test for getGetRequests() and refactor method --- 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. }