From: David ‘Bombe’ Roden Date: Mon, 19 Jan 2026 19:15:44 +0000 (+0100) Subject: ✅ Add test for getTrustees() and fix bug in reply parser X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=1a3d4faf9e9eb357bec486f6cc1485b87dd93fe1;p=jFCPlib.git ✅ Add test for getTrustees() and fix bug in reply parser --- diff --git a/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java b/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java index 7ae0532..ceebf75 100644 --- a/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java +++ b/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java @@ -275,7 +275,7 @@ public class WebOfTrustPlugin { throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!"); } Map identityTrusts = new HashMap<>(); - for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) { + for (int identityIndex = 0; replies.containsKey("Identity" + identityIndex); identityIndex++) { String identifier = replies.get("Identity" + identityIndex); String nickname = replies.get("Nickname" + identityIndex); String requestUri = replies.get("RequestURI" + identityIndex); diff --git a/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java b/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java index 7d5dab3..4bd1c2c 100644 --- a/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java +++ b/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java @@ -520,6 +520,54 @@ public class WebOfTrustPluginTest { } + public static class GetTrusteesTests extends Common { + + @Test + public void getTrusteesSendsCorrectCommand() throws Exception { + TestFcpConnection fcpConnection = createConnectionThatSendsTrustees(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + Identity identity = new Identity("test-owner", "Test Owner", "request-uri", emptySet(), emptyMap()); + webOfTrustPlugin.getTrustees(identity, "test"); + assertThat(fcpConnection.sentMessages.get(0), allOf( + isNamed(equalTo("FCPPluginMessage")), + hasField("Identifier", notNullValue()), + hasField("PluginName", equalTo("plugins.WebOfTrust.WebOfTrust")), + hasField("Param.Message", equalTo("GetTrustees")), + hasField("Param.Identity", equalTo("test-owner")), + hasField("Param.Context", equalTo("test")) + )); + } + + @Test + public void getTrusteesThrowsExceptionWhenDifferentReplyIsSentByPlugin() { + FcpConnection fcpConnection = createConnectionThatSendsOtherMessage(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + Identity identity = new Identity("test-owner", "Test Owner", "request-uri", emptySet(), emptyMap()); + assertThrows(FcpException.class, () -> webOfTrustPlugin.getTrustees(identity, "test")); + } + + @Test + public void getTrusteesParsesTheResponseCorrectly() throws Exception { + TestFcpConnection fcpConnection = createConnectionThatSendsTrustees(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + Identity identity = new Identity("test-owner", "Test Owner", "request-uri", emptySet(), emptyMap()); + Map trustees = webOfTrustPlugin.getTrustees(identity, "test"); + assertThat(trustees, allOf( + hasEntry(hasId("id0"), allOf(IdentityTrustMatchers.hasTrust(123), hasComment("comment0"))), + hasEntry(hasId("id1"), allOf(IdentityTrustMatchers.hasTrust(99), hasComment("comment1"))) + )); + } + + private TestFcpConnection createConnectionThatSendsTrustees() { + return createConnection("Identities", + entries("Identity0", "id0", "Nickname0", "nickname0", "RequestURI0", "request-uri0", "Value0", "123", "Comment0", "comment0", "Properties0.Property0.Name", "property1", "Properties0.Property0.Value", "value1"), + entries("Identity1", "id1", "Nickname1", "nickname1", "RequestURI1", "request-uri1", "Value1", "99", "Comment1", "comment1", "Contexts1.Context0", "context1", "Contexts1.Context1", "context2"), + entries("Amount", "2") + ); + } + + } + private static class Common { @SafeVarargs