🚧 Add properties to WoT identities
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 8 Jan 2025 07:31:29 +0000 (08:31 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 8 Jan 2025 07:39:12 +0000 (08:39 +0100)
src/main/java/net/pterodactylus/fcp/plugin/Identity.java
src/main/java/net/pterodactylus/fcp/plugin/OwnIdentity.java
src/main/java/net/pterodactylus/fcp/plugin/WebOfTrustPlugin.java
src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java

index f04f37b..69ce365 100644 (file)
@@ -19,7 +19,9 @@ package net.pterodactylus.fcp.plugin;
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -39,6 +41,7 @@ public class Identity {
        private final String requestUri;
 
        private final Set<String> contexts = new HashSet<>();
+       private final Map<String, String> properties = new HashMap<>();
 
        /**
         * Creates a new identity.
@@ -50,11 +53,12 @@ public class Identity {
         * @param requestUri
         *            The request URI of the identity
         */
-       public Identity(String identifier, String nickname, String requestUri, Collection<String> contexts) {
+       public Identity(String identifier, String nickname, String requestUri, Collection<String> contexts, Map<String, String> properties) {
                this.identifier = identifier;
                this.nickname = nickname;
                this.requestUri = requestUri;
                this.contexts.addAll(contexts);
+               this.properties.putAll(properties);
        }
 
        /**
@@ -88,6 +92,10 @@ public class Identity {
                return Collections.unmodifiableSet(contexts);
        }
 
+       public Map<String, String> getProperties() {
+               return Collections.unmodifiableMap(properties);
+       }
+
        /**
         * {@inheritDoc}
         */
index 35c19a8..0e4622e 100644 (file)
@@ -18,6 +18,7 @@
 package net.pterodactylus.fcp.plugin;
 
 import java.util.Collection;
+import java.util.Map;
 import java.util.StringJoiner;
 
 /**
@@ -42,8 +43,8 @@ public class OwnIdentity extends Identity {
         * @param insertUri
         *            The insert URI of the identity
         */
-       public OwnIdentity(String identifier, String nickname, String requestUri, String insertUri, Collection<String> contexts) {
-               super(identifier, nickname, requestUri, contexts);
+       public OwnIdentity(String identifier, String nickname, String requestUri, String insertUri, Collection<String> contexts, Map<String, String> properties) {
+               super(identifier, nickname, requestUri, contexts, properties);
                this.insertUri = insertUri;
        }
 
@@ -64,6 +65,7 @@ public class OwnIdentity extends Identity {
                                .add("requestUri='" + getRequestUri() + "'")
                                .add("insertUri='" + insertUri + "'")
                                .add("contexts='" + getContexts() + "'")
+                               .add("properties='" + getProperties() + "'")
                                .toString();
        }
 
index 287769a..0882471 100644 (file)
@@ -26,6 +26,7 @@ import java.util.Set;
 import net.pterodactylus.fcp.highlevel.FcpClient;
 import net.pterodactylus.fcp.highlevel.FcpException;
 
+import static java.util.Collections.emptyMap;
 import static java.util.Collections.emptySet;
 
 /**
@@ -90,7 +91,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, emptySet());
+               return new OwnIdentity(identifier, nickname, newRequestUri, newInsertUri, emptySet(), emptyMap());
        }
 
        /**
@@ -132,7 +133,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, emptySet()));
+                       ownIdentities.add(new OwnIdentity(identity, nickname, requestUri, insertUri, emptySet(), emptyMap()));
                }
                return ownIdentities;
        }
@@ -195,7 +196,7 @@ public class WebOfTrustPlugin {
                }
                String identifier = replies.get("ID");
                String nickname = replies.get("Nickname");
-               return new Identity(identifier, nickname, requestUri, emptySet());
+               return new Identity(identifier, nickname, requestUri, emptySet(), emptyMap());
        }
 
        /**
@@ -225,7 +226,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, emptySet()));
+                       identities.add(new Identity(identifier, nickname, requestUri, emptySet(), emptyMap()));
                }
                return identities;
        }
@@ -255,7 +256,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, emptySet()), new IdentityTrust(trust, comment));
+                       identityTrusts.put(new Identity(identifier, nickname, requestUri, emptySet(), emptyMap()), new IdentityTrust(trust, comment));
                }
                return identityTrusts;
        }
@@ -285,7 +286,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, emptySet()), new IdentityTrust(trust, comment));
+                       identityTrusts.put(new Identity(identifier, nickname, requestUri, emptySet(), emptyMap()), new IdentityTrust(trust, comment));
                }
                return identityTrusts;
        }
index 19b7497..310b6cb 100644 (file)
@@ -6,6 +6,7 @@ import org.hamcrest.Description;
 import org.hamcrest.Matcher;
 import org.hamcrest.TypeSafeDiagnosingMatcher;
 
+import java.util.Map;
 import java.util.Set;
 
 public class IdentityMatchers {
@@ -60,4 +61,22 @@ public class IdentityMatchers {
                };
        }
 
+       public static Matcher<Identity> hasProperties(Matcher<? super Map<String, String>> propertiesMatcher) {
+               return new TypeSafeDiagnosingMatcher<Identity>() {
+                       @Override
+                       protected boolean matchesSafely(Identity identity, Description mismatchDescription) {
+                               if (!propertiesMatcher.matches(identity.getProperties())) {
+                                       propertiesMatcher.describeMismatch(identity.getProperties(), mismatchDescription);
+                                       return false;
+                               }
+                               return true;
+                       }
+
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText("has properties ").appendValue(propertiesMatcher);
+                       }
+               };
+       }
+
 }