2 * Sone - IdentityChangeEventSenderTest.java - Copyright © 2013–2016 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 static com.google.common.collect.ImmutableMap.of;
21 import static java.util.Arrays.asList;
22 import static net.pterodactylus.sone.freenet.wot.Identities.createIdentity;
23 import static net.pterodactylus.sone.freenet.wot.Identities.createOwnIdentity;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
34 import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent;
35 import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent;
36 import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent;
37 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent;
38 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
40 import com.google.common.eventbus.EventBus;
41 import org.junit.Test;
44 * Unit test for {@link IdentityChangeEventSender}.
46 public class IdentityChangeEventSenderTest {
48 private final EventBus eventBus = mock(EventBus.class);
49 private final List<OwnIdentity> ownIdentities = asList(
50 createOwnIdentity("O1", asList("Test"), of("KeyA", "ValueA")),
51 createOwnIdentity("O2", asList("Test2"), of("KeyB", "ValueB")),
52 createOwnIdentity("O3", asList("Test3"), of("KeyC", "ValueC"))
54 private final List<Identity> identities = asList(
55 createIdentity("I1", Collections.<String>emptyList(), Collections.<String, String>emptyMap()),
56 createIdentity("I2", Collections.<String>emptyList(), Collections.<String, String>emptyMap()),
57 createIdentity("I3", Collections.<String>emptyList(), Collections.<String, String>emptyMap()),
58 createIdentity("I2", asList("Test"), Collections.<String, String>emptyMap())
60 private final IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, createOldIdentities());
63 public void addingAnOwnIdentityIsDetectedAndReportedCorrectly() {
64 Map<OwnIdentity, Collection<Identity>> newIdentities = createNewIdentities();
65 identityChangeEventSender.detectChanges(newIdentities);
66 verify(eventBus).post(eq(new OwnIdentityRemovedEvent(ownIdentities.get(0))));
67 verify(eventBus).post(eq(new IdentityRemovedEvent(ownIdentities.get(0), identities.get(0))));
68 verify(eventBus).post(eq(new IdentityRemovedEvent(ownIdentities.get(0), identities.get(1))));
69 verify(eventBus).post(eq(new OwnIdentityAddedEvent(ownIdentities.get(2))));
70 verify(eventBus).post(eq(new IdentityAddedEvent(ownIdentities.get(2), identities.get(1))));
71 verify(eventBus).post(eq(new IdentityAddedEvent(ownIdentities.get(2), identities.get(2))));
72 verify(eventBus).post(eq(new IdentityRemovedEvent(ownIdentities.get(1), identities.get(0))));
73 verify(eventBus).post(eq(new IdentityAddedEvent(ownIdentities.get(1), identities.get(2))));
74 verify(eventBus).post(eq(new IdentityUpdatedEvent(ownIdentities.get(1), identities.get(1))));
77 private Map<OwnIdentity, Collection<Identity>> createNewIdentities() {
78 Map<OwnIdentity, Collection<Identity>> oldIdentities = new HashMap<>();
79 oldIdentities.put(ownIdentities.get(1), asList(identities.get(3), identities.get(2)));
80 oldIdentities.put(ownIdentities.get(2), asList(identities.get(1), identities.get(2)));
84 private Map<OwnIdentity, Collection<Identity>> createOldIdentities() {
85 Map<OwnIdentity, Collection<Identity>> oldIdentities = new HashMap<>();
86 oldIdentities.put(ownIdentities.get(0), asList(identities.get(0), identities.get(1)));
87 oldIdentities.put(ownIdentities.get(1), asList(identities.get(0), identities.get(1)));