From: David ‘Bombe’ Roden Date: Fri, 6 Sep 2024 12:57:03 +0000 (+0200) Subject: ✅ Deprecate and replace modifyPeer(), add test X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=546860eb61ae85e546582747addb21e93ad33468;p=jFCPlib.git ✅ Deprecate and replace modifyPeer(), add test --- diff --git a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java index 1dd6e9f..1144ab3 100644 --- a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java @@ -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 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); } /** diff --git a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java index e455233..e86bb85 100644 --- a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java @@ -453,6 +453,22 @@ public class FcpClientTest { } @Test + public void modifyPeerSendsCorrectMessage() throws Exception { + List 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")) {