Add reparse filter
[Sone.git] / src / test / java / net / pterodactylus / sone / template / IdentityAccessorTest.java
1 package net.pterodactylus.sone.template;
2
3 import static java.util.Arrays.asList;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.when;
7
8 import java.util.HashSet;
9 import java.util.Set;
10
11 import net.pterodactylus.sone.core.Core;
12 import net.pterodactylus.sone.freenet.wot.Identity;
13 import net.pterodactylus.sone.freenet.wot.IdentityManager;
14 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
15
16 import org.hamcrest.Matchers;
17 import org.junit.Before;
18 import org.junit.Test;
19
20 /**
21  * Unit test for {@link IdentityAccessor}.
22  *
23  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
24  */
25 public class IdentityAccessorTest {
26
27         private static final String TEST_ID =
28                         "LrNQbyBBZW-7pHqChtp9lfPA7eXFPW~FLbJ2WrvEx5g";
29         private static final String TEST_ID_WITH_CHANGED_LETTER =
30                         "LrMQbyBBZW-7pHqChtp9lfPA7eXFPW~FLbJ2WrvEx5g";
31         private final Core core = mock(Core.class);
32         private final IdentityAccessor accessor = new IdentityAccessor(core);
33         private final IdentityManager identityManager =
34                         mock(IdentityManager.class);
35         private final OwnIdentity identity = mock(OwnIdentity.class);
36
37         @Before
38         public void setupCore() {
39                 when(core.getIdentityManager()).thenReturn(identityManager);
40         }
41
42         @Before
43         public void setupIdentity() {
44                 setupIdentity(identity, TEST_ID, "Test");
45         }
46
47         private void setupIdentity(Identity identity, String id,
48                         String nickname) {
49                 when(identity.getId()).thenReturn(id);
50                 when(identity.getNickname()).thenReturn(nickname);
51         }
52
53         private void serveIdentities(Set<OwnIdentity> identities) {
54                 when(identityManager.getAllOwnIdentities()).thenReturn(identities);
55         }
56
57         @Test
58         public void accessorReturnsTheCorrectlyAbbreviatedNickname() {
59                 OwnIdentity ownIdentity = mock(OwnIdentity.class);
60                 setupIdentity(ownIdentity, TEST_ID_WITH_CHANGED_LETTER, "Test");
61                 serveIdentities(new HashSet(asList(identity, ownIdentity)));
62                 assertThat(accessor.get(null, identity, "uniqueNickname"),
63                                 Matchers.<Object>is("Test@LrN"));
64         }
65
66         @Test
67         public void accessorComparesTheFullLengthIfNecessary() {
68                 OwnIdentity ownIdentity = mock(OwnIdentity.class);
69                 setupIdentity(ownIdentity, TEST_ID, "Test");
70                 serveIdentities(new HashSet(asList(identity, ownIdentity)));
71                 assertThat(accessor.get(null, identity, "uniqueNickname"),
72                                 Matchers.<Object>is("Test@" + TEST_ID));
73         }
74
75         @Test
76         public void reflectionAccessorIsUsedForOtherMembers() {
77                 assertThat(accessor.get(null, identity, "hashCode"),
78                                 Matchers.<Object>is(identity.hashCode()));
79         }
80
81 }