forked from kptfh/feign-reactive
-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves: #383 Inspired by https://github.com/stas29a/coroutine-feign-client ## TODO - [ ] Separate Kotlin support module - [ ] Enhance test case - [ ] Refactoring - [ ] Clean up pom.xml
- Loading branch information
Showing
10 changed files
with
219 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
feign-reactor-core/src/main/java/reactivefeign/KotlinExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@file:JvmName("KotlinExtensions") | ||
|
||
package reactivefeign | ||
|
||
import kotlinx.coroutines.reactor.awaitSingle | ||
import reactivefeign.client.ReactiveHttpResponse | ||
import reactor.core.publisher.Mono | ||
import java.lang.reflect.Method | ||
import java.lang.reflect.Type | ||
import kotlin.reflect.jvm.javaType | ||
import kotlin.reflect.jvm.kotlinFunction | ||
|
||
internal suspend fun Mono<*>.awaitReactiveHttpResponse(): Any { | ||
val result = awaitSingle() | ||
if (result is ReactiveHttpResponse<*>) { | ||
val body = result.body() | ||
require(body is Mono<*>) { "Only Mono type is allowed for suspend method" } | ||
return body.awaitSingle() | ||
} | ||
|
||
return result | ||
} | ||
|
||
internal fun Method.isSuspendMethod(): Boolean = | ||
kotlinFunction?.isSuspend ?: false | ||
|
||
internal fun Method.getKotlinMethodReturnType(): Type? = | ||
kotlinFunction?.returnType?.javaType |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
feign-reactor-core/src/test/java/reactivefeign/SuspendTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package reactivefeign | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException | ||
import kotlinx.coroutines.runBlocking | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.AfterClass | ||
import org.junit.BeforeClass | ||
import org.junit.Test | ||
import reactivefeign.resttemplate.client.RestTemplateFakeReactiveFeign | ||
import reactivefeign.testcase.SuspendIceCreamServiceApi | ||
import reactivefeign.testcase.domain.OrderGenerator | ||
import reactor.core.publisher.Mono | ||
import reactor.netty.DisposableServer | ||
import reactor.netty.http.HttpProtocol | ||
import reactor.netty.http.server.HttpServer | ||
import reactor.netty.http.server.HttpServerRequest | ||
import reactor.netty.http.server.HttpServerResponse | ||
import reactor.netty.http.server.HttpServerRoutes | ||
import java.time.Duration | ||
|
||
class SuspendTest { | ||
companion object { | ||
private lateinit var server: DisposableServer | ||
private const val DELAY_IN_MILLIS: Long = 500L | ||
private val cannedValue = OrderGenerator().generate(1) | ||
|
||
@BeforeClass | ||
@JvmStatic | ||
@Throws(JsonProcessingException::class) | ||
fun startServer() { | ||
val data = TestUtils.MAPPER.writeValueAsString(cannedValue).toByteArray() | ||
server = HttpServer.create() | ||
.protocol(HttpProtocol.HTTP11, HttpProtocol.H2C) | ||
.route { routes: HttpServerRoutes -> | ||
routes[ | ||
"/icecream/orders/1", { req: HttpServerRequest?, res: HttpServerResponse -> | ||
res.header("Content-Type", "application/json") | ||
Mono.delay(Duration.ofMillis(DELAY_IN_MILLIS)) | ||
.thenEmpty(res.sendByteArray(Mono.just(data))) | ||
} | ||
] | ||
} | ||
.bindNow() | ||
} | ||
|
||
@JvmStatic | ||
@AfterClass | ||
fun stopServer() { | ||
server.disposeNow() | ||
} | ||
} | ||
|
||
@Test | ||
fun shouldRun(): Unit = runBlocking { | ||
val client = RestTemplateFakeReactiveFeign | ||
.builder<SuspendIceCreamServiceApi>() | ||
.target( | ||
SuspendIceCreamServiceApi::class.java, | ||
"http://localhost:" + server.port() | ||
) | ||
|
||
val firstOrder = client.findOrder(orderId = 1) | ||
|
||
assertThat(firstOrder).usingRecursiveComparison().isEqualTo(cannedValue) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
feign-reactor-core/src/test/java/reactivefeign/testcase/SuspendIceCreamServiceApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package reactivefeign.testcase | ||
|
||
import feign.Param | ||
import feign.RequestLine | ||
import reactivefeign.testcase.domain.IceCreamOrder | ||
|
||
interface SuspendIceCreamServiceApi { | ||
@RequestLine("GET /icecream/orders/{orderId}") | ||
suspend fun findOrder(@Param("orderId") orderId: Int): IceCreamOrder | ||
} |