From: David ‘Bombe’ Roden Date: Thu, 9 Jul 2015 05:01:21 +0000 (+0200) Subject: Add test for TestDDA sequence X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=12eb8641be8a71957856ec76c9522d80394e5cd6;p=jFCPlib.git Add test for TestDDA sequence --- diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java index 43d6c99..f0a6706 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java @@ -3,6 +3,7 @@ package net.pterodactylus.fcp.quelaton; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.util.Objects; import java.util.Optional; import java.util.concurrent.ExecutorService; @@ -13,8 +14,13 @@ import java.util.concurrent.atomic.AtomicReference; import net.pterodactylus.fcp.ClientPut; import net.pterodactylus.fcp.FcpMessage; import net.pterodactylus.fcp.Key; +import net.pterodactylus.fcp.ProtocolError; import net.pterodactylus.fcp.PutFailed; import net.pterodactylus.fcp.PutSuccessful; +import net.pterodactylus.fcp.TestDDAComplete; +import net.pterodactylus.fcp.TestDDAReply; +import net.pterodactylus.fcp.TestDDARequest; +import net.pterodactylus.fcp.TestDDAResponse; import net.pterodactylus.fcp.UploadFrom; import com.google.common.util.concurrent.ListenableFuture; @@ -112,7 +118,9 @@ class ClientPutCommandImpl implements ClientPutCommand { private class ClientPutReplySequence extends FcpReplySequence> { + private final AtomicReference originalClientPut = new AtomicReference<>(); private final AtomicReference identifier = new AtomicReference<>(); + private final AtomicReference directory = new AtomicReference<>(); private final AtomicReference finalKey = new AtomicReference<>(); private final AtomicBoolean putFinished = new AtomicBoolean(); @@ -132,7 +140,12 @@ class ClientPutCommandImpl implements ClientPutCommand { @Override public ListenableFuture> send(FcpMessage fcpMessage) throws IOException { + originalClientPut.set(fcpMessage); identifier.set(fcpMessage.getField("Identifier")); + String filename = fcpMessage.getField("Filename"); + if (filename != null) { + directory.set(new File(filename).getParent()); + } return super.send(fcpMessage); } @@ -152,9 +165,37 @@ class ClientPutCommandImpl implements ClientPutCommand { } @Override + protected void consumeProtocolError(ProtocolError protocolError) { + if (protocolError.getIdentifier().equals(identifier.get()) && (protocolError.getCode() == 25)) { + sendMessage(new TestDDARequest(directory.get(), true, false)); + } + } + + @Override + protected void consumeTestDDAReply(TestDDAReply testDDAReply) { + if (testDDAReply.getDirectory().equals(directory.get())) { + try { + String readContent = Files.readAllLines(new File(testDDAReply.getReadFilename()).toPath()).get(0); + sendMessage(new TestDDAResponse(directory.get(), readContent)); + } catch (IOException e) { + e.printStackTrace(); + sendMessage(new TestDDAResponse(directory.get(), "failed-to-read")); + } + } + } + + @Override + protected void consumeTestDDAComplete(TestDDAComplete testDDAComplete) { + if (testDDAComplete.getDirectory().equals(directory.get())) { + sendMessage(originalClientPut.get()); + } + } + + @Override protected void consumeConnectionClosed(Throwable throwable) { putFinished.set(true); } + } } diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index 16c6190..f26fff4 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -21,6 +21,7 @@ import net.pterodactylus.fcp.fake.FakeTcpServer; import net.pterodactylus.fcp.quelaton.ClientGetCommand.Data; import com.google.common.io.ByteStreams; +import com.google.common.io.Files; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeDiagnosingMatcher; @@ -380,4 +381,58 @@ public class DefaultFcpClientTest { matchesFcpMessage("ClientPut", "UploadFrom=disk", "URI=KSK@foo.txt", "Filename=/tmp/data.txt")); } + @Test + public void clientPutWithFileCanCompleteTestDdaSequence() + throws IOException, ExecutionException, InterruptedException { + File tempFile = createTempFile(); + fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).key(new Key("KSK@foo.txt")); + connectNode(); + List lines = fcpServer.collectUntil(is("EndMessage")); + String identifier = extractIdentifier(lines); + fcpServer.writeLine( + "ProtocolError", + "Identifier=" + identifier, + "Code=25", + "EndMessage" + ); + lines = fcpServer.collectUntil(is("EndMessage")); + assertThat(lines, matchesFcpMessage( + "TestDDARequest", + "Directory=" + tempFile.getParent(), + "WantReadDirectory=true", + "WantWriteDirectory=false", + "EndMessage" + )); + fcpServer.writeLine( + "TestDDAReply", + "Directory=" + tempFile.getParent(), + "ReadFilename=" + tempFile, + "EndMessage" + ); + lines = fcpServer.collectUntil(is("EndMessage")); + assertThat(lines, matchesFcpMessage( + "TestDDAResponse", + "Directory=" + tempFile.getParent(), + "ReadContent=test-content", + "EndMessage" + )); + fcpServer.writeLine( + "TestDDAComplete", + "Directory=" + tempFile.getParent(), + "ReadDirectoryAllowed=true", + "EndMessage" + ); + lines = fcpServer.collectUntil(is("EndMessage")); + assertThat(lines, + matchesFcpMessage("ClientPut", "UploadFrom=disk", "URI=KSK@foo.txt", + "Filename=" + new File(tempFile.getParent(), "test.dat"))); + } + + private File createTempFile() throws IOException { + File tempFile = File.createTempFile("test-dda-", ".dat"); + tempFile.deleteOnExit(); + Files.write("test-content", tempFile, StandardCharsets.UTF_8); + return tempFile; + } + }