From: David ‘Bombe’ Roden Date: Thu, 19 Sep 2019 19:42:55 +0000 (+0200) Subject: 🎨 Replace IdentityChangeDetectorTest with Kotlin version X-Git-Tag: v81^2~124 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=c81b4be138453811e354898e842585a933050c4f;p=Sone.git 🎨 Replace IdentityChangeDetectorTest with Kotlin version --- diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.java deleted file mode 100644 index e1eb1c8..0000000 --- a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.java +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Sone - IdentityChangeDetectorTest.java - Copyright © 2013–2019 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.freenet.wot; - -import static com.google.common.collect.ImmutableMap.of; -import static com.google.common.collect.Lists.newArrayList; -import static java.util.Arrays.asList; -import static net.pterodactylus.sone.freenet.wot.Identities.createIdentity; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.Matchers.empty; - -import java.util.Collection; - -import net.pterodactylus.sone.freenet.wot.IdentityChangeDetector.IdentityProcessor; - -import org.junit.Before; -import org.junit.Test; - -/** - * Unit test for {@link IdentityChangeDetector}. - */ -public class IdentityChangeDetectorTest { - - private final IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(createOldIdentities()); - private final Collection newIdentities = newArrayList(); - private final Collection removedIdentities = newArrayList(); - private final Collection changedIdentities = newArrayList(); - private final Collection unchangedIdentities = newArrayList(); - - @Before - public void setup() { - identityChangeDetector.onNewIdentity(new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - newIdentities.add(identity); - } - }); - identityChangeDetector.onRemovedIdentity(new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - removedIdentities.add(identity); - } - }); - identityChangeDetector.onChangedIdentity(new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - changedIdentities.add(identity); - } - }); - identityChangeDetector.onUnchangedIdentity(new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - unchangedIdentities.add(identity); - } - }); - } - - @Test - public void noDifferencesAreDetectedWhenSendingTheOldIdentitiesAgain() { - identityChangeDetector.detectChanges(createOldIdentities()); - assertThat(newIdentities, empty()); - assertThat(removedIdentities, empty()); - assertThat(changedIdentities, empty()); - assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3())); - } - - @Test - public void detectThatAnIdentityWasRemoved() { - identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity3())); - assertThat(newIdentities, empty()); - assertThat(removedIdentities, containsInAnyOrder(createIdentity2())); - assertThat(changedIdentities, empty()); - assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3())); - } - - @Test - public void detectThatAnIdentityWasAdded() { - identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4())); - assertThat(newIdentities, containsInAnyOrder(createIdentity4())); - assertThat(removedIdentities, empty()); - assertThat(changedIdentities, empty()); - assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3())); - } - - @Test - public void detectThatAContextWasRemoved() { - Identity identity2 = createIdentity2(); - identity2.removeContext("Context C"); - identityChangeDetector.detectChanges(asList(createIdentity1(), identity2, createIdentity3())); - assertThat(newIdentities, empty()); - assertThat(removedIdentities, empty()); - assertThat(changedIdentities, containsInAnyOrder(identity2)); - assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3())); - } - - @Test - public void detectThatAContextWasAdded() { - Identity identity2 = createIdentity2(); - identity2.addContext("Context C1"); - identityChangeDetector.detectChanges(asList(createIdentity1(), identity2, createIdentity3())); - assertThat(newIdentities, empty()); - assertThat(removedIdentities, empty()); - assertThat(changedIdentities, containsInAnyOrder(identity2)); - assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3())); - } - - @Test - public void detectThatAPropertyWasRemoved() { - Identity identity1 = createIdentity1(); - identity1.removeProperty("Key A"); - identityChangeDetector.detectChanges(asList(identity1, createIdentity2(), createIdentity3())); - assertThat(newIdentities, empty()); - assertThat(removedIdentities, empty()); - assertThat(changedIdentities, containsInAnyOrder(identity1)); - assertThat(unchangedIdentities, containsInAnyOrder(createIdentity2(), createIdentity3())); - } - - @Test - public void detectThatAPropertyWasAdded() { - Identity identity3 = createIdentity3(); - identity3.setProperty("Key A", "Value A"); - identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), identity3)); - assertThat(newIdentities, empty()); - assertThat(removedIdentities, empty()); - assertThat(changedIdentities, containsInAnyOrder(identity3)); - assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2())); - } - - @Test - public void detectThatAPropertyWasChanged() { - Identity identity3 = createIdentity3(); - identity3.setProperty("Key E", "Value F"); - identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), identity3)); - assertThat(newIdentities, empty()); - assertThat(removedIdentities, empty()); - assertThat(changedIdentities, containsInAnyOrder(identity3)); - assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2())); - } - - @Test - public void noRemovedIdentitiesAreDetectedWithoutAnIdentityProcessor() { - identityChangeDetector.onRemovedIdentity(null); - identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity3())); - } - - @Test - public void noAddedIdentitiesAreDetectedWithoutAnIdentityProcessor() { - identityChangeDetector.onNewIdentity(null); - identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4())); - } - - private static Collection createOldIdentities() { - return asList(createIdentity1(), createIdentity2(), createIdentity3()); - } - - private static Identity createIdentity1() { - return createIdentity("Test1", asList("Context A", "Context B"), of("Key A", "Value A", "Key B", "Value B")); - } - - private static Identity createIdentity2() { - return createIdentity("Test2", asList("Context C", "Context D"), of("Key C", "Value C", "Key D", "Value D")); - } - - private static Identity createIdentity3() { - return createIdentity("Test3", asList("Context E", "Context F"), of("Key E", "Value E", "Key F", "Value F")); - } - - private static Identity createIdentity4() { - return createIdentity("Test4", asList("Context G", "Context H"), of("Key G", "Value G", "Key H", "Value H")); - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.kt new file mode 100644 index 0000000..188a60a --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.kt @@ -0,0 +1,161 @@ +/* + * Sone - IdentityChangeDetectorTest.java - Copyright © 2013–2019 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.freenet.wot + +import net.pterodactylus.sone.freenet.wot.Identities.createIdentity +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.containsInAnyOrder +import org.hamcrest.Matchers.empty + +import org.junit.Before +import org.junit.Test + +/** + * Unit test for [IdentityChangeDetector]. + */ +class IdentityChangeDetectorTest { + + private val identityChangeDetector = IdentityChangeDetector(createOldIdentities()) + private val newIdentities = mutableListOf() + private val removedIdentities = mutableListOf() + private val changedIdentities = mutableListOf() + private val unchangedIdentities = mutableListOf() + + @Before + fun setup() { + identityChangeDetector.onNewIdentity { identity -> newIdentities.add(identity) } + identityChangeDetector.onRemovedIdentity { identity -> removedIdentities.add(identity) } + identityChangeDetector.onChangedIdentity { identity -> changedIdentities.add(identity) } + identityChangeDetector.onUnchangedIdentity { identity -> unchangedIdentities.add(identity) } + } + + @Test + fun `no differences are detected when sending the old identities again`() { + identityChangeDetector.detectChanges(createOldIdentities()) + assertThat>(newIdentities, empty()) + assertThat>(removedIdentities, empty()) + assertThat>(changedIdentities, empty()) + assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3())) + } + + @Test + fun `detect that an identity was removed`() { + identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity3())) + assertThat>(newIdentities, empty()) + assertThat>(removedIdentities, containsInAnyOrder(createIdentity2())) + assertThat>(changedIdentities, empty()) + assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3())) + } + + @Test + fun `detect that an identity was added`() { + identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4())) + assertThat>(newIdentities, containsInAnyOrder(createIdentity4())) + assertThat>(removedIdentities, empty()) + assertThat>(changedIdentities, empty()) + assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3())) + } + + @Test + fun `detect that a context was removed`() { + val identity2 = createIdentity2() + identity2.removeContext("Context C") + identityChangeDetector.detectChanges(listOf(createIdentity1(), identity2, createIdentity3())) + assertThat>(newIdentities, empty()) + assertThat>(removedIdentities, empty()) + assertThat>(changedIdentities, containsInAnyOrder(identity2)) + assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3())) + } + + @Test + fun `detect that a context was added`() { + val identity2 = createIdentity2() + identity2.addContext("Context C1") + identityChangeDetector.detectChanges(listOf(createIdentity1(), identity2, createIdentity3())) + assertThat>(newIdentities, empty()) + assertThat>(removedIdentities, empty()) + assertThat>(changedIdentities, containsInAnyOrder(identity2)) + assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3())) + } + + @Test + fun `detect that a property was removed`() { + val identity1 = createIdentity1() + identity1.removeProperty("Key A") + identityChangeDetector.detectChanges(listOf(identity1, createIdentity2(), createIdentity3())) + assertThat>(newIdentities, empty()) + assertThat>(removedIdentities, empty()) + assertThat>(changedIdentities, containsInAnyOrder(identity1)) + assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity2(), createIdentity3())) + } + + @Test + fun `detect that a property was added`() { + val identity3 = createIdentity3() + identity3.setProperty("Key A", "Value A") + identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity2(), identity3)) + assertThat>(newIdentities, empty()) + assertThat>(removedIdentities, empty()) + assertThat>(changedIdentities, containsInAnyOrder(identity3)) + assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2())) + } + + @Test + fun `detect that a property was changed`() { + val identity3 = createIdentity3() + identity3.setProperty("Key E", "Value F") + identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity2(), identity3)) + assertThat>(newIdentities, empty()) + assertThat>(removedIdentities, empty()) + assertThat>(changedIdentities, containsInAnyOrder(identity3)) + assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2())) + } + + @Test + fun `no removed identities are detected without an identity processor`() { + identityChangeDetector.onRemovedIdentity(null) + identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity3())) + } + + @Test + fun `no added identities are detected without an identity processor`() { + identityChangeDetector.onNewIdentity(null) + identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4())) + } + + private fun createOldIdentities(): Collection { + return listOf(createIdentity1(), createIdentity2(), createIdentity3()) + } + + private fun createIdentity1(): Identity { + return createIdentity("Test1", listOf("Context A", "Context B"), mapOf("Key A" to "Value A", "Key B" to "Value B")) + } + + private fun createIdentity2(): Identity { + return createIdentity("Test2", listOf("Context C", "Context D"), mapOf("Key C" to "Value C", "Key D" to "Value D")) + } + + private fun createIdentity3(): Identity { + return createIdentity("Test3", listOf("Context E", "Context F"), mapOf("Key E" to "Value E", "Key F" to "Value F")) + } + + private fun createIdentity4(): Identity { + return createIdentity("Test4", listOf("Context G", "Context H"), mapOf("Key G" to "Value G", "Key H" to "Value H")) + } + +}