2 * Sone - DefaultIdentityTest.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.freenet.wot;
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.not;
28 import static org.hamcrest.Matchers.nullValue;
29 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
30 import static org.mockito.Mockito.mock;
32 import java.util.Collections;
34 import net.pterodactylus.sone.Matchers;
36 import org.junit.Test;
39 * Unit test for {@link DefaultIdentity}.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class DefaultIdentityTest {
45 protected final DefaultIdentity identity = createIdentity();
47 protected DefaultIdentity createIdentity() {
48 return new DefaultIdentity("Id", "Nickname", "RequestURI");
52 public void identityCanBeCreated() {
53 assertThat(identity.getId(), is("Id"));
54 assertThat(identity.getNickname(), is("Nickname"));
55 assertThat(identity.getRequestUri(), is("RequestURI"));
56 assertThat(identity.getContexts(), empty());
57 assertThat(identity.getProperties(), is(Collections.<String, String>emptyMap()));
61 public void contextsAreAddedCorrectly() {
62 identity.addContext("Test");
63 assertThat(identity.getContexts(), contains("Test"));
64 assertThat(identity.hasContext("Test"), is(true));
68 public void contextsAreRemovedCorrectly() {
69 identity.addContext("Test");
70 identity.removeContext("Test");
71 assertThat(identity.getContexts(), empty());
72 assertThat(identity.hasContext("Test"), is(false));
76 public void contextsAreSetCorrectlyInBulk() {
77 identity.addContext("Test");
78 identity.setContexts(asList("Test1", "Test2"));
79 assertThat(identity.getContexts(), containsInAnyOrder("Test1", "Test2"));
80 assertThat(identity.hasContext("Test"), is(false));
81 assertThat(identity.hasContext("Test1"), is(true));
82 assertThat(identity.hasContext("Test2"), is(true));
86 public void propertiesAreAddedCorrectly() {
87 identity.setProperty("Key", "Value");
88 assertThat(identity.getProperties().size(), is(1));
89 assertThat(identity.getProperties(), hasEntry("Key", "Value"));
90 assertThat(identity.getProperty("Key"), is("Value"));
94 public void propertiesAreRemovedCorrectly() {
95 identity.setProperty("Key", "Value");
96 identity.removeProperty("Key");
97 assertThat(identity.getProperties(), is(Collections.<String, String>emptyMap()));
98 assertThat(identity.getProperty("Key"), nullValue());
102 public void propertiesAreSetCorrectlyInBulk() {
103 identity.setProperty("Key", "Value");
104 identity.setProperties(of("Key1", "Value1", "Key2", "Value2"));
105 assertThat(identity.getProperties().size(), is(2));
106 assertThat(identity.getProperty("Key"), nullValue());
107 assertThat(identity.getProperty("Key1"), is("Value1"));
108 assertThat(identity.getProperty("Key2"), is("Value2"));
112 public void trustRelationshipsAreAddedCorrectly() {
113 OwnIdentity ownIdentity = mock(OwnIdentity.class);
114 Trust trust = mock(Trust.class);
115 identity.setTrust(ownIdentity, trust);
116 assertThat(identity.getTrust(ownIdentity), is(trust));
120 public void trustRelationshipsAreRemovedCorrectly() {
121 OwnIdentity ownIdentity = mock(OwnIdentity.class);
122 Trust trust = mock(Trust.class);
123 identity.setTrust(ownIdentity, trust);
124 identity.removeTrust(ownIdentity);
125 assertThat(identity.getTrust(ownIdentity), nullValue());
129 public void identitiesWithTheSameIdAreEqual() {
130 DefaultIdentity identity2 = new DefaultIdentity("Id", "Nickname2", "RequestURI2");
131 assertThat(identity2, is(identity));
132 assertThat(identity, is(identity2));
136 public void twoEqualIdentitiesHaveTheSameHashCode() {
137 DefaultIdentity identity2 = new DefaultIdentity("Id", "Nickname2", "RequestURI2");
138 assertThat(identity.hashCode(), is(identity2.hashCode()));
142 public void nullDoesNotMatchAnIdentity() {
143 assertThat(identity, not(is((Object) null)));
147 public void toStringContainsIdAndNickname() {
148 String identityString = identity.toString();
149 assertThat(identityString, Matchers.matches("\\bId\\b"));
150 assertThat(identityString, Matchers.matches("\\bNickname\\b"));