✅ Add test for DataFound for unknown identifiers not touching anything else
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 22 Nov 2024 23:45:40 +0000 (00:45 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 22 Nov 2024 23:45:40 +0000 (00:45 +0100)
src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java
src/test/java/net/pterodactylus/fcp/test/Requests.java

index d1423de..d71fc3c 100644 (file)
@@ -56,6 +56,7 @@ 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 net.pterodactylus.fcp.test.Requests.isRequest;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.allOf;
 import static org.hamcrest.Matchers.anything;
@@ -683,12 +684,34 @@ public class FcpClientTest {
                }
        }
 
+       @Test
+       public void getRequestsIgnoresDataFoundForUnknownIdentifier() throws IOException, FcpException {
+               FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("ListPersistentRequests"), sendRequests(this::sendRequests, this::sendDataFoundForUnknownIdentifier));
+               try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+                       Collection<Request> requests = fcpClient.getRequests(true);
+                       assertThat(requests, containsInAnyOrder(
+                                       isRequest(equalTo("get1"), matches("complete", m -> !m.isComplete())),
+                                       isRequest(equalTo("get1-global"), matches("complete", m -> !m.isComplete())),
+                                       isRequest(equalTo("put1"), matches("complete", m -> !m.isComplete())),
+                                       isRequest(equalTo("put1-global"), matches("complete", m -> !m.isComplete())),
+                                       isRequest(equalTo("put2"), matches("complete", m -> !m.isComplete())),
+                                       isRequest(equalTo("put2-global"), matches("complete", m -> !m.isComplete()))
+                       ));
+               }
+       }
+
        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 sendDataFoundForUnknownIdentifier(FcpListener listener, FcpConnection connection) {
+               listener.receivedDataFound(connection, new DataFound(
+                               new FcpMessage("DataFound").put("Identifier", "unknown").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")));
index 65d5734..092ce59 100644 (file)
@@ -12,6 +12,15 @@ import static org.hamcrest.Matchers.instanceOf;
 
 public class Requests {
 
+       public static Matcher<Request> isRequest() {
+               return isRequest(anything());
+       }
+
+       @SafeVarargs
+       public static Matcher<Request> isRequest(Matcher<? super String> identifier, Matcher<? super Request>... requestMatchers) {
+               return isRequest(anything(), identifier, requestMatchers);
+       }
+
        public static Matcher<Request> isGetRequest() {
                return isGetRequest(anything());
        }