From: David ‘Bombe’ Roden Date: Mon, 15 Dec 2025 21:01:54 +0000 (+0100) Subject: ✅ Add matchers for Identity X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=7965e158f824629e10f7c48c70974198c4393fdd;p=jFCPlib.git ✅ Add matchers for Identity --- diff --git a/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java b/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java index 829a50d..f4d39b2 100644 --- a/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java +++ b/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java @@ -44,6 +44,15 @@ public class IdentityMatchers { }; } + /** + * Creates a {@link Matcher} for {@link Identity}s that verifies + * their {@link Identity#getContexts() contexts} against the given + * {@link Matcher}. + * + * @param contextsMatcher The {@link Matcher} to verify the + * {@link Identity#getContexts() contexts} against + * @return A {@link Matcher} for {@link Identity}s + */ public static Matcher hasContexts(Matcher> contextsMatcher) { return new TypeSafeDiagnosingMatcher() { @Override @@ -57,7 +66,7 @@ public class IdentityMatchers { @Override public void describeTo(Description description) { - description.appendText("has contexts ").appendValue(contextsMatcher); + description.appendText("has contexts ").appendDescriptionOf(contextsMatcher); } }; } @@ -116,7 +125,7 @@ public class IdentityMatchers { * @param id The ID to match * @return A {@link Matcher} for {@link Identity Identities} */ - public static Matcher hasId(String id) { + public static Matcher hasId(String id) { return hasId(equalTo(id)); } @@ -128,7 +137,7 @@ public class IdentityMatchers { * @param idMatcher The matcher to verify the ID against * @return A {@link Matcher} for {@link Identity Identities} */ - public static Matcher hasId(Matcher idMatcher) { + public static Matcher hasId(Matcher idMatcher) { return new TypeSafeDiagnosingMatcher() { @Override protected boolean matchesSafely(Identity identity, Description mismatchDescription) { @@ -154,7 +163,7 @@ public class IdentityMatchers { * @param nickname The nickname to match * @return A {@link Matcher} for {@link Identity Identities} */ - public static Matcher hasNickname(String nickname) { + public static Matcher hasNickname(String nickname) { return hasNickname(equalTo(nickname)); } @@ -166,7 +175,7 @@ public class IdentityMatchers { * @param nicknameMatcher The matcher to verify the nickname against * @return A {@link Matcher} for {@link Identity Identities} */ - public static Matcher hasNickname(Matcher nicknameMatcher) { + public static Matcher hasNickname(Matcher nicknameMatcher) { return new TypeSafeDiagnosingMatcher() { @Override protected boolean matchesSafely(Identity identity, Description mismatchDescription) { @@ -184,4 +193,110 @@ public class IdentityMatchers { }; } + /** + * Creates a {@link Matcher} for {@link Identity identities} which + * verifies the {@link Identity#getRequestUri() request URI} against + * the given request URI. + * + * @param requestUri The {@link Identity#getRequestUri() request URI} + * to match + * @return A {@link Matcher} for an {@link Identity} + */ + public static Matcher hasRequestUri(String requestUri) { + return hasRequestUri(equalTo(requestUri)); + } + + /** + * Creates a {@link Matcher} for {@link Identity identities} which + * verifies the {@link Identity#getRequestUri() request URI} against + * the given {@link Matcher}. + * + * @param requestUriMatcher The {@link Matcher} for the + * {@link Identity#getRequestUri() request URI} + * @return A {@link Matcher} for an {@link Identity} + */ + public static Matcher hasRequestUri(Matcher requestUriMatcher) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(Identity identity, Description mismatchDescription) { + if (!requestUriMatcher.matches(identity.getRequestUri())) { + mismatchDescription.appendText("was ").appendValue(identity.getRequestUri()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("has request URI matching ").appendDescriptionOf(requestUriMatcher); + } + }; + } + + /** + * Creates a {@link Matcher} for {@link OwnIdentity}s which + * verifies the {@link OwnIdentity#getInsertUri() insert URI} against + * the given insert URI. + * + * @param insertUri The insert URI to verify the + * {@link OwnIdentity#getInsertUri() insert URI} against + * @return A {@link Matcher} for {@link OwnIdentity}s + */ + public static Matcher hasInsertUri(String insertUri) { + return hasInsertUri(equalTo(insertUri)); + } + + /** + * Creates a {@link Matcher} for {@link OwnIdentity}s which + * verifies the {@link OwnIdentity#getInsertUri() insert URI} against + * the given {@link Matcher}. + * + * @param insertUriMatcher The {@link Matcher} for the + * {@link OwnIdentity#getInsertUri() insert URI} + * @return A {@link Matcher} for {@link OwnIdentity}s + */ + public static Matcher hasInsertUri(Matcher insertUriMatcher) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(OwnIdentity ownIdentity, Description mismatchDescription) { + if (!insertUriMatcher.matches(ownIdentity.getInsertUri())) { + mismatchDescription.appendText("has insert URI ").appendValue(ownIdentity.getInsertUri()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("has insert URI ").appendDescriptionOf(insertUriMatcher); + } + }; + } + + /** + * Creates a {@link Matcher} for {@link Identity}s which verifies the + * {@link Identity#getContexts() contexts} contain the context with the + * given name. + * + * @param context The name of the context to verify + * @return A {@link Matcher} for {@link Identity}s + */ + public static Matcher hasContext(String context) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(Identity identity, Description mismatchDescription) { + if (!identity.getContexts().contains(context)) { + mismatchDescription.appendText("no context matches"); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("has context ").appendValue(context); + } + }; + } + } diff --git a/src/test/java/net/pterodactylus/fcp/test/IdentityMatchersTest.java b/src/test/java/net/pterodactylus/fcp/test/IdentityMatchersTest.java index 6d6e93d..cb79bd4 100644 --- a/src/test/java/net/pterodactylus/fcp/test/IdentityMatchersTest.java +++ b/src/test/java/net/pterodactylus/fcp/test/IdentityMatchersTest.java @@ -1,5 +1,6 @@ package net.pterodactylus.fcp.test; +import java.util.stream.Stream; import net.pterodactylus.fcp.plugin.Identity; import net.pterodactylus.fcp.plugin.OwnIdentity; import org.hamcrest.Description; @@ -8,11 +9,19 @@ import org.junit.Test; import static java.util.Collections.emptyList; import static java.util.Collections.emptyMap; +import static java.util.stream.Collectors.toSet; +import static net.pterodactylus.fcp.test.IdentityMatchers.hasContext; +import static net.pterodactylus.fcp.test.IdentityMatchers.hasContexts; import static net.pterodactylus.fcp.test.IdentityMatchers.hasId; +import static net.pterodactylus.fcp.test.IdentityMatchers.hasInsertUri; import static net.pterodactylus.fcp.test.IdentityMatchers.hasNickname; +import static net.pterodactylus.fcp.test.IdentityMatchers.hasRequestUri; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.any; +import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; public class IdentityMatchersTest { @@ -124,6 +133,163 @@ public class IdentityMatchersTest { assertThat(hasNickname("nickname").matches(ownIdentity), equalTo(true)); } + @Test + public void hasRequestUriWithMatcherDoesNotMatchIfRequestUriIsDifferent() { + assertThat(hasRequestUri(equalTo("abc")).matches(identity), equalTo(false)); + } + + @Test + public void hasRequestUriWithMatcherDescribesMismatchCorrectly() { + hasRequestUri(equalTo("abc")).describeMismatch(identity, description); + assertThat(description.toString(), equalTo("was \"request-uri\"")); + } + + @Test + public void hasRequestUriWithMatcherMatchesIfRequestUriIsTheSame() { + assertThat(hasRequestUri(equalTo("request-uri")).matches(identity), equalTo(true)); + } + + @Test + public void hasRequestUriWithMatcherDescribesItselfCorrectly() { + hasRequestUri(equalTo("abc")).describeTo(description); + assertThat(description.toString(), equalTo("has request URI matching \"abc\"")); + } + + @Test + public void hasRequestUriWithMatcherMatchesOwnIdentityAsWell() { + assertThat(hasRequestUri(equalTo("request-uri")).matches(ownIdentity), equalTo(true)); + } + + @Test + public void hasRequestUriWithStringDoesNotMatchIfRequestUriIsDifferent() { + assertThat(hasRequestUri("abc").matches(identity), equalTo(false)); + } + + @Test + public void hasRequestUriWithStringDescribesMismatchCorrectly() { + hasRequestUri("abc").describeMismatch(identity, description); + assertThat(description.toString(), equalTo("was \"request-uri\"")); + } + + @Test + public void hasRequestUriWithStringMatchesIfRequestUriIsTheSame() { + assertThat(hasRequestUri("request-uri").matches(identity), equalTo(true)); + } + + @Test + public void hasRequestUriWithStringDescribesItselfCorrectly() { + hasRequestUri("abc").describeTo(description); + assertThat(description.toString(), equalTo("has request URI matching \"abc\"")); + } + + @Test + public void hasRequestUriWithStringMatchesOwnIdentityAsWell() { + assertThat(hasRequestUri("request-uri").matches(ownIdentity), equalTo(true)); + } + + @Test + public void hasInsertUriWithMatcherDoesNotMatchIfInsertUriIsDifferent() { + assertThat(hasInsertUri(equalTo("abc")).matches(ownIdentity), equalTo(false)); + } + + @Test + public void hasInsertUriWithMatcherDescribesMismatchCorrectly() { + hasInsertUri(equalTo("abc")).describeMismatch(ownIdentity, description); + assertThat(description.toString(), equalTo("has insert URI \"insert-uri\"")); + } + + @Test + public void hasInsertUriWithMatcherMatchesIfInsertUriIsTheSame() { + assertThat(hasInsertUri(equalTo("insert-uri")).matches(ownIdentity), equalTo(true)); + } + + @Test + public void hasInsertUriWithMatcherDescribesItselfCorrectly() { + hasInsertUri(equalTo("abc")).describeTo(description); + assertThat(description.toString(), equalTo("has insert URI \"abc\"")); + } + + @Test + public void hasInsertUriWithStringDoesNotMatchIfInsertUriIsDifferent() { + assertThat(hasInsertUri("abc").matches(ownIdentity), equalTo(false)); + } + + @Test + public void hasInsertUriWithStringDescribesMismatchCorrectly() { + hasInsertUri("abc").describeMismatch(ownIdentity, description); + assertThat(description.toString(), equalTo("has insert URI \"insert-uri\"")); + } + + @Test + public void hasInsertUriWithStringMatchesIfInsertUriIsTheSame() { + assertThat(hasInsertUri("insert-uri").matches(ownIdentity), equalTo(true)); + } + + @Test + public void hasInsertUriWithStringDescribesItselfCorrectly() { + hasInsertUri("abc").describeTo(description); + assertThat(description.toString(), equalTo("has insert URI \"abc\"")); + } + + @Test + public void hasContextWithStringDoesNotMatchIfContextDoesNotExist() { + assertThat(hasContext("abc").matches(identity), equalTo(false)); + } + + @Test + public void hasContextWithStringDescribesMismatchCorrectly() { + hasContext("abc").describeMismatch(identity, description); + assertThat(description.toString(), equalTo("no context matches")); + } + + @Test + public void hasContextWithStringMatchesIfContextExists() { + Identity identity = new Identity("", "", "", Stream.of("test-context").collect(toSet()), emptyMap()); + assertThat(hasContext("test-context").matches(identity), equalTo(true)); + } + + @Test + public void hasContextWithStringDescribesItselfCorrectly() { + hasContext("test-context").describeTo(description); + assertThat(description.toString(), equalTo("has context \"test-context\"")); + } + + @Test + public void hasContextWithStringCanBeUsedOnAnOwnIdentity() { + OwnIdentity ownIdentity = new OwnIdentity("", "", "", "", Stream.of("test-context").collect(toSet()), emptyMap()); + assertThat(hasContext("test-context").matches(ownIdentity), equalTo(true)); + } + + @Test + public void hasContextsWithMatcherDoesNotMatchIfContextsAreDifferent() { + Identity identity = new Identity("", "", "", Stream.of("test-context").collect(toSet()), emptyMap()); + assertThat(hasContexts(allOf(hasItem("abc"), hasItem("def"))).matches(identity), equalTo(false)); + } + + @Test + public void hasContextsWithMatcherDescribesMismatchCorrectly() { + Identity identity = new Identity("", "", "", Stream.of("test-context").collect(toSet()), emptyMap()); + hasContexts(allOf(hasItem("abc"), hasItem("def"))).describeMismatch(identity, description); + assertThat(description.toString(), equalTo("a collection containing \"abc\" mismatches were: [was \"test-context\"]")); + } + + @Test + public void hasContextsWithMatcherMatchesIfContextsAreTheSame() { + Identity identity = new Identity("", "", "", Stream.of("abc", "def").collect(toSet()), emptyMap()); + assertThat(hasContexts(allOf(hasItem("abc"), hasItem("def"))).matches(identity), equalTo(true)); + } + + @Test + public void hasContextsWithMatcherDescribesItselfCorrectly() { + hasContexts(allOf(hasItem("abc"), hasItem("def"))).describeTo(description); + assertThat(description.toString(), equalTo("has contexts (a collection containing \"abc\" and a collection containing \"def\")")); + } + + @Test + public void hasContextsWithMatcherMatchesOwnIdentity() { + assertThat(hasContexts(empty()).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();