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