Add unit test for DefaultIdentity.
[Sone.git] / src / test / java / net / pterodactylus / sone / freenet / wot / DefaultIdentityTest.java
1 /*
2  * Sone - DefaultIdentityTest.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 java.util.Arrays.asList;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.containsInAnyOrder;
24 import static org.hamcrest.Matchers.empty;
25 import static org.hamcrest.Matchers.hasEntry;
26 import static org.hamcrest.Matchers.is;
27 import static org.hamcrest.Matchers.nullValue;
28 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
29 import static org.mockito.Mockito.mock;
30
31 import java.util.Collections;
32
33 import org.junit.Test;
34
35 /**
36  * Unit test for {@link DefaultIdentity}.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class DefaultIdentityTest {
41
42         private final DefaultIdentity identity = new DefaultIdentity("Id", "Nickname", "RequestURI");
43
44         @Test
45         public void identityCanBeCreated() {
46                 assertThat(identity.getId(), is("Id"));
47                 assertThat(identity.getNickname(), is("Nickname"));
48                 assertThat(identity.getRequestUri(), is("RequestURI"));
49                 assertThat(identity.getContexts(), empty());
50                 assertThat(identity.getProperties(), is(Collections.<String, String>emptyMap()));
51         }
52
53         @Test
54         public void contextsAreAddedCorrectly() {
55                 identity.addContext("Test");
56                 assertThat(identity.getContexts(), contains("Test"));
57                 assertThat(identity.hasContext("Test"), is(true));
58         }
59
60         @Test
61         public void contextsAreRemovedCorrectly() {
62                 identity.addContext("Test");
63                 identity.removeContext("Test");
64                 assertThat(identity.getContexts(), empty());
65                 assertThat(identity.hasContext("Test"), is(false));
66         }
67
68         @Test
69         public void contextsAreSetCorrectlyInBulk() {
70                 identity.addContext("Test");
71                 identity.setContexts(asList("Test1", "Test2"));
72                 assertThat(identity.getContexts(), containsInAnyOrder("Test1", "Test2"));
73                 assertThat(identity.hasContext("Test"), is(false));
74                 assertThat(identity.hasContext("Test1"), is(true));
75                 assertThat(identity.hasContext("Test2"), is(true));
76         }
77
78         @Test
79         public void propertiesAreAddedCorrectly() {
80                 identity.setProperty("Key", "Value");
81                 assertThat(identity.getProperties().size(), is(1));
82                 assertThat(identity.getProperties(), hasEntry("Key", "Value"));
83                 assertThat(identity.getProperty("Key"), is("Value"));
84         }
85
86         @Test
87         public void propertiesAreRemovedCorrectly() {
88                 identity.setProperty("Key", "Value");
89                 identity.removeProperty("Key");
90                 assertThat(identity.getProperties(), is(Collections.<String, String>emptyMap()));
91                 assertThat(identity.getProperty("Key"), nullValue());
92         }
93
94         @Test
95         public void propertiesAreSetCorrectlyInBulk() {
96                 identity.setProperty("Key", "Value");
97                 identity.setProperties(of("Key1", "Value1", "Key2", "Value2"));
98                 assertThat(identity.getProperties().size(), is(2));
99                 assertThat(identity.getProperty("Key"), nullValue());
100                 assertThat(identity.getProperty("Key1"), is("Value1"));
101                 assertThat(identity.getProperty("Key2"), is("Value2"));
102         }
103
104         @Test
105         public void trustRelationshipsAreAddedCorrectly() {
106                 OwnIdentity ownIdentity = mock(OwnIdentity.class);
107                 Trust trust = mock(Trust.class);
108                 identity.setTrust(ownIdentity, trust);
109                 assertThat(identity.getTrust(ownIdentity), is(trust));
110         }
111
112         @Test
113         public void trustRelationshipsAreRemovedCorrectly() {
114                 OwnIdentity ownIdentity = mock(OwnIdentity.class);
115                 Trust trust = mock(Trust.class);
116                 identity.setTrust(ownIdentity, trust);
117                 identity.removeTrust(ownIdentity);
118                 assertThat(identity.getTrust(ownIdentity), nullValue());
119         }
120
121         @Test
122         public void identitiesWithTheSameIdAreEqual() {
123                 DefaultIdentity identity2 = new DefaultIdentity("Id", "Nickname2", "RequestURI2");
124                 assertThat(identity2, is(identity));
125                 assertThat(identity, is(identity2));
126         }
127
128         @Test
129         public void twoEqualIdentitiesHaveTheSameHashCode() {
130                 DefaultIdentity identity2 = new DefaultIdentity("Id", "Nickname2", "RequestURI2");
131                 assertThat(identity.hashCode(), is(identity2.hashCode()));
132
133         }
134
135 }