Add method to add wait time for reloading plugins
[jFCPlib.git] / src / test / java / net / pterodactylus / fcp / quelaton / DefaultFcpClientTest.java
index 5d70ff2..d4d67c5 100644 (file)
@@ -1,8 +1,10 @@
 package net.pterodactylus.fcp.quelaton;
 
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.allOf;
 import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.hasItem;
 import static org.hamcrest.Matchers.hasSize;
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.not;
@@ -22,6 +24,7 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
+import java.util.function.Supplier;
 import java.util.stream.Collectors;
 
 import net.pterodactylus.fcp.ARK;
@@ -40,18 +43,21 @@ import net.pterodactylus.fcp.quelaton.ClientGetCommand.Data;
 
 import com.google.common.io.ByteStreams;
 import com.google.common.io.Files;
+import com.nitorcreations.junit.runners.NestedRunner;
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
 import org.hamcrest.TypeSafeDiagnosingMatcher;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 
 /**
  * Unit test for {@link DefaultFcpClient}.
  *
  * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
  */
+@RunWith(NestedRunner.class)
 public class DefaultFcpClientTest {
 
        private static final String INSERT_URI =
@@ -1987,157 +1993,185 @@ public class DefaultFcpClientTest {
                assertThat(newConfigData.get().getCurrent("foo.bar"), is("baz"));
        }
 
-       @Test
-       public void defaultFcpClientCanLoadPluginFromFreenet() 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);
-               assertThat(lines, matchesFcpMessage(
-                       "LoadPlugin",
-                       "Identifier=" + identifier,
-                       "PluginURL=superPlugin",
-                       "URLType=official",
-                       "OfficialSource=freenet",
-                       "EndMessage"
-               ));
-               assertThat(lines, not(contains(startsWith("Store="))));
-               fcpServer.writeLine(
-                       "PluginInfo",
-                       "Identifier=" + identifier,
-                       "PluginName=superPlugin",
-                       "IsTalkable=true",
-                       "LongVersion=1.2.3",
-                       "Version=42",
-                       "OriginUri=superPlugin",
-                       "Started=true",
-                       "EndMessage"
-               );
-               assertThat(pluginInfo.get().get().getPluginName(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().getOriginalURI(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().isTalkable(), is(true));
-               assertThat(pluginInfo.get().get().getVersion(), is("42"));
-               assertThat(pluginInfo.get().get().getLongVersion(), is("1.2.3"));
-               assertThat(pluginInfo.get().get().isStarted(), is(true));
-       }
+       public class PluginCommands {
 
-       @Test
-       public void defaultFcpClientCanLoadPersistentPluginFromFreenet() throws ExecutionException, InterruptedException,
-       IOException {
-               Future<Optional<PluginInfo>> pluginInfo =
-                       fcpClient.loadPlugin().addToConfig().officialFromFreenet("superPlugin").execute();
-               connectNode();
-               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
-               String identifier = extractIdentifier(lines);
-               assertThat(lines, matchesFcpMessage(
-                       "LoadPlugin",
-                       "Identifier=" + identifier,
-                       "PluginURL=superPlugin",
-                       "URLType=official",
-                       "OfficialSource=freenet",
-                       "Store=true",
-                       "EndMessage"
-               ));
-               fcpServer.writeLine(
-                       "PluginInfo",
-                       "Identifier=" + identifier,
-                       "PluginName=superPlugin",
-                       "IsTalkable=true",
-                       "LongVersion=1.2.3",
-                       "Version=42",
-                       "OriginUri=superPlugin",
-                       "Started=true",
-                       "EndMessage"
-               );
-               assertThat(pluginInfo.get().get().getPluginName(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().getOriginalURI(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().isTalkable(), is(true));
-               assertThat(pluginInfo.get().get().getVersion(), is("42"));
-               assertThat(pluginInfo.get().get().getLongVersion(), is("1.2.3"));
-               assertThat(pluginInfo.get().get().isStarted(), is(true));
-       }
+               private List<String> lines;
+               private String identifier;
 
-       @Test
-       public void defaultFcpClientCanLoadPluginFromHttps() throws ExecutionException, InterruptedException,
-       IOException {
-               Future<Optional<PluginInfo>> pluginInfo = fcpClient.loadPlugin().officialFromHttps("superPlugin").execute();
-               connectNode();
-               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
-               String identifier = extractIdentifier(lines);
-               assertThat(lines, matchesFcpMessage(
-                       "LoadPlugin",
-                       "Identifier=" + identifier,
-                       "PluginURL=superPlugin",
-                       "URLType=official",
-                       "OfficialSource=https",
-                       "EndMessage"
-               ));
-               fcpServer.writeLine(
-                       "PluginInfo",
-                       "Identifier=" + identifier,
-                       "PluginName=superPlugin",
-                       "IsTalkable=true",
-                       "LongVersion=1.2.3",
-                       "Version=42",
-                       "OriginUri=superPlugin",
-                       "Started=true",
-                       "EndMessage"
-               );
-               assertThat(pluginInfo.get().get().getPluginName(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().getOriginalURI(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().isTalkable(), is(true));
-               assertThat(pluginInfo.get().get().getVersion(), is("42"));
-               assertThat(pluginInfo.get().get().getLongVersion(), is("1.2.3"));
-               assertThat(pluginInfo.get().get().isStarted(), is(true));
-       }
+               private void connectAndAssert(Supplier<Matcher<List<String>>> requestMatcher)
+               throws InterruptedException, ExecutionException, IOException {
+                       connectNode();
+                       lines = fcpServer.collectUntil(is("EndMessage"));
+                       identifier = extractIdentifier(lines);
+                       assertThat(lines, requestMatcher.get());
+               }
 
-       @Test
-       public void defaultFcpClientCanLoadPluginFromFile() throws ExecutionException, InterruptedException,
-       IOException {
-               Future<Optional<PluginInfo>> pluginInfo = fcpClient.loadPlugin().fromFile("/path/to/plugin.jar").execute();
-               connectNode();
-               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
-               String identifier = extractIdentifier(lines);
-               assertThat(lines, matchesFcpMessage(
-                       "LoadPlugin",
-                       "Identifier=" + identifier,
-                       "PluginURL=/path/to/plugin.jar",
-                       "URLType=file",
-                       "EndMessage"
-               ));
-               fcpServer.writeLine(
-                       "PluginInfo",
-                       "Identifier=" + identifier,
-                       "PluginName=superPlugin",
-                       "IsTalkable=true",
-                       "LongVersion=1.2.3",
-                       "Version=42",
-                       "OriginUri=superPlugin",
-                       "Started=true",
-                       "EndMessage"
-               );
-               assertThat(pluginInfo.get().get().getPluginName(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().getOriginalURI(), is("superPlugin"));
-               assertThat(pluginInfo.get().get().isTalkable(), is(true));
-               assertThat(pluginInfo.get().get().getVersion(), is("42"));
-               assertThat(pluginInfo.get().get().getLongVersion(), is("1.2.3"));
-               assertThat(pluginInfo.get().get().isStarted(), is(true));
-       }
+               private void replyWithPluginInfo() throws IOException {
+                       fcpServer.writeLine(
+                               "PluginInfo",
+                               "Identifier=" + identifier,
+                               "PluginName=superPlugin",
+                               "IsTalkable=true",
+                               "LongVersion=1.2.3",
+                               "Version=42",
+                               "OriginUri=superPlugin",
+                               "Started=true",
+                               "EndMessage"
+                       );
+               }
+
+               private void verifyPluginInfo(Future<Optional<PluginInfo>> pluginInfo)
+               throws InterruptedException, ExecutionException {
+                       assertThat(pluginInfo.get().get().getPluginName(), is("superPlugin"));
+                       assertThat(pluginInfo.get().get().getOriginalURI(), is("superPlugin"));
+                       assertThat(pluginInfo.get().get().isTalkable(), is(true));
+                       assertThat(pluginInfo.get().get().getVersion(), is("42"));
+                       assertThat(pluginInfo.get().get().getLongVersion(), is("1.2.3"));
+                       assertThat(pluginInfo.get().get().isStarted(), is(true));
+               }
+
+               public class LoadPlugin {
+
+                       public class OfficialPlugins {
+
+                               @Test
+                               public void fromFreenet() throws ExecutionException, InterruptedException, IOException {
+                                       Future<Optional<PluginInfo>> pluginInfo =
+                                               fcpClient.loadPlugin().officialFromFreenet("superPlugin").execute();
+                                       connectAndAssert(() -> createMatcherForOfficialSource("freenet"));
+                                       assertThat(lines, not(contains(startsWith("Store="))));
+                                       replyWithPluginInfo();
+                                       verifyPluginInfo(pluginInfo);
+                               }
+
+                               @Test
+                               public void persistentFromFreenet() throws ExecutionException, InterruptedException, IOException {
+                                       Future<Optional<PluginInfo>> pluginInfo =
+                                               fcpClient.loadPlugin().addToConfig().officialFromFreenet("superPlugin").execute();
+                                       connectAndAssert(() -> createMatcherForOfficialSource("freenet"));
+                                       assertThat(lines, hasItem("Store=true"));
+                                       replyWithPluginInfo();
+                                       verifyPluginInfo(pluginInfo);
+                               }
+
+                               @Test
+                               public void fromHttps() throws ExecutionException, InterruptedException, IOException {
+                                       Future<Optional<PluginInfo>> pluginInfo =
+                                               fcpClient.loadPlugin().officialFromHttps("superPlugin").execute();
+                                       connectAndAssert(() -> createMatcherForOfficialSource("https"));
+                                       replyWithPluginInfo();
+                                       verifyPluginInfo(pluginInfo);
+                               }
+
+                               private Matcher<List<String>> createMatcherForOfficialSource(String officialSource) {
+                                       return matchesFcpMessage(
+                                               "LoadPlugin",
+                                               "Identifier=" + identifier,
+                                               "PluginURL=superPlugin",
+                                               "URLType=official",
+                                               "OfficialSource=" + officialSource,
+                                               "EndMessage"
+                                       );
+                               }
+
+                       }
+
+                       public class FromOtherSources {
+
+                               private static final String FILE_PATH = "/path/to/plugin.jar";
+                               private static final String URL = "http://server.com/plugin.jar";
+                               private static final String KEY = "KSK@plugin.jar";
+
+                               @Test
+                               public void fromFile() throws ExecutionException, InterruptedException, IOException {
+                                       Future<Optional<PluginInfo>> pluginInfo = fcpClient.loadPlugin().fromFile(FILE_PATH).execute();
+                                       connectAndAssert(() -> createMatcher("file", FILE_PATH));
+                                       replyWithPluginInfo();
+                                       verifyPluginInfo(pluginInfo);
+                               }
+
+                               @Test
+                               public void fromUrl() throws ExecutionException, InterruptedException, IOException {
+                                       Future<Optional<PluginInfo>> pluginInfo = fcpClient.loadPlugin().fromUrl(URL).execute();
+                                       connectAndAssert(() -> createMatcher("url", URL));
+                                       replyWithPluginInfo();
+                                       verifyPluginInfo(pluginInfo);
+                               }
+
+                               @Test
+                               public void fromFreenet() throws ExecutionException, InterruptedException, IOException {
+                                       Future<Optional<PluginInfo>> pluginInfo = fcpClient.loadPlugin().fromFreenet(KEY).execute();
+                                       connectAndAssert(() -> createMatcher("freenet", KEY));
+                                       replyWithPluginInfo();
+                                       verifyPluginInfo(pluginInfo);
+                               }
+
+                               private Matcher<List<String>> createMatcher(String urlType, String url) {
+                                       return matchesFcpMessage(
+                                               "LoadPlugin",
+                                               "Identifier=" + identifier,
+                                               "PluginURL=" + url,
+                                               "URLType=" + urlType,
+                                               "EndMessage"
+                                       );
+                               }
+
+                       }
+
+                       public class Failed {
+
+                               @Test
+                               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"
+                                       );
+                                       assertThat(pluginInfo.get().isPresent(), is(false));
+                               }
+
+                       }
+
+               }
+
+               public class ReloadPlugin {
+
+                       private static final String CLASS_NAME = "foo.plugin.Plugin";
+
+                       @Test
+                       public void reloadingPluginWorks() throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<PluginInfo>> pluginInfo = fcpClient.reloadPlugin().plugin(CLASS_NAME).execute();
+                               connectAndAssert(() -> matchReloadPluginMessage());
+                               replyWithPluginInfo();
+                               verifyPluginInfo(pluginInfo);
+                       }
+
+                       @Test
+                       public void reloadingPluginWithMaxWaitTimeWorks()
+                       throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<PluginInfo>> pluginInfo =
+                                       fcpClient.reloadPlugin().waitFor(1234).plugin(CLASS_NAME).execute();
+                               connectAndAssert(() -> allOf(matchReloadPluginMessage(), hasItem("MaxWaitTime=1234")));
+                               replyWithPluginInfo();
+                               verifyPluginInfo(pluginInfo);
+                       }
+
+                       private Matcher<List<String>> matchReloadPluginMessage() {
+                               return matchesFcpMessage(
+                                       "ReloadPlugin",
+                                       "Identifier=" + identifier,
+                                       "PluginName=" + CLASS_NAME,
+                                       "EndMessage"
+                               );
+                       }
+
+               }
 
-       @Test
-       public void failedLoadingPluginIsRecognized() 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"
-               );
-               assertThat(pluginInfo.get().isPresent(), is(false));
        }
 
 }