Add function that converts a Sone into its insert URI.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 1 Mar 2014 13:08:49 +0000 (14:08 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 1 Mar 2014 13:08:49 +0000 (14:08 +0100)
src/main/java/net/pterodactylus/sone/data/Sone.java
src/test/java/net/pterodactylus/sone/data/SoneTest.java

index a8e8009..bca7895 100644 (file)
@@ -30,11 +30,14 @@ import java.util.Comparator;
 import java.util.List;
 import java.util.Set;
 
+import javax.annotation.Nullable;
+
 import net.pterodactylus.sone.core.Options;
 import net.pterodactylus.sone.database.AlbumBuilder;
 import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostReplyBuilder;
 import net.pterodactylus.sone.freenet.wot.Identity;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 
 import freenet.keys.FreenetURI;
 
@@ -186,6 +189,13 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
                }
        };
 
+       public static final Function<Sone, FreenetURI> TO_INSERT_URI = new Function<Sone, FreenetURI>() {
+               @Override
+               public FreenetURI apply(@Nullable Sone sone) {
+                       return ((sone == null) || !sone.isLocal()) ? null : create(((OwnIdentity) sone.getIdentity()).getInsertUri());
+               }
+       };
+
        public static final Function<Sone, List<Post>> TO_POSTS = new Function<Sone, List<Post>>() {
                @Override
                public List<Post> apply(Sone sone) {
index d54c072..dae7379 100644 (file)
 
 package net.pterodactylus.sone.data;
 
+import static freenet.keys.InsertableClientSSK.createRandom;
+import static net.pterodactylus.sone.data.Sone.TO_INSERT_URI;
 import static net.pterodactylus.sone.data.Sone.TO_POSTS;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
 import static org.mockito.Mockito.when;
 
+import freenet.crypt.DummyRandomSource;
+import freenet.keys.InsertableClientSSK;
+
 import org.junit.Test;
 
 /**
@@ -46,4 +52,17 @@ public class SoneTest {
                assertThat(TO_POSTS.apply(sone), contains(is(post3), is(post2), is(post1)));
        }
 
+       @Test
+       public void soneCanBeTransformedIntoAnInsertUri() {
+               InsertableClientSSK newKeypair = createRandom(new DummyRandomSource(), "Test");
+               Sone localSone = mocks.mockSone("A").local().insertUri(newKeypair.getInsertURI().toString()).create();
+               assertThat(TO_INSERT_URI.apply(localSone).toString(), is(newKeypair.getInsertURI().setDocName("Sone").toString()));
+       }
+
+       @Test
+       public void nonLocalSoneCanNotBeTransformedIntoAnInsertUri() {
+               Sone remoteSone = mocks.mockSone("A").create();
+               assertThat(TO_INSERT_URI.apply(remoteSone), nullValue());
+       }
+
 }