From: David ‘Bombe’ Roden Date: Thu, 14 Nov 2013 21:41:40 +0000 (+0100) Subject: Add unit test for IdentityChangeEventSender. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=a05ce7308e3cc981f97756a374e1a5bb0f34e51d;p=Sone.git Add unit test for IdentityChangeEventSender. --- diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java new file mode 100644 index 0000000..fe4f070 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java @@ -0,0 +1,96 @@ +/* + * Sone - IdentityChangeEventSenderTest.java - Copyright © 2013 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.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.util.Collections; +import java.util.List; + +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.collect.ImmutableMultimap; +import com.google.common.collect.Multimap; +import com.google.common.eventbus.EventBus; +import org.junit.Test; + +/** + * Unit test for {@link IdentityChangeEventSender}. + * + * @author David ‘Bombe’ Roden + */ +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() { + Multimap 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 Multimap createNewIdentities() { + ImmutableMultimap.Builder oldIdentities = ImmutableMultimap.builder(); + oldIdentities.put(ownIdentities.get(1), identities.get(3)); + oldIdentities.put(ownIdentities.get(1), identities.get(2)); + oldIdentities.put(ownIdentities.get(2), identities.get(1)); + oldIdentities.put(ownIdentities.get(2), identities.get(2)); + return oldIdentities.build(); + } + + private Multimap createOldIdentities() { + ImmutableMultimap.Builder oldIdentities = ImmutableMultimap.builder(); + oldIdentities.put(ownIdentities.get(0), identities.get(0)); + oldIdentities.put(ownIdentities.get(0), identities.get(1)); + oldIdentities.put(ownIdentities.get(1), identities.get(0)); + oldIdentities.put(ownIdentities.get(1), identities.get(1)); + return oldIdentities.build(); + } + +}