1
+ /*
2
+ * Copyright 2002-present the original author or authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package org.springframework.messaging.rsocket.service
18
+
19
+ import io.rsocket.util.DefaultPayload
20
+ import kotlinx.coroutines.runBlocking
21
+ import org.assertj.core.api.Assertions.assertThat
22
+ import org.junit.jupiter.api.BeforeEach
23
+ import org.junit.jupiter.api.Test
24
+ import org.springframework.messaging.rsocket.RSocketRequester
25
+ import org.springframework.messaging.rsocket.RSocketStrategies
26
+ import org.springframework.messaging.rsocket.TestRSocket
27
+ import org.springframework.util.MimeTypeUtils.TEXT_PLAIN
28
+ import reactor.core.publisher.Mono
29
+
30
+ /* *
31
+ * Kotlin tests for [RSocketServiceMethod].
32
+ *
33
+ * @author Dmitry Sulman
34
+ */
35
+ class RSocketServiceMethodKotlinTests {
36
+
37
+ private lateinit var rsocket: TestRSocket
38
+
39
+ private lateinit var proxyFactory: RSocketServiceProxyFactory
40
+
41
+ @BeforeEach
42
+ fun setUp () {
43
+ rsocket = TestRSocket ()
44
+ val requester = RSocketRequester .wrap(rsocket, TEXT_PLAIN , TEXT_PLAIN , RSocketStrategies .create())
45
+ proxyFactory = RSocketServiceProxyFactory .builder(requester).build()
46
+ }
47
+
48
+ @Test
49
+ fun fireAndForget (): Unit = runBlocking {
50
+ val service = proxyFactory.createClient(SuspendingFunctionsService ::class .java)
51
+
52
+ val requestPayload = " request"
53
+ service.fireAndForget(requestPayload)
54
+
55
+ assertThat(rsocket.savedMethodName).isEqualTo(" fireAndForget" )
56
+ assertThat(rsocket.savedPayload?.metadataUtf8).isEqualTo(" ff" )
57
+ assertThat(rsocket.savedPayload?.dataUtf8).isEqualTo(requestPayload)
58
+ }
59
+
60
+ @Test
61
+ fun requestResponse (): Unit = runBlocking {
62
+ val service = proxyFactory.createClient(SuspendingFunctionsService ::class .java)
63
+
64
+ val requestPayload = " request"
65
+ val responsePayload = " response"
66
+ rsocket.setPayloadMonoToReturn(Mono .just(DefaultPayload .create(responsePayload)))
67
+ val response = service.requestResponse(requestPayload)
68
+
69
+ assertThat(response).isEqualTo(responsePayload)
70
+ assertThat(rsocket.savedMethodName).isEqualTo(" requestResponse" )
71
+ assertThat(rsocket.savedPayload?.metadataUtf8).isEqualTo(" rr" )
72
+ assertThat(rsocket.savedPayload?.dataUtf8).isEqualTo(requestPayload)
73
+ }
74
+
75
+ private interface SuspendingFunctionsService {
76
+
77
+ @RSocketExchange(" ff" )
78
+ suspend fun fireAndForget (input : String )
79
+
80
+ @RSocketExchange(" rr" )
81
+ suspend fun requestResponse (input : String ): String
82
+ }
83
+ }
0 commit comments