Add unit test for Identity.
[Sone.git] / src / test / java / net / pterodactylus / sone / freenet / wot / IdentityTest.java
1 /*
2  * Sone - IdentityTest.java - Copyright © 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.freenet.wot;
19
20 import static com.google.common.collect.ImmutableMap.of;
21 import static com.google.common.collect.Sets.newHashSet;
22 import static net.pterodactylus.sone.freenet.wot.Identity.TO_CONTEXTS;
23 import static net.pterodactylus.sone.freenet.wot.Identity.TO_PROPERTIES;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.Matchers.containsInAnyOrder;
26 import static org.hamcrest.Matchers.empty;
27 import static org.hamcrest.Matchers.hasEntry;
28 import static org.hamcrest.Matchers.hasSize;
29 import static org.hamcrest.Matchers.is;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
32
33 import java.util.Map;
34 import java.util.Set;
35
36 import org.junit.Test;
37
38 /**
39  * Unit test for {@link Identity}.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class IdentityTest {
44
45         @Test
46         public void convertingAnIdentityIntoItsSones() {
47                 Identity identity = mock(Identity.class);
48                 when(identity.getContexts()).thenReturn(newHashSet("Test", "Test2"));
49                 Set<String> contexts = TO_CONTEXTS.apply(identity);
50                 assertThat(contexts, containsInAnyOrder("Test", "Test2"));
51         }
52
53         @Test
54         public void convertingNullIntoAnEmptySet() {
55                 assertThat(TO_CONTEXTS.apply(null), empty());
56         }
57
58         @Test
59         public void convertingAnIdentityIntoItsProperties() {
60                 Identity identity = mock(Identity.class);
61                 when(identity.getProperties()).thenReturn(of("KeyA", "ValueA", "KeyB", "ValueB"));
62                 Map<String, String> properties = TO_PROPERTIES.apply(identity);
63                 assertThat(properties.entrySet(), hasSize(2));
64                 assertThat(properties, hasEntry("KeyA", "ValueA"));
65                 assertThat(properties, hasEntry("KeyB", "ValueB"));
66         }
67
68         @Test
69         public void convertingNullIntoAnEmptyMap() {
70                 assertThat(TO_PROPERTIES.apply(null).size(), is(0));
71         }
72
73 }