From e4e296df8c892a0d09bb3a712fa72fb989848551 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 4 Sep 2024 13:48:21 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=A8=20Add=20tests=20for=20getPeers()=20met?= =?utf8?q?hod?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Also, some more matchers for FCP messages as well as Peer objects were added. --- .../pterodactylus/fcp/highlevel/FcpClientTest.java | 59 ++++++++++++++++++++++ .../java/net/pterodactylus/fcp/test/Matchers.java | 19 +++++++ .../net/pterodactylus/fcp/test/PeerMatchers.java | 28 ++++++++++ 3 files changed, 106 insertions(+) create mode 100644 src/test/java/net/pterodactylus/fcp/test/PeerMatchers.java diff --git a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java index 3d9a660..314be74 100644 --- a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java @@ -1,12 +1,15 @@ package net.pterodactylus.fcp.highlevel; import net.pterodactylus.fcp.AllData; +import net.pterodactylus.fcp.EndListPeers; import net.pterodactylus.fcp.FcpConnection; import net.pterodactylus.fcp.FcpListener; import net.pterodactylus.fcp.FcpMessage; import net.pterodactylus.fcp.GetFailed; import net.pterodactylus.fcp.NodeHello; +import net.pterodactylus.fcp.Peer; import net.pterodactylus.fcp.SSKKeypair; +import org.hamcrest.Matcher; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; @@ -14,13 +17,19 @@ import org.junit.rules.Timeout; import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; import java.util.function.Function; import static net.pterodactylus.fcp.test.InputStreamMatchers.streamContaining; +import static net.pterodactylus.fcp.test.Matchers.hasField; +import static net.pterodactylus.fcp.test.PeerMatchers.peerWithIdentity; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anything; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.equalTo; public class FcpClientTest { @@ -145,6 +154,56 @@ public class FcpClientTest { assertThat(getResult.getInputStream(), streamContaining('D', 'a', 't', 'a')); } } + + @Test + public void getPeersWithMetadataFlagSetSendsCorrectMessage() throws Exception { + sendListPeersAndVerifySentMessagesAndReturnedPeers(true, false, contains(hasField("WithMetadata", equalTo("true"))), anything()); + } + + @Test + public void getPeersWithMetadataFlagNotSetSendsCorrectMessage() throws Exception { + sendListPeersAndVerifySentMessagesAndReturnedPeers(false, false, contains(hasField("WithMetadata", equalTo("false"))), anything()); + } + + @Test + public void getPeersWithVolatileFlagSetSendsCorrectMessage() throws Exception { + sendListPeersAndVerifySentMessagesAndReturnedPeers(false, true, contains(hasField("WithVolatile", equalTo("true"))), anything()); + } + + @Test + public void getPeersWithVolatileFlagNotSetSendsCorrectMessage() throws Exception { + sendListPeersAndVerifySentMessagesAndReturnedPeers(false, false, contains(hasField("WithVolatile", equalTo("false"))), anything()); + } + + @Test + public void getPeersReturnsPeersWithCorrectIdentifier() throws Exception { + sendListPeersAndVerifySentMessagesAndReturnedPeers(false, false, anything(), containsInAnyOrder(peerWithIdentity(equalTo("1")), peerWithIdentity(equalTo("2")), peerWithIdentity(equalTo("3")))); + } + + private static void sendListPeersAndVerifySentMessagesAndReturnedPeers(boolean withMetadataFlag, boolean withVolatileFlag, Matcher> sentMessagesMatcher, Matcher> peersMatcher) throws IOException, FcpException { + List sentMessages = new ArrayList<>(); + FcpConnection fcpConnection = createFcpConnection(message -> { + if (message.getName().equals("ListPeers")) { + sentMessages.add(message); + return (listener, connection) -> { + String identifier = message.getField("Identifier"); + listener.receivedPeer(connection, new Peer(new FcpMessage("Peer").put("Identifier", identifier).put("identity", "1"))); + listener.receivedPeer(connection, new Peer(new FcpMessage("Peer").put("Identifier", "Other Identifier").put("identity", "4"))); + listener.receivedPeer(connection, new Peer(new FcpMessage("Peer").put("Identifier", identifier).put("identity", "2"))); + listener.receivedEndListPeers(connection, new EndListPeers(new FcpMessage("EndListPeers").put("Identifier", "Other Identifier"))); + listener.receivedPeer(connection, new Peer(new FcpMessage("Peer").put("Identifier", identifier).put("identity", "3"))); + listener.receivedEndListPeers(connection, new EndListPeers(new FcpMessage("EndListPeers").put("Identifier", identifier))); + }; + } + return FcpClientTest::doNothing; + }); + try (FcpClient fcpClient = new FcpClient(fcpConnection)) { + Collection peers = fcpClient.getPeers(withMetadataFlag, withVolatileFlag); + assertThat(sentMessages, sentMessagesMatcher); + assertThat(peers, peersMatcher); + } + } + @Test public void generatingKeyPairSendsCorrectMessage() throws IOException, FcpException { FcpConnection fcpConnection = createFcpConnection(message -> { diff --git a/src/test/java/net/pterodactylus/fcp/test/Matchers.java b/src/test/java/net/pterodactylus/fcp/test/Matchers.java index 645b4a0..871869f 100644 --- a/src/test/java/net/pterodactylus/fcp/test/Matchers.java +++ b/src/test/java/net/pterodactylus/fcp/test/Matchers.java @@ -81,4 +81,23 @@ public class Matchers { }; } + public static Matcher hasField(String name, Matcher valueMatcher) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(FcpMessage fcpMessage, Description mismatchDescription) { + Object value = fcpMessage.getField(name); + if (!valueMatcher.matches(value)) { + valueMatcher.describeMismatch(value, mismatchDescription); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("has field ").appendValue(name).appendText(" with value ").appendDescriptionOf(valueMatcher); + } + }; + } + } diff --git a/src/test/java/net/pterodactylus/fcp/test/PeerMatchers.java b/src/test/java/net/pterodactylus/fcp/test/PeerMatchers.java new file mode 100644 index 0000000..cd3d826 --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/test/PeerMatchers.java @@ -0,0 +1,28 @@ +package net.pterodactylus.fcp.test; + +import net.pterodactylus.fcp.Peer; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeDiagnosingMatcher; + +public class PeerMatchers { + + public static Matcher peerWithIdentity(Matcher identityMatcher) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(Peer peer, Description mismatchDescription) { + if (!identityMatcher.matches(peer.getIdentity())) { + identityMatcher.describeMismatch(peer.getIdentity(), mismatchDescription); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("is peer with identity ").appendValue(identityMatcher); + } + }; + } + +} -- 2.7.4