From 0a4b6fc252003c71f4bdef09560e87982838d9c8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 1 Mar 2014 14:08:49 +0100 Subject: [PATCH] Add function that converts a Sone into its insert URI. --- src/main/java/net/pterodactylus/sone/data/Sone.java | 10 ++++++++++ .../java/net/pterodactylus/sone/data/SoneTest.java | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) 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()); + } + } -- 2.7.4