From: David ‘Bombe’ Roden Date: Fri, 19 Dec 2025 15:20:20 +0000 (+0100) Subject: ✅ Add test for getTrusters() and fix bug in reply parser X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=d5f530c46b39abf826789ff1296be2e28b081537;p=jFCPlib.git ✅ Add test for getTrusters() 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 23cc507..7ae0532 100644 --- a/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java +++ b/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java @@ -245,7 +245,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 e19510f..7d5dab3 100644 --- a/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java +++ b/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java @@ -17,6 +17,7 @@ import net.pterodactylus.fcp.FcpListener; import net.pterodactylus.fcp.FcpMessage; import net.pterodactylus.fcp.highlevel.FcpClient; import net.pterodactylus.fcp.highlevel.FcpException; +import net.pterodactylus.fcp.test.IdentityTrustMatchers; import net.pterodactylus.fcp.test.TestFcpConnection; import org.junit.Rule; import org.junit.Test; @@ -32,6 +33,7 @@ import static net.pterodactylus.fcp.test.IdentityMatchers.hasInsertUri; import static net.pterodactylus.fcp.test.IdentityMatchers.hasNickname; import static net.pterodactylus.fcp.test.IdentityMatchers.hasProperties; import static net.pterodactylus.fcp.test.IdentityMatchers.hasRequestUri; +import static net.pterodactylus.fcp.test.IdentityTrustMatchers.hasComment; import static net.pterodactylus.fcp.test.Matchers.hasField; import static net.pterodactylus.fcp.test.Matchers.isNamed; import static net.pterodactylus.fcp.test.TrustMatchers.hasNoRank; @@ -470,6 +472,54 @@ public class WebOfTrustPluginTest { } + public static class GetTrustersTests extends Common { + + @Test + public void getTrustersSendsCorrectCommand() throws Exception { + TestFcpConnection fcpConnection = createConnectionThatSendsTrusters(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + Identity identity = new Identity("test-owner", "Test Owner", "request-uri", emptySet(), emptyMap()); + webOfTrustPlugin.getTrusters(identity, "test"); + assertThat(fcpConnection.sentMessages.get(0), allOf( + isNamed(equalTo("FCPPluginMessage")), + hasField("Identifier", notNullValue()), + hasField("PluginName", equalTo("plugins.WebOfTrust.WebOfTrust")), + hasField("Param.Message", equalTo("GetTrusters")), + hasField("Param.Identity", equalTo("test-owner")), + hasField("Param.Context", equalTo("test")) + )); + } + + @Test + public void getTrustersThrowsExceptionWhenDifferentReplyIsSentByPlugin() { + FcpConnection fcpConnection = createConnectionThatSendsOtherMessage(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + Identity identity = new Identity("test-owner", "Test Owner", "request-uri", emptySet(), emptyMap()); + assertThrows(FcpException.class, () -> webOfTrustPlugin.getTrusters(identity, "test")); + } + + @Test + public void getTrustersParsesResponseCorrectly() throws Exception { + TestFcpConnection fcpConnection = createConnectionThatSendsTrusters(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + Identity identity = new Identity("test-owner", "Test Owner", "request-uri", emptySet(), emptyMap()); + Map trusters = webOfTrustPlugin.getTrusters(identity, "test"); + assertThat(trusters, allOf( + aMapWithSize(2), + hasEntry(hasId("id0"), allOf(IdentityTrustMatchers.hasTrust(123), hasComment("comment0"))), + hasEntry(hasId("id1"), allOf(IdentityTrustMatchers.hasTrust(99), hasComment("comment1"))) + )); + } + + private TestFcpConnection createConnectionThatSendsTrusters() { + return createConnection("Identities", + entries("Identity0", "id0", "Nickname0", "nickname0", "RequestURI0", "request-uri0", "Value0", "123", "Comment0", "comment0"), + entries("Identity1", "id1", "Nickname1", "nickname1", "RequestURI1", "request-uri1", "Value1", "99", "Comment1", "comment1") + ); + } + + } + private static class Common { @SafeVarargs