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;
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);
}
@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;
}
@Override
+ protected void consumeURIGenerated(URIGenerated uriGenerated) {
+ keyGeneratedConsumers.forEach(consumer -> consumer.accept(uriGenerated.getURI()));
+ }
+
+ @Override
protected void consumeProtocolError(ProtocolError protocolError) {
finish();
}
));
}
+ @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);