Remove @author tags
[Sone.git] / src / test / java / net / pterodactylus / sone / freenet / wot / DefaultIdentityTest.java
1 /*
2  * Sone - DefaultIdentityTest.java - Copyright © 2013–2016 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.test.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 public class DefaultIdentityTest {
41
42         protected final DefaultIdentity identity = createIdentity();
43
44         protected DefaultIdentity createIdentity() {
45                 return new DefaultIdentity("Id", "Nickname", "RequestURI");
46         }
47
48         @Test
49         public void identityCanBeCreated() {
50                 assertThat(identity.getId(), is("Id"));
51                 assertThat(identity.getNickname(), is("Nickname"));
52                 assertThat(identity.getRequestUri(), is("RequestURI"));
53                 assertThat(identity.getContexts(), empty());
54                 assertThat(identity.getProperties(), is(Collections.<String, String>emptyMap()));
55         }
56
57         @Test
58         public void contextsAreAddedCorrectly() {
59                 identity.addContext("Test");
60                 assertThat(identity.getContexts(), contains("Test"));
61                 assertThat(identity.hasContext("Test"), is(true));
62         }
63
64         @Test
65         public void contextsAreRemovedCorrectly() {
66                 identity.addContext("Test");
67                 identity.removeContext("Test");
68                 assertThat(identity.getContexts(), empty());
69                 assertThat(identity.hasContext("Test"), is(false));
70         }
71
72         @Test
73         public void contextsAreSetCorrectlyInBulk() {
74                 identity.addContext("Test");
75                 identity.setContexts(asList("Test1", "Test2"));
76                 assertThat(identity.getContexts(), containsInAnyOrder("Test1", "Test2"));
77                 assertThat(identity.hasContext("Test"), is(false));
78                 assertThat(identity.hasContext("Test1"), is(true));
79                 assertThat(identity.hasContext("Test2"), is(true));
80         }
81
82         @Test
83         public void propertiesAreAddedCorrectly() {
84                 identity.setProperty("Key", "Value");
85                 assertThat(identity.getProperties().size(), is(1));
86                 assertThat(identity.getProperties(), hasEntry("Key", "Value"));
87                 assertThat(identity.getProperty("Key"), is("Value"));
88         }
89
90         @Test
91         public void propertiesAreRemovedCorrectly() {
92                 identity.setProperty("Key", "Value");
93                 identity.removeProperty("Key");
94                 assertThat(identity.getProperties(), is(Collections.<String, String>emptyMap()));
95                 assertThat(identity.getProperty("Key"), nullValue());
96         }
97
98         @Test
99         public void propertiesAreSetCorrectlyInBulk() {
100                 identity.setProperty("Key", "Value");
101                 identity.setProperties(of("Key1", "Value1", "Key2", "Value2"));
102                 assertThat(identity.getProperties().size(), is(2));
103                 assertThat(identity.getProperty("Key"), nullValue());
104                 assertThat(identity.getProperty("Key1"), is("Value1"));
105                 assertThat(identity.getProperty("Key2"), is("Value2"));
106         }
107
108         @Test
109         public void trustRelationshipsAreAddedCorrectly() {
110                 OwnIdentity ownIdentity = mock(OwnIdentity.class);
111                 Trust trust = mock(Trust.class);
112                 identity.setTrust(ownIdentity, trust);
113                 assertThat(identity.getTrust(ownIdentity), is(trust));
114         }
115
116         @Test
117         public void trustRelationshipsAreRemovedCorrectly() {
118                 OwnIdentity ownIdentity = mock(OwnIdentity.class);
119                 Trust trust = mock(Trust.class);
120                 identity.setTrust(ownIdentity, trust);
121                 identity.removeTrust(ownIdentity);
122                 assertThat(identity.getTrust(ownIdentity), nullValue());
123         }
124
125         @Test
126         public void identitiesWithTheSameIdAreEqual() {
127                 DefaultIdentity identity2 = new DefaultIdentity("Id", "Nickname2", "RequestURI2");
128                 assertThat(identity2, is(identity));
129                 assertThat(identity, is(identity2));
130         }
131
132         @Test
133         public void twoEqualIdentitiesHaveTheSameHashCode() {
134                 DefaultIdentity identity2 = new DefaultIdentity("Id", "Nickname2", "RequestURI2");
135                 assertThat(identity.hashCode(), is(identity2.hashCode()));
136         }
137
138         @Test
139         public void nullDoesNotMatchAnIdentity() {
140                 assertThat(identity, not(is((Object) null)));
141         }
142
143         @Test
144         public void toStringContainsIdAndNickname() {
145                 String identityString = identity.toString();
146                 assertThat(identityString, matchesRegex(".*\\bId\\b.*"));
147                 assertThat(identityString, matchesRegex(".*\\bNickname\\b.*"));
148         }
149
150 }