🔀 Merge next
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / freenet / wot / IdentityChangeEventSenderTest.kt
1 /*
2  * Sone - IdentityChangeEventSenderTest.java - Copyright Â© 2013–2019 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.freenet.wot
19
20 import com.google.common.eventbus.*
21 import net.pterodactylus.sone.freenet.wot.event.*
22 import net.pterodactylus.sone.test.*
23 import org.junit.*
24 import org.mockito.ArgumentMatchers.eq
25 import org.mockito.Mockito.verify
26
27 /**
28  * Unit test for [IdentityChangeEventSender].
29  */
30 class IdentityChangeEventSenderTest {
31
32         private val eventBus = mock<EventBus>()
33         private val ownIdentities = listOf(
34                         createOwnIdentity("O1", setOf("Test"), "KeyA" to "ValueA"),
35                         createOwnIdentity("O2", setOf("Test2"), "KeyB" to "ValueB"),
36                         createOwnIdentity("O3", setOf("Test3"), "KeyC" to "ValueC")
37         )
38         private val identities = listOf(
39                         createIdentity("I1", setOf()),
40                         createIdentity("I2", setOf()),
41                         createIdentity("I3", setOf()),
42                         createIdentity("I2", setOf("Test"))
43         )
44         private val identityChangeEventSender = IdentityChangeEventSender(eventBus, createOldIdentities())
45
46         @Test
47         fun addingAnOwnIdentityIsDetectedAndReportedCorrectly() {
48                 val newIdentities = createNewIdentities()
49                 identityChangeEventSender.detectChanges(newIdentities)
50                 verify(eventBus).post(eq(OwnIdentityRemovedEvent(ownIdentities[0])))
51                 verify(eventBus).post(eq(IdentityRemovedEvent(ownIdentities[0], identities[0])))
52                 verify(eventBus).post(eq(IdentityRemovedEvent(ownIdentities[0], identities[1])))
53                 verify(eventBus).post(eq(OwnIdentityAddedEvent(ownIdentities[2])))
54                 verify(eventBus).post(eq(IdentityAddedEvent(ownIdentities[2], identities[1])))
55                 verify(eventBus).post(eq(IdentityAddedEvent(ownIdentities[2], identities[2])))
56                 verify(eventBus).post(eq(IdentityRemovedEvent(ownIdentities[1], identities[0])))
57                 verify(eventBus).post(eq(IdentityAddedEvent(ownIdentities[1], identities[2])))
58                 verify(eventBus).post(eq(IdentityUpdatedEvent(ownIdentities[1], identities[1])))
59         }
60
61         private fun createNewIdentities() = mapOf(
62                         ownIdentities[1] to listOf(identities[3], identities[2]),
63                         ownIdentities[2] to listOf(identities[1], identities[2])
64         )
65
66         private fun createOldIdentities() = mapOf(
67                         ownIdentities[0] to listOf(identities[0], identities[1]),
68                         ownIdentities[1] to listOf(identities[0], identities[1])
69         )
70
71 }