Add unit test for identity accessor.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 4 Nov 2014 06:07:30 +0000 (07:07 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 4 Nov 2014 06:07:30 +0000 (07:07 +0100)
src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java b/src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java
new file mode 100644 (file)
index 0000000..33149e0
--- /dev/null
@@ -0,0 +1,81 @@
+package net.pterodactylus.sone.template;
+
+import static java.util.Arrays.asList;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.freenet.wot.Identity;
+import net.pterodactylus.sone.freenet.wot.IdentityManager;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
+
+import org.hamcrest.Matchers;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IdentityAccessor}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class IdentityAccessorTest {
+
+       private static final String TEST_ID =
+                       "LrNQbyBBZW-7pHqChtp9lfPA7eXFPW~FLbJ2WrvEx5g";
+       private static final String TEST_ID_WITH_CHANGED_LETTER =
+                       "LrMQbyBBZW-7pHqChtp9lfPA7eXFPW~FLbJ2WrvEx5g";
+       private final Core core = mock(Core.class);
+       private final IdentityAccessor accessor = new IdentityAccessor(core);
+       private final IdentityManager identityManager =
+                       mock(IdentityManager.class);
+       private final OwnIdentity identity = mock(OwnIdentity.class);
+
+       @Before
+       public void setupCore() {
+               when(core.getIdentityManager()).thenReturn(identityManager);
+       }
+
+       @Before
+       public void setupIdentity() {
+               setupIdentity(identity, TEST_ID, "Test");
+       }
+
+       private void setupIdentity(Identity identity, String id,
+                       String nickname) {
+               when(identity.getId()).thenReturn(id);
+               when(identity.getNickname()).thenReturn(nickname);
+       }
+
+       private void serveIdentities(Set<OwnIdentity> identities) {
+               when(identityManager.getAllOwnIdentities()).thenReturn(identities);
+       }
+
+       @Test
+       public void accessorReturnsTheCorrectlyAbbreviatedNickname() {
+               OwnIdentity ownIdentity = mock(OwnIdentity.class);
+               setupIdentity(ownIdentity, TEST_ID_WITH_CHANGED_LETTER, "Test");
+               serveIdentities(new HashSet(asList(identity, ownIdentity)));
+               assertThat(accessor.get(null, identity, "uniqueNickname"),
+                               Matchers.<Object>is("Test@LrN"));
+       }
+
+       @Test
+       public void accessorComparesTheFullLengthIfNecessary() {
+               OwnIdentity ownIdentity = mock(OwnIdentity.class);
+               setupIdentity(ownIdentity, TEST_ID, "Test");
+               serveIdentities(new HashSet(asList(identity, ownIdentity)));
+               assertThat(accessor.get(null, identity, "uniqueNickname"),
+                               Matchers.<Object>is("Test@" + TEST_ID));
+       }
+
+       @Test
+       public void reflectionAccessorIsUsedForOtherMembers() {
+               assertThat(accessor.get(null, identity, "hashCode"),
+                               Matchers.<Object>is(identity.hashCode()));
+       }
+
+}