Add unit test for WebSocketController

Also move the controller into an event package
parent e381f835
......@@ -24,6 +24,8 @@ import org.briarproject.bramble.plugin.tor.CircumventionProvider
import org.briarproject.bramble.plugin.tor.LinuxTorPluginFactory
import org.briarproject.bramble.system.JavaSystemModule
import org.briarproject.bramble.util.StringUtils.fromHexString
import org.briarproject.briar.headless.event.WebSocketController
import org.briarproject.briar.headless.event.WebSocketControllerImpl
import org.briarproject.briar.headless.messaging.MessagingModule
import java.io.File
import java.security.GeneralSecurityException
......@@ -108,7 +110,8 @@ internal class HeadlessModule(private val appDir: File) {
@Provides
@Singleton
internal fun provideWebSocketHandler(
webSocketController: WebSocketControllerImpl): WebSocketController {
webSocketController: WebSocketControllerImpl
): WebSocketController {
return webSocketController
}
......
......@@ -8,6 +8,7 @@ import io.javalin.core.util.ContextUtil
import io.javalin.core.util.Header
import org.briarproject.briar.headless.blogs.BlogController
import org.briarproject.briar.headless.contact.ContactController
import org.briarproject.briar.headless.event.WebSocketController
import org.briarproject.briar.headless.forums.ForumController
import org.briarproject.briar.headless.messaging.MessagingController
import java.lang.Runtime.getRuntime
......
package org.briarproject.briar.headless.event
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
import org.briarproject.briar.headless.messaging.output
import javax.annotation.concurrent.Immutable
@Immutable
internal class OutputEvent(val name: String, val data: Any) {
val type = "event"
}
internal fun PrivateMessageReceivedEvent<*>.output(body: String) =
messageHeader.output(contactId, body)
package org.briarproject.briar.headless
package org.briarproject.briar.headless.event
import io.javalin.json.JavalinJson
import io.javalin.json.JavalinJson.toJson
import io.javalin.websocket.WsSession
import java.util.concurrent.ConcurrentHashMap
import javax.annotation.concurrent.Immutable
......@@ -9,21 +9,16 @@ import javax.inject.Singleton
@Immutable
@Singleton
internal class WebSocketControllerImpl @Inject constructor() : WebSocketController {
internal class WebSocketControllerImpl @Inject constructor() :
WebSocketController {
override val sessions: MutableSet<WsSession> = ConcurrentHashMap.newKeySet<WsSession>()
override fun sendEvent(name: String, obj: Any) {
sessions.forEach { session ->
val event = OutputEvent(name, obj)
val json = JavalinJson.toJsonMapper.map(event)
session.send(json)
session.send(toJson(event))
}
}
}
@Immutable
internal class OutputEvent(val name: String, val data: Any) {
val type = "event"
}
......@@ -14,7 +14,8 @@ import org.briarproject.bramble.api.system.Clock
import org.briarproject.briar.api.messaging.*
import org.briarproject.briar.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
import org.briarproject.briar.headless.WebSocketController
import org.briarproject.briar.headless.event.WebSocketController
import org.briarproject.briar.headless.event.output
import java.util.concurrent.Executor
import javax.annotation.concurrent.Immutable
import javax.inject.Inject
......
......@@ -3,7 +3,6 @@ package org.briarproject.briar.headless.messaging
import org.briarproject.bramble.api.contact.ContactId
import org.briarproject.briar.api.messaging.PrivateMessage
import org.briarproject.briar.api.messaging.PrivateMessageHeader
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
import javax.annotation.concurrent.Immutable
@Immutable
......@@ -57,6 +56,3 @@ internal fun PrivateMessageHeader.output(
internal fun PrivateMessage.output(contactId: ContactId, body: String) =
OutputPrivateMessageHeader(this, contactId, body)
internal fun PrivateMessageReceivedEvent<*>.output(body: String) =
messageHeader.output(contactId, body)
package org.briarproject.briar.headless.event
import io.javalin.json.JavalinJson.toJson
import io.javalin.websocket.WsSession
import io.mockk.*
import org.briarproject.briar.api.messaging.PrivateMessageHeader
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
import org.briarproject.briar.headless.ControllerTest
import org.briarproject.briar.headless.messaging.EVENT_PRIVATE_MESSAGE
import org.briarproject.briar.headless.messaging.output
import org.junit.jupiter.api.Test
internal class WebSocketControllerTest : ControllerTest() {
private val session = mockk<WsSession>()
private val controller = WebSocketControllerImpl()
private val header =
PrivateMessageHeader(message.id, group.id, timestamp, true, true, true, true)
private val event = PrivateMessageReceivedEvent(header, contact.id)
private val outputEvent = OutputEvent(EVENT_PRIVATE_MESSAGE, event.output(body))
@Test
fun testSessionSend() {
val slot = CapturingSlot<String>()
every { session.send(capture(slot)) } just Runs
controller.sessions.add(session)
controller.sendEvent(EVENT_PRIVATE_MESSAGE, event.output(body))
assertJsonEquals(slot.captured, outputEvent)
}
@Test
fun testOutputPrivateMessageReceivedEvent() {
val json = """
{
"type": "event",
"name": "org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent",
"data": ${toJson(header.output(contact.id, body))}
}
"""
assertJsonEquals(json, outputEvent)
}
}
......@@ -13,7 +13,8 @@ import org.briarproject.briar.api.messaging.*
import org.briarproject.briar.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
import org.briarproject.briar.headless.ControllerTest
import org.briarproject.briar.headless.WebSocketController
import org.briarproject.briar.headless.event.WebSocketController
import org.briarproject.briar.headless.event.output
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment