From 41baca0f74fa1797d0f59531a0fa8c5012c4ccba Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 23 Nov 2024 00:30:51 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Add=20test=20for=20DataFound=20being?= =?utf8?q?=20used=20in=20getRequests()?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/fcp/highlevel/FcpClientTest.java | 30 ++++++++++++++++++++++ .../java/net/pterodactylus/fcp/test/Matchers.java | 29 +++++++++++++++++---- .../java/net/pterodactylus/fcp/test/Requests.java | 14 +++++++--- 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java index 4a3a49f..d1423de 100644 --- a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java @@ -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 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 sendRequests(BiConsumer... messageConsumers) { + return (listener, connection) -> { + stream(messageConsumers).forEach(consumer -> consumer.accept(listener, connection)); + }; + } + private static void doNothing(FcpListener listener, FcpConnection connection) { // do nothing. } diff --git a/src/test/java/net/pterodactylus/fcp/test/Matchers.java b/src/test/java/net/pterodactylus/fcp/test/Matchers.java index f3c4897..264851f 100644 --- a/src/test/java/net/pterodactylus/fcp/test/Matchers.java +++ b/src/test/java/net/pterodactylus/fcp/test/Matchers.java @@ -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 Matcher matches(String name, Predicate predicate) { + return new TypeSafeDiagnosingMatcher() { + @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); + } + }; + } + } diff --git a/src/test/java/net/pterodactylus/fcp/test/Requests.java b/src/test/java/net/pterodactylus/fcp/test/Requests.java index 8f452b7..65d5734 100644 --- a/src/test/java/net/pterodactylus/fcp/test/Requests.java +++ b/src/test/java/net/pterodactylus/fcp/test/Requests.java @@ -16,8 +16,9 @@ public class Requests { return isGetRequest(anything()); } - public static Matcher isGetRequest(Matcher identifier) { - return isRequest(instanceOf(GetRequest.class), identifier); + @SafeVarargs + public static Matcher isGetRequest(Matcher identifier, Matcher... requestMatchers) { + return isRequest(instanceOf(GetRequest.class), identifier, requestMatchers); } public static Matcher isPutRequest() { @@ -28,7 +29,8 @@ public class Requests { return isRequest(instanceOf(PutRequest.class), identifier); } - private static TypeSafeDiagnosingMatcher isRequest(Matcher> requestClass, Matcher identifier) { + @SafeVarargs + private static TypeSafeDiagnosingMatcher isRequest(Matcher> requestClass, Matcher identifier, Matcher... requestMatchers) { return new TypeSafeDiagnosingMatcher() { @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 requestMatcher : requestMatchers) { + if (!requestMatcher.matches(item)) { + requestMatcher.describeMismatch(item, mismatchDescription); + return false; + } + } return true; } -- 2.7.4