✨ Add function to get random name from WoT
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Tue, 20 Jan 2026 17:58:52 +0000 (18:58 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Tue, 20 Jan 2026 17:58:52 +0000 (18:58 +0100)
src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java
src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java

index 76606d2..6df8149 100644 (file)
@@ -408,6 +408,21 @@ public class WebOfTrustPlugin {
                }
        }
 
+       /**
+        * Creates a random name.
+        *
+        * @return A random name
+        * @throws IOException if an I/O error occurs
+        * @throws FcpException if an FCP error occurs
+        */
+       public String getRandomName() throws IOException, FcpException {
+               Map<String, String> replies = fcpClient.sendPluginMessage(webOfTrustPluginName, createParameters("Message", "RandomName"));
+               if (!replies.get("Message").equals("Name")) {
+                       throw new FcpException("WebOfTrust Plugin did not reply with “Name” message!");
+               }
+               return replies.get("Name");
+       }
+
        //
        // PRIVATE METHODS
        //
index 20932a3..d40c10d 100644 (file)
@@ -46,6 +46,7 @@ import static net.pterodactylus.fcp.test.TrustMatchers.hasTrust;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.aMapWithSize;
 import static org.hamcrest.Matchers.allOf;
+import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.containsInAnyOrder;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.hasEntry;
@@ -773,6 +774,42 @@ public class WebOfTrustPluginTest {
 
        }
 
+       public static class GetRandomNameTests extends Common {
+
+               @Test
+               public void getRandomNameSendsTheCorrectCommand() throws Exception {
+                       TestFcpConnection fcpConnection = createConnectionThatSendsName();
+                       WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+                       webOfTrustPlugin.getRandomName();
+                       assertThat(fcpConnection.sentMessages, contains(allOf(
+                                       isNamed(equalTo("FCPPluginMessage")),
+                                       hasField("Identifier", notNullValue()),
+                                       hasField("PluginName", equalTo("plugins.WebOfTrust.WebOfTrust")),
+                                       hasField("Param.Message", equalTo("RandomName"))
+                       )));
+               }
+
+               @Test
+               public void getRandomNameReturnsCorrectName() throws Exception {
+                       TestFcpConnection fcpConnection = createConnectionThatSendsName();
+                       WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+                       String name = webOfTrustPlugin.getRandomName();
+                       assertThat(name, equalTo("R. A. Ndom"));
+               }
+
+               @Test
+               public void getRandomNameThrowsExceptionWhenDifferentReplyIsSentByPlugin() {
+                       FcpConnection fcpConnection = createConnectionThatSendsOtherMessage();
+                       WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+                       assertThrows(FcpException.class, () -> webOfTrustPlugin.getRandomName());
+               }
+
+               private TestFcpConnection createConnectionThatSendsName() {
+                       return createConnection("Name", entries("Name", "R. A. Ndom"));
+               }
+
+       }
+
        private static class Common {
 
                @SafeVarargs