2 * Sone - IdentityChangeEventSenderTest.java - Copyright © 2013–2019 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.freenet.wot
20 import com.google.common.eventbus.*
21 import net.pterodactylus.sone.freenet.wot.Identities.*
22 import net.pterodactylus.sone.freenet.wot.event.*
23 import net.pterodactylus.sone.test.*
25 import org.mockito.ArgumentMatchers.eq
26 import org.mockito.Mockito.verify
29 * Unit test for [IdentityChangeEventSender].
31 class IdentityChangeEventSenderTest {
33 private val eventBus = mock<EventBus>()
34 private val ownIdentities = listOf(
35 createOwnIdentity("O1", listOf("Test"), mapOf("KeyA" to "ValueA")),
36 createOwnIdentity("O2", listOf("Test2"), mapOf("KeyB" to "ValueB")),
37 createOwnIdentity("O3", listOf("Test3"), mapOf("KeyC" to "ValueC"))
39 private val identities = listOf(
40 createIdentity("I1", listOf(), mapOf()),
41 createIdentity("I2", listOf(), mapOf()),
42 createIdentity("I3", listOf(), mapOf()),
43 createIdentity("I2", listOf("Test"), mapOf())
45 private val identityChangeEventSender = IdentityChangeEventSender(eventBus, createOldIdentities())
48 fun addingAnOwnIdentityIsDetectedAndReportedCorrectly() {
49 val newIdentities = createNewIdentities()
50 identityChangeEventSender.detectChanges(newIdentities)
51 verify(eventBus).post(eq(OwnIdentityRemovedEvent(ownIdentities[0])))
52 verify(eventBus).post(eq(IdentityRemovedEvent(ownIdentities[0], identities[0])))
53 verify(eventBus).post(eq(IdentityRemovedEvent(ownIdentities[0], identities[1])))
54 verify(eventBus).post(eq(OwnIdentityAddedEvent(ownIdentities[2])))
55 verify(eventBus).post(eq(IdentityAddedEvent(ownIdentities[2], identities[1])))
56 verify(eventBus).post(eq(IdentityAddedEvent(ownIdentities[2], identities[2])))
57 verify(eventBus).post(eq(IdentityRemovedEvent(ownIdentities[1], identities[0])))
58 verify(eventBus).post(eq(IdentityAddedEvent(ownIdentities[1], identities[2])))
59 verify(eventBus).post(eq(IdentityUpdatedEvent(ownIdentities[1], identities[1])))
62 private fun createNewIdentities() = mapOf(
63 ownIdentities[1] to listOf(identities[3], identities[2]),
64 ownIdentities[2] to listOf(identities[1], identities[2])
67 private fun createOldIdentities() = mapOf(
68 ownIdentities[0] to listOf(identities[0], identities[1]),
69 ownIdentities[1] to listOf(identities[0], identities[1])