From dd5ef478adcf707ccffdea3a6c0e3f1dee5803bb Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 11 Feb 2020 18:51:43 +0100 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Replace=20Sone=20URI=20crea?= =?utf8?q?tor=20with=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/sone/core/SoneUri.java | 54 --------------- .../net/pterodactylus/sone/core/SoneUriCreator.kt | 26 +++++++ .../pterodactylus/sone/core/SoneUriCreatorTest.kt | 79 ++++++++++++++++++++++ .../net/pterodactylus/sone/core/SoneUriTest.java | 36 ---------- .../kotlin/net/pterodactylus/sone/test/Mocks.kt | 1 + 5 files changed, 106 insertions(+), 90 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/core/SoneUri.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/core/SoneUriCreator.kt create mode 100644 src/test/java/net/pterodactylus/sone/core/SoneUriCreatorTest.kt delete mode 100644 src/test/java/net/pterodactylus/sone/core/SoneUriTest.java 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 index a929950..0000000 --- a/src/main/java/net/pterodactylus/sone/core/SoneUri.java +++ /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 . - */ - -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 index 0000000..115c748 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/core/SoneUriCreator.kt @@ -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 index 0000000..cd7d541 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/core/SoneUriCreatorTest.kt @@ -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 index 3525c9b..0000000 --- a/src/test/java/net/pterodactylus/sone/core/SoneUriTest.java +++ /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()); - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt b/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt index 312f158..b273b82 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt @@ -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 -- 2.7.4