X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneInserterTest.kt;fp=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneInserterTest.kt;h=3da9e88bc78746dafd192087ee9530f012d858fc;hp=2f902509596fc153b818fac27581b28e538107eb;hb=5c5bee980f9cab5792e34d1c9840f73b8b191830;hpb=faf66247a34f64946990a985d2ea3003465969cb diff --git a/src/test/kotlin/net/pterodactylus/sone/core/SoneInserterTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/SoneInserterTest.kt index 2f90250..3da9e88 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/SoneInserterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/SoneInserterTest.kt @@ -4,12 +4,12 @@ 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.* 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.* @@ -38,6 +38,9 @@ class SoneInserterTest { private val core = mock() private val eventBus = mock() private val freenetInterface = mock() + private val soneUriCreator = object : SoneUriCreator() { + override fun getInsertUri(sone: Sone): FreenetURI = expectedInsertUri + } @Before fun setupCore() { @@ -49,14 +52,15 @@ 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)) } private fun createSone(insertUri: FreenetURI, fingerprint: String = "fingerprint"): Sone { + val ownIdentity = DefaultOwnIdentity("", "", "", insertUri.toString()) val sone = mock() - 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) @@ -67,47 +71,46 @@ class SoneInserterTest { fun `isModified is true if modification detector says so`() { val soneModificationDetector = mock() 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() - 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() } @Test fun `sone inserter inserts a sone if it is eligible`() { - val insertUri = mock() val finalUri = mock() val sone = createSone(insertUri) val soneModificationDetector = mock() whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true) - whenever(freenetInterface.insertDirectory(eq(insertUri), any>(), eq("index.html"))).thenReturn(finalUri) - val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1) + whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any>(), eq("index.html"))).thenReturn(finalUri) + val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1) doAnswer { soneInserter.stop() null }.whenever(core).touchConfiguration() soneInserter.serviceRun() val soneEvents = ArgumentCaptor.forClass(SoneEvent::class.java) - verify(freenetInterface).insertDirectory(eq(insertUri), any>(), eq("index.html")) + verify(freenetInterface).insertDirectory(eq(expectedInsertUri), 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)) @@ -117,19 +120,18 @@ class SoneInserterTest { @Test fun `sone inserter bails out if it is stopped while inserting`() { - val insertUri = mock() val finalUri = mock() val sone = createSone(insertUri) val soneModificationDetector = mock() whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true) - val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1) - whenever(freenetInterface.insertDirectory(eq(insertUri), any>(), eq("index.html"))).thenAnswer { + val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1) + whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any>(), eq("index.html"))).thenAnswer { soneInserter.stop() finalUri } soneInserter.serviceRun() val soneEvents = ArgumentCaptor.forClass(SoneEvent::class.java) - verify(freenetInterface).insertDirectory(eq(insertUri), any>(), eq("index.html")) + verify(freenetInterface).insertDirectory(eq(expectedInsertUri), 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)) @@ -140,10 +142,9 @@ class SoneInserterTest { @Test fun `sone inserter does not insert sone if it is not eligible`() { - val insertUri = mock() createSone(insertUri) val soneModificationDetector = mock() - 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) @@ -154,25 +155,24 @@ class SoneInserterTest { soneInserter.stop() }).start() soneInserter.serviceRun() - verify(freenetInterface, never()).insertDirectory(eq(insertUri), any>(), eq("index.html")) + verify(freenetInterface, never()).insertDirectory(eq(expectedInsertUri), 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`() { - val insertUri = mock() val sone = createSone(insertUri) val soneModificationDetector = mock() 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(insertUri), any>(), eq("index.html"))).thenAnswer { + whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any>(), eq("index.html"))).thenAnswer { soneInserter.stop() throw soneException } soneInserter.serviceRun() val soneEvents = ArgumentCaptor.forClass(SoneEvent::class.java) - verify(freenetInterface).insertDirectory(eq(insertUri), any>(), eq("index.html")) + verify(freenetInterface).insertDirectory(eq(expectedInsertUri), 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)) @@ -184,7 +184,7 @@ class SoneInserterTest { @Test fun `sone inserter exits if sone is unknown`() { val soneModificationDetector = mock() - 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() @@ -193,7 +193,7 @@ class SoneInserterTest { @Test fun `sone inserter catches exception and continues`() { val soneModificationDetector = mock() - val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1) + val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1) val stopInserterAndThrowException = Answer> { soneInserter.stop() throw NullPointerException() @@ -212,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() val manifestCreator = ManifestCreator(core, soneProperties) assertThat(manifestCreator.createManifestElement("test.txt", @@ -241,13 +241,12 @@ class SoneInserterTest { @Test fun `successful insert updates metrics`() { - val insertUri = mock() val finalUri = mock() createSone(insertUri) val soneModificationDetector = mock() whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true) - whenever(freenetInterface.insertDirectory(eq(insertUri), any>(), eq("index.html"))).thenReturn(finalUri) - val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry,"SoneId", soneModificationDetector, 1) + whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any>(), eq("index.html"))).thenReturn(finalUri) + val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1) doAnswer { soneInserter.stop() null @@ -259,12 +258,11 @@ class SoneInserterTest { @Test fun `unsuccessful insert does not update histogram but records error`() { - val insertUri = mock() createSone(insertUri) val soneModificationDetector = mock() whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true) - val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1) - whenever(freenetInterface.insertDirectory(eq(insertUri), any>(), eq("index.html"))).thenAnswer { + val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, soneUriCreator, "SoneId", soneModificationDetector, 1) + whenever(freenetInterface.insertDirectory(eq(expectedInsertUri), any>(), eq("index.html"))).thenAnswer { soneInserter.stop() throw SoneException(Exception()) } @@ -276,3 +274,6 @@ class SoneInserterTest { } } + +val insertUri = createInsertUri +val expectedInsertUri = createInsertUri