1
+ package io.virtualdig
2
+
3
+
4
+ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
5
+ import com.fasterxml.jackson.module.kotlin.readValue
6
+ import io.virtualdig.exceptions.DigWebsiteException
7
+ import org.springframework.beans.factory.config.BeanDefinition.SCOPE_SINGLETON
8
+ import org.springframework.context.annotation.Scope
9
+ import org.springframework.web.socket.TextMessage
10
+ import org.springframework.web.socket.WebSocketSession
11
+ import org.springframework.web.socket.handler.TextWebSocketHandler
12
+ import java.net.URI
13
+ import java.net.URL
14
+ import java.util.*
15
+ import java.util.concurrent.CompletableFuture
16
+ import java.util.concurrent.TimeUnit
17
+ import kotlin.properties.Delegates
18
+
19
+ @Scope(SCOPE_SINGLETON )
20
+ class DigController : TextWebSocketHandler () {
21
+ private val futureSession: CompletableFuture <WebSocketSession > = CompletableFuture ()
22
+ private fun webSocketSession () = futureSession.get(5 , TimeUnit .SECONDS )
23
+
24
+ var messageListeners: MutableList < (String ) -> Unit > = mutableListOf ()
25
+ fun listenToNextMessage (handler : (String ) -> Unit ) {
26
+ synchronized(messageListeners) {
27
+ messageListeners.add(handler)
28
+ }
29
+ }
30
+
31
+ var message: String by Delegates .observable(" latestMessage" ) {
32
+ _, _, new ->
33
+ synchronized(messageListeners) {
34
+ messageListeners.forEach { it(new) }
35
+ messageListeners.clear()
36
+ }
37
+ }
38
+
39
+ @Throws(Exception ::class )
40
+ override fun afterConnectionEstablished (session : WebSocketSession ) {
41
+ if (futureSession.isDone && futureSession.get().id != session.id) {
42
+ throw Exception (" Session ids do not match. VirtualDig does not support multiple websocket connections at once" )
43
+ }
44
+
45
+ if (! futureSession.isDone) {
46
+ futureSession.complete(session)
47
+ }
48
+ }
49
+
50
+ override fun handleTextMessage (session : WebSocketSession ? , message : TextMessage ? ) {
51
+ if (message == null ) {
52
+ throw Exception (" Message was null in the main websocket hanndler!" )
53
+ }
54
+ if (session == null ) {
55
+ throw Exception (" Session was null in the main websocket hanndler!" )
56
+ }
57
+
58
+ if (futureSession.isDone && futureSession.get().id != session.id) {
59
+ throw Exception (" Session ids do not match. VirtualDig does not support multiple websocket connections at once" )
60
+ }
61
+
62
+ if (message.payload == null ) {
63
+ throw Exception (" Response was empty, something went wrong" )
64
+ }
65
+
66
+ this .message = message.payload
67
+ val payload = message.payload
68
+ println (" RECEIVED MESSAGE $payload " )
69
+ }
70
+
71
+ fun goTo (uri : URI ) {
72
+ val resultWaiter: CompletableFuture <TestResult > = CompletableFuture ()
73
+ listenToNextMessage({ message ->
74
+ val result: TestResult = jacksonObjectMapper().readValue(message)
75
+ resultWaiter.complete(result)
76
+ })
77
+
78
+ val session: WebSocketSession = webSocketSession() ? : throw Exception (" No session exists yet" )
79
+
80
+ val url: URL = uri.toURL() ? : throw Exception (" URI provided was invalid" )
81
+
82
+ val urlString = url.toExternalForm()
83
+
84
+ val goToAction = GoToAction (uri = urlString)
85
+ session.sendMessage(TextMessage (jacksonObjectMapper().writeValueAsString(goToAction)))
86
+
87
+ val (result, testMessage) = resultWaiter.get(10 , TimeUnit .SECONDS )
88
+
89
+ if (result == " Failure" ) throw DigWebsiteException (" Browser failed to go to URL: $urlString \n\n $testMessage " )
90
+ }
91
+
92
+
93
+ fun clickLink (withText : String , withId : String? = null) {
94
+ TODO (" not implemented" ) // To change body of created functions use File | Settings | File Templates.
95
+ }
96
+ }
0 commit comments