Skip to content

Commit 465d333

Browse files
committed
inline extension functions added
1 parent f480d40 commit 465d333

File tree

2 files changed

+93
-38
lines changed

2 files changed

+93
-38
lines changed

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

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,6 @@ import java.util.stream.Stream
4343
*/
4444
inline fun <reified R, reified Q> QueryGateway.queryMany(query: Q): CompletableFuture<List<R>> = this.query(query, ResponseTypes.multipleInstancesOf(R::class.java))
4545

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))
59-
6046
/**
6147
* Reified version of [QueryGateway.query] with explicit query name
6248
* which expects a collection as a response using [org.axonframework.messaging.responsetypes.MultipleInstancesResponseType]
@@ -244,4 +230,35 @@ inline fun <reified R, reified Q> QueryGateway.scatterGatherOptional(query: Q, t
244230
inline fun <reified R, reified Q> QueryGateway.scatterGatherOptional(queryName: String, query: Q, timeout: Long,
245231
timeUnit: TimeUnit): Stream<Optional<R>> {
246232
return this.scatterGather(queryName, query, ResponseTypes.optionalInstanceOf(R::class.java), timeout, timeUnit)
247-
}
233+
}
234+
235+
/**
236+
* Reified version of [QueryGateway.subscriptionQuery]
237+
* which expects a single object as a response using [org.axonframework.messaging.responsetypes.InstanceResponseType]
238+
* @param query Query to send
239+
* @param Q the type of payload of the query
240+
* @param I the type of initial response
241+
* @param U the type of update response
242+
* @return [SubscriptionQueryResult] wrapping the result of the query
243+
* @see QueryGateway.subscriptionQuery
244+
* @see ResponseTypes
245+
* @since 0.1.0
246+
*/
247+
inline fun <reified Q, reified I, reified U> QueryGateway.subscriptionQuery(query: Q): SubscriptionQueryResult<I, U> =
248+
this.subscriptionQuery(query, ResponseTypes.instanceOf(I::class.java), ResponseTypes.instanceOf(U::class.java))
249+
250+
/**
251+
* Reified version of [QueryGateway.subscriptionQuery]
252+
* which expects a single object as a response using [org.axonframework.messaging.responsetypes.InstanceResponseType]
253+
* @param queryName Name of the query
254+
* @param query Query to send
255+
* @param Q the type of payload of the query
256+
* @param I the type of initial response
257+
* @param U the type of update response
258+
* @return [SubscriptionQueryResult] wrapping the result of the query
259+
* @see QueryGateway.subscriptionQuery
260+
* @see ResponseTypes
261+
* @since 0.1.0
262+
*/
263+
inline fun <reified Q, reified I, reified U> QueryGateway.subscriptionQuery(queryName: String, query: Q): SubscriptionQueryResult<I, U> =
264+
this.subscriptionQuery(queryName, query, ResponseTypes.instanceOf(I::class.java), ResponseTypes.instanceOf(U::class.java))

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

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ internal class QueryGatewayExtensionsTest {
6464
every { subjectGateway.scatterGather(queryName, exampleQuery, matchMultipleInstancesResponseType<String>(), timeout, timeUnit) } returns streamMultipleReturnValue
6565
every { subjectGateway.scatterGather(queryName, exampleQuery, matchOptionalResponseType<String>(), timeout, timeUnit) } returns streamOptionalReturnValue
6666
every { subjectGateway.subscriptionQuery(exampleQuery, matchInstanceResponseType<String>(), matchInstanceResponseType<UpdateType>()) } returns subscriptionQueryResult
67+
every {
68+
subjectGateway.subscriptionQuery(
69+
queryName,
70+
exampleQuery,
71+
matchInstanceResponseType<String>(),
72+
matchInstanceResponseType<UpdateType>()
73+
)
74+
} returns subscriptionQueryResult
6775
}
6876

6977
@AfterTest
@@ -80,15 +88,6 @@ internal class QueryGatewayExtensionsTest {
8088
}
8189
}
8290

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-
9291
@Test
9392
fun `Query without queryName should invoke query method and not require explicit generic types`() {
9493
val queryResult: CompletableFuture<String> = subjectGateway.query(query = exampleQuery)
@@ -98,23 +97,9 @@ internal class QueryGatewayExtensionsTest {
9897
}
9998
}
10099

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-
114100
@Test
115101
fun `Query without queryName Optional should invoke query method with correct generic parameters`() {
116102
val queryResult = subjectGateway.queryOptional<String, ExampleQuery>(query = exampleQuery)
117-
118103
assertSame(queryResult, optionalReturnValue)
119104
verify(exactly = 1) { subjectGateway.query(exampleQuery, matchExpectedResponseType(String::class.java)) }
120105
}
@@ -294,4 +279,57 @@ internal class QueryGatewayExtensionsTest {
294279
verify(exactly = 1) { subjectGateway.scatterGather(queryName, exampleQuery, matchOptionalResponseType<String>(), timeout, timeUnit) }
295280
}
296281

282+
@Test
283+
fun `Query without queryName should invoke subscription query method with correct generic parameters`() {
284+
val queryResult = subjectGateway.subscriptionQuery<ExampleQuery, String, UpdateType>(query = exampleQuery)
285+
assertSame(queryResult, subscriptionQueryResult)
286+
verify(exactly = 1) {
287+
subjectGateway.subscriptionQuery(
288+
exampleQuery,
289+
matchExpectedResponseType(String::class.java),
290+
matchExpectedResponseType(UpdateType::class.java)
291+
)
292+
}
293+
}
294+
295+
@Test
296+
fun `Query without queryName should invoke subscriptionQuery method and not require explicit generic types`() {
297+
val queryResult: SubscriptionQueryResult<String, UpdateType> = subjectGateway.subscriptionQuery(query = exampleQuery)
298+
assertSame(queryResult, subscriptionQueryResult)
299+
verify(exactly = 1) {
300+
subjectGateway.subscriptionQuery(
301+
exampleQuery,
302+
matchExpectedResponseType(String::class.java),
303+
matchExpectedResponseType(UpdateType::class.java)
304+
)
305+
}
306+
}
307+
308+
@Test
309+
fun `Query should invoke subscriptionQuery method with correct generic parameters`() {
310+
val queryResult = subjectGateway.subscriptionQuery<ExampleQuery, String, UpdateType>(queryName = queryName, query = exampleQuery)
311+
assertSame(queryResult, subscriptionQueryResult)
312+
verify(exactly = 1) {
313+
subjectGateway.subscriptionQuery(
314+
queryName,
315+
exampleQuery,
316+
matchExpectedResponseType(String::class.java),
317+
matchExpectedResponseType(UpdateType::class.java)
318+
)
319+
}
320+
}
321+
322+
@Test
323+
fun `Query should invoke subscriptionQuery method and not require explicit generic types`() {
324+
val queryResult: SubscriptionQueryResult<String, UpdateType> = subjectGateway.subscriptionQuery(queryName = queryName, query = exampleQuery)
325+
assertSame(queryResult, subscriptionQueryResult)
326+
verify(exactly = 1) {
327+
subjectGateway.subscriptionQuery(
328+
queryName,
329+
exampleQuery,
330+
matchExpectedResponseType(String::class.java),
331+
matchExpectedResponseType(UpdateType::class.java)
332+
)
333+
}
334+
}
297335
}

0 commit comments

Comments
 (0)