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

index 78985cc..7450a35 100644 (file)
@@ -17,7 +17,6 @@
 
 package net.pterodactylus.fcp.highlevel;
 
-import com.google.common.base.Predicate;
 import net.pterodactylus.fcp.AddPeer;
 import net.pterodactylus.fcp.AddPeer.Trust;
 import net.pterodactylus.fcp.AddPeer.Visibility;
@@ -80,7 +79,6 @@ import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Consumer;
 
-import static com.google.common.collect.FluentIterable.from;
 import static java.util.stream.Collectors.toList;
 import static java.util.stream.Collectors.toMap;
 
@@ -936,12 +934,7 @@ public class FcpClient implements Closeable {
         *             if an FCP error occurs
         */
        public Collection<Request> getPutRequests(final boolean global) throws IOException, FcpException {
-               return from(getRequests(global)).filter(new Predicate<Request>() {
-                       @Override
-                       public boolean apply(Request request) {
-                               return request instanceof PutRequest;
-                       }
-               }).toList();
+               return getRequests(global).stream().filter(request -> request instanceof PutRequest).collect(toList());
        }
 
        /**
index 89f9f76..c18e9f8 100644 (file)
@@ -52,6 +52,7 @@ 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 net.pterodactylus.fcp.test.Requests.isPutRequest;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.allOf;
 import static org.hamcrest.Matchers.anything;
@@ -626,21 +627,32 @@ public class FcpClientTest {
 
        @Test
        public void getGetRequestsReturnsGetRequests() throws IOException, FcpException {
-               FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("ListPersistentRequests"), (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")));
-               });
+               FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("ListPersistentRequests"), this::sendRequests);
+               try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+                       Collection<Request> requests = fcpClient.getGetRequests(false);
+                       assertThat(requests, contains(isGetRequest(equalTo("get1"))));
+               }
+       }
+
+       @Test
+       public void getPutRequestsReturnsPutRequests() throws IOException, FcpException {
+               FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("ListPersistentRequests"), this::sendRequests);
                try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
-                       Collection<Request> getRequests = fcpClient.getGetRequests(false);
-                       assertThat(getRequests, contains(isGetRequest(equalTo("get1"))));
+                       Collection<Request> requests = fcpClient.getPutRequests(false);
+                       assertThat(requests, contains(isPutRequest(equalTo("put1")), isPutRequest(equalTo("put2"))));
                }
        }
 
+       private void sendRequests(FcpListener listener, FcpConnection 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")));
+       }
+
        private static void doNothing(FcpListener listener, FcpConnection connection) {
                // do nothing.
        }
index 09b9057..8f452b7 100644 (file)
@@ -1,23 +1,38 @@
 package net.pterodactylus.fcp.test;
 
 import net.pterodactylus.fcp.highlevel.GetRequest;
+import net.pterodactylus.fcp.highlevel.PutRequest;
 import net.pterodactylus.fcp.highlevel.Request;
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
-import org.hamcrest.Matchers;
 import org.hamcrest.TypeSafeDiagnosingMatcher;
 
+import static org.hamcrest.Matchers.anything;
+import static org.hamcrest.Matchers.instanceOf;
+
 public class Requests {
 
        public static Matcher<Request> isGetRequest() {
-               return isGetRequest(Matchers.anything());
+               return isGetRequest(anything());
        }
 
        public static Matcher<Request> isGetRequest(Matcher<? super String> identifier) {
+               return isRequest(instanceOf(GetRequest.class), identifier);
+       }
+
+       public static Matcher<Request> isPutRequest() {
+               return isPutRequest(anything());
+       }
+
+       public static Matcher<Request> isPutRequest(Matcher<? super String> identifier) {
+               return isRequest(instanceOf(PutRequest.class), identifier);
+       }
+
+       private static TypeSafeDiagnosingMatcher<Request> isRequest(Matcher<? super Class<?>> requestClass, Matcher<? super String> identifier) {
                return new TypeSafeDiagnosingMatcher<Request>() {
                        @Override
                        protected boolean matchesSafely(Request item, Description mismatchDescription) {
-                               if (!(item instanceof GetRequest)) {
+                               if (!requestClass.matches(item)) {
                                        mismatchDescription.appendText("is a " + item.getClass().getSimpleName());
                                        return false;
                                }
@@ -30,7 +45,7 @@ public class Requests {
 
                        @Override
                        public void describeTo(Description description) {
-                               description.appendText("get request");
+                               description.appendDescriptionOf(requestClass);
                        }
                };
        }