mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 21:29:54 +01:00
Add unit test for WebSocketController
Also move the controller into an event package
This commit is contained in:
@@ -24,6 +24,8 @@ import org.briarproject.bramble.plugin.tor.CircumventionProvider
|
|||||||
import org.briarproject.bramble.plugin.tor.LinuxTorPluginFactory
|
import org.briarproject.bramble.plugin.tor.LinuxTorPluginFactory
|
||||||
import org.briarproject.bramble.system.JavaSystemModule
|
import org.briarproject.bramble.system.JavaSystemModule
|
||||||
import org.briarproject.bramble.util.StringUtils.fromHexString
|
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 org.briarproject.briar.headless.messaging.MessagingModule
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.security.GeneralSecurityException
|
import java.security.GeneralSecurityException
|
||||||
@@ -108,7 +110,8 @@ internal class HeadlessModule(private val appDir: File) {
|
|||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
internal fun provideWebSocketHandler(
|
internal fun provideWebSocketHandler(
|
||||||
webSocketController: WebSocketControllerImpl): WebSocketController {
|
webSocketController: WebSocketControllerImpl
|
||||||
|
): WebSocketController {
|
||||||
return webSocketController
|
return webSocketController
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import io.javalin.core.util.ContextUtil
|
|||||||
import io.javalin.core.util.Header
|
import io.javalin.core.util.Header
|
||||||
import org.briarproject.briar.headless.blogs.BlogController
|
import org.briarproject.briar.headless.blogs.BlogController
|
||||||
import org.briarproject.briar.headless.contact.ContactController
|
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.forums.ForumController
|
||||||
import org.briarproject.briar.headless.messaging.MessagingController
|
import org.briarproject.briar.headless.messaging.MessagingController
|
||||||
import java.lang.Runtime.getRuntime
|
import java.lang.Runtime.getRuntime
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
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)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.briarproject.briar.headless
|
package org.briarproject.briar.headless.event
|
||||||
|
|
||||||
import io.javalin.websocket.WsSession
|
import io.javalin.websocket.WsSession
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
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 io.javalin.websocket.WsSession
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import javax.annotation.concurrent.Immutable
|
import javax.annotation.concurrent.Immutable
|
||||||
@@ -9,21 +9,16 @@ import javax.inject.Singleton
|
|||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
@Singleton
|
@Singleton
|
||||||
internal class WebSocketControllerImpl @Inject constructor() : WebSocketController {
|
internal class WebSocketControllerImpl @Inject constructor() :
|
||||||
|
WebSocketController {
|
||||||
|
|
||||||
override val sessions: MutableSet<WsSession> = ConcurrentHashMap.newKeySet<WsSession>()
|
override val sessions: MutableSet<WsSession> = ConcurrentHashMap.newKeySet<WsSession>()
|
||||||
|
|
||||||
override fun sendEvent(name: String, obj: Any) {
|
override fun sendEvent(name: String, obj: Any) {
|
||||||
sessions.forEach { session ->
|
sessions.forEach { session ->
|
||||||
val event = OutputEvent(name, obj)
|
val event = OutputEvent(name, obj)
|
||||||
val json = JavalinJson.toJsonMapper.map(event)
|
session.send(toJson(event))
|
||||||
session.send(json)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@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.*
|
||||||
import org.briarproject.briar.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH
|
import org.briarproject.briar.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH
|
||||||
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
|
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 java.util.concurrent.Executor
|
||||||
import javax.annotation.concurrent.Immutable
|
import javax.annotation.concurrent.Immutable
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package org.briarproject.briar.headless.messaging
|
|||||||
import org.briarproject.bramble.api.contact.ContactId
|
import org.briarproject.bramble.api.contact.ContactId
|
||||||
import org.briarproject.briar.api.messaging.PrivateMessage
|
import org.briarproject.briar.api.messaging.PrivateMessage
|
||||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader
|
import org.briarproject.briar.api.messaging.PrivateMessageHeader
|
||||||
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
|
|
||||||
import javax.annotation.concurrent.Immutable
|
import javax.annotation.concurrent.Immutable
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
@@ -57,6 +56,3 @@ internal fun PrivateMessageHeader.output(
|
|||||||
|
|
||||||
internal fun PrivateMessage.output(contactId: ContactId, body: String) =
|
internal fun PrivateMessage.output(contactId: ContactId, body: String) =
|
||||||
OutputPrivateMessageHeader(this, contactId, body)
|
OutputPrivateMessageHeader(this, contactId, body)
|
||||||
|
|
||||||
internal fun PrivateMessageReceivedEvent<*>.output(body: String) =
|
|
||||||
messageHeader.output(contactId, body)
|
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
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.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH
|
||||||
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
|
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
|
||||||
import org.briarproject.briar.headless.ControllerTest
|
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.assertEquals
|
||||||
import org.junit.jupiter.api.Assertions.assertThrows
|
import org.junit.jupiter.api.Assertions.assertThrows
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
|||||||
Reference in New Issue
Block a user