From af8de156c939bedc29dcaec35fc4a5a9094b01aa Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 22 Nov 2024 23:27:12 +0100 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Add=20method=20for=20easier?= =?utf8?q?=20creation=20of=20single-message=20responses?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/fcp/highlevel/FcpClientTest.java | 57 ++++++++++------------ 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java index 2c04f33..89f9f76 100644 --- a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java @@ -14,7 +14,6 @@ import net.pterodactylus.fcp.NodeHello; import net.pterodactylus.fcp.NodeRef; import net.pterodactylus.fcp.Peer; import net.pterodactylus.fcp.PeerNote; -import net.pterodactylus.fcp.PeerNoteType; import net.pterodactylus.fcp.PeerRemoved; import net.pterodactylus.fcp.PersistentGet; import net.pterodactylus.fcp.PersistentPut; @@ -38,6 +37,7 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Function; +import java.util.function.Predicate; import static java.nio.charset.StandardCharsets.UTF_8; import static net.pterodactylus.fcp.AddPeer.Trust.HIGH; @@ -531,12 +531,7 @@ public class FcpClientTest { } private static void removePeerAndVerifyThatNodeIdentifierIsNotBeingIgnored(BiConsumer responseGenerator) { - FcpConnection fcpConnection = createFcpConnection(message -> { - if (message.getName().equals("RemovePeer")) { - return responseGenerator; - } - return FcpClientTest::doNothing; - }); + FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("RemovePeer"), responseGenerator); try (FcpClient fcpClient = new FcpClient(fcpConnection)) { FcpProtocolException fcpProtocolException = assertThrows(FcpProtocolException.class, () -> fcpClient.removePeer(createPeer())); assertThat(fcpProtocolException.getCode(), equalTo(123)); @@ -569,12 +564,7 @@ public class FcpClientTest { @Test public void getPeerNoteWithInvalidNodeIdentifierReturnsNull() throws Exception { - FcpConnection fcpConnection = createFcpConnection(message -> { - if (message.getName().equals("ListPeerNotes")) { - return (listener, connection) -> listener.receivedEndListPeerNotes(connection, new EndListPeerNotes(new FcpMessage("EndListPeerNotes"))); - } - return FcpClientTest::doNothing; - }); + FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("ListPeerNotes"), (listener, connection) -> listener.receivedEndListPeerNotes(connection, new EndListPeerNotes(new FcpMessage("EndListPeerNotes")))); try (FcpClient fcpClient = new FcpClient(fcpConnection)) { PeerNote peerNote = fcpClient.getPeerNote(createPeer()); assertThat(peerNote, nullValue()); @@ -626,12 +616,7 @@ public class FcpClientTest { @Test public void generatingKeyPairSendsCorrectMessage() throws IOException, FcpException { - FcpConnection fcpConnection = createFcpConnection(message -> { - if (message.getName().equals("GenerateSSK")) { - return ((listener, connection) -> listener.receivedSSKKeypair(connection, new SSKKeypair(new FcpMessage("SSKKeypair").put("InsertURI", "insert-uri").put("RequestURI", "request-uri")))); - } - return FcpClientTest::doNothing; - }); + FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("GenerateSSK"), (listener, connection) -> listener.receivedSSKKeypair(connection, new SSKKeypair(new FcpMessage("SSKKeypair").put("InsertURI", "insert-uri").put("RequestURI", "request-uri")))); try (FcpClient fcpClient = new FcpClient(fcpConnection)) { SSKKeypair keypair = fcpClient.generateKeyPair(); assertThat(keypair.getInsertURI(), equalTo("insert-uri")); @@ -641,19 +626,14 @@ public class FcpClientTest { @Test public void getGetRequestsReturnsGetRequests() throws IOException, FcpException { - FcpConnection fcpConnection = createFcpConnection(message -> { - if (message.getName().equals("ListPersistentRequests")) { - return (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"))); - }; - } - return FcpClientTest::doNothing; + 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"))); }); try (FcpClient fcpClient = new FcpClient(fcpConnection)) { Collection getRequests = fcpClient.getGetRequests(false); @@ -669,6 +649,15 @@ public class FcpClientTest { return createFcpConnection(m -> FcpClientTest::doNothing); } + private static FcpConnection createFcpConnectionReactingToSingleMessage(Predicate messageFilter, BiConsumer messageConsumer) { + return createFcpConnection(message -> { + if (messageFilter.test(message)) { + return messageConsumer; + } + return FcpClientTest::doNothing; + }); + } + private static FcpConnection createFcpConnection(Function> messageConsumer) { return new FcpConnection() { private final List listeners = new ArrayList<>(); @@ -711,6 +700,10 @@ public class FcpClientTest { }; } + private static Predicate named(String name) { + return message -> message.getName().equals(name); + } + @Rule public final Timeout timeout = Timeout.seconds(5); -- 2.7.4