+ private fun createPost(id: String, text: String, recipient: Sone?) = mock<Post>().apply {
+ whenever(this.id).thenReturn(id)
+ val recipientId = recipient?.id
+ whenever(this.recipientId).thenReturn(recipientId.asOptional())
+ whenever(this.recipient).thenReturn(recipient.asOptional())
+ whenever(this.text).thenReturn(text)
+ }
+
+ private fun createSone(id: String, firstName: String, middleName: String, lastName: String) = mock<Sone>().apply {
+ whenever(this.id).thenReturn(id)
+ whenever(this.name).thenReturn(id)
+ whenever(this.profile).thenReturn(Profile(this).apply {
+ this.firstName = firstName
+ this.middleName = middleName
+ this.lastName = lastName
+ })
+ }
+
+ @Test
+ fun `searching for a recipient finds the correct post`() {
+ val recipient = createSone("recipient", "reci", "pi", "ent")
+ val postWithMatch = createSoneWithPost("with-match", "test", recipient)
+ createSoneWithPost("without-match", "no match")
+ addHttpRequestParameter("query", "recipient")
+ page.handleRequest(freenetRequest, templateContext)
+ assertThat(this["postHits"], contains<Post>(postWithMatch))
+ }
+
+ @Test
+ fun `searching for a field value finds the correct sone`() {
+ val soneWithProfileField = createSone("sone", "s", "o", "ne")
+ soneWithProfileField.profile.addField("field").value = "value"
+ createSoneWithPost("with-match", "test", sender = soneWithProfileField)
+ createSoneWithPost("without-match", "no match")
+ addHttpRequestParameter("query", "value")
+ page.handleRequest(freenetRequest, templateContext)
+ assertThat(this["soneHits"], contains(soneWithProfileField))
+ }
+
+ @Suppress("UNCHECKED_CAST")
+ private operator fun <T> get(key: String): T? = templateContext[key] as? T
+