✅ Add matcher for an Identity’s ID
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 12 Dec 2025 16:50:00 +0000 (17:50 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 12 Dec 2025 22:03:01 +0000 (23:03 +0100)
src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java
src/test/java/net/pterodactylus/fcp/test/IdentityMatchersTest.java [new file with mode: 0644]

index 982e154..829a50d 100644 (file)
@@ -1,13 +1,14 @@
 package net.pterodactylus.fcp.test;
 
+import java.util.Map;
+import java.util.Set;
 import net.pterodactylus.fcp.plugin.Identity;
 import net.pterodactylus.fcp.plugin.OwnIdentity;
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
 import org.hamcrest.TypeSafeDiagnosingMatcher;
 
-import java.util.Map;
-import java.util.Set;
+import static org.hamcrest.Matchers.equalTo;
 
 public class IdentityMatchers {
 
@@ -107,4 +108,80 @@ public class IdentityMatchers {
                };
        }
 
+       /**
+        * Creates a {@link Matcher} for {@link Identity Identities} which
+        * verifies that  the {@link Identity#getIdentifier() ID} of the
+        * {@link Identity} matches the given ID.
+        *
+        * @param id The ID to match
+        * @return A {@link Matcher} for {@link Identity Identities}
+        */
+       public static Matcher<? extends Identity> hasId(String id) {
+               return hasId(equalTo(id));
+       }
+
+       /**
+        * Creates a {@link Matcher} for {@link Identity Identities} which
+        * verifies the {@link Identity#getIdentifier() ID} of the
+        * {@link Identity} against the given {@link Matcher}.
+        *
+        * @param idMatcher The matcher to verify the ID against
+        * @return A {@link Matcher} for {@link Identity Identities}
+        */
+       public static Matcher<? extends Identity> hasId(Matcher<? super String> idMatcher) {
+               return new TypeSafeDiagnosingMatcher<Identity>() {
+                       @Override
+                       protected boolean matchesSafely(Identity identity, Description mismatchDescription) {
+                               if (!idMatcher.matches(identity.getIdentifier())) {
+                                       mismatchDescription.appendText("was ").appendValue(identity.getIdentifier());
+                                       return false;
+                               }
+                               return true;
+                       }
+
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText("has ID matching ").appendDescriptionOf(idMatcher);
+                       }
+               };
+       }
+
+       /**
+        * Creates a {@link Matcher} for {@link Identity Identities} which
+        * verifies the {@link Identity#getNickname() nickname} of the
+        * {@link Identity} against the given nickname.
+        *
+        * @param nickname The nickname to match
+        * @return A {@link Matcher} for {@link Identity Identities}
+        */
+       public static Matcher<? extends Identity> hasNickname(String nickname) {
+               return hasNickname(equalTo(nickname));
+       }
+
+       /**
+        * Creates a {@link Matcher} for {@link Identity Identities} which
+        * verifies the {@link Identity#getNickname() nickname} of the
+        * {@link Identity} against the given {@link Matcher}.
+        *
+        * @param nicknameMatcher The matcher to verify the nickname against
+        * @return A {@link Matcher} for {@link Identity Identities}
+        */
+       public static Matcher<? extends Identity> hasNickname(Matcher<? super String> nicknameMatcher) {
+               return new TypeSafeDiagnosingMatcher<Identity>() {
+                       @Override
+                       protected boolean matchesSafely(Identity identity, Description mismatchDescription) {
+                               if (!nicknameMatcher.matches(identity.getNickname())) {
+                                       mismatchDescription.appendText("was ").appendValue(identity.getNickname());
+                                       return false;
+                               }
+                               return true;
+                       }
+
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText("has nickname matching ").appendDescriptionOf(nicknameMatcher);
+                       }
+               };
+       }
+
 }
diff --git a/src/test/java/net/pterodactylus/fcp/test/IdentityMatchersTest.java b/src/test/java/net/pterodactylus/fcp/test/IdentityMatchersTest.java
new file mode 100644 (file)
index 0000000..6d6e93d
--- /dev/null
@@ -0,0 +1,131 @@
+package net.pterodactylus.fcp.test;
+
+import net.pterodactylus.fcp.plugin.Identity;
+import net.pterodactylus.fcp.plugin.OwnIdentity;
+import org.hamcrest.Description;
+import org.hamcrest.StringDescription;
+import org.junit.Test;
+
+import static java.util.Collections.emptyList;
+import static java.util.Collections.emptyMap;
+import static net.pterodactylus.fcp.test.IdentityMatchers.hasId;
+import static net.pterodactylus.fcp.test.IdentityMatchers.hasNickname;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.any;
+import static org.hamcrest.Matchers.equalTo;
+
+public class IdentityMatchersTest {
+
+       @Test
+       public void hasIdWithMatcherDoesNotMatchIfIdIsDifferent() {
+               assertThat(hasId(equalTo("abc")).matches(identity), equalTo(false));
+       }
+
+       @Test
+       public void hasIdWithMatcherDescribesMismatchCorrectly() {
+               hasId(equalTo("abc")).describeMismatch(identity, description);
+               assertThat(description.toString(), equalTo("was \"id\""));
+       }
+
+       @Test
+       public void hasIdWithMatcherMatchesIfIdMatches() {
+               assertThat(hasId(equalTo("id")).matches(identity), equalTo(true));
+       }
+
+       @Test
+       public void hasIdWithMatcherDescribesItselfCorrectly() {
+               hasId(any(String.class)).describeTo(description);
+               assertThat(description.toString(), equalTo("has ID matching an instance of java.lang.String"));
+       }
+
+       @Test
+       public void hasIdWithMatcherMatchesOwnIdentityAsWell() {
+               assertThat(hasId(equalTo("id")).matches(ownIdentity), equalTo(true));
+       }
+
+       @Test
+       public void hasIdWithStringDoesNotMatchIfIdIsDifferent() {
+               assertThat(hasId("abc").matches(identity), equalTo(false));
+       }
+
+       @Test
+       public void hasIdWithStringDescribesMismatchCorrectly() {
+               hasId("abc").describeMismatch(identity, description);
+               assertThat(description.toString(), equalTo("was \"id\""));
+       }
+
+       @Test
+       public void hasIdWithStringMatchesIfIdMatches() {
+               assertThat(hasId("id").matches(identity), equalTo(true));
+       }
+
+       @Test
+       public void hasIdWithStringDescribesItselfCorrectly() {
+               hasId("abc").describeTo(description);
+               assertThat(description.toString(), equalTo("has ID matching \"abc\""));
+       }
+
+       @Test
+       public void hasIdWithStringMatchesOwnIdentityAsWell() {
+               assertThat(hasId("id").matches(ownIdentity), equalTo(true));
+       }
+
+       @Test
+       public void hasNicknameWithMatcherDoesNotMatchIfNicknameIsDifferent() {
+               assertThat(hasNickname(equalTo("foo")).matches(identity), equalTo(false));
+       }
+
+       @Test
+       public void hasNicknameWithMatcherDescribesMismatchCorrectly() {
+               hasNickname(equalTo("foo")).describeMismatch(identity, description);
+               assertThat(description.toString(), equalTo("was \"nickname\""));
+       }
+
+       @Test
+       public void hasNicknameWithMatcherMatchesIfNicknameMatches() {
+               assertThat(hasNickname(equalTo("nickname")).matches(identity), equalTo(true));
+       }
+
+       @Test
+       public void hasNicknameWithMatcherDescribesItselfCorrectly() {
+               hasNickname(equalTo("nickname")).describeTo(description);
+               assertThat(description.toString(), equalTo("has nickname matching \"nickname\""));
+       }
+
+       @Test
+       public void hasNicknameWithMatcherMatchesOwnIdentityAsWell() {
+               assertThat(hasNickname(equalTo("nickname")).matches(ownIdentity), equalTo(true));
+       }
+
+       @Test
+       public void hasNicknameWithStringDoesNotMatchIfNicknameIsDifferent() {
+               assertThat(hasNickname("foo").matches(identity), equalTo(false));
+       }
+
+       @Test
+       public void hasNicknameWithStringDescribesMismatchCorrectly() {
+               hasNickname("foo").describeMismatch(identity, description);
+               assertThat(description.toString(), equalTo("was \"nickname\""));
+       }
+
+       @Test
+       public void hasNicknameWithStringMatchesIfNicknameMatches() {
+               assertThat(hasNickname("nickname").matches(identity), equalTo(true));
+       }
+
+       @Test
+       public void hasNicknameWithStringDescribesItselfCorrectly() {
+               hasNickname("nickname").describeTo(description);
+               assertThat(description.toString(), equalTo("has nickname matching \"nickname\""));
+       }
+
+       @Test
+       public void hasNicknameWithStringMatchesOwnIdentityAsWell() {
+               assertThat(hasNickname("nickname").matches(ownIdentity), equalTo(true));
+       }
+
+       private final Identity identity = new Identity("id", "nickname", "request-uri", emptyList(), emptyMap());
+       private final OwnIdentity ownIdentity = new OwnIdentity("id", "nickname", "request-uri", "insert-uri", emptyList(), emptyMap());
+       private final Description description = new StringDescription();
+
+}