✅ Deprecate and replace modifyPeer(), add test
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 6 Sep 2024 12:57:03 +0000 (14:57 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 6 Sep 2024 12:57:03 +0000 (14:57 +0200)
src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java
src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java

index 1dd6e9f..1144ab3 100644 (file)
@@ -76,6 +76,7 @@ import java.util.Map.Entry;
 import java.util.Set;
 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;
@@ -660,8 +661,35 @@ public class FcpClient implements Closeable {
         *             if an I/O error occurs
         * @throws FcpException
         *             if an FCP error occurs
+        * @deprecated Use {@link #modifyPeer(Peer, Consumer)}
         */
+       @Deprecated
        public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException {
+               modifyPeer(peer, modifyPeer -> {
+                       if (allowLocalAddresses != null) {
+                               modifyPeer.setAllowLocalAddresses(allowLocalAddresses);
+                       }
+                       if (disabled != null) {
+                               modifyPeer.setEnabled(!disabled);
+                       }
+                       if (listenOnly != null) {
+                               modifyPeer.setListenOnly(listenOnly);
+                       }
+               });
+       }
+
+       /**
+        * Modifies the given peer.
+        *
+        * @param peer The peer to modify
+        * @param modifyPeerConsumer A lambda that modifies a {@link ModifyPeer}
+        *              object to change the peer’s configuration
+        * @throws IOException if an I/O error occurs
+        * @throws FcpException if an FCP error occurs
+        */
+       public void modifyPeer(Peer peer, Consumer<ModifyPeer> modifyPeerConsumer) throws IOException, FcpException {
+               ModifyPeer modifyPeer = new ModifyPeer(createIdentifier("modify-peer"), peer.getIdentifier());
+               modifyPeerConsumer.accept(modifyPeer);
                new ExtendedFcpAdapter() {
 
                        /**
@@ -670,7 +698,7 @@ public class FcpClient implements Closeable {
                        @Override
                        @SuppressWarnings("synthetic-access")
                        public void run() throws IOException {
-                               sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
+                               sendMessage(modifyPeer);
                        }
 
                        /**
index e455233..e86bb85 100644 (file)
@@ -453,6 +453,22 @@ public class FcpClientTest {
        }
 
        @Test
+       public void modifyPeerSendsCorrectMessage() throws Exception {
+               List<FcpMessage> sentMessages = new ArrayList<>();
+               FcpConnection fcpConnection = createFcpConnection(message -> {
+                       if (message.getName().equals("ModifyPeer")) {
+                               sentMessages.add(message);
+                               return (listener, connection) -> listener.receivedPeer(connection, createPeer());
+                       }
+                       return FcpClientTest::doNothing;
+               });
+               try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+                       fcpClient.modifyPeer(createPeer(), modifyPeer -> modifyPeer.setAllowLocalAddresses(true));
+                       assertThat(sentMessages, contains(hasField("AllowLocalAddresses", equalTo("true"))));
+               }
+       }
+
+       @Test
        public void generatingKeyPairSendsCorrectMessage() throws IOException, FcpException {
                FcpConnection fcpConnection = createFcpConnection(message -> {
                        if (message.getName().equals("GenerateSSK")) {