✅ Add test for DataFound being used in getRequests()
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 22 Nov 2024 23:30:51 +0000 (00:30 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 22 Nov 2024 23:31:51 +0000 (00:31 +0100)
src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java
src/test/java/net/pterodactylus/fcp/test/Matchers.java
src/test/java/net/pterodactylus/fcp/test/Requests.java

index 4a3a49f..d1423de 100644 (file)
@@ -3,6 +3,7 @@ package net.pterodactylus.fcp.highlevel;
 import net.pterodactylus.fcp.AddPeer.Trust;
 import net.pterodactylus.fcp.AddPeer.Visibility;
 import net.pterodactylus.fcp.AllData;
+import net.pterodactylus.fcp.DataFound;
 import net.pterodactylus.fcp.EndListPeerNotes;
 import net.pterodactylus.fcp.EndListPeers;
 import net.pterodactylus.fcp.EndListPersistentRequests;
@@ -40,6 +41,7 @@ import java.util.function.Function;
 import java.util.function.Predicate;
 
 import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Arrays.stream;
 import static net.pterodactylus.fcp.AddPeer.Trust.HIGH;
 import static net.pterodactylus.fcp.AddPeer.Trust.LOW;
 import static net.pterodactylus.fcp.AddPeer.Trust.NORMAL;
@@ -48,6 +50,7 @@ import static net.pterodactylus.fcp.AddPeer.Visibility.NO;
 import static net.pterodactylus.fcp.AddPeer.Visibility.YES;
 import static net.pterodactylus.fcp.test.InputStreamMatchers.streamContaining;
 import static net.pterodactylus.fcp.test.Matchers.hasField;
+import static net.pterodactylus.fcp.test.Matchers.matches;
 import static net.pterodactylus.fcp.test.NodeRefs.createNodeRef;
 import static net.pterodactylus.fcp.test.PeerMatchers.peerWithIdentity;
 import static net.pterodactylus.fcp.test.Peers.createPeer;
@@ -666,6 +669,26 @@ public class FcpClientTest {
                }
        }
 
+       @Test
+       public void getGetRequestsIsCompleteWhenDataFoundMessageIsReceived() throws IOException, FcpException {
+               FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("ListPersistentRequests"), sendRequests(this::sendRequests, this::sendDataFound));
+               try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+                       Collection<Request> requests = fcpClient.getGetRequests(false);
+                       assertThat(requests, contains(isGetRequest(
+                                       equalTo("get1"),
+                                       matches("complete", Request::isComplete),
+                                       matches("content type", m -> m.getContentType().equals("application/test")),
+                                       matches("data length", m -> m.getLength() == 12345L)
+                       )));
+               }
+       }
+
+       private void sendDataFound(FcpListener listener, FcpConnection connection) {
+               listener.receivedDataFound(connection, new DataFound(
+                               new FcpMessage("DataFound").put("Identifier", "get1").put("Metadata.ContentType", "application/test").put("DataLength", "12345"))
+               );
+       }
+
        private void sendRequests(FcpListener listener, FcpConnection connection) {
                listener.receivedPersistentGet(connection, new PersistentGet(new FcpMessage("PersistentGet").put("Identifier", "get1").put("Global", "false")));
                listener.receivedPersistentPut(connection, new PersistentPut(new FcpMessage("PersistentPut").put("Identifier", "put1").put("Global", "false")));
@@ -682,6 +705,13 @@ public class FcpClientTest {
                listener.receivedEndListPersistentRequests(connection, new EndListPersistentRequests(new FcpMessage("EndListPersistentRequests")));
        }
 
+       @SafeVarargs
+       private final BiConsumer<FcpListener, FcpConnection> sendRequests(BiConsumer<FcpListener, FcpConnection>... messageConsumers) {
+               return (listener, connection) -> {
+                       stream(messageConsumers).forEach(consumer -> consumer.accept(listener, connection));
+               };
+       }
+
        private static void doNothing(FcpListener listener, FcpConnection connection) {
                // do nothing.
        }
index f3c4897..264851f 100644 (file)
@@ -1,15 +1,16 @@
 package net.pterodactylus.fcp.test;
 
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
 import net.pterodactylus.fcp.FcpMessage;
-
+import net.pterodactylus.fcp.highlevel.Request;
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
 import org.hamcrest.TypeSafeDiagnosingMatcher;
 
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.function.Predicate;
+
 import static org.hamcrest.Matchers.anything;
 import static org.hamcrest.Matchers.containsInAnyOrder;
 import static org.hamcrest.Matchers.equalTo;
@@ -100,4 +101,22 @@ public class Matchers {
                };
        }
 
+       public static <R extends Request> Matcher<R> matches(String name, Predicate<R> predicate) {
+               return new TypeSafeDiagnosingMatcher<R>() {
+                       @Override
+                       protected boolean matchesSafely(R item, Description mismatchDescription) {
+                               if (!predicate.test(item)) {
+                                       mismatchDescription.appendText("not ").appendValue(name);
+                                       return false;
+                               }
+                               return true;
+                       }
+
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText(name);
+                       }
+               };
+       }
+
 }
index 8f452b7..65d5734 100644 (file)
@@ -16,8 +16,9 @@ public class Requests {
                return isGetRequest(anything());
        }
 
-       public static Matcher<Request> isGetRequest(Matcher<? super String> identifier) {
-               return isRequest(instanceOf(GetRequest.class), identifier);
+       @SafeVarargs
+       public static Matcher<Request> isGetRequest(Matcher<? super String> identifier, Matcher<? super Request>... requestMatchers) {
+               return isRequest(instanceOf(GetRequest.class), identifier, requestMatchers);
        }
 
        public static Matcher<Request> isPutRequest() {
@@ -28,7 +29,8 @@ public class Requests {
                return isRequest(instanceOf(PutRequest.class), identifier);
        }
 
-       private static TypeSafeDiagnosingMatcher<Request> isRequest(Matcher<? super Class<?>> requestClass, Matcher<? super String> identifier) {
+       @SafeVarargs
+       private static TypeSafeDiagnosingMatcher<Request> isRequest(Matcher<? super Class<?>> requestClass, Matcher<? super String> identifier, Matcher<? super Request>... requestMatchers) {
                return new TypeSafeDiagnosingMatcher<Request>() {
                        @Override
                        protected boolean matchesSafely(Request item, Description mismatchDescription) {
@@ -40,6 +42,12 @@ public class Requests {
                                        mismatchDescription.appendText("identifier is ").appendValue(item.getIdentifier());
                                        return false;
                                }
+                               for (Matcher<? super Request> requestMatcher : requestMatchers) {
+                                       if (!requestMatcher.matches(item)) {
+                                               requestMatcher.describeMismatch(item, mismatchDescription);
+                                               return false;
+                                       }
+                               }
                                return true;
                        }