✅ Add test for getGetRequests() and refactor method
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 22 Nov 2024 22:15:15 +0000 (23:15 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 22 Nov 2024 22:15:15 +0000 (23:15 +0100)
src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java
src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java

index 491a219..78985cc 100644 (file)
@@ -919,12 +919,7 @@ public class FcpClient implements Closeable {
         *             if an FCP error occurs
         */
        public Collection<Request> getGetRequests(final boolean global) throws IOException, FcpException {
-               return from(getRequests(global)).filter(new Predicate<Request>() {
-                       @Override
-                       public boolean apply(Request request) {
-                               return request instanceof GetRequest;
-                       }
-               }).toList();
+               return getRequests(global).stream().filter(request -> request instanceof GetRequest).collect(toList());
        }
 
        /**
index 040575a..2c04f33 100644 (file)
@@ -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<Request> getRequests = fcpClient.getGetRequests(false);
+                       assertThat(getRequests, contains(isGetRequest(equalTo("get1"))));
+               }
+       }
+
        private static void doNothing(FcpListener listener, FcpConnection connection) {
                // do nothing.
        }