Add unit test for IdentityAccessor.
[Sone.git] / src / test / java / net / pterodactylus / sone / template / IdentityAccessorTest.java
1 /*
2  * Sone - IdentityAccessorTest.java - Copyright © 2010–2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.template;
19
20 import static com.google.common.collect.Sets.newHashSet;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.is;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import net.pterodactylus.sone.data.Mocks;
27 import net.pterodactylus.sone.freenet.wot.DefaultOwnIdentity;
28 import net.pterodactylus.sone.freenet.wot.IdentityManager;
29 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
30
31 import org.junit.Before;
32 import org.junit.Test;
33
34 /**
35  * Unit test for {@link IdentityAccessor}.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class IdentityAccessorTest {
40
41         private final Mocks mocks = new Mocks();
42         private final IdentityAccessor identityAccessor = new IdentityAccessor(mocks.core);
43
44         @Before
45         public void setup() {
46                 IdentityManager identityManager = mock(IdentityManager.class);
47                 when(mocks.core.getIdentityManager()).thenReturn(identityManager);
48         }
49
50         @Test
51         public void uniqueNicknameWithoutIdIsFound() {
52                 when(mocks.core.getIdentityManager().getAllOwnIdentities()).thenReturn(newHashSet(
53                                 (OwnIdentity) new DefaultOwnIdentity("Test", "Test", "R", "I")
54                 ));
55                 OwnIdentity ownIdentity = new DefaultOwnIdentity("Unique", "Unique", "R", "I");
56                 String uniqueNickname = (String) identityAccessor.get(null, ownIdentity, "uniqueNickname");
57                 assertThat(uniqueNickname, is("Unique"));
58         }
59
60         @Test
61         public void uniqueNicknameWithIdIsFound() {
62                 OwnIdentity ownIdentity = new DefaultOwnIdentity("Unique", "Unique", "R", "I");
63                 when(mocks.core.getIdentityManager().getAllOwnIdentities()).thenReturn(newHashSet(
64                                 ownIdentity,
65                                 new DefaultOwnIdentity("Unequivocal", "Unique", "R", "I")
66                 ));
67                 String uniqueNickname = (String) identityAccessor.get(null, ownIdentity, "uniqueNickname");
68                 assertThat(uniqueNickname, is("Unique@Uni"));
69         }
70
71         @Test
72         public void reflectionAccessorIsUsed() {
73                 OwnIdentity ownIdentity = new DefaultOwnIdentity("Unique", "Unique", "R", "I");
74                 String id = (String) identityAccessor.get(null, ownIdentity, "id");
75                 assertThat(id, is(ownIdentity.getId()));
76         }
77
78 }