Make test timezone-independent
[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.hamcrest.Matchers.notNullValue;
6 import static org.hamcrest.Matchers.sameInstance;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.when;
9
10 import java.util.HashSet;
11 import java.util.Set;
12
13 import net.pterodactylus.sone.core.Core;
14 import net.pterodactylus.sone.freenet.wot.Identity;
15 import net.pterodactylus.sone.freenet.wot.IdentityManager;
16 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
17 import net.pterodactylus.sone.test.GuiceKt;
18
19 import com.google.inject.Guice;
20 import com.google.inject.Injector;
21 import org.hamcrest.Matchers;
22 import org.junit.Before;
23 import org.junit.Test;
24
25 /**
26  * Unit test for {@link IdentityAccessor}.
27  *
28  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29  */
30 public class IdentityAccessorTest {
31
32         private static final String TEST_ID =
33                         "LrNQbyBBZW-7pHqChtp9lfPA7eXFPW~FLbJ2WrvEx5g";
34         private static final String TEST_ID_WITH_CHANGED_LETTER =
35                         "LrMQbyBBZW-7pHqChtp9lfPA7eXFPW~FLbJ2WrvEx5g";
36         private final Core core = mock(Core.class);
37         private final IdentityAccessor accessor = new IdentityAccessor(core);
38         private final IdentityManager identityManager =
39                         mock(IdentityManager.class);
40         private final OwnIdentity identity = mock(OwnIdentity.class);
41
42         @Before
43         public void setupCore() {
44                 when(core.getIdentityManager()).thenReturn(identityManager);
45         }
46
47         @Before
48         public void setupIdentity() {
49                 setupIdentity(identity, TEST_ID, "Test");
50         }
51
52         private void setupIdentity(Identity identity, String id,
53                         String nickname) {
54                 when(identity.getId()).thenReturn(id);
55                 when(identity.getNickname()).thenReturn(nickname);
56         }
57
58         private void serveIdentities(Set<OwnIdentity> identities) {
59                 when(identityManager.getAllOwnIdentities()).thenReturn(identities);
60         }
61
62         @Test
63         public void accessorReturnsTheCorrectlyAbbreviatedNickname() {
64                 OwnIdentity ownIdentity = mock(OwnIdentity.class);
65                 setupIdentity(ownIdentity, TEST_ID_WITH_CHANGED_LETTER, "Test");
66                 serveIdentities(new HashSet(asList(identity, ownIdentity)));
67                 assertThat(accessor.get(null, identity, "uniqueNickname"),
68                                 Matchers.<Object>is("Test@LrN"));
69         }
70
71         @Test
72         public void accessorComparesTheFullLengthIfNecessary() {
73                 OwnIdentity ownIdentity = mock(OwnIdentity.class);
74                 setupIdentity(ownIdentity, TEST_ID, "Test");
75                 serveIdentities(new HashSet(asList(identity, ownIdentity)));
76                 assertThat(accessor.get(null, identity, "uniqueNickname"),
77                                 Matchers.<Object>is("Test@" + TEST_ID));
78         }
79
80         @Test
81         public void reflectionAccessorIsUsedForOtherMembers() {
82                 assertThat(accessor.get(null, identity, "hashCode"),
83                                 Matchers.<Object>is(identity.hashCode()));
84         }
85
86         @Test
87         public void accessorCanBeCreatedByGuice() {
88                 Injector injector = Guice.createInjector(
89                                 GuiceKt.supply(Core.class).byInstance(mock(Core.class))
90                 );
91                 assertThat(injector.getInstance(IdentityAccessor.class), notNullValue());
92         }
93
94         @Test
95         public void accessorIsCreatedAsSingleton() {
96                 Injector injector = Guice.createInjector(
97                                 GuiceKt.supply(Core.class).byInstance(mock(Core.class))
98                 );
99                 IdentityAccessor firstAccessor = injector.getInstance(IdentityAccessor.class);
100                 IdentityAccessor secondAccessor = injector.getInstance(IdentityAccessor.class);
101                 assertThat(firstAccessor, sameInstance(secondAccessor));
102         }
103
104 }