2 * Sone - DefaultIdentityTest.java - Copyright © 2013–2019 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 com.google.common.collect.ImmutableMap.*
21 import net.pterodactylus.sone.test.*
22 import net.pterodactylus.sone.test.Matchers.*
23 import org.hamcrest.MatcherAssert.*
24 import org.hamcrest.Matchers.containsInAnyOrder
25 import org.hamcrest.Matchers.empty
26 import org.hamcrest.Matchers.equalTo
27 import org.hamcrest.Matchers.hasEntry
28 import org.hamcrest.Matchers.not
29 import org.hamcrest.Matchers.nullValue
30 import org.hamcrest.collection.IsIterableContainingInOrder.contains
34 * Unit test for [DefaultIdentity].
36 open class DefaultIdentityTest {
38 protected open val identity = DefaultIdentity("Id", "Nickname", "RequestURI")
41 fun `identity can be created`() {
42 assertThat(identity.id, equalTo("Id"))
43 assertThat(identity.nickname, equalTo("Nickname"))
44 assertThat(identity.requestUri, equalTo("RequestURI"))
45 assertThat(identity.contexts, empty())
46 assertThat(identity.properties, equalTo(emptyMap()))
50 fun `contexts are added correctly`() {
51 identity.addContext("Test")
52 assertThat(identity.contexts, contains("Test"))
53 assertThat(identity.hasContext("Test"), equalTo(true))
57 fun `contexts are removed correctly`() {
58 identity.addContext("Test")
59 identity.removeContext("Test")
60 assertThat(identity.contexts, empty())
61 assertThat(identity.hasContext("Test"), equalTo(false))
65 fun `contexts are set correctly in bulk`() {
66 identity.addContext("Test")
67 identity.contexts = setOf("Test1", "Test2")
68 assertThat(identity.contexts, containsInAnyOrder("Test1", "Test2"))
69 assertThat(identity.hasContext("Test"), equalTo(false))
70 assertThat(identity.hasContext("Test1"), equalTo(true))
71 assertThat(identity.hasContext("Test2"), equalTo(true))
75 fun `properties are added correctly`() {
76 identity.setProperty("Key", "Value")
77 assertThat(identity.properties.size, equalTo(1))
78 assertThat(identity.properties, hasEntry("Key", "Value"))
79 assertThat(identity.getProperty("Key"), equalTo("Value"))
83 fun `properties are removed correctly`() {
84 identity.setProperty("Key", "Value")
85 identity.removeProperty("Key")
86 assertThat(identity.properties, equalTo(emptyMap()))
87 assertThat(identity.getProperty("Key"), nullValue())
91 fun `properties are set correctly in bulk`() {
92 identity.setProperty("Key", "Value")
93 identity.properties = of("Key1", "Value1", "Key2", "Value2")
94 assertThat(identity.properties.size, equalTo(2))
95 assertThat(identity.getProperty("Key"), nullValue())
96 assertThat(identity.getProperty("Key1"), equalTo("Value1"))
97 assertThat(identity.getProperty("Key2"), equalTo("Value2"))
101 fun `trust relationships are added correctly`() {
102 val ownIdentity = mock<OwnIdentity>()
103 val trust = mock<Trust>()
104 identity.setTrust(ownIdentity, trust)
105 assertThat(identity.getTrust(ownIdentity), equalTo(trust))
109 fun `trust relationships are removed correctly`() {
110 val ownIdentity = mock<OwnIdentity>()
111 val trust = mock<Trust>()
112 identity.setTrust(ownIdentity, trust)
113 identity.removeTrust(ownIdentity)
114 assertThat(identity.getTrust(ownIdentity), nullValue())
118 fun `identities with the same id are equal`() {
119 val identity2 = DefaultIdentity("Id", "Nickname2", "RequestURI2")
120 assertThat(identity2, equalTo(identity))
121 assertThat(identity, equalTo(identity2))
125 fun `two equal identities have the same hash code`() {
126 val identity2 = DefaultIdentity("Id", "Nickname2", "RequestURI2")
127 assertThat(identity.hashCode(), equalTo(identity2.hashCode()))
131 fun `null does not match an identity`() {
132 assertThat(identity, not(equalTo<Any>(null as Any?)))
136 fun `toString() contains id and nickname`() {
137 val identityString = identity.toString()
138 assertThat(identityString, matchesRegex(".*\\bId\\b.*"))
139 assertThat(identityString, matchesRegex(".*\\bNickname\\b.*"))