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;
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;
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;
);
}
- 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")
);
}
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 {
@Test
public void commandIsSentCorrectly() throws InterruptedException, ExecutionException, IOException {
Future<Optional<Key>> key = fcpClient.clientPutDiskDir().fromDirectory(folder.getRoot()).uri("CHK@").execute();
- connectAndAssert(() -> matchesFcpMessage(
+ connectAndAssert(this::matchesClientPutDiskDir);
+ fcpServer.writeLine("PutSuccessful", "Identifier=" + identifier, "URI=CHK@abc", "EndMessage");
+ assertThat(key.get().get().getKey(), is("CHK@abc"));
+ }
+
+ @Test
+ public void protocolErrorAbortsCommand() throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<Key>> key = fcpClient.clientPutDiskDir().fromDirectory(folder.getRoot()).uri("CHK@").execute();
+ connectAndAssert(this::matchesClientPutDiskDir);
+ replyWithProtocolError();
+ assertThat(key.get().isPresent(), is(false));
+ }
+
+ private Matcher<List<String>> matchesClientPutDiskDir() {
+ return matchesFcpMessage(
"ClientPutDiskDir",
"Identifier=" + identifier,
"URI=CHK@",
"Filename=" + folder.getRoot().getPath()
- ));
- fcpServer.writeLine("PutSuccessful", "Identifier=" + identifier, "URI=CHK@abc", "EndMessage");
- assertThat(key.get().get().getKey(), is("CHK@abc"));
+ );
}
}