From 7b02d59b884cf8ea7da77891ae707a465e57851c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 20 Jan 2026 18:58:52 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=A8=20Add=20function=20to=20get=20random?= =?utf8?q?=20name=20from=20WoT?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/fcp/plugin/WebOfTrustPlugin.java | 15 +++++++++ .../fcp/plugin/WebOfTrustPluginTest.java | 37 ++++++++++++++++++++++ 2 files changed, 52 insertions(+) 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 -- 2.7.4