From: David ‘Bombe’ Roden Date: Tue, 20 Jan 2026 17:58:52 +0000 (+0100) Subject: ✨ Add function to get random name from WoT X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=7b02d59b884cf8ea7da77891ae707a465e57851c;p=jFCPlib.git ✨ Add function to get random name from WoT --- diff --git a/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java b/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java index 76606d2..6df8149 100644 --- a/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java +++ b/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java @@ -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 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 // diff --git a/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java b/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java index 20932a3..d40c10d 100644 --- a/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java +++ b/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java @@ -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