♻️ Use Sone’s identity to create insert URI
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 11 Feb 2020 17:01:09 +0000 (18:01 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 11 Feb 2020 17:52:27 +0000 (18:52 +0100)
src/main/java/net/pterodactylus/sone/core/SoneInserter.java
src/test/kotlin/net/pterodactylus/sone/core/SoneInserterTest.kt
src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt

index 7ccb374..f18776b 100644 (file)
@@ -27,12 +27,12 @@ import java.io.Closeable;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.StringWriter;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.StringWriter;
+import java.net.MalformedURLException;
 import java.nio.charset.Charset;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 import java.nio.charset.Charset;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.*;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -49,6 +49,7 @@ import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 import net.pterodactylus.sone.main.SonePlugin;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.sone.main.SonePlugin;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.service.AbstractService;
@@ -237,7 +238,7 @@ public class SoneInserter extends AbstractService {
                                                long insertTime = currentTimeMillis();
                                                eventBus.post(new SoneInsertingEvent(sone));
                                                Stopwatch stopwatch = Stopwatch.createStarted();
                                                long insertTime = currentTimeMillis();
                                                eventBus.post(new SoneInsertingEvent(sone));
                                                Stopwatch stopwatch = Stopwatch.createStarted();
-                                               FreenetURI finalUri = freenetInterface.insertDirectory(sone.getInsertUri(), insertInformation.generateManifestEntries(), "index.html");
+                                               FreenetURI finalUri = freenetInterface.insertDirectory(getSoneInsertUri(sone), insertInformation.generateManifestEntries(), "index.html");
                                                stopwatch.stop();
                                                soneInsertDurationHistogram.update(stopwatch.elapsed(MICROSECONDS));
                                                eventBus.post(new SoneInsertedEvent(sone, stopwatch.elapsed(MILLISECONDS), insertInformation.getFingerprint()));
                                                stopwatch.stop();
                                                soneInsertDurationHistogram.update(stopwatch.elapsed(MICROSECONDS));
                                                eventBus.post(new SoneInsertedEvent(sone, stopwatch.elapsed(MILLISECONDS), insertInformation.getFingerprint()));
@@ -285,6 +286,14 @@ public class SoneInserter extends AbstractService {
                setInsertionDelay(insertionDelayChangedEvent.getInsertionDelay());
        }
 
                setInsertionDelay(insertionDelayChangedEvent.getInsertionDelay());
        }
 
+       private FreenetURI getSoneInsertUri(Sone sone) throws MalformedURLException {
+               return new FreenetURI(((OwnIdentity) sone.getIdentity()).getInsertUri())
+                               .setKeyType("USK")
+                               .setDocName("Sone")
+                               .setMetaString(new String[0])
+                               .setSuggestedEdition(sone.getLatestEdition());
+       }
+
        /**
         * Container for information that are required to insert a Sone. This
         * container merely exists to copy all relevant data without holding a lock
        /**
         * Container for information that are required to insert a Sone. This
         * container merely exists to copy all relevant data without holding a lock
index 2f90250..7ac9e06 100644 (file)
@@ -10,6 +10,7 @@ import freenet.keys.*
 import net.pterodactylus.sone.core.SoneInserter.*
 import net.pterodactylus.sone.core.event.*
 import net.pterodactylus.sone.data.*
 import net.pterodactylus.sone.core.SoneInserter.*
 import net.pterodactylus.sone.core.event.*
 import net.pterodactylus.sone.data.*
+import net.pterodactylus.sone.freenet.wot.*
 import net.pterodactylus.sone.main.*
 import net.pterodactylus.sone.test.*
 import org.hamcrest.MatcherAssert.*
 import net.pterodactylus.sone.main.*
 import net.pterodactylus.sone.test.*
 import org.hamcrest.MatcherAssert.*
@@ -55,8 +56,9 @@ class SoneInserterTest {
        }
 
        private fun createSone(insertUri: FreenetURI, fingerprint: String = "fingerprint"): Sone {
        }
 
        private fun createSone(insertUri: FreenetURI, fingerprint: String = "fingerprint"): Sone {
+               val ownIdentity = DefaultOwnIdentity("", "", "", insertUri.toString())
                val sone = mock<Sone>()
                val sone = mock<Sone>()
-               whenever(sone.insertUri).thenReturn(insertUri)
+               whenever(sone.identity).thenReturn(ownIdentity)
                whenever(sone.fingerprint).thenReturn(fingerprint)
                whenever(sone.rootAlbum).thenReturn(mock())
                whenever(core.getSone(anyString())).thenReturn(sone)
                whenever(sone.fingerprint).thenReturn(fingerprint)
                whenever(sone.rootAlbum).thenReturn(mock())
                whenever(core.getSone(anyString())).thenReturn(sone)
@@ -94,12 +96,11 @@ class SoneInserterTest {
 
        @Test
        fun `sone inserter inserts a sone if it is eligible`() {
 
        @Test
        fun `sone inserter inserts a sone if it is eligible`() {
-               val insertUri = mock<FreenetURI>()
                val finalUri = mock<FreenetURI>()
                val sone = createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
                val finalUri = mock<FreenetURI>()
                val sone = createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
-               whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenReturn(finalUri)
+               whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenReturn(finalUri)
                val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
                doAnswer {
                        soneInserter.stop()
                val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
                doAnswer {
                        soneInserter.stop()
@@ -107,7 +108,7 @@ class SoneInserterTest {
                }.whenever(core).touchConfiguration()
                soneInserter.serviceRun()
                val soneEvents = ArgumentCaptor.forClass(SoneEvent::class.java)
                }.whenever(core).touchConfiguration()
                soneInserter.serviceRun()
                val soneEvents = ArgumentCaptor.forClass(SoneEvent::class.java)
-               verify(freenetInterface).insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))
+               verify(freenetInterface).insertDirectory(eq(expectedInsertUri), any<HashMap<String, Any>>(), eq("index.html"))
                verify(eventBus, times(2)).post(soneEvents.capture())
                assertThat(soneEvents.allValues[0], instanceOf(SoneInsertingEvent::class.java))
                assertThat(soneEvents.allValues[0].sone, equalTo(sone))
                verify(eventBus, times(2)).post(soneEvents.capture())
                assertThat(soneEvents.allValues[0], instanceOf(SoneInsertingEvent::class.java))
                assertThat(soneEvents.allValues[0].sone, equalTo(sone))
@@ -117,19 +118,18 @@ class SoneInserterTest {
 
        @Test
        fun `sone inserter bails out if it is stopped while inserting`() {
 
        @Test
        fun `sone inserter bails out if it is stopped while inserting`() {
-               val insertUri = mock<FreenetURI>()
                val finalUri = mock<FreenetURI>()
                val sone = createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
                val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
                val finalUri = mock<FreenetURI>()
                val sone = createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
                val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
-               whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
+               whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
                        soneInserter.stop()
                        finalUri
                }
                soneInserter.serviceRun()
                val soneEvents = ArgumentCaptor.forClass(SoneEvent::class.java)
                        soneInserter.stop()
                        finalUri
                }
                soneInserter.serviceRun()
                val soneEvents = ArgumentCaptor.forClass(SoneEvent::class.java)
-               verify(freenetInterface).insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))
+               verify(freenetInterface).insertDirectory(eq(expectedInsertUri), any<HashMap<String, Any>>(), eq("index.html"))
                verify(eventBus, times(2)).post(soneEvents.capture())
                assertThat(soneEvents.allValues[0], instanceOf(SoneInsertingEvent::class.java))
                assertThat(soneEvents.allValues[0].sone, equalTo(sone))
                verify(eventBus, times(2)).post(soneEvents.capture())
                assertThat(soneEvents.allValues[0], instanceOf(SoneInsertingEvent::class.java))
                assertThat(soneEvents.allValues[0].sone, equalTo(sone))
@@ -140,7 +140,6 @@ class SoneInserterTest {
 
        @Test
        fun `sone inserter does not insert sone if it is not eligible`() {
 
        @Test
        fun `sone inserter does not insert sone if it is not eligible`() {
-               val insertUri = mock<FreenetURI>()
                createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
                createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
@@ -154,25 +153,24 @@ class SoneInserterTest {
                        soneInserter.stop()
                }).start()
                soneInserter.serviceRun()
                        soneInserter.stop()
                }).start()
                soneInserter.serviceRun()
-               verify(freenetInterface, never()).insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))
+               verify(freenetInterface, never()).insertDirectory(eq(expectedInsertUri), any<HashMap<String, Any>>(), eq("index.html"))
                verify(eventBus, never()).post(argThat(org.hamcrest.Matchers.any(SoneEvent::class.java)))
        }
 
        @Test
        fun `sone inserter posts aborted event if an exception occurs`() {
                verify(eventBus, never()).post(argThat(org.hamcrest.Matchers.any(SoneEvent::class.java)))
        }
 
        @Test
        fun `sone inserter posts aborted event if an exception occurs`() {
-               val insertUri = mock<FreenetURI>()
                val sone = createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
                val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
                val soneException = SoneException(Exception())
                val sone = createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
                val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
                val soneException = SoneException(Exception())
-               whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
+               whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
                        soneInserter.stop()
                        throw soneException
                }
                soneInserter.serviceRun()
                val soneEvents = ArgumentCaptor.forClass(SoneEvent::class.java)
                        soneInserter.stop()
                        throw soneException
                }
                soneInserter.serviceRun()
                val soneEvents = ArgumentCaptor.forClass(SoneEvent::class.java)
-               verify(freenetInterface).insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))
+               verify(freenetInterface).insertDirectory(eq(expectedInsertUri), any<HashMap<String, Any>>(), eq("index.html"))
                verify(eventBus, times(2)).post(soneEvents.capture())
                assertThat(soneEvents.allValues[0], instanceOf(SoneInsertingEvent::class.java))
                assertThat(soneEvents.allValues[0].sone, equalTo(sone))
                verify(eventBus, times(2)).post(soneEvents.capture())
                assertThat(soneEvents.allValues[0], instanceOf(SoneInsertingEvent::class.java))
                assertThat(soneEvents.allValues[0].sone, equalTo(sone))
@@ -241,12 +239,11 @@ class SoneInserterTest {
 
        @Test
        fun `successful insert updates metrics`() {
 
        @Test
        fun `successful insert updates metrics`() {
-               val insertUri = mock<FreenetURI>()
                val finalUri = mock<FreenetURI>()
                createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
                val finalUri = mock<FreenetURI>()
                createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
-               whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenReturn(finalUri)
+               whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenReturn(finalUri)
                val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry,"SoneId", soneModificationDetector, 1)
                doAnswer {
                        soneInserter.stop()
                val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry,"SoneId", soneModificationDetector, 1)
                doAnswer {
                        soneInserter.stop()
@@ -259,12 +256,11 @@ class SoneInserterTest {
 
        @Test
        fun `unsuccessful insert does not update histogram but records error`() {
 
        @Test
        fun `unsuccessful insert does not update histogram but records error`() {
-               val insertUri = mock<FreenetURI>()
                createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
                val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
                createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
                val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
-               whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
+               whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
                        soneInserter.stop()
                        throw SoneException(Exception())
                }
                        soneInserter.stop()
                        throw SoneException(Exception())
                }
@@ -276,3 +272,10 @@ class SoneInserterTest {
        }
 
 }
        }
 
 }
+
+val insertUri = createInsertUri
+val expectedInsertUri: FreenetURI = FreenetURI(insertUri.toString())
+               .setKeyType("USK")
+               .setDocName("Sone")
+               .setMetaString(kotlin.emptyArray())
+               .setSuggestedEdition(0)
index 48fb9bf..312f158 100644 (file)
@@ -32,6 +32,7 @@ val remoteSone2 = createRemoteSone()
 val localSone1 = createLocalSone()
 val localSone2 = createLocalSone()
 
 val localSone1 = createLocalSone()
 val localSone2 = createLocalSone()
 
+val createInsertUri: FreenetURI get() = InsertableClientSSK.createRandom(DummyRandomSource(), "").insertURI
 fun createId() = InsertableClientSSK.createRandom(DummyRandomSource(), "").uri.routingKey.asFreenetBase64
 
 fun createLocalSone(id: String? = createId()) = object : IdOnlySone(id) {
 fun createId() = InsertableClientSSK.createRandom(DummyRandomSource(), "").uri.routingKey.asFreenetBase64
 
 fun createLocalSone(id: String? = createId()) = object : IdOnlySone(id) {