✅ Add test for WebOfTrustPlugin.addIdentity()
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 8 Jan 2025 20:33:41 +0000 (21:33 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 8 Jan 2025 20:33:41 +0000 (21:33 +0100)
src/test/java/net/pterodactylus/fcp/plugin/WebOfTrustPluginTest.java
src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java

index a615b58..f63c6cf 100644 (file)
@@ -28,6 +28,7 @@ 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.isIdentity;
 import static net.pterodactylus.fcp.test.IdentityMatchers.isOwnIdentity;
 import static net.pterodactylus.fcp.test.Matchers.hasField;
 import static net.pterodactylus.fcp.test.Matchers.isNamed;
@@ -325,6 +326,54 @@ public class WebOfTrustPluginTest {
 
        }
 
+       public static class AddIdentityTests extends Common {
+
+               @Test
+               public void addIdentitySendsCorrectMessage() throws Exception {
+                       TestFcpConnection fcpConnection = createConnectionThatConfirmsAddedIdentityWithNickname();
+                       WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+                       webOfTrustPlugin.addIdentity("USK@foo,bar/baz/123");
+                       assertThat(fcpConnection.sentMessages.get(0), allOf(
+                                       isNamed(equalTo("FCPPluginMessage")),
+                                       hasField("Identifier", not(nullValue())),
+                                       hasField("PluginName", equalTo("plugins.WebOfTrust.WebOfTrust")),
+                                       hasField("Param.RequestURI", equalTo("USK@foo,bar/baz/123"))
+                       ));
+               }
+
+               @Test
+               public void addIdentityParsesIdentityWithNicknameCorrectly() throws Exception {
+                       TestFcpConnection fcpConnection = createConnectionThatConfirmsAddedIdentityWithNickname();
+                       WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+                       Identity addedIdentity = webOfTrustPlugin.addIdentity("USK@foo,bar/baz/123");
+                       assertThat(addedIdentity, isIdentity(equalTo("new-id"), equalTo("new-nick"), equalTo("USK@foo,bar/baz/123")));
+               }
+
+               @Test
+               public void addIdentityParsesIdentityWithoutNicknameCorrectly() throws Exception {
+                       TestFcpConnection fcpConnection = createConnectionThatConfirmsAddedIdentityWithoutNickname();
+                       WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+                       Identity addedIdentity = webOfTrustPlugin.addIdentity("USK@foo,bar/baz/123");
+                       assertThat(addedIdentity, isIdentity(equalTo("new-id"), nullValue(), equalTo("USK@foo,bar/baz/123")));
+               }
+
+               @Test
+               public void addIdentityFailsWhenDifferentReplyIsSentByPlugin() {
+                       FcpConnection fcpConnection = createConnectionThatSendsOtherMessage();
+                       WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+                       assertThrows(FcpException.class, () -> webOfTrustPlugin.addIdentity("USK@foo,bar/baz/123"));
+               }
+
+               private TestFcpConnection createConnectionThatConfirmsAddedIdentityWithNickname() {
+                       return createConnection("IdentityAdded", entries("ID", "new-id", "Nickname", "new-nick"));
+               }
+
+               private TestFcpConnection createConnectionThatConfirmsAddedIdentityWithoutNickname() {
+                       return createConnection("IdentityAdded", entries("ID", "new-id"));
+               }
+
+       }
+
        private static class Common {
 
                @SafeVarargs
index 310b6cb..982e154 100644 (file)
@@ -79,4 +79,32 @@ public class IdentityMatchers {
                };
        }
 
+       public static Matcher<Identity> isIdentity(Matcher<? super String> identifier, Matcher<? super String> nickname, Matcher<? super String> requestUri) {
+               return new TypeSafeDiagnosingMatcher<Identity>(Identity.class) {
+                       @Override
+                       protected boolean matchesSafely(Identity identity, Description mismatchDescription) {
+                               if (!identifier.matches(identity.getIdentifier())) {
+                                       identifier.describeMismatch(identity.getIdentifier(), mismatchDescription);
+                                       return false;
+                               }
+                               if (!nickname.matches(identity.getNickname())) {
+                                       nickname.describeMismatch(identity.getNickname(), mismatchDescription);
+                                       return false;
+                               }
+                               if (!requestUri.matches(identity.getRequestUri())) {
+                                       requestUri.describeMismatch(identity.getRequestUri(), mismatchDescription);
+                                       return false;
+                               }
+                               return true;
+                       }
+
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText("is identity (").appendValue(identifier).appendText(", nicknamed ").appendValue(nickname)
+                                               .appendText(", public uri ").appendValue(requestUri).appendText(")");
+                       }
+
+               };
+       }
+
 }