♻️ Replace Sone URI creator with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 11 Feb 2020 17:51:43 +0000 (18:51 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 11 Feb 2020 17:52:32 +0000 (18:52 +0100)
src/main/java/net/pterodactylus/sone/core/SoneUri.java [deleted file]
src/main/kotlin/net/pterodactylus/sone/core/SoneUriCreator.kt [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/core/SoneUriCreatorTest.kt [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/core/SoneUriTest.java [deleted file]
src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt

diff --git a/src/main/java/net/pterodactylus/sone/core/SoneUri.java b/src/main/java/net/pterodactylus/sone/core/SoneUri.java
deleted file mode 100644 (file)
index a929950..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Sone - SoneUri.java - Copyright © 2013–2020 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.core;
-
-import static java.util.logging.Logger.getLogger;
-
-import java.net.MalformedURLException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import freenet.keys.FreenetURI;
-
-/**
- * Helper class that creates {@link FreenetURI}s for Sone to insert to and
- * request from.
- */
-public class SoneUri {
-
-       /** The logger. */
-       private static final Logger logger = getLogger(SoneUri.class.getName());
-
-       /**
-        * Generate a Sone URI from the given URI.
-        *
-        * @param uri
-        *            The URI to derive the Sone URI from
-        * @return The derived URI
-        */
-       public static FreenetURI create(String uri) {
-               try {
-                       return new FreenetURI(uri).setDocName("Sone").setMetaString(new String[0]);
-               } catch (MalformedURLException mue1) {
-                       /* this should never happen. */
-                       logger.log(Level.WARNING, String.format("Could not create Sone URI from URI: %s", uri), mue1);
-                       return null;
-               }
-       }
-
-}
diff --git a/src/main/kotlin/net/pterodactylus/sone/core/SoneUriCreator.kt b/src/main/kotlin/net/pterodactylus/sone/core/SoneUriCreator.kt
new file mode 100644 (file)
index 0000000..115c748
--- /dev/null
@@ -0,0 +1,26 @@
+package net.pterodactylus.sone.core
+
+import freenet.keys.FreenetURI
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.freenet.wot.OwnIdentity
+
+/**
+ * Injectable helper class that can create request and insert URIs for [Sones][Sone].
+ */
+class SoneUriCreator {
+
+       fun getRequestUri(sone: Sone): FreenetURI = sone.identity.requestUri
+                       .let(::FreenetURI)
+                       .sonify(sone.latestEdition)
+
+       fun getInsertUri(sone: Sone): FreenetURI? = (sone.identity as? OwnIdentity)?.insertUri
+                       ?.let(::FreenetURI)
+                       ?.sonify(sone.latestEdition)
+
+}
+
+private fun FreenetURI.sonify(edition: Long): FreenetURI =
+               setKeyType("USK")
+                               .setDocName("Sone")
+                               .setMetaString(emptyArray())
+                               .setSuggestedEdition(edition)
diff --git a/src/test/java/net/pterodactylus/sone/core/SoneUriCreatorTest.kt b/src/test/java/net/pterodactylus/sone/core/SoneUriCreatorTest.kt
new file mode 100644 (file)
index 0000000..cd7d541
--- /dev/null
@@ -0,0 +1,79 @@
+package net.pterodactylus.sone.core
+
+import net.pterodactylus.sone.data.impl.IdOnlySone
+import net.pterodactylus.sone.freenet.wot.DefaultIdentity
+import net.pterodactylus.sone.freenet.wot.DefaultOwnIdentity
+import net.pterodactylus.sone.test.createInsertUri
+import net.pterodactylus.sone.test.createRequestUri
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.emptyArray
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.nullValue
+import kotlin.test.Test
+
+/**
+ * Unit test for [SoneUriCreator].
+ */
+class SoneUriCreatorTest {
+
+       private val soneUriCreator = SoneUriCreator()
+
+       private val requestUri = soneUriCreator.getRequestUri(sone)
+       private val insertUri = soneUriCreator.getInsertUri(sone)
+
+       @Test
+       fun `generated request URI is a USK`() {
+               assertThat(requestUri.keyType, equalTo("USK"))
+       }
+
+       @Test
+       fun `generated request URI has correct doc name`() {
+               assertThat(requestUri.docName, equalTo("Sone"))
+       }
+
+       @Test
+       fun `generated request URI has no meta strings`() {
+               assertThat(requestUri.allMetaStrings, emptyArray())
+       }
+
+       @Test
+       fun `generated request URI has correct edition`() {
+               assertThat(requestUri.suggestedEdition, equalTo(123L))
+       }
+
+       @Test
+       fun `insert URI is null if sone’s identity is not an own identity`() {
+               val remoteSone = object : IdOnlySone("id") {
+                       override fun getIdentity() = DefaultIdentity("id", "name", createRequestUri.toString())
+               }
+               assertThat(soneUriCreator.getInsertUri(remoteSone), nullValue())
+       }
+
+       @Test
+       fun `generated insert URI is a USK`() {
+               assertThat(insertUri!!.keyType, equalTo("USK"))
+       }
+
+       @Test
+       fun `generated insert URI has correct doc name`() {
+               assertThat(insertUri!!.docName, equalTo("Sone"))
+       }
+
+       @Test
+       fun `generated insert URI has no meta strings`() {
+               assertThat(insertUri!!.allMetaStrings, emptyArray())
+       }
+
+       @Test
+       fun `generated insert URI has correct edition`() {
+               assertThat(insertUri!!.suggestedEdition, equalTo(123L))
+       }
+
+}
+
+private val sone = object : IdOnlySone("id") {
+       override fun getIdentity() =
+                       DefaultOwnIdentity("id", "name", createRequestUri.toString(), createInsertUri.toString())
+
+       override fun getLatestEdition() = 123L
+}
diff --git a/src/test/java/net/pterodactylus/sone/core/SoneUriTest.java b/src/test/java/net/pterodactylus/sone/core/SoneUriTest.java
deleted file mode 100644 (file)
index 3525c9b..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-package net.pterodactylus.sone.core;
-
-import static freenet.keys.InsertableClientSSK.createRandom;
-import static net.pterodactylus.sone.core.SoneUri.create;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-
-import freenet.crypt.DummyRandomSource;
-import freenet.keys.FreenetURI;
-
-import org.junit.Test;
-
-/**
- * Unit test for {@link SoneUri}.
- */
-public class SoneUriTest {
-
-       @Test
-       public void callConstructorForIncreasedTestCoverage() {
-               new SoneUri();
-       }
-
-       @Test
-       public void returnedUriHasCorrectDocNameAndMetaStrings() {
-               FreenetURI uri = createRandom(new DummyRandomSource(), "test-0").getURI().uskForSSK();
-               assertThat(create(uri.toString()).getDocName(), is("Sone"));
-               assertThat(create(uri.toString()).getAllMetaStrings(), is(new String[0]));
-       }
-
-       @Test
-       public void malformedUriReturnsNull() {
-               assertThat(create("not a key"), nullValue());
-       }
-
-}
index 312f158..b273b82 100644 (file)
@@ -32,6 +32,7 @@ val remoteSone2 = createRemoteSone()
 val localSone1 = createLocalSone()
 val localSone2 = createLocalSone()
 
+val createRequestUri: FreenetURI get() = InsertableClientSSK.createRandom(DummyRandomSource(), "").uri
 val createInsertUri: FreenetURI get() = InsertableClientSSK.createRandom(DummyRandomSource(), "").insertURI
 fun createId() = InsertableClientSSK.createRandom(DummyRandomSource(), "").uri.routingKey.asFreenetBase64