Add test for protocol error on GetPluginInfo
[jFCPlib.git] / src / test / java / net / pterodactylus / fcp / quelaton / DefaultFcpClientTest.java
index fafee42..557350b 100644 (file)
@@ -2126,14 +2126,8 @@ public class DefaultFcpClientTest {
                                public void failedLoad() throws ExecutionException, InterruptedException, IOException {
                                        Future<Optional<PluginInfo>> pluginInfo =
                                                fcpClient.loadPlugin().officialFromFreenet("superPlugin").execute();
-                                       connectNode();
-                                       List<String> lines = fcpServer.collectUntil(is("EndMessage"));
-                                       String identifier = extractIdentifier(lines);
-                                       fcpServer.writeLine(
-                                               "ProtocolError",
-                                               "Identifier=" + identifier,
-                                               "EndMessage"
-                                       );
+                                       connectAndAssert(() -> matchesFcpMessage("LoadPlugin", "EndMessage"));
+                                       replyWithProtocolError();
                                        assertThat(pluginInfo.get().isPresent(), is(false));
                                }
 
@@ -2141,6 +2135,14 @@ public class DefaultFcpClientTest {
 
                }
 
+               private void replyWithProtocolError() throws IOException {
+                       fcpServer.writeLine(
+                               "ProtocolError",
+                               "Identifier=" + identifier,
+                               "EndMessage"
+                       );
+               }
+
                public class ReloadPlugin {
 
                        @Test
@@ -2202,6 +2204,24 @@ public class DefaultFcpClientTest {
                                assertThat(pluginRemoved.get(), is(true));
                        }
 
+                       @Test
+                       public void removingPluginWithMaxWaitTimeWorks()
+                       throws InterruptedException, ExecutionException, IOException {
+                               Future<Boolean> pluginRemoved = fcpClient.removePlugin().waitFor(1234).plugin(CLASS_NAME).execute();
+                               connectAndAssert(() -> allOf(matchPluginRemovedMessage(), hasItem("MaxWaitTime=1234")));
+                               replyWithPluginRemoved();
+                               assertThat(pluginRemoved.get(), is(true));
+                       }
+
+                       @Test
+                       public void removingPluginWithPurgeWorks()
+                       throws InterruptedException, ExecutionException, IOException {
+                               Future<Boolean> pluginRemoved = fcpClient.removePlugin().purge().plugin(CLASS_NAME).execute();
+                               connectAndAssert(() -> allOf(matchPluginRemovedMessage(), hasItem("Purge=true")));
+                               replyWithPluginRemoved();
+                               assertThat(pluginRemoved.get(), is(true));
+                       }
+
                        private void replyWithPluginRemoved() throws IOException {
                                fcpServer.writeLine(
                                        "PluginRemoved",
@@ -2222,6 +2242,47 @@ public class DefaultFcpClientTest {
 
                }
 
+               public class GetPluginInfo {
+
+                       @Test
+                       public void gettingPluginInfoWorks() throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<PluginInfo>> pluginInfo = fcpClient.getPluginInfo().plugin(CLASS_NAME).execute();
+                               connectAndAssert(() -> matchGetPluginInfoMessage());
+                               replyWithPluginInfo();
+                               verifyPluginInfo(pluginInfo);
+                       }
+
+                       @Test
+                       public void gettingPluginInfoWithDetailsWorks()
+                       throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<PluginInfo>> pluginInfo =
+                                       fcpClient.getPluginInfo().detailed().plugin(CLASS_NAME).execute();
+                               connectAndAssert(() -> allOf(matchGetPluginInfoMessage(), hasItem("Detailed=true")));
+                               replyWithPluginInfo();
+                               verifyPluginInfo(pluginInfo);
+                       }
+
+                       @Test
+                       public void protocolErrorIsRecognizedAsFailure()
+                       throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<PluginInfo>> pluginInfo =
+                                       fcpClient.getPluginInfo().detailed().plugin(CLASS_NAME).execute();
+                               connectAndAssert(() -> allOf(matchGetPluginInfoMessage(), hasItem("Detailed=true")));
+                               replyWithProtocolError();
+                               assertThat(pluginInfo.get(), is(Optional.empty()));
+                       }
+
+                       private Matcher<List<String>> matchGetPluginInfoMessage() {
+                               return matchesFcpMessage(
+                                       "GetPluginInfo",
+                                       "Identifier=" + identifier,
+                                       "PluginName=" + CLASS_NAME,
+                                       "EndMessage"
+                               );
+                       }
+
+               }
+
        }
 
 }