🔀 Merge branch 'release/v82'
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / freenet / wot / DefaultIdentityTest.kt
1 /*
2  * Sone - DefaultIdentityTest.kt - Copyright Â© 2013–2020 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 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
31 import org.junit.*
32
33 /**
34  * Unit test for [DefaultIdentity].
35  */
36 open class DefaultIdentityTest {
37
38         protected open val identity = DefaultIdentity("Id", "Nickname", "RequestURI")
39
40         @Test
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()))
47         }
48
49         @Test
50         fun `contexts are added correctly`() {
51                 identity.addContext("Test")
52                 assertThat(identity.contexts, contains("Test"))
53                 assertThat(identity.hasContext("Test"), equalTo(true))
54         }
55
56         @Test
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))
62         }
63
64         @Test
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))
72         }
73
74         @Test
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"))
80         }
81
82         @Test
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())
88         }
89
90         @Test
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"))
98         }
99
100         @Test
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))
106         }
107
108         @Test
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())
115         }
116
117         @Test
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))
122         }
123
124         @Test
125         fun `two equal identities have the same hash code`() {
126                 val identity2 = DefaultIdentity("Id", "Nickname2", "RequestURI2")
127                 assertThat(identity.hashCode(), equalTo(identity2.hashCode()))
128         }
129
130         @Test
131         fun `null does not match an identity`() {
132                 assertThat(identity, not(equalTo<Any>(null as Any?)))
133         }
134
135         @Test
136         fun `toString() contains id and nickname`() {
137                 val identityString = identity.toString()
138                 assertThat(identityString, matchesRegex(".*\\bId\\b.*"))
139                 assertThat(identityString, matchesRegex(".*\\bNickname\\b.*"))
140         }
141
142 }