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.nullValue;
28 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
29 import static org.mockito.Mockito.mock;
31 import java.util.Collections;
33 import org.junit.Test;
36 * Unit test for {@link DefaultIdentity}.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class DefaultIdentityTest {
42 protected final DefaultIdentity identity = createIdentity();
44 protected DefaultIdentity createIdentity() {
45 return new DefaultIdentity("Id", "Nickname", "RequestURI");
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()));
58 public void contextsAreAddedCorrectly() {
59 identity.addContext("Test");
60 assertThat(identity.getContexts(), contains("Test"));
61 assertThat(identity.hasContext("Test"), is(true));
65 public void contextsAreRemovedCorrectly() {
66 identity.addContext("Test");
67 identity.removeContext("Test");
68 assertThat(identity.getContexts(), empty());
69 assertThat(identity.hasContext("Test"), is(false));
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));
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"));
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());
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"));
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));
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());
126 public void identitiesWithTheSameIdAreEqual() {
127 DefaultIdentity identity2 = new DefaultIdentity("Id", "Nickname2", "RequestURI2");
128 assertThat(identity2, is(identity));
129 assertThat(identity, is(identity2));
133 public void twoEqualIdentitiesHaveTheSameHashCode() {
134 DefaultIdentity identity2 = new DefaultIdentity("Id", "Nickname2", "RequestURI2");
135 assertThat(identity.hashCode(), is(identity2.hashCode()));