Add method to load plugin from official HTTPS
[jFCPlib.git] / src / test / java / net / pterodactylus / fcp / quelaton / DefaultFcpClientTest.java
index 88f25a8..f54298f 100644 (file)
@@ -33,6 +33,7 @@ import net.pterodactylus.fcp.NodeData;
 import net.pterodactylus.fcp.NodeRef;
 import net.pterodactylus.fcp.Peer;
 import net.pterodactylus.fcp.PeerNote;
+import net.pterodactylus.fcp.PluginInfo;
 import net.pterodactylus.fcp.Priority;
 import net.pterodactylus.fcp.fake.FakeTcpServer;
 import net.pterodactylus.fcp.quelaton.ClientGetCommand.Data;
@@ -58,13 +59,13 @@ public class DefaultFcpClientTest {
        private static final String REQUEST_URI =
                "SSK@wtbgd2loNcJCXvtQVOftl2tuWBomDQHfqS6ytpPRhfw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQACAAE/";
 
-       private static int threadCounter = 0;
+       private int threadCounter = 0;
+       private final ExecutorService threadPool =
+               Executors.newCachedThreadPool(r -> new Thread(r, "Test-Thread-" + threadCounter++));
        private final FakeTcpServer fcpServer;
        private final DefaultFcpClient fcpClient;
 
        public DefaultFcpClientTest() throws IOException {
-               ExecutorService threadPool =
-                       Executors.newCachedThreadPool(r -> new Thread(r, "Test-Thread-" + threadCounter++));
                fcpServer = new FakeTcpServer(threadPool);
                fcpClient = new DefaultFcpClient(threadPool, "localhost", fcpServer.getPort(), () -> "Test");
        }
@@ -72,6 +73,7 @@ public class DefaultFcpClientTest {
        @After
        public void tearDown() throws IOException {
                fcpServer.close();
+               threadPool.shutdown();
        }
 
        @Test(expected = ExecutionException.class)
@@ -1964,4 +1966,145 @@ public class DefaultFcpClientTest {
                assertThat(configData.get().getDataType("foo"), is("number"));
        }
 
+       @Test
+       public void defaultFcpClientCanModifyConfigData() throws InterruptedException, ExecutionException, IOException {
+               Future<ConfigData> newConfigData = fcpClient.modifyConfig().set("foo.bar").to("baz").execute();
+               connectNode();
+               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+               String identifier = extractIdentifier(lines);
+               assertThat(lines, matchesFcpMessage(
+                       "ModifyConfig",
+                       "Identifier=" + identifier,
+                       "foo.bar=baz",
+                       "EndMessage"
+               ));
+               fcpServer.writeLine(
+                       "ConfigData",
+                       "Identifier=" + identifier,
+                       "current.foo.bar=baz",
+                       "EndMessage"
+               );
+               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));
+       }
+
+       @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));
+       }
+
+       @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));
+       }
+
+       @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));
+       }
+
 }