Add method to allow local addresses for a peer
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 13 Jul 2015 17:58:49 +0000 (19:58 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 13 Jul 2015 17:58:49 +0000 (19:58 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommand.java
src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommandImpl.java
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

index 09f01ec..6987d9a 100644 (file)
@@ -13,6 +13,7 @@ public interface ModifyPeerCommand {
 
        ModifyPeerCommand enable();
        ModifyPeerCommand disable();
+       ModifyPeerCommand allowLocalAddresses();
 
        Executable<Optional<Peer>> byName(String name);
        Executable<Optional<Peer>> byIdentity(String nodeIdentity);
index 189e2e2..35d6d57 100644 (file)
@@ -26,6 +26,7 @@ public class ModifyPeerCommandImpl implements ModifyPeerCommand {
        private final ConnectionSupplier connectionSupplier;
        private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
        private final AtomicReference<Boolean> enabled = new AtomicReference<>();
+       private final AtomicReference<Boolean> allowLocalAddresses = new AtomicReference<>();
 
        public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
                this.threadPool = MoreExecutors.listeningDecorator(threadPool);
@@ -45,6 +46,12 @@ public class ModifyPeerCommandImpl implements ModifyPeerCommand {
        }
 
        @Override
+       public ModifyPeerCommand allowLocalAddresses() {
+               allowLocalAddresses.set(true);
+               return this;
+       }
+
+       @Override
        public Executable<Optional<Peer>> byName(String name) {
                nodeIdentifier.set(name);
                return this::execute;
@@ -69,6 +76,7 @@ public class ModifyPeerCommandImpl implements ModifyPeerCommand {
        private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
                ModifyPeer modifyPeer = new ModifyPeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
                Optional.ofNullable(enabled.get()).ifPresent(enabled -> modifyPeer.setEnabled(enabled));
+               Optional.ofNullable(allowLocalAddresses.get()).ifPresent(allowed -> modifyPeer.setAllowLocalAddresses(allowed));
                try (ModifyPeerDialog modifyPeerDialog = new ModifyPeerDialog()) {
                        return modifyPeerDialog.send(modifyPeer).get();
                }
index 39d2c6a..b826881 100644 (file)
@@ -5,7 +5,9 @@ import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.containsInAnyOrder;
 import static org.hamcrest.Matchers.hasSize;
 import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
 import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.startsWith;
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
@@ -1335,4 +1337,29 @@ public class DefaultFcpClientTest {
                assertThat(peer.get().get().getIdentity(), is("id1"));
        }
 
+       @Test
+       public void defaultFcpClientCanAllowLocalAddressesOfPeer()
+       throws InterruptedException, ExecutionException, IOException {
+               Future<Optional<Peer>> peer = fcpClient.modifyPeer().allowLocalAddresses().byIdentity("id1").execute();
+               connectNode();
+               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+               String identifier = extractIdentifier(lines);
+               assertThat(lines, matchesFcpMessage(
+                       "ModifyPeer",
+                       "Identifier=" + identifier,
+                       "NodeIdentifier=id1",
+                       "AllowLocalAddresses=true",
+                       "EndMessage"
+               ));
+               assertThat(lines, not(contains(startsWith("IsDisabled="))));
+               fcpServer.writeLine(
+                       "Peer",
+                       "Identifier=" + identifier,
+                       "NodeIdentifier=Friend1",
+                       "identity=id1",
+                       "EndMessage"
+               );
+               assertThat(peer.get().get().getIdentity(), is("id1"));
+       }
+
 }