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 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48 public class IdentityChangeEventSenderTest {
50 private final EventBus eventBus = mock(EventBus.class);
51 private final List<OwnIdentity> ownIdentities = asList(
52 createOwnIdentity("O1", asList("Test"), of("KeyA", "ValueA")),
53 createOwnIdentity("O2", asList("Test2"), of("KeyB", "ValueB")),
54 createOwnIdentity("O3", asList("Test3"), of("KeyC", "ValueC"))
56 private final List<Identity> identities = asList(
57 createIdentity("I1", Collections.<String>emptyList(), Collections.<String, String>emptyMap()),
58 createIdentity("I2", Collections.<String>emptyList(), Collections.<String, String>emptyMap()),
59 createIdentity("I3", Collections.<String>emptyList(), Collections.<String, String>emptyMap()),
60 createIdentity("I2", asList("Test"), Collections.<String, String>emptyMap())
62 private final IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, createOldIdentities());
65 public void addingAnOwnIdentityIsDetectedAndReportedCorrectly() {
66 Map<OwnIdentity, Collection<Identity>> newIdentities = createNewIdentities();
67 identityChangeEventSender.detectChanges(newIdentities);
68 verify(eventBus).post(eq(new OwnIdentityRemovedEvent(ownIdentities.get(0))));
69 verify(eventBus).post(eq(new IdentityRemovedEvent(ownIdentities.get(0), identities.get(0))));
70 verify(eventBus).post(eq(new IdentityRemovedEvent(ownIdentities.get(0), identities.get(1))));
71 verify(eventBus).post(eq(new OwnIdentityAddedEvent(ownIdentities.get(2))));
72 verify(eventBus).post(eq(new IdentityAddedEvent(ownIdentities.get(2), identities.get(1))));
73 verify(eventBus).post(eq(new IdentityAddedEvent(ownIdentities.get(2), identities.get(2))));
74 verify(eventBus).post(eq(new IdentityRemovedEvent(ownIdentities.get(1), identities.get(0))));
75 verify(eventBus).post(eq(new IdentityAddedEvent(ownIdentities.get(1), identities.get(2))));
76 verify(eventBus).post(eq(new IdentityUpdatedEvent(ownIdentities.get(1), identities.get(1))));
79 private Map<OwnIdentity, Collection<Identity>> createNewIdentities() {
80 Map<OwnIdentity, Collection<Identity>> oldIdentities = new HashMap<OwnIdentity, Collection<Identity>>();
81 oldIdentities.put(ownIdentities.get(1), asList(identities.get(3), identities.get(2)));
82 oldIdentities.put(ownIdentities.get(2), asList(identities.get(1), identities.get(2)));
86 private Map<OwnIdentity, Collection<Identity>> createOldIdentities() {
87 Map<OwnIdentity, Collection<Identity>> oldIdentities = new HashMap<OwnIdentity, Collection<Identity>>();
88 oldIdentities.put(ownIdentities.get(0), asList(identities.get(0), identities.get(1)));
89 oldIdentities.put(ownIdentities.get(1), asList(identities.get(0), identities.get(1)));