From: David ‘Bombe’ Roden Date: Wed, 8 Jan 2025 10:53:26 +0000 (+0100) Subject: ✅ Add first test for WebOfTrustPlugin.getIdentityTrust() X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=c6bd5f86815f30290468a6b384c3aa1ce4be01ad;p=jFCPlib.git ✅ Add first test for WebOfTrustPlugin.getIdentityTrust() --- diff --git a/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java b/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java index 7ec12a3..6079fa3 100644 --- a/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java +++ b/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java @@ -179,25 +179,25 @@ public class WebOfTrustPlugin { * if an FCP error occurs */ public CalculatedTrust getIdentityTrust(OwnIdentity ownIdentity, String identifier) throws IOException, FcpException { - Map replies = fcpClient.sendPluginMessage(webOfTrustPluginName, createParameters("Message", "GetIdentity", "TreeOwner", ownIdentity.getIdentifier(), "Identity", identifier)); + Map replies = fcpClient.sendPluginMessage(webOfTrustPluginName, createParameters("Message", "GetIdentity", "Truster", ownIdentity.getIdentifier(), "Identity", identifier)); if (!replies.get("Message").equals("Identity")) { throw new FcpException("WebOfTrust Plugin did not reply with “Identity” message!"); } Byte trust = null; try { - trust = Byte.valueOf(replies.get("Trust")); + trust = Byte.valueOf(replies.get("Trusts.0.Value")); } catch (NumberFormatException nfe1) { /* ignore. */ } Integer score = null; try { - score = Integer.valueOf(replies.get("Score")); + score = Integer.valueOf(replies.get("Scores.0.Value")); } catch (NumberFormatException nfe1) { /* ignore. */ } Integer rank = null; try { - rank = Integer.valueOf(replies.get("Rank")); + rank = Integer.valueOf(replies.get("Scores.0.Rank")); } catch (NumberFormatException nfe1) { /* ignore. */ } diff --git a/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java b/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java index 8139cab..8b55ade 100644 --- a/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java +++ b/src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java @@ -18,6 +18,8 @@ import java.util.Set; import java.util.function.BiConsumer; import java.util.function.Function; +import static java.util.Collections.emptyMap; +import static java.util.Collections.emptySet; import static net.pterodactylus.fcp.test.IdentityMatchers.hasContexts; import static net.pterodactylus.fcp.test.IdentityMatchers.hasProperties; import static net.pterodactylus.fcp.test.IdentityMatchers.isOwnIdentity; @@ -223,6 +225,56 @@ public class WebOfTrustPluginTest { assertThrows(FcpException.class, webOfTrustPlugin::getOwnIdentites); } + @Test + public void getIdentityTrustSendsCorrectMessage() throws Exception { + TestFcpConnection fcpConnection = createConnectionThatDeliversIdentityTrustAndScore(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + OwnIdentity ownIdentity = new OwnIdentity("own-id", "Own ID", "own-pub", "own-priv", emptySet(), emptyMap()); + webOfTrustPlugin.getIdentityTrust(ownIdentity, "other-id"); + assertThat(fcpConnection.sentMessages.get(0), allOf( + isNamed(equalTo("FCPPluginMessage")), + hasField("Identifier", not(nullValue())), + hasField("PluginName", equalTo("plugins.WebOfTrust.WebOfTrust")), + hasField("Param.Message", equalTo("GetIdentity")), + hasField("Param.Truster", equalTo("own-id")), + hasField("Param.Identity", equalTo("other-id")) + )); + } + + @Test + public void getIdentityTrustParsesTrustCorrectly() throws Exception { + TestFcpConnection fcpConnection = createConnectionThatDeliversIdentityTrustAndScore(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + OwnIdentity ownIdentity = new OwnIdentity("own-id", "Own ID", "own-pub", "own-priv", emptySet(), emptyMap()); + CalculatedTrust calculatedTrust = webOfTrustPlugin.getIdentityTrust(ownIdentity, "other-id"); + assertThat(calculatedTrust.getTrust(), equalTo((byte) 56)); + assertThat(calculatedTrust.getScore(), equalTo(56)); + assertThat(calculatedTrust.getRank(), equalTo(1)); + } + + private TestFcpConnection createConnectionThatDeliversIdentityTrustAndScore() { + return createFcpConnection(message -> (listener, connection) -> { + Map fields = createWebOfTrustReplyFields("Identity", true, + "Identities.Amount", "1", + "Identities.0.Type", "Identity", "Identities.0.Nickname", "Nick Name", "Identities.0.ReqestURI", "nick-pub", "Identities.0.ID", "nick-id", "Identities.0.VersionID", "0-0", "Identities.0.PublishesTrustList", "true", "Identities.0.CurrentEditionFetchState", "Fetched", + "Identities.0.Contexts.0.Name", "context-1", "Identities.0.Contexts.1.Name", "context-2", "Identities.0.Contexts.Amount", "2", + "Identities.0.Properties.0.Name", "prop1", "Identities.0.Properties.0.Value", "value1", "Identities.0.Properties.1.Name", "prop2", "Identities.0.Properties.1.Value", "value2", "Identities.0.Properties.Amount", "2", + "Trusts.Amount", "1", "Trusts.0.Truster", "own-id", "Trusts.0.Trustee", "nick-id", "Trusts.0.Value", "56", "Trusts.0.Comment", "some trust", "Trusts.0.TrusterEdition", "123", "Trusts.0.VersionID", "1-1", + "Scores.Amount", "1", "Scores.0.Truster", "own-id", "Scores.0.Trustee", "nick-id", "Scores.0.Capacity", "40", "Scores.0.Rank", "1", "Scores.0.Value", "56", "Scores.0.VersionID", "2-2" + ); + FcpMessage replyMessage = createPluginReply(message, fields); + listener.receivedFCPPluginReply(connection, new FCPPluginReply(replyMessage, null)); + }); + } + + @Test + public void getIdentityTrustFailsWhenDifferentReplyIsSentByPlugin() { + FcpConnection fcpConnection = createConnectionThatSendsOtherMessage(); + WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection); + OwnIdentity ownIdentity = new OwnIdentity("own-id", "Own ID", "own-pub", "own-priv", emptySet(), emptyMap()); + assertThrows(FcpException.class, () -> webOfTrustPlugin.getIdentityTrust(ownIdentity, "other-id")); + } + private FcpConnection createConnectionThatSendsOtherMessage() { return createFcpConnection(message -> (listener, connection) -> { Map fields = createWebOfTrustReplyFields("OtherMessage", true);