♻️ Add freenet client interface
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 7 Aug 2019 05:35:33 +0000 (07:35 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 7 Aug 2019 05:35:33 +0000 (07:35 +0200)
src/main/kotlin/net/pterodactylus/sone/freenet/FreenetClient.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/freenet/FreenetClientTest.kt [new file with mode: 0644]

diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/FreenetClient.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/FreenetClient.kt
new file mode 100644 (file)
index 0000000..8fbbe57
--- /dev/null
@@ -0,0 +1,20 @@
+package net.pterodactylus.sone.freenet
+
+import freenet.client.*
+import freenet.keys.*
+
+/**
+ * Facade for Freenet’s [freenet.client.HighLevelSimpleClient] to allow testing.
+ */
+interface FreenetClient {
+
+       fun fetch(freenetKey: FreenetURI): FetchResult
+
+}
+
+class DefaultFreenetClient(private val highLevelSimpleClient: HighLevelSimpleClient) : FreenetClient {
+
+       override fun fetch(freenetKey: FreenetURI): FetchResult =
+                       highLevelSimpleClient.fetch(freenetKey)
+
+}
diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/FreenetClientTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/FreenetClientTest.kt
new file mode 100644 (file)
index 0000000..f6d5883
--- /dev/null
@@ -0,0 +1,23 @@
+package net.pterodactylus.sone.freenet
+
+import freenet.client.*
+import freenet.keys.*
+import freenet.support.io.*
+import net.pterodactylus.sone.test.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import kotlin.test.*
+
+class FreenetClientTest {
+
+       private val highLevelSimpleClient = mock<HighLevelSimpleClient>()
+       private val freenetClient = DefaultFreenetClient(highLevelSimpleClient)
+
+       @Test
+       fun `fetch method calls method on hlsc`() {
+               val fetchResult = FetchResult(ClientMetadata(), NullBucket())
+               whenever(highLevelSimpleClient.fetch(FreenetURI("KSK@GPL.txt"))).thenReturn(fetchResult)
+               assertThat(freenetClient.fetch(FreenetURI("KSK@GPL.txt")), equalTo(fetchResult))
+       }
+
+}