Skip to content

Commit 1024066

Browse files
inline extension functions added
1 parent 5fa693f commit 1024066

File tree

4 files changed

+107
-43
lines changed

4 files changed

+107
-43
lines changed

kotlin/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
<artifactId>axon-configuration</artifactId>
4040
<scope>provided</scope>
4141
</dependency>
42+
<dependency>
43+
<groupId>io.projectreactor</groupId>
44+
<artifactId>reactor-core</artifactId>
45+
<version>3.4.5</version>
46+
<scope>test</scope>
47+
</dependency>
4248
</dependencies>
4349

4450
<build>

kotlin/src/main/kotlin/org/axonframework/extensions/kotlin/QueryGatewayExtensions.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.axonframework.extensions.kotlin
1818

1919
import org.axonframework.messaging.responsetypes.ResponseTypes
2020
import org.axonframework.queryhandling.QueryGateway
21+
import org.axonframework.queryhandling.SubscriptionQueryResult
2122
import java.util.*
2223
import java.util.concurrent.CompletableFuture
2324
import java.util.concurrent.TimeUnit
@@ -40,9 +41,21 @@ import java.util.stream.Stream
4041
* @see ResponseTypes
4142
* @since 0.1.0
4243
*/
43-
inline fun <reified R, reified Q> QueryGateway.queryMany(query: Q): CompletableFuture<List<R>> {
44-
return this.query(query, ResponseTypes.multipleInstancesOf(R::class.java))
45-
}
44+
inline fun <reified R, reified Q> QueryGateway.queryMany(query: Q): CompletableFuture<List<R>> = this.query(query, ResponseTypes.multipleInstancesOf(R::class.java))
45+
46+
/**
47+
* Reified version of [QueryGateway.subscriptionQuery]
48+
* which expects a collection as a response using [org.axonframework.messaging.responsetypes.MultipleInstancesResponseType]
49+
* @param query Query to send
50+
* @param Q the type of payload of the query
51+
* @param R the type of result of the query
52+
* @return [SubscriptionQueryResult] wrapping the result of the query
53+
* @see QueryGateway.subscriptionQuery
54+
* @see ResponseTypes
55+
* @since 0.1.0
56+
*/
57+
inline fun <reified Q, reified I, reified U> QueryGateway.subscriptionQuery(query: Q): SubscriptionQueryResult<I, U> =
58+
this.subscriptionQuery(query, ResponseTypes.instanceOf(I::class.java), ResponseTypes.instanceOf(U::class.java))
4659

4760
/**
4861
* Reified version of [QueryGateway.query] with explicit query name

kotlin/src/test/kotlin/org/axonframework/extensions/kotlin/QueryGatewayExtensionsTest.kt

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ import io.mockk.clearMocks
1919
import io.mockk.every
2020
import io.mockk.mockk
2121
import io.mockk.verify
22-
import org.axonframework.messaging.responsetypes.AbstractResponseType
23-
import org.axonframework.messaging.responsetypes.InstanceResponseType
2422
import org.axonframework.queryhandling.QueryGateway
23+
import org.axonframework.queryhandling.SubscriptionQueryResult
2524
import java.util.*
2625
import java.util.concurrent.CompletableFuture
2726
import java.util.concurrent.TimeUnit
@@ -47,6 +46,8 @@ internal class QueryGatewayExtensionsTest {
4746
private val streamInstanceReturnValue = Stream.of("Value")
4847
private val streamMultipleReturnValue = Stream.of(listOf("Value", "Second Value"))
4948
private val streamOptionalReturnValue = Stream.of(Optional.of("Value"))
49+
private val subscriptionQueryResult = ExampleSubscriptionQueryResult()
50+
5051

5152
@BeforeTest
5253
fun before() {
@@ -62,6 +63,7 @@ internal class QueryGatewayExtensionsTest {
6263
every { subjectGateway.scatterGather(queryName, exampleQuery, matchInstanceResponseType<String>(), timeout, timeUnit) } returns streamInstanceReturnValue
6364
every { subjectGateway.scatterGather(queryName, exampleQuery, matchMultipleInstancesResponseType<String>(), timeout, timeUnit) } returns streamMultipleReturnValue
6465
every { subjectGateway.scatterGather(queryName, exampleQuery, matchOptionalResponseType<String>(), timeout, timeUnit) } returns streamOptionalReturnValue
66+
every { subjectGateway.subscriptionQuery(exampleQuery, matchInstanceResponseType<String>(), matchInstanceResponseType<UpdateType>()) } returns subscriptionQueryResult
6567
}
6668

6769
@AfterTest
@@ -78,15 +80,37 @@ internal class QueryGatewayExtensionsTest {
7880
}
7981
}
8082

83+
@Test
84+
fun `Query without queryName should invoke subscription query method with correct generic parameters`() {
85+
val queryResult = subjectGateway.subscriptionQuery<ExampleQuery, String, UpdateType>(query = exampleQuery)
86+
assertSame(queryResult, subscriptionQueryResult)
87+
verify(exactly = 1) {
88+
subjectGateway.subscriptionQuery(exampleQuery, matchExpectedResponseType(String::class.java), matchExpectedResponseType(UpdateType::class.java))
89+
}
90+
}
91+
8192
@Test
8293
fun `Query without queryName should invoke query method and not require explicit generic types`() {
83-
val queryResult:CompletableFuture<String> = subjectGateway.query(query = exampleQuery)
94+
val queryResult: CompletableFuture<String> = subjectGateway.query(query = exampleQuery)
8495
assertSame(queryResult, instanceReturnValue)
8596
verify(exactly = 1) {
8697
subjectGateway.query(exampleQuery, matchExpectedResponseType(String::class.java))
8798
}
8899
}
89100

101+
@Test
102+
fun `Query without queryName should invoke subscription query method and not require explicit generic types`() {
103+
val queryResult: SubscriptionQueryResult<String, UpdateType> = subjectGateway.subscriptionQuery(query = exampleQuery)
104+
assertSame(queryResult, subscriptionQueryResult)
105+
verify(exactly = 1) {
106+
subjectGateway.subscriptionQuery(
107+
exampleQuery,
108+
matchExpectedResponseType(String::class.java),
109+
matchExpectedResponseType(UpdateType::class.java)
110+
)
111+
}
112+
}
113+
90114
@Test
91115
fun `Query without queryName Optional should invoke query method with correct generic parameters`() {
92116
val queryResult = subjectGateway.queryOptional<String, ExampleQuery>(query = exampleQuery)
@@ -127,7 +151,7 @@ internal class QueryGatewayExtensionsTest {
127151
}
128152

129153
val queryResult = nullableQueryGateway.query<String?, ExampleQuery>(query = exampleQuery)
130-
154+
131155
assertSame(queryResult, nullInstanceReturnValue)
132156
assertEquals(nullInstanceReturnValue.get(), null)
133157
verify(exactly = 1) { nullableQueryGateway.query(exampleQuery, matchExpectedResponseType(String::class.java)) }
@@ -185,7 +209,7 @@ internal class QueryGatewayExtensionsTest {
185209
fun `Query should handle nullable responses`() {
186210
val nullInstanceReturnValue: CompletableFuture<String?> = CompletableFuture.completedFuture(null)
187211
val nullableQueryGateway = mockk<QueryGateway> {
188-
every { query(queryName, exampleQuery, matchInstanceResponseType<String?>() ) } returns nullInstanceReturnValue
212+
every { query(queryName, exampleQuery, matchInstanceResponseType<String?>()) } returns nullInstanceReturnValue
189213
}
190214

191215
val queryResult = nullableQueryGateway.query<String?, ExampleQuery>(queryName = queryName, query = exampleQuery)
@@ -195,36 +219,36 @@ internal class QueryGatewayExtensionsTest {
195219
verify(exactly = 1) { nullableQueryGateway.query(queryName, exampleQuery, matchExpectedResponseType(String::class.java)) }
196220
}
197221

198-
@Test
199-
fun `ScatterGather for Single should invoke scatterGather method with correct generic parameters`() {
200-
val result = subjectGateway.scatterGather<String, ExampleQuery>(
201-
query = exampleQuery,
202-
timeout = timeout,
203-
timeUnit = timeUnit
204-
)
222+
@Test
223+
fun `ScatterGather for Single should invoke scatterGather method with correct generic parameters`() {
224+
val result = subjectGateway.scatterGather<String, ExampleQuery>(
225+
query = exampleQuery,
226+
timeout = timeout,
227+
timeUnit = timeUnit
228+
)
205229

206-
assertSame(result, streamInstanceReturnValue)
207-
verify(exactly = 1) { subjectGateway.scatterGather(exampleQuery, matchExpectedResponseType(String::class.java), timeout, timeUnit) }
208-
}
230+
assertSame(result, streamInstanceReturnValue)
231+
verify(exactly = 1) { subjectGateway.scatterGather(exampleQuery, matchExpectedResponseType(String::class.java), timeout, timeUnit) }
232+
}
209233

210-
@Test
211-
fun `ScatterGather for Multiple should invoke scatterGather method with correct generic parameters`() {
212-
val result = subjectGateway.scatterGatherMany<String, ExampleQuery>(
213-
query = exampleQuery,
214-
timeout = timeout,
215-
timeUnit = timeUnit
216-
)
234+
@Test
235+
fun `ScatterGather for Multiple should invoke scatterGather method with correct generic parameters`() {
236+
val result = subjectGateway.scatterGatherMany<String, ExampleQuery>(
237+
query = exampleQuery,
238+
timeout = timeout,
239+
timeUnit = timeUnit
240+
)
217241

218-
assertSame(result, streamMultipleReturnValue)
219-
verify(exactly = 1) { subjectGateway.scatterGather(exampleQuery, matchMultipleInstancesResponseType<String>(), timeout, timeUnit) }
220-
}
242+
assertSame(result, streamMultipleReturnValue)
243+
verify(exactly = 1) { subjectGateway.scatterGather(exampleQuery, matchMultipleInstancesResponseType<String>(), timeout, timeUnit) }
244+
}
221245

222246
@Test
223247
fun `ScatterGather for Optional should invoke scatterGather method with correct generic parameters`() {
224248
val result = subjectGateway.scatterGatherOptional<String, ExampleQuery>(
225-
query = exampleQuery,
226-
timeout = timeout,
227-
timeUnit = timeUnit
249+
query = exampleQuery,
250+
timeout = timeout,
251+
timeUnit = timeUnit
228252
)
229253

230254
assertSame(result, streamOptionalReturnValue)
@@ -234,10 +258,10 @@ internal class QueryGatewayExtensionsTest {
234258
@Test
235259
fun `ScatterGather for Single should invoke scatterGather method with explicit query name`() {
236260
val result = subjectGateway.scatterGather<String, ExampleQuery>(
237-
queryName = queryName,
238-
query = exampleQuery,
239-
timeout = timeout,
240-
timeUnit = timeUnit
261+
queryName = queryName,
262+
query = exampleQuery,
263+
timeout = timeout,
264+
timeUnit = timeUnit
241265
)
242266

243267
assertSame(result, streamInstanceReturnValue)
@@ -247,10 +271,10 @@ internal class QueryGatewayExtensionsTest {
247271
@Test
248272
fun `ScatterGather for Multiple should invoke scatterGather method with explicit query name`() {
249273
val result = subjectGateway.scatterGatherMany<String, ExampleQuery>(
250-
queryName = queryName,
251-
query = exampleQuery,
252-
timeout = timeout,
253-
timeUnit = timeUnit
274+
queryName = queryName,
275+
query = exampleQuery,
276+
timeout = timeout,
277+
timeUnit = timeUnit
254278
)
255279

256280
assertSame(result, streamMultipleReturnValue)
@@ -260,10 +284,10 @@ internal class QueryGatewayExtensionsTest {
260284
@Test
261285
fun `ScatterGather for Optional should invoke scatterGather method with explicit query name`() {
262286
val result = subjectGateway.scatterGatherOptional<String, ExampleQuery>(
263-
queryName = queryName,
264-
query = exampleQuery,
265-
timeout = timeout,
266-
timeUnit = timeUnit
287+
queryName = queryName,
288+
query = exampleQuery,
289+
timeout = timeout,
290+
timeUnit = timeUnit
267291
)
268292

269293
assertSame(result, streamOptionalReturnValue)

kotlin/src/test/kotlin/org/axonframework/extensions/kotlin/testObjects.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.axonframework.extensions.kotlin
22

33
import org.axonframework.modelling.command.TargetAggregateIdentifier
4+
import org.axonframework.queryhandling.SubscriptionQueryResult
5+
import reactor.core.publisher.Flux
6+
import reactor.core.publisher.Mono
47

58
/**
69
* Simple Query class to be used in tests.
@@ -11,3 +14,21 @@ internal data class ExampleQuery(val value: Number)
1114
* Simple Command class to be used in tests.
1215
*/
1316
internal data class ExampleCommand(@TargetAggregateIdentifier val id: String)
17+
18+
internal data class UpdateType(val dummy:String)
19+
20+
internal class ExampleSubscriptionQueryResult:SubscriptionQueryResult<String, UpdateType> {
21+
override fun cancel(): Boolean {
22+
TODO("Not yet implemented")
23+
}
24+
25+
override fun initialResult(): Mono<String> {
26+
TODO("Not yet implemented")
27+
}
28+
29+
override fun updates(): Flux<UpdateType> {
30+
TODO("Not yet implemented")
31+
}
32+
33+
34+
}

0 commit comments

Comments
 (0)