Add basic ClientPutDiskDir command implementation
[jFCPlib.git] / src / test / java / net / pterodactylus / fcp / quelaton / DefaultFcpClientTest.java
index f7823d6..0adb977 100644 (file)
@@ -55,7 +55,10 @@ import org.hamcrest.Matchers;
 import org.hamcrest.TypeSafeDiagnosingMatcher;
 import org.junit.After;
 import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Ignore;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 import org.junit.runner.RunWith;
 
 /**
@@ -215,6 +218,14 @@ public class DefaultFcpClientTest {
                assertThat(lines, requestMatcher.get());
        }
 
+       private void replyWithProtocolError() throws IOException {
+               fcpServer.writeLine(
+                       "ProtocolError",
+                       "Identifier=" + identifier,
+                       "EndMessage"
+               );
+       }
+
        public class ConnectionsAndKeyPairs {
 
                public class Connections {
@@ -438,6 +449,14 @@ public class DefaultFcpClientTest {
                                        assertThat(peer.get().get().getIdentity(), is("id1"));
                                }
 
+                               @Test
+                               public void protocolErrorEndsCommand() throws InterruptedException, ExecutionException, IOException {
+                                       Future<Optional<Peer>> peer = fcpClient.addPeer().fromFile(new File("/tmp/ref.txt")).execute();
+                                       connectAndAssert(() -> allOf(matchesAddPeer(), hasItem("File=/tmp/ref.txt")));
+                                       replyWithProtocolError();
+                                       assertThat(peer.get().isPresent(), is(false));
+                               }
+
                                private NodeRef createNodeRef() {
                                        NodeRef nodeRef = new NodeRef();
                                        nodeRef.setIdentity("id1");
@@ -974,14 +993,6 @@ public class DefaultFcpClientTest {
 
                }
 
-               private void replyWithProtocolError() throws IOException {
-                       fcpServer.writeLine(
-                               "ProtocolError",
-                               "Identifier=" + identifier,
-                               "EndMessage"
-                       );
-               }
-
                public class ReloadPlugin {
 
                        @Test
@@ -1022,6 +1033,15 @@ public class DefaultFcpClientTest {
                                verifyPluginInfo(pluginInfo);
                        }
 
+                       @Test
+                       public void protocolErrorIsRecognizedAsFailure()
+                       throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<PluginInfo>> pluginInfo = fcpClient.reloadPlugin().plugin(CLASS_NAME).execute();
+                               connectAndAssert(() -> matchReloadPluginMessage());
+                               replyWithProtocolError();
+                               assertThat(pluginInfo.get().isPresent(), is(false));
+                       }
+
                        private Matcher<List<String>> matchReloadPluginMessage() {
                                return matchesFcpMessage(
                                        "ReloadPlugin",
@@ -1601,6 +1621,39 @@ public class DefaultFcpClientTest {
 
        }
 
+       public class ClientPutDiskDir {
+
+               private final TemporaryFolder folder = new TemporaryFolder();
+
+               @Before
+               public void setup() throws IOException {
+                       folder.create();
+                       Files.write("file1\n", folder.newFile("file1.txt"), StandardCharsets.UTF_8);
+                       Files.write("file2\n", folder.newFile("file2.txt"), StandardCharsets.UTF_8);
+                       File directory = folder.newFolder("dir");
+                       Files.write("file3\n", new File(directory, "file3.txt"), StandardCharsets.UTF_8);
+               }
+
+               @After
+               public void removeFolder() {
+                   folder.delete();
+               }
+
+               @Test
+               public void commandIsSentCorrectly() throws InterruptedException, ExecutionException, IOException {
+                       Future<Optional<Key>> key = fcpClient.clientPutDiskDir().fromDirectory(folder.getRoot()).uri("CHK@").execute();
+                       connectAndAssert(() -> 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"));
+               }
+
+       }
+
        public class ConfigCommand {
 
                public class GetConfig {