Add progress consumer to ClientPut command
[jFCPlib.git] / src / test / java / net / pterodactylus / fcp / quelaton / DefaultFcpClientTest.java
index 771cd25..a4dc5a9 100644 (file)
@@ -1,5 +1,6 @@
 package net.pterodactylus.fcp.quelaton;
 
+import static net.pterodactylus.fcp.RequestProgressMatcher.isRequestProgress;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.allOf;
 import static org.hamcrest.Matchers.contains;
@@ -16,6 +17,7 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
@@ -43,6 +45,7 @@ import net.pterodactylus.fcp.Peer;
 import net.pterodactylus.fcp.PeerNote;
 import net.pterodactylus.fcp.PluginInfo;
 import net.pterodactylus.fcp.Priority;
+import net.pterodactylus.fcp.RequestProgress;
 import net.pterodactylus.fcp.fake.FakeTcpServer;
 import net.pterodactylus.fcp.quelaton.ClientGetCommand.Data;
 
@@ -1542,10 +1545,12 @@ public class DefaultFcpClientTest {
                        );
                }
 
-               private Matcher<List<String>> matchesDirectClientPut() {
+               private Matcher<List<String>> matchesDirectClientPut(String... additionalLines) {
+                       List<String> lines = new ArrayList<>(Arrays.asList("UploadFrom=direct", "DataLength=6", "URI=KSK@foo.txt"));
+                       Arrays.asList(additionalLines).forEach(lines::add);
                        return allOf(
                                hasHead("ClientPut"),
-                               hasParameters(1, 2, "UploadFrom=direct", "DataLength=6", "URI=KSK@foo.txt"),
+                               hasParameters(1, 2, lines.toArray(new String[lines.size()])),
                                hasTail("EndMessage", "Hello")
                        );
                }
@@ -1619,6 +1624,45 @@ public class DefaultFcpClientTest {
                        assertThat(generatedKeys, contains("KSK@foo.txt"));
                }
 
+               @Test
+               public void clientPutSendsNotificationOnProgress() throws InterruptedException, ExecutionException, IOException {
+                       List<RequestProgress> requestProgress = new ArrayList<>();
+                   Future<Optional<Key>> key = fcpClient.clientPut()
+                               .onProgress(requestProgress::add)
+                               .from(new ByteArrayInputStream("Hello\n".getBytes()))
+                               .length(6)
+                               .uri("KSK@foo.txt")
+                               .execute();
+                       connectNode();
+                       readMessage("Hello", () -> matchesDirectClientPut("Verbosity=1"));
+                       replyWithSimpleProgress(1, 2, 3, 4, 5, 6, true, 8);
+                       replyWithSimpleProgress(11, 12, 13, 14, 15, 16, false, 18);
+                       replyWithPutSuccessful(identifier);
+                       assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
+                       assertThat(requestProgress, contains(
+                               isRequestProgress(1, 2, 3, 4, 5, 6, true, 8),
+                               isRequestProgress(11, 12, 13, 14, 15, 16, false, 18)
+                       ));
+               }
+
+               private void replyWithSimpleProgress(
+                       int total, int required, int failed, int fatallyFailed, int succeeded, int lastProgress,
+                       boolean finalizedTotal, int minSuccessFetchBlocks) throws IOException {
+                       fcpServer.writeLine(
+                               "SimpleProgress",
+                               "Identifier=" + identifier,
+                               "Total=" + total,
+                               "Required=" + required,
+                               "Failed=" + failed,
+                               "FatallyFailed=" + fatallyFailed,
+                               "Succeeded=" + succeeded,
+                               "LastProgress=" + lastProgress,
+                               "FinalizedTotal=" + finalizedTotal,
+                               "MinSuccessFetchBlocks=" + minSuccessFetchBlocks,
+                               "EndMessage"
+                       );
+               }
+
        }
 
        public class ClientPutDiskDir {