From: David ‘Bombe’ Roden Date: Fri, 20 Sep 2019 05:39:00 +0000 (+0200) Subject: 🎨 Replace IdentityChangeEventSenderTest with Kotlin version X-Git-Tag: v81^2~123 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=7e812259c2df1d4cff6ed622ca4c84f4f73d6e8b;p=Sone.git 🎨 Replace IdentityChangeEventSenderTest with Kotlin version --- diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/Identities.java b/src/test/java/net/pterodactylus/sone/freenet/wot/Identities.java index 7ed2b59..089cc5a 100644 --- a/src/test/java/net/pterodactylus/sone/freenet/wot/Identities.java +++ b/src/test/java/net/pterodactylus/sone/freenet/wot/Identities.java @@ -19,18 +19,21 @@ package net.pterodactylus.sone.freenet.wot; import java.util.Collection; import java.util.Map; +import javax.annotation.*; /** * Creates {@link Identity}s and {@link OwnIdentity}s. */ public class Identities { + @Nonnull public static OwnIdentity createOwnIdentity(String id, Collection contexts, Map properties) { DefaultOwnIdentity ownIdentity = new DefaultOwnIdentity(id, "Nickname" + id, "Request" + id, "Insert" + id); setContextsAndPropertiesOnIdentity(ownIdentity, contexts, properties); return ownIdentity; } + @Nonnull public static Identity createIdentity(String id, Collection contexts, Map properties) { DefaultIdentity identity = new DefaultIdentity(id, "Nickname" + id, "Request" + id); setContextsAndPropertiesOnIdentity(identity, contexts, properties); diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java deleted file mode 100644 index ff442a4..0000000 --- a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Sone - IdentityChangeEventSenderTest.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 java.util.Arrays.asList; -import static net.pterodactylus.sone.freenet.wot.Identities.createIdentity; -import static net.pterodactylus.sone.freenet.wot.Identities.createOwnIdentity; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent; -import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent; -import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent; -import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent; -import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent; - -import com.google.common.eventbus.EventBus; -import org.junit.Test; - -/** - * Unit test for {@link IdentityChangeEventSender}. - */ -public class IdentityChangeEventSenderTest { - - private final EventBus eventBus = mock(EventBus.class); - private final List ownIdentities = asList( - createOwnIdentity("O1", asList("Test"), of("KeyA", "ValueA")), - createOwnIdentity("O2", asList("Test2"), of("KeyB", "ValueB")), - createOwnIdentity("O3", asList("Test3"), of("KeyC", "ValueC")) - ); - private final List identities = asList( - createIdentity("I1", Collections.emptyList(), Collections.emptyMap()), - createIdentity("I2", Collections.emptyList(), Collections.emptyMap()), - createIdentity("I3", Collections.emptyList(), Collections.emptyMap()), - createIdentity("I2", asList("Test"), Collections.emptyMap()) - ); - private final IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, createOldIdentities()); - - @Test - public void addingAnOwnIdentityIsDetectedAndReportedCorrectly() { - Map> newIdentities = createNewIdentities(); - identityChangeEventSender.detectChanges(newIdentities); - verify(eventBus).post(eq(new OwnIdentityRemovedEvent(ownIdentities.get(0)))); - verify(eventBus).post(eq(new IdentityRemovedEvent(ownIdentities.get(0), identities.get(0)))); - verify(eventBus).post(eq(new IdentityRemovedEvent(ownIdentities.get(0), identities.get(1)))); - verify(eventBus).post(eq(new OwnIdentityAddedEvent(ownIdentities.get(2)))); - verify(eventBus).post(eq(new IdentityAddedEvent(ownIdentities.get(2), identities.get(1)))); - verify(eventBus).post(eq(new IdentityAddedEvent(ownIdentities.get(2), identities.get(2)))); - verify(eventBus).post(eq(new IdentityRemovedEvent(ownIdentities.get(1), identities.get(0)))); - verify(eventBus).post(eq(new IdentityAddedEvent(ownIdentities.get(1), identities.get(2)))); - verify(eventBus).post(eq(new IdentityUpdatedEvent(ownIdentities.get(1), identities.get(1)))); - } - - private Map> createNewIdentities() { - Map> oldIdentities = new HashMap<>(); - oldIdentities.put(ownIdentities.get(1), asList(identities.get(3), identities.get(2))); - oldIdentities.put(ownIdentities.get(2), asList(identities.get(1), identities.get(2))); - return oldIdentities; - } - - private Map> createOldIdentities() { - Map> oldIdentities = new HashMap<>(); - oldIdentities.put(ownIdentities.get(0), asList(identities.get(0), identities.get(1))); - oldIdentities.put(ownIdentities.get(1), asList(identities.get(0), identities.get(1))); - return oldIdentities; - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.kt new file mode 100644 index 0000000..69fc9d4 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.kt @@ -0,0 +1,72 @@ +/* + * Sone - IdentityChangeEventSenderTest.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 com.google.common.eventbus.* +import net.pterodactylus.sone.freenet.wot.Identities.* +import net.pterodactylus.sone.freenet.wot.event.* +import net.pterodactylus.sone.test.* +import org.junit.* +import org.mockito.ArgumentMatchers.eq +import org.mockito.Mockito.verify + +/** + * Unit test for [IdentityChangeEventSender]. + */ +class IdentityChangeEventSenderTest { + + private val eventBus = mock() + private val ownIdentities = listOf( + createOwnIdentity("O1", listOf("Test"), mapOf("KeyA" to "ValueA")), + createOwnIdentity("O2", listOf("Test2"), mapOf("KeyB" to "ValueB")), + createOwnIdentity("O3", listOf("Test3"), mapOf("KeyC" to "ValueC")) + ) + private val identities = listOf( + createIdentity("I1", listOf(), mapOf()), + createIdentity("I2", listOf(), mapOf()), + createIdentity("I3", listOf(), mapOf()), + createIdentity("I2", listOf("Test"), mapOf()) + ) + private val identityChangeEventSender = IdentityChangeEventSender(eventBus, createOldIdentities()) + + @Test + fun addingAnOwnIdentityIsDetectedAndReportedCorrectly() { + val newIdentities = createNewIdentities() + identityChangeEventSender.detectChanges(newIdentities) + verify(eventBus).post(eq(OwnIdentityRemovedEvent(ownIdentities[0]))) + verify(eventBus).post(eq(IdentityRemovedEvent(ownIdentities[0], identities[0]))) + verify(eventBus).post(eq(IdentityRemovedEvent(ownIdentities[0], identities[1]))) + verify(eventBus).post(eq(OwnIdentityAddedEvent(ownIdentities[2]))) + verify(eventBus).post(eq(IdentityAddedEvent(ownIdentities[2], identities[1]))) + verify(eventBus).post(eq(IdentityAddedEvent(ownIdentities[2], identities[2]))) + verify(eventBus).post(eq(IdentityRemovedEvent(ownIdentities[1], identities[0]))) + verify(eventBus).post(eq(IdentityAddedEvent(ownIdentities[1], identities[2]))) + verify(eventBus).post(eq(IdentityUpdatedEvent(ownIdentities[1], identities[1]))) + } + + private fun createNewIdentities() = mapOf( + ownIdentities[1] to listOf(identities[3], identities[2]), + ownIdentities[2] to listOf(identities[1], identities[2]) + ) + + private fun createOldIdentities() = mapOf( + ownIdentities[0] to listOf(identities[0], identities[1]), + ownIdentities[1] to listOf(identities[0], identities[1]) + ) + +}