Send generated URIs on ClientPutDiskDir to registered consumers
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 16 Oct 2015 06:30:50 +0000 (08:30 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 16 Oct 2015 06:30:50 +0000 (08:30 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommand.java
src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommandImpl.java
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

index dad6750..1bcd116 100644 (file)
@@ -15,6 +15,7 @@ import net.pterodactylus.fcp.RequestProgress;
 public interface ClientPutDiskDirCommand {
 
        ClientPutDiskDirCommand onProgress(Consumer<RequestProgress> requestProgressConsumer);
 public interface ClientPutDiskDirCommand {
 
        ClientPutDiskDirCommand onProgress(Consumer<RequestProgress> requestProgressConsumer);
+       ClientPutDiskDirCommand onKeyGenerated(Consumer<String> keyGenerated);
 
        WithUri fromDirectory(File directory);
 
 
        WithUri fromDirectory(File directory);
 
index 2cf10c7..d223da2 100644 (file)
@@ -18,6 +18,7 @@ import net.pterodactylus.fcp.ProtocolError;
 import net.pterodactylus.fcp.PutSuccessful;
 import net.pterodactylus.fcp.RequestProgress;
 import net.pterodactylus.fcp.SimpleProgress;
 import net.pterodactylus.fcp.PutSuccessful;
 import net.pterodactylus.fcp.RequestProgress;
 import net.pterodactylus.fcp.SimpleProgress;
+import net.pterodactylus.fcp.URIGenerated;
 import net.pterodactylus.fcp.Verbosity;
 
 import com.google.common.util.concurrent.ListeningExecutorService;
 import net.pterodactylus.fcp.Verbosity;
 
 import com.google.common.util.concurrent.ListeningExecutorService;
@@ -36,6 +37,7 @@ public class ClientPutDiskDirCommandImpl implements ClientPutDiskDirCommand {
        private final AtomicReference<String> directory = new AtomicReference<>();
        private final AtomicReference<String> uri = new AtomicReference<>();
        private final List<Consumer<RequestProgress>> requestProgressConsumers = new CopyOnWriteArrayList<>();
        private final AtomicReference<String> directory = new AtomicReference<>();
        private final AtomicReference<String> uri = new AtomicReference<>();
        private final List<Consumer<RequestProgress>> requestProgressConsumers = new CopyOnWriteArrayList<>();
+       private final List<Consumer<String>> keyGeneratedConsumers = new CopyOnWriteArrayList<>();
 
        public ClientPutDiskDirCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
                this.threadPool = MoreExecutors.listeningDecorator(threadPool);
 
        public ClientPutDiskDirCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
                this.threadPool = MoreExecutors.listeningDecorator(threadPool);
@@ -50,6 +52,12 @@ public class ClientPutDiskDirCommandImpl implements ClientPutDiskDirCommand {
        }
 
        @Override
        }
 
        @Override
+       public ClientPutDiskDirCommand onKeyGenerated(Consumer<String> keyGenerated) {
+               keyGeneratedConsumers.add(Objects.requireNonNull(keyGenerated));
+               return this;
+       }
+
+       @Override
        public WithUri fromDirectory(File directory) {
                this.directory.set(Objects.requireNonNull(directory).getPath());
                return this::uri;
        public WithUri fromDirectory(File directory) {
                this.directory.set(Objects.requireNonNull(directory).getPath());
                return this::uri;
@@ -97,6 +105,11 @@ public class ClientPutDiskDirCommandImpl implements ClientPutDiskDirCommand {
                }
 
                @Override
                }
 
                @Override
+               protected void consumeURIGenerated(URIGenerated uriGenerated) {
+                       keyGeneratedConsumers.forEach(consumer -> consumer.accept(uriGenerated.getURI()));
+               }
+
+               @Override
                protected void consumeProtocolError(ProtocolError protocolError) {
                        finish();
                }
                protected void consumeProtocolError(ProtocolError protocolError) {
                        finish();
                }
index 77573c0..0a39451 100644 (file)
@@ -1706,6 +1706,18 @@ public class DefaultFcpClientTest {
                                ));
                        }
 
                                ));
                        }
 
+                       @Test
+                       public void generatedUriIsSentToConsumerCorrectly() throws InterruptedException, ExecutionException, IOException {
+                               List<String> generatedKeys = new ArrayList<>();
+                               Future<Optional<Key>> key = fcpClient.clientPutDiskDir().onKeyGenerated(generatedKeys::add)
+                                       .fromDirectory(new File("")).uri("CHK@").execute();
+                               connectAndAssert(this::matchesClientPutDiskDir);
+                               replyWithGeneratedUri();
+                               replyWithPutSuccessful(identifier);
+                               assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
+                               assertThat(generatedKeys, contains("KSK@foo.txt"));
+                       }
+
                        private Matcher<List<String>> matchesClientPutDiskDir(String... additionalLines) {
                                List<String> lines = new ArrayList<>(Arrays.asList("Identifier=" + identifier, "URI=CHK@", "Filename=" + new File("").getPath()));
                                Arrays.asList(additionalLines).forEach(lines::add);
                        private Matcher<List<String>> matchesClientPutDiskDir(String... additionalLines) {
                                List<String> lines = new ArrayList<>(Arrays.asList("Identifier=" + identifier, "URI=CHK@", "Filename=" + new File("").getPath()));
                                Arrays.asList(additionalLines).forEach(lines::add);