From 6c2fff556178ba34cfb0639135da3203e5b86daa Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 8 Jan 2025 08:29:20 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=9A=A7=20Add=20contexts=20to=20WoT=20ident?= =?utf8?q?ities?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/fcp/plugin/Identity.java | 14 +++++++++++++- .../net/pterodactylus/fcp/plugin/OwnIdentity.java | 18 ++++++++++++++++-- .../pterodactylus/fcp/plugin/WebOfTrustPlugin.java | 14 ++++++++------ .../pterodactylus/fcp/test/IdentityMatchers.java | 21 +++++++++++++++++++++ 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/pterodactylus/fcp/plugin/Identity.java b/src/main/java/net/pterodactylus/fcp/plugin/Identity.java index f05e95f..f04f37b 100644 --- a/src/main/java/net/pterodactylus/fcp/plugin/Identity.java +++ b/src/main/java/net/pterodactylus/fcp/plugin/Identity.java @@ -17,6 +17,11 @@ package net.pterodactylus.fcp.plugin; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + /** * Wrapper around a web-of-trust identity. * @@ -33,6 +38,8 @@ public class Identity { /** The identity’s request URI. */ private final String requestUri; + private final Set contexts = new HashSet<>(); + /** * Creates a new identity. * @@ -43,10 +50,11 @@ public class Identity { * @param requestUri * The request URI of the identity */ - public Identity(String identifier, String nickname, String requestUri) { + public Identity(String identifier, String nickname, String requestUri, Collection contexts) { this.identifier = identifier; this.nickname = nickname; this.requestUri = requestUri; + this.contexts.addAll(contexts); } /** @@ -76,6 +84,10 @@ public class Identity { return requestUri; } + public Set getContexts() { + return Collections.unmodifiableSet(contexts); + } + /** * {@inheritDoc} */ diff --git a/src/main/java/net/pterodactylus/fcp/plugin/OwnIdentity.java b/src/main/java/net/pterodactylus/fcp/plugin/OwnIdentity.java index 8e66ed2..35c19a8 100644 --- a/src/main/java/net/pterodactylus/fcp/plugin/OwnIdentity.java +++ b/src/main/java/net/pterodactylus/fcp/plugin/OwnIdentity.java @@ -17,6 +17,9 @@ package net.pterodactylus.fcp.plugin; +import java.util.Collection; +import java.util.StringJoiner; + /** * Wrapper around a web-of-trust own identity. * @@ -39,8 +42,8 @@ public class OwnIdentity extends Identity { * @param insertUri * The insert URI of the identity */ - public OwnIdentity(String identifier, String nickname, String requestUri, String insertUri) { - super(identifier, nickname, requestUri); + public OwnIdentity(String identifier, String nickname, String requestUri, String insertUri, Collection contexts) { + super(identifier, nickname, requestUri, contexts); this.insertUri = insertUri; } @@ -53,4 +56,15 @@ public class OwnIdentity extends Identity { return insertUri; } + @Override + public String toString() { + return new StringJoiner(", ", OwnIdentity.class.getSimpleName() + "[", "]") + .add("identifier='" + getIdentifier() + "'") + .add("nickname='" + getNickname() + "'") + .add("requestUri='" + getRequestUri() + "'") + .add("insertUri='" + insertUri + "'") + .add("contexts='" + getContexts() + "'") + .toString(); + } + } diff --git a/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java b/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java index d118c46..287769a 100644 --- a/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java +++ b/src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java @@ -26,6 +26,8 @@ import java.util.Set; import net.pterodactylus.fcp.highlevel.FcpClient; import net.pterodactylus.fcp.highlevel.FcpException; +import static java.util.Collections.emptySet; + /** * Simplifies handling of the web-of-trust plugin. * @@ -88,7 +90,7 @@ public class WebOfTrustPlugin { String identifier = replies.get("ID"); String newRequestUri = replies.get("RequestURI"); String newInsertUri = replies.get("InsertURI"); - return new OwnIdentity(identifier, nickname, newRequestUri, newInsertUri); + return new OwnIdentity(identifier, nickname, newRequestUri, newInsertUri, emptySet()); } /** @@ -130,7 +132,7 @@ public class WebOfTrustPlugin { String nickname = replies.get("Nickname" + identityIndex); String requestUri = replies.get("RequestURI" + identityIndex); String insertUri = replies.get("InsertURI" + identityIndex); - ownIdentities.add(new OwnIdentity(identity, nickname, requestUri, insertUri)); + ownIdentities.add(new OwnIdentity(identity, nickname, requestUri, insertUri, emptySet())); } return ownIdentities; } @@ -193,7 +195,7 @@ public class WebOfTrustPlugin { } String identifier = replies.get("ID"); String nickname = replies.get("Nickname"); - return new Identity(identifier, nickname, requestUri); + return new Identity(identifier, nickname, requestUri, emptySet()); } /** @@ -223,7 +225,7 @@ public class WebOfTrustPlugin { String identifier = replies.get("Identity" + identityIndex); String nickname = replies.get("Nickname" + identityIndex); String requestUri = replies.get("RequestURI" + identityIndex); - identities.add(new Identity(identifier, nickname, requestUri)); + identities.add(new Identity(identifier, nickname, requestUri, emptySet())); } return identities; } @@ -253,7 +255,7 @@ public class WebOfTrustPlugin { String requestUri = replies.get("RequestURI" + identityIndex); byte trust = Byte.parseByte(replies.get("Value" + identityIndex)); String comment = replies.get("Comment" + identityIndex); - identityTrusts.put(new Identity(identifier, nickname, requestUri), new IdentityTrust(trust, comment)); + identityTrusts.put(new Identity(identifier, nickname, requestUri, emptySet()), new IdentityTrust(trust, comment)); } return identityTrusts; } @@ -283,7 +285,7 @@ public class WebOfTrustPlugin { String requestUri = replies.get("RequestURI" + identityIndex); byte trust = Byte.parseByte(replies.get("Value" + identityIndex)); String comment = replies.get("Comment" + identityIndex); - identityTrusts.put(new Identity(identifier, nickname, requestUri), new IdentityTrust(trust, comment)); + identityTrusts.put(new Identity(identifier, nickname, requestUri, emptySet()), new IdentityTrust(trust, comment)); } return identityTrusts; } diff --git a/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java b/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java index 80ddde5..19b7497 100644 --- a/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java +++ b/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java @@ -1,10 +1,13 @@ package net.pterodactylus.fcp.test; +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.Set; + public class IdentityMatchers { public static Matcher isOwnIdentity(Matcher identifier, Matcher nickname, Matcher requestUri, Matcher insertUri) { @@ -39,4 +42,22 @@ public class IdentityMatchers { }; } + public static Matcher hasContexts(Matcher> contextsMatcher) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(Identity identity, Description mismatchDescription) { + if (!contextsMatcher.matches(identity.getContexts())) { + contextsMatcher.describeMismatch(identity.getContexts(), mismatchDescription); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("has contexts ").appendValue(contextsMatcher); + } + }; + } + } -- 2.7.4