From: David ‘Bombe’ Roden Date: Fri, 22 Nov 2024 22:41:32 +0000 (+0100) Subject: ✅ Add test for getPutRequests() and refactor method X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=5109c594b9103ee791573e4b9f12e3d88c3d8a96;p=jFCPlib.git ✅ Add test for getPutRequests() and refactor method --- diff --git a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java index 78985cc..7450a35 100644 --- a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java @@ -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 getPutRequests(final boolean global) throws IOException, FcpException { - return from(getRequests(global)).filter(new Predicate() { - @Override - public boolean apply(Request request) { - return request instanceof PutRequest; - } - }).toList(); + return getRequests(global).stream().filter(request -> request instanceof PutRequest).collect(toList()); } /** diff --git a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java index 89f9f76..c18e9f8 100644 --- a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java @@ -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 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 getRequests = fcpClient.getGetRequests(false); - assertThat(getRequests, contains(isGetRequest(equalTo("get1")))); + Collection 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. } diff --git a/src/test/java/net/pterodactylus/fcp/test/Requests.java b/src/test/java/net/pterodactylus/fcp/test/Requests.java index 09b9057..8f452b7 100644 --- a/src/test/java/net/pterodactylus/fcp/test/Requests.java +++ b/src/test/java/net/pterodactylus/fcp/test/Requests.java @@ -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 isGetRequest() { - return isGetRequest(Matchers.anything()); + return isGetRequest(anything()); } public static Matcher isGetRequest(Matcher identifier) { + return isRequest(instanceOf(GetRequest.class), identifier); + } + + public static Matcher isPutRequest() { + return isPutRequest(anything()); + } + + public static Matcher isPutRequest(Matcher identifier) { + return isRequest(instanceOf(PutRequest.class), identifier); + } + + private static TypeSafeDiagnosingMatcher isRequest(Matcher> requestClass, Matcher identifier) { return new TypeSafeDiagnosingMatcher() { @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); } }; }