✏️ Fix test name
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / core / SoneInserterTest.kt
index 8b061a7..7d06552 100644 (file)
@@ -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.*
@@ -55,8 +55,9 @@ class SoneInserterTest {
        }
 
        private fun createSone(insertUri: FreenetURI, fingerprint: String = "fingerprint"): Sone {
+               val ownIdentity = DefaultOwnIdentity("", "", "", insertUri.toString())
                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)
@@ -94,20 +95,19 @@ class SoneInserterTest {
 
        @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)
-               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()
                        null
-               }.`when`(core).touchConfiguration()
+               }.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))
@@ -117,19 +117,18 @@ class SoneInserterTest {
 
        @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)
-               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)
-               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))
@@ -140,7 +139,6 @@ class SoneInserterTest {
 
        @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)
@@ -154,25 +152,24 @@ class SoneInserterTest {
                        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`() {
-               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())
-               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)
-               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))
@@ -212,14 +209,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",
@@ -241,17 +238,16 @@ class SoneInserterTest {
 
        @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)
-               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()
                        null
-               }.`when`(core).touchConfiguration()
+               }.whenever(core).touchConfiguration()
                soneInserter.serviceRun()
                val histogram = metricRegistry.histogram("sone.insert.duration")
                assertThat(histogram.count, equalTo(1L))
@@ -259,12 +255,11 @@ class SoneInserterTest {
 
        @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)
-               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())
                }
@@ -276,3 +271,10 @@ class SoneInserterTest {
        }
 
 }
+
+val insertUri = createInsertUri
+val expectedInsertUri: FreenetURI = FreenetURI(insertUri.toString())
+               .setKeyType("USK")
+               .setDocName("Sone")
+               .setMetaString(kotlin.emptyArray())
+               .setSuggestedEdition(0)