From: David ‘Bombe’ Roden Date: Sat, 1 Mar 2014 13:08:49 +0000 (+0100) Subject: Add function that converts a Sone into its insert URI. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=0a4b6fc252003c71f4bdef09560e87982838d9c8;p=Sone.git Add function that converts a Sone into its insert URI. --- diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index a8e8009..bca7895 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -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 { } }; + public static final Function TO_INSERT_URI = new Function() { + @Override + public FreenetURI apply(@Nullable Sone sone) { + return ((sone == null) || !sone.isLocal()) ? null : create(((OwnIdentity) sone.getIdentity()).getInsertUri()); + } + }; + public static final Function> TO_POSTS = new Function>() { @Override public List apply(Sone sone) { diff --git a/src/test/java/net/pterodactylus/sone/data/SoneTest.java b/src/test/java/net/pterodactylus/sone/data/SoneTest.java index d54c072..dae7379 100644 --- a/src/test/java/net/pterodactylus/sone/data/SoneTest.java +++ b/src/test/java/net/pterodactylus/sone/data/SoneTest.java @@ -17,12 +17,18 @@ 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()); + } + }