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 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;
33 import java.util.Collections;
35 import org.junit.Test;
38 * Unit test for {@link DefaultIdentity}.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class DefaultIdentityTest {
44 protected final DefaultIdentity identity = createIdentity();
46 protected DefaultIdentity createIdentity() {
47 return new DefaultIdentity("Id", "Nickname", "RequestURI");
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()));
60 public void contextsAreAddedCorrectly() {
61 identity.addContext("Test");
62 assertThat(identity.getContexts(), contains("Test"));
63 assertThat(identity.hasContext("Test"), is(true));
67 public void contextsAreRemovedCorrectly() {
68 identity.addContext("Test");
69 identity.removeContext("Test");
70 assertThat(identity.getContexts(), empty());
71 assertThat(identity.hasContext("Test"), is(false));
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));
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"));
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());
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"));
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));
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());
128 public void identitiesWithTheSameIdAreEqual() {
129 DefaultIdentity identity2 = new DefaultIdentity("Id", "Nickname2", "RequestURI2");
130 assertThat(identity2, is(identity));
131 assertThat(identity, is(identity2));
135 public void twoEqualIdentitiesHaveTheSameHashCode() {
136 DefaultIdentity identity2 = new DefaultIdentity("Id", "Nickname2", "RequestURI2");
137 assertThat(identity.hashCode(), is(identity2.hashCode()));
141 public void nullDoesNotMatchAnIdentity() {
142 assertThat(identity, not(is((Object) null)));
146 public void toStringContainsIdAndNickname() {
147 String identityString = identity.toString();
148 assertThat(identityString, matchesRegex(".*\\bId\\b.*"));
149 assertThat(identityString, matchesRegex(".*\\bNickname\\b.*"));