🎨 Replace WOT connector with Kotlin version
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / freenet / wot / WebOfTrustConnectorTest.kt
index 41fbbbf..2d02e3a 100644 (file)
@@ -17,17 +17,17 @@ import kotlin.test.*
 class WebOfTrustConnectorTest {
 
        private val ownIdentity = DefaultOwnIdentity("id", "nickname", "requestUri", "insertUri")
+       private val identity = DefaultIdentity("id-a", "alpha", "url://alpha")
 
        @Test
        fun `wot plugin can be pinged`() {
-               val pluginConnector = createPluginConnector("Ping")
-               val connector = WebOfTrustConnector(pluginConnector)
-               connector.ping()
+               createPluginConnector("Ping")
+                               .connect { ping() }
        }
 
        @Test
        fun `own identities are returned correctly`() {
-               val pluginConnector = createPluginConnector("GetOwnIdentities") {
+               val ownIdentities = createPluginConnector("GetOwnIdentities") {
                        put("Identity0", "id-0")
                        put("RequestURI0", "request-uri-0")
                        put("InsertURI0", "insert-uri-0")
@@ -42,9 +42,7 @@ class WebOfTrustConnectorTest {
                        put("Contexts1.Context0", "id-1-context-0")
                        put("Properties1.Property0.Name", "id-1-property-0-name")
                        put("Properties1.Property0.Value", "id-1-property-0-value")
-               }
-               val connector = WebOfTrustConnector(pluginConnector)
-               val ownIdentities = connector.loadAllOwnIdentities()
+               }.connect { loadAllOwnIdentities() }
                assertThat(ownIdentities, containsInAnyOrder(
                                isOwnIdentity("id-0", "nickname-0", "request-uri-0", "insert-uri-0", contains("id-0-context-0"), hasEntry("id-0-property-0-name", "id-0-property-0-value")),
                                isOwnIdentity("id-1", "nickname-1", "request-uri-1", "insert-uri-1", contains("id-1-context-0"), hasEntry("id-1-property-0-name", "id-1-property-0-value"))
@@ -53,59 +51,51 @@ class WebOfTrustConnectorTest {
 
        @Test
        fun `trusted identities are requested with correct own identity`() {
-               val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Truster", equalTo("id")))
-               val connector = WebOfTrustConnector(pluginConnector)
-               connector.loadTrustedIdentities(ownIdentity)
+               createPluginConnector("GetIdentitiesByScore", hasField("Truster", equalTo("id")))
+                               .connect { loadTrustedIdentities(ownIdentity) }
        }
 
        @Test
        fun `trusted identities are requested with correct selection parameter`() {
-               val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Selection", equalTo("+")))
-               val connector = WebOfTrustConnector(pluginConnector)
-               connector.loadTrustedIdentities(ownIdentity)
+               createPluginConnector("GetIdentitiesByScore", hasField("Selection", equalTo("+")))
+                               .connect { loadTrustedIdentities(ownIdentity) }
        }
 
        @Test
        fun `trusted identities are requested with empty context if null context requested`() {
-               val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("")))
-               val connector = WebOfTrustConnector(pluginConnector)
-               connector.loadTrustedIdentities(ownIdentity)
+               createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("")))
+                               .connect { loadTrustedIdentities(ownIdentity) }
        }
 
        @Test
        fun `trusted identities are requested with context if context requested`() {
-               val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("TestContext")))
-               val connector = WebOfTrustConnector(pluginConnector)
-               connector.loadTrustedIdentities(ownIdentity, "TestContext")
+               createPluginConnector("GetIdentitiesByScore", hasField("Context", equalTo("TestContext")))
+                               .connect { loadTrustedIdentities(ownIdentity, "TestContext") }
        }
 
        @Test
        fun `trusted identities are requested with trust values`() {
-               val pluginConnector = createPluginConnector("GetIdentitiesByScore", hasField("WantTrustValues", equalTo("true")))
-               val connector = WebOfTrustConnector(pluginConnector)
-               val trustedIdentities = connector.loadTrustedIdentities(ownIdentity)
+               createPluginConnector("GetIdentitiesByScore", hasField("WantTrustValues", equalTo("true")))
+                               .connect { loadTrustedIdentities(ownIdentity) }
        }
 
        @Test
        fun `empty list of trusted identities is returned correctly`() {
-               val pluginConnector = createPluginConnector("GetIdentitiesByScore")
-               val connector = WebOfTrustConnector(pluginConnector)
-               val trustedIdentities = connector.loadTrustedIdentities(ownIdentity)
+               val trustedIdentities = createPluginConnector("GetIdentitiesByScore")
+                               .connect { loadTrustedIdentities(ownIdentity) }
                assertThat(trustedIdentities, empty())
        }
 
        @Test
        fun `trusted identities without context, properties, or trust value are returned correctly`() {
-               val pluginConnector = createPluginConnector("GetIdentitiesByScore") {
+               val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
                        put("Identity0", "id0")
                        put("Nickname0", "nickname0")
                        put("RequestURI0", "request-uri0")
                        put("Identity1", "id1")
                        put("Nickname1", "nickname1")
                        put("RequestURI1", "request-uri1")
-               }
-               val connector = WebOfTrustConnector(pluginConnector)
-               val trustedIdentities = connector.loadTrustedIdentities(ownIdentity)
+               }.connect { loadTrustedIdentities(ownIdentity) }
                assertThat(trustedIdentities, contains(
                                allOf(
                                                isIdentity("id0", "nickname0", "request-uri0", empty<String>(), isEmptyMap()),
@@ -120,15 +110,13 @@ class WebOfTrustConnectorTest {
 
        @Test
        fun `trusted identity with contexts is returned correctly`() {
-               val pluginConnector = createPluginConnector("GetIdentitiesByScore") {
+               val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
                        put("Identity0", "id0")
                        put("Nickname0", "nickname0")
                        put("RequestURI0", "request-uri0")
                        put("Contexts0.Context0", "Context0")
                        put("Contexts0.Context1", "Context1")
-               }
-               val connector = WebOfTrustConnector(pluginConnector)
-               val trustedIdentities = connector.loadTrustedIdentities(ownIdentity)
+               }.connect { loadTrustedIdentities(ownIdentity) }
                assertThat(trustedIdentities, contains(
                                isIdentity("id0", "nickname0", "request-uri0", containsInAnyOrder("Context0", "Context1"), isEmptyMap())
                ))
@@ -136,7 +124,7 @@ class WebOfTrustConnectorTest {
 
        @Test
        fun `trusted identity with properties is returned correctly`() {
-               val pluginConnector = createPluginConnector("GetIdentitiesByScore") {
+               val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
                        put("Identity0", "id0")
                        put("Nickname0", "nickname0")
                        put("RequestURI0", "request-uri0")
@@ -144,9 +132,7 @@ class WebOfTrustConnectorTest {
                        put("Properties0.Property0.Value", "bar")
                        put("Properties0.Property1.Name", "baz")
                        put("Properties0.Property1.Value", "quo")
-               }
-               val connector = WebOfTrustConnector(pluginConnector)
-               val trustedIdentities = connector.loadTrustedIdentities(ownIdentity)
+               }.connect { loadTrustedIdentities(ownIdentity) }
                assertThat(trustedIdentities, contains(
                                isIdentity("id0", "nickname0", "request-uri0", empty(), allOf(hasEntry("foo", "bar"), hasEntry("baz", "quo")))
                ))
@@ -154,16 +140,14 @@ class WebOfTrustConnectorTest {
 
        @Test
        fun `trusted identity with trust value is returned correctly`() {
-               val pluginConnector = createPluginConnector("GetIdentitiesByScore") {
+               val trustedIdentities = createPluginConnector("GetIdentitiesByScore") {
                        put("Identity0", "id0")
                        put("Nickname0", "nickname0")
                        put("RequestURI0", "request-uri0")
                        put("Trust0", "12")
                        put("Score0", "34")
                        put("Rank0", "56")
-               }
-               val connector = WebOfTrustConnector(pluginConnector)
-               val trustedIdentities = connector.loadTrustedIdentities(ownIdentity)
+               }.connect { loadTrustedIdentities(ownIdentity) }
                assertThat(trustedIdentities, contains(
                                allOf(
                                                isIdentity("id0", "nickname0", "request-uri0", empty(), isEmptyMap()),
@@ -172,8 +156,153 @@ class WebOfTrustConnectorTest {
                ))
        }
 
+       @Test
+       fun `adding a context sends the correct own identity id`() {
+               createPluginConnector("AddContext", hasField("Identity", equalTo(ownIdentity.id)))
+                               .connect { addContext(ownIdentity, "TestContext") }
+       }
+
+       @Test
+       fun `adding a context sends the correct context`() {
+               createPluginConnector("AddContext", hasField("Context", equalTo("TestContext")))
+                               .connect { addContext(ownIdentity, "TestContext") }
+       }
+
+       @Test
+       fun `removing a context sends the correct own identity id`() {
+               createPluginConnector("RemoveContext", hasField("Identity", equalTo(ownIdentity.id)))
+                               .connect { removeContext(ownIdentity, "TestContext") }
+       }
+
+       @Test
+       fun `removing a context sends the correct context`() {
+               createPluginConnector("RemoveContext", hasField("Context", equalTo("TestContext")))
+                               .connect { removeContext(ownIdentity, "TestContext") }
+       }
+
+       @Test
+       fun `setting a property sends the correct identity id`() {
+               createPluginConnector("SetProperty", hasField("Identity", equalTo(ownIdentity.id)))
+                               .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
+       }
+
+       @Test
+       fun `setting a property sends the correct property name`() {
+               createPluginConnector("SetProperty", hasField("Property", equalTo("TestProperty")))
+                               .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
+       }
+
+       @Test
+       fun `setting a property sends the correct property value`() {
+               createPluginConnector("SetProperty", hasField("Value", equalTo("TestValue")))
+                               .connect { setProperty(ownIdentity, "TestProperty", "TestValue") }
+       }
+
+       @Test
+       fun `removing a property sends the correct identity id`() {
+               createPluginConnector("RemoveProperty", hasField("Identity", equalTo(ownIdentity.id)))
+                               .connect { removeProperty(ownIdentity, "TestProperty") }
+       }
+
+       @Test
+       fun `removing a property sends the correct property name`() {
+               createPluginConnector("RemoveProperty", hasField("Property", equalTo("TestProperty")))
+                               .connect { removeProperty(ownIdentity, "TestProperty") }
+       }
+
+       @Test
+       fun `getting trust sends correct own identity id`() {
+               createPluginConnector("GetIdentity", hasField("Truster", equalTo(ownIdentity.id)))
+                               .connect { getTrust(ownIdentity, identity) }
+       }
+
+       @Test
+       fun `getting trust sends correct identity id`() {
+               createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id)))
+                               .connect { getTrust(ownIdentity, identity) }
+       }
+
+       @Test
+       fun `getting trust returns correct trust values`() {
+               val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
+                       put("Trust", "12")
+                       put("Score", "34")
+                       put("Rank", "56")
+               }.connect { getTrust(ownIdentity, identity) }
+               assertThat(trust, isTrust(12, 34, 56))
+       }
+
+       @Test
+       fun `getting trust reads incorrect numbers for trust as null`() {
+               val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
+                       put("Trust", "incorrect")
+                       put("Score", "34")
+                       put("Rank", "56")
+               }.connect { getTrust(ownIdentity, identity) }
+               assertThat(trust, isTrust(null, 34, 56))
+       }
+
+       @Test
+       fun `getting trust reads incorrect numbers for score as null`() {
+               val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
+                       put("Trust", "12")
+                       put("Score", "incorrect")
+                       put("Rank", "56")
+               }.connect { getTrust(ownIdentity, identity) }
+               assertThat(trust, isTrust(12, null, 56))
+       }
+
+       @Test
+       fun `getting trust reads incorrect numbers for rank as null`() {
+               val trust = createPluginConnector("GetIdentity", hasField("Identity", equalTo(identity.id))) {
+                       put("Trust", "12")
+                       put("Score", "34")
+                       put("Rank", "incorrect")
+               }.connect { getTrust(ownIdentity, identity) }
+               assertThat(trust, isTrust(12, 34, null))
+       }
+
+       @Test
+       fun `setting trust sends correct own identity id`() {
+               createPluginConnector("SetTrust", hasField("Truster", equalTo(ownIdentity.id)))
+                               .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
+       }
+
+       @Test
+       fun `setting trust sends correct identity id`() {
+               createPluginConnector("SetTrust", hasField("Trustee", equalTo(identity.id)))
+                               .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
+       }
+
+       @Test
+       fun `setting trust sends correct trust value`() {
+               createPluginConnector("SetTrust", hasField("Value", equalTo("123")))
+                               .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
+       }
+
+       @Test
+       fun `setting trust sends correct comment`() {
+               createPluginConnector("SetTrust", hasField("Comment", equalTo("Test Trust")))
+                               .connect { setTrust(ownIdentity, identity, 123, "Test Trust") }
+       }
+
+       @Test
+       fun `removing trust sends correct own identity id`() {
+               createPluginConnector("RemoveTrust", hasField("Truster", equalTo(ownIdentity.id)))
+                               .connect { removeTrust(ownIdentity, identity) }
+       }
+
+       @Test
+       fun `removing trust sends correct identity id`() {
+               createPluginConnector("RemoveTrust", hasField("Trustee", equalTo(identity.id)))
+                               .connect { removeTrust(ownIdentity, identity) }
+       }
+
 }
 
+private fun <R> PluginConnector.connect(block: WebOfTrustConnector.() -> R) =
+               WebOfTrustConnector(this).let(block)
+
 fun createPluginConnector(message: String, fieldsMatcher: Matcher<SimpleFieldSet> = IsAnything<SimpleFieldSet>(), build: SimpleFieldSetBuilder.() -> Unit = {}) =
                object : PluginConnector {
                        override fun sendRequest(pluginName: String, identifier: String, fields: SimpleFieldSet, data: Bucket?) =