Add subscription cancelling
[jFCPlib.git] / src / test / java / net / pterodactylus / fcp / quelaton / DefaultFcpClientTest.java
index 75667b3..c716ed5 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;
@@ -18,10 +20,15 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Supplier;
 import java.util.stream.Collectors;
 
 import net.pterodactylus.fcp.ARK;
@@ -1990,100 +1997,26 @@ public class DefaultFcpClientTest {
                assertThat(newConfigData.get().getCurrent("foo.bar"), is("baz"));
        }
 
-       public class LoadPlugin {
+       private List<String> lines;
+       private String identifier;
 
-               @Test
-               public void fromFreenet() 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="))));
-                       replyWithPluginInfo(identifier);
-                       verifyPluginInfo(pluginInfo);
-               }
-
-               @Test
-               public void persistentFromFreenet() 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"
-                       ));
-                       replyWithPluginInfo(identifier);
-                       verifyPluginInfo(pluginInfo);
-               }
+       private void connectAndAssert(Supplier<Matcher<List<String>>> requestMatcher)
+       throws InterruptedException, ExecutionException, IOException {
+               connectNode();
+               readMessage(requestMatcher);
+       }
 
-               @Test
-               public void fromHttps() 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"
-                       ));
-                       replyWithPluginInfo(identifier);
-                       verifyPluginInfo(pluginInfo);
-               }
+       private void readMessage(Supplier<Matcher<List<String>>> requestMatcher) throws IOException {
+               lines = fcpServer.collectUntil(is("EndMessage"));
+               identifier = extractIdentifier(lines);
+               assertThat(lines, requestMatcher.get());
+       }
 
-               @Test
-               public void fromFile() 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"
-                       ));
-                       replyWithPluginInfo(identifier);
-                       verifyPluginInfo(pluginInfo);
-               }
+       public class PluginCommands {
 
-               @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));
-               }
+               private static final String CLASS_NAME = "foo.plugin.Plugin";
 
-               private void replyWithPluginInfo(String identifier) throws IOException {
+               private void replyWithPluginInfo() throws IOException {
                        fcpServer.writeLine(
                                "PluginInfo",
                                "Identifier=" + identifier,
@@ -2107,6 +2040,334 @@ public class DefaultFcpClientTest {
                        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();
+                                       connectAndAssert(() -> matchesFcpMessage("LoadPlugin", "EndMessage"));
+                                       replyWithProtocolError();
+                                       assertThat(pluginInfo.get().isPresent(), is(false));
+                               }
+
+                       }
+
+               }
+
+               private void replyWithProtocolError() throws IOException {
+                       fcpServer.writeLine(
+                               "ProtocolError",
+                               "Identifier=" + identifier,
+                               "EndMessage"
+                       );
+               }
+
+               public class ReloadPlugin {
+
+                       @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);
+                       }
+
+                       @Test
+                       public void reloadingPluginWithPurgeWorks()
+                       throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<PluginInfo>> pluginInfo =
+                                       fcpClient.reloadPlugin().purge().plugin(CLASS_NAME).execute();
+                               connectAndAssert(() -> allOf(matchReloadPluginMessage(), hasItem("Purge=true")));
+                               replyWithPluginInfo();
+                               verifyPluginInfo(pluginInfo);
+                       }
+
+                       @Test
+                       public void reloadingPluginWithStoreWorks()
+                       throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<PluginInfo>> pluginInfo =
+                                       fcpClient.reloadPlugin().addToConfig().plugin(CLASS_NAME).execute();
+                               connectAndAssert(() -> allOf(matchReloadPluginMessage(), hasItem("Store=true")));
+                               replyWithPluginInfo();
+                               verifyPluginInfo(pluginInfo);
+                       }
+
+                       private Matcher<List<String>> matchReloadPluginMessage() {
+                               return matchesFcpMessage(
+                                       "ReloadPlugin",
+                                       "Identifier=" + identifier,
+                                       "PluginName=" + CLASS_NAME,
+                                       "EndMessage"
+                               );
+                       }
+
+               }
+
+               public class RemovePlugin {
+
+                       @Test
+                       public void removingPluginWorks() throws InterruptedException, ExecutionException, IOException {
+                               Future<Boolean> pluginRemoved = fcpClient.removePlugin().plugin(CLASS_NAME).execute();
+                               connectAndAssert(() -> matchPluginRemovedMessage());
+                               replyWithPluginRemoved();
+                               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",
+                                       "Identifier=" + identifier,
+                                       "PluginName=" + CLASS_NAME,
+                                       "EndMessage"
+                               );
+                       }
+
+                       private Matcher<List<String>> matchPluginRemovedMessage() {
+                               return matchesFcpMessage(
+                                       "RemovePlugin",
+                                       "Identifier=" + identifier,
+                                       "PluginName=" + CLASS_NAME,
+                                       "EndMessage"
+                               );
+                       }
+
+               }
+
+               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"
+                               );
+                       }
+
+               }
+
+       }
+
+       public class UskSubscriptionCommands {
+
+               private static final String URI = "USK@some,uri/file.txt";
+
+               @Test
+               public void subscriptionWorks() throws InterruptedException, ExecutionException, IOException {
+                       Future<Optional<UskSubscription>> uskSubscription = fcpClient.subscribeUsk().uri(URI).execute();
+                       connectAndAssert(() -> matchesFcpMessage("SubscribeUSK", "URI=" + URI, "EndMessage"));
+                       replyWithSubscribed();
+                       assertThat(uskSubscription.get().get().getUri(), is(URI));
+                       AtomicInteger edition = new AtomicInteger();
+                       CountDownLatch updated = new CountDownLatch(2);
+                       uskSubscription.get().get().onUpdate(e -> {
+                               edition.set(e);
+                               updated.countDown();
+                       });
+                       sendUpdateNotification(23);
+                       sendUpdateNotification(24);
+                       assertThat("updated in time", updated.await(5, TimeUnit.SECONDS), is(true));
+                       assertThat(edition.get(), is(24));
+               }
+
+               @Test
+               public void subscriptionUpdatesMultipleTimes() throws InterruptedException, ExecutionException, IOException {
+                       Future<Optional<UskSubscription>> uskSubscription = fcpClient.subscribeUsk().uri(URI).execute();
+                       connectAndAssert(() -> matchesFcpMessage("SubscribeUSK", "URI=" + URI, "EndMessage"));
+                       replyWithSubscribed();
+                       assertThat(uskSubscription.get().get().getUri(), is(URI));
+                       AtomicInteger edition = new AtomicInteger();
+                       CountDownLatch updated = new CountDownLatch(2);
+                       uskSubscription.get().get().onUpdate(e -> {
+                               edition.set(e);
+                               updated.countDown();
+                       });
+                       uskSubscription.get().get().onUpdate(e -> updated.countDown());
+                       sendUpdateNotification(23);
+                       assertThat("updated in time", updated.await(5, TimeUnit.SECONDS), is(true));
+                       assertThat(edition.get(), is(23));
+               }
+
+               @Test
+               public void subscriptionCanBeCancelled() throws InterruptedException, ExecutionException, IOException {
+                       Future<Optional<UskSubscription>> uskSubscription = fcpClient.subscribeUsk().uri(URI).execute();
+                       connectAndAssert(() -> matchesFcpMessage("SubscribeUSK", "URI=" + URI, "EndMessage"));
+                       replyWithSubscribed();
+                       assertThat(uskSubscription.get().get().getUri(), is(URI));
+                       AtomicBoolean updated = new AtomicBoolean();
+                       uskSubscription.get().get().onUpdate(e -> updated.set(true));
+                       uskSubscription.get().get().cancel();
+                       readMessage(() -> matchesFcpMessage("UnsubscribeUSK", "Identifier=" + identifier, "EndMessage"));
+                       sendUpdateNotification(23);
+                       assertThat(updated.get(), is(false));
+               }
+
+               private void replyWithSubscribed() throws IOException {
+                       fcpServer.writeLine(
+                               "SubscribedUSK",
+                               "Identifier=" + identifier,
+                               "URI=" + URI,
+                               "DontPoll=false",
+                               "EndMessage"
+                       );
+               }
+
+               private void sendUpdateNotification(int edition, String... additionalLines) throws IOException {
+                       fcpServer.writeLine(
+                               "SubscribedUSKUpdate",
+                               "Identifier=" + identifier,
+                               "URI=" + URI,
+                               "Edition=" + edition
+                       );
+                       fcpServer.writeLine(additionalLines);
+                       fcpServer.writeLine("EndMessage");
+               }
+
        }
 
 }