🔀 Merge branch 'release/v82'
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / core / SoneInserterTest.kt
index 7ac9e06..3da9e88 100644 (file)
@@ -4,7 +4,6 @@ import com.codahale.metrics.*
 import com.google.common.base.*
 import com.google.common.base.Optional
 import com.google.common.eventbus.*
-import com.google.common.io.ByteStreams.*
 import com.google.common.util.concurrent.MoreExecutors.*
 import freenet.keys.*
 import net.pterodactylus.sone.core.SoneInserter.*
@@ -39,6 +38,9 @@ class SoneInserterTest {
        private val core = mock<Core>()
        private val eventBus = mock<EventBus>()
        private val freenetInterface = mock<FreenetInterface>()
+       private val soneUriCreator = object : SoneUriCreator() {
+               override fun getInsertUri(sone: Sone): FreenetURI = expectedInsertUri
+       }
 
        @Before
        fun setupCore() {
@@ -50,7 +52,7 @@ class SoneInserterTest {
        @Test
        fun `insertion delay is forwarded to sone inserter`() {
                val eventBus = AsyncEventBus(directExecutor())
-               eventBus.register(SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId"))
+               eventBus.register(SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId"))
                eventBus.post(InsertionDelayChangedEvent(15))
                assertThat(SoneInserter.getInsertionDelay().get(), equalTo(15))
        }
@@ -69,27 +71,27 @@ class SoneInserterTest {
        fun `isModified is true if modification detector says so`() {
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isModified).thenReturn(true)
-               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
+               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1)
                assertThat(soneInserter.isModified, equalTo(true))
        }
 
        @Test
        fun `isModified is false if modification detector says so`() {
                val soneModificationDetector = mock<SoneModificationDetector>()
-               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
+               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1)
                assertThat(soneInserter.isModified, equalTo(false))
        }
 
        @Test
        fun `last fingerprint is stored correctly`() {
-               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId")
+               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId")
                soneInserter.lastInsertFingerprint = "last-fingerprint"
                assertThat(soneInserter.lastInsertFingerprint, equalTo("last-fingerprint"))
        }
 
        @Test
        fun `sone inserter stops when it should`() {
-               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId")
+               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId")
                soneInserter.stop()
                soneInserter.serviceRun()
        }
@@ -101,7 +103,7 @@ class SoneInserterTest {
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
                whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenReturn(finalUri)
-               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
+               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1)
                doAnswer {
                        soneInserter.stop()
                        null
@@ -122,7 +124,7 @@ class SoneInserterTest {
                val sone = createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
-               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
+               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1)
                whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
                        soneInserter.stop()
                        finalUri
@@ -142,7 +144,7 @@ class SoneInserterTest {
        fun `sone inserter does not insert sone if it is not eligible`() {
                createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
-               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
+               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1)
                Thread(Runnable {
                        try {
                                Thread.sleep(500)
@@ -162,7 +164,7 @@ class SoneInserterTest {
                val sone = createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
-               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
+               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1)
                val soneException = SoneException(Exception())
                whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
                        soneInserter.stop()
@@ -182,7 +184,7 @@ class SoneInserterTest {
        @Test
        fun `sone inserter exits if sone is unknown`() {
                val soneModificationDetector = mock<SoneModificationDetector>()
-               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
+               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1)
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
                whenever(core.getSone("SoneId")).thenReturn(null)
                soneInserter.serviceRun()
@@ -191,7 +193,7 @@ class SoneInserterTest {
        @Test
        fun `sone inserter catches exception and continues`() {
                val soneModificationDetector = mock<SoneModificationDetector>()
-               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
+               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1)
                val stopInserterAndThrowException = Answer<Optional<Sone>> {
                        soneInserter.stop()
                        throw NullPointerException()
@@ -210,14 +212,14 @@ class SoneInserterTest {
                val manifestElement = manifestCreator.createManifestElement("test.txt", "plain/text; charset=utf-8", "sone-inserter-manifest.txt")
                assertThat(manifestElement!!.name, equalTo("test.txt"))
                assertThat(manifestElement.mimeTypeOverride, equalTo("plain/text; charset=utf-8"))
-               val templateContent = String(toByteArray(manifestElement.data.inputStream), Charsets.UTF_8)
+               val templateContent = String(manifestElement.data.inputStream.readBytes(), Charsets.UTF_8)
                assertThat(templateContent, containsString("Sone Version: ${SonePlugin.getPluginVersion()}\n"))
                assertThat(templateContent, containsString("Core Startup: $now\n"))
                assertThat(templateContent, containsString("Sone ID: SoneId\n"))
        }
 
        @Test
-       fun `invalid template returns anull manifest element`() {
+       fun `invalid template returns a null manifest element`() {
                val soneProperties = HashMap<String, Any>()
                val manifestCreator = ManifestCreator(core, soneProperties)
                assertThat(manifestCreator.createManifestElement("test.txt",
@@ -244,7 +246,7 @@ class SoneInserterTest {
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
                whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenReturn(finalUri)
-               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry,"SoneId", soneModificationDetector, 1)
+               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1)
                doAnswer {
                        soneInserter.stop()
                        null
@@ -259,7 +261,7 @@ class SoneInserterTest {
                createSone(insertUri)
                val soneModificationDetector = mock<SoneModificationDetector>()
                whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
-               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
+               val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1)
                whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
                        soneInserter.stop()
                        throw SoneException(Exception())
@@ -274,8 +276,4 @@ class SoneInserterTest {
 }
 
 val insertUri = createInsertUri
-val expectedInsertUri: FreenetURI = FreenetURI(insertUri.toString())
-               .setKeyType("USK")
-               .setDocName("Sone")
-               .setMetaString(kotlin.emptyArray())
-               .setSuggestedEdition(0)
+val expectedInsertUri = createInsertUri