✨ Add tests for getPeers() method
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 4 Sep 2024 11:48:21 +0000 (13:48 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 4 Sep 2024 12:01:18 +0000 (14:01 +0200)
Also, some more matchers for FCP messages as well as Peer objects were
added.

src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java
src/test/java/net/pterodactylus/fcp/test/Matchers.java
src/test/java/net/pterodactylus/fcp/test/PeerMatchers.java [new file with mode: 0644]

index 3d9a660..314be74 100644 (file)
@@ -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<? super Iterable<? extends FcpMessage>> sentMessagesMatcher, Matcher<? super Collection<Peer>> peersMatcher) throws IOException, FcpException {
+               List<FcpMessage> 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<Peer> peers = fcpClient.getPeers(withMetadataFlag, withVolatileFlag);
+                       assertThat(sentMessages, sentMessagesMatcher);
+                       assertThat(peers, peersMatcher);
+               }
+       }
+
        @Test
        public void generatingKeyPairSendsCorrectMessage() throws IOException, FcpException {
                FcpConnection fcpConnection = createFcpConnection(message -> {
index 645b4a0..871869f 100644 (file)
@@ -81,4 +81,23 @@ public class Matchers {
                };
        }
 
+       public static <V> Matcher<FcpMessage> hasField(String name, Matcher<? super V> valueMatcher) {
+               return new TypeSafeDiagnosingMatcher<FcpMessage>() {
+                       @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 (file)
index 0000000..cd3d826
--- /dev/null
@@ -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<Peer> peerWithIdentity(Matcher<? super String> identityMatcher) {
+               return new TypeSafeDiagnosingMatcher<Peer>() {
+                       @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);
+                       }
+               };
+       }
+
+}