✅ Add tests for getSeedPeers()
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 6 Sep 2024 11:35:49 +0000 (13:35 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 6 Sep 2024 11:35:49 +0000 (13:35 +0200)
src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java

index e808c5c..a307c6c 100644 (file)
@@ -231,6 +231,31 @@ public class FcpClientTest {
                sendListPeersAndVerifySentMessagesAndReturnedPeers(FcpClientTest::getOpennetPeers, false, false, anything(), contains(peerWithIdentity(equalTo("1"))));
        }
 
+       @Test
+       public void getSeedPeersWithMetadataFlagSetSendsCorrectMessage() throws Exception {
+               sendListPeersAndVerifySentMessagesAndReturnedPeers(FcpClientTest::getSeedPeers, true, false, contains(hasField("WithMetadata", equalTo("true"))), anything());
+       }
+
+       @Test
+       public void getSeedPeersWithMetadataFlagNotSetSendsCorrectMessage() throws Exception {
+               sendListPeersAndVerifySentMessagesAndReturnedPeers(FcpClientTest::getSeedPeers, false, false, contains(hasField("WithMetadata", equalTo("false"))), anything());
+       }
+
+       @Test
+       public void getSeedPeersWithVolatileFlagSetSendsCorrectMessage() throws Exception {
+               sendListPeersAndVerifySentMessagesAndReturnedPeers(FcpClientTest::getSeedPeers, false, true, contains(hasField("WithVolatile", equalTo("true"))), anything());
+       }
+
+       @Test
+       public void getSeedPeersWithVolatileFlagNotSetSendsCorrectMessage() throws Exception {
+               sendListPeersAndVerifySentMessagesAndReturnedPeers(FcpClientTest::getSeedPeers, false, false, contains(hasField("WithVolatile", equalTo("false"))), anything());
+       }
+
+       @Test
+       public void getSeedPeersReturnsPeersWithCorrectIdentifier() throws Exception {
+               sendListPeersAndVerifySentMessagesAndReturnedPeers(FcpClientTest::getSeedPeers, false, false, anything(), contains(peerWithIdentity(equalTo("2"))));
+       }
+
        private static BiFunction<Boolean, Boolean, Collection<Peer>> getPeers(FcpClient fcpClient) {
                return (withMetadata, withVolatile) -> {
                        try {
@@ -261,6 +286,16 @@ public class FcpClientTest {
                };
        }
 
+       private static BiFunction<Boolean, Boolean, Collection<Peer>> getSeedPeers(FcpClient fcpClient) {
+               return (withMetadata, withVolatile) -> {
+                       try {
+                               return fcpClient.getSeedPeers(withMetadata, withVolatile);
+                       } catch (IOException | FcpException e) {
+                               throw new RuntimeException(e);
+                       }
+               };
+       }
+
        private static void sendListPeersAndVerifySentMessagesAndReturnedPeers(Function<FcpClient, BiFunction<Boolean, Boolean, Collection<Peer>>> peerRetrieval, 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 -> {