✅ Add test for getTrustees() and fix bug in reply parser
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 19 Jan 2026 19:15:44 +0000 (20:15 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 19 Jan 2026 19:15:44 +0000 (20:15 +0100)
src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java
src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java

index 7ae0532..ceebf75 100644 (file)
@@ -275,7 +275,7 @@ public class WebOfTrustPlugin {
                        throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
                }
                Map<Identity, IdentityTrust> 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);
index 7d5dab3..4bd1c2c 100644 (file)
@@ -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<Identity, IdentityTrust> 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