forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves: OpenFeign#1565 Inspired by PlaytikaOSS/feign-reactive#486 ## TODO - [ ] Separate Kotlin support module - [ ] Enhance test case - [ ] Refactoring - [ ] Clean up pom.xml
- Loading branch information
Showing
6 changed files
with
271 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@file:JvmName("KotlinExtensions") | ||
|
||
package feign | ||
|
||
import kotlinx.coroutines.future.await | ||
import java.lang.reflect.Method | ||
import java.lang.reflect.Type | ||
import java.util.concurrent.CompletableFuture | ||
import kotlin.reflect.jvm.javaType | ||
import kotlin.reflect.jvm.kotlinFunction | ||
|
||
internal suspend fun CompletableFuture<*>.awaitRequest(): Any? = | ||
this.await() | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package feign | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.JsonIOException | ||
import feign.codec.Decoder | ||
import feign.codec.Encoder | ||
import feign.codec.ErrorDecoder | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.MockWebServer | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Test | ||
import java.io.IOException | ||
import java.lang.reflect.Type | ||
|
||
class SuspendTest { | ||
@Test | ||
fun shouldRun1(): Unit = runBlocking { | ||
// Arrange | ||
val server = MockWebServer() | ||
val expected = "Hello Worlda" | ||
server.enqueue(MockResponse().setBody(expected)) | ||
val client = TestInterfaceAsyncBuilder() | ||
.target("http://localhost:" + server.port) | ||
|
||
// Act | ||
val firstOrder = client.findOrder1(orderId = 1) | ||
|
||
// Assert | ||
assertThat(firstOrder).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun shouldRun2(): Unit = runBlocking { | ||
// Arrange | ||
val server = MockWebServer() | ||
val expected = IceCreamOrder( | ||
id = "HELLO WORLD", | ||
no = 999, | ||
) | ||
server.enqueue(MockResponse().setBody("{ id: '${expected.id}', no: '${expected.no}'}")) | ||
|
||
val client = TestInterfaceAsyncBuilder() | ||
.decoder(GsonDecoder()) | ||
.target("http://localhost:" + server.port) | ||
|
||
// Act | ||
val firstOrder = client.findOrder2(orderId = 1) | ||
|
||
// Assert | ||
assertThat(firstOrder).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun shouldRun3(): Unit = runBlocking { | ||
// Arrange | ||
val server = MockWebServer() | ||
server.enqueue(MockResponse().setBody("HELLO WORLD")) | ||
|
||
val client = TestInterfaceAsyncBuilder() | ||
.target("http://localhost:" + server.port) | ||
|
||
// Act | ||
val firstOrder = client.findOrder3(orderId = 1) | ||
|
||
// Assert | ||
assertThat(firstOrder).isNull() | ||
} | ||
|
||
@Test | ||
fun shouldRun4(): Unit = runBlocking { | ||
// Arrange | ||
val server = MockWebServer() | ||
server.enqueue(MockResponse().setBody("HELLO WORLD")) | ||
|
||
val client = TestInterfaceAsyncBuilder() | ||
.target("http://localhost:" + server.port) | ||
|
||
// Act | ||
val firstOrder = client.findOrder4(orderId = 1) | ||
|
||
// Assert | ||
assertThat(firstOrder).isEqualTo(Unit) | ||
} | ||
|
||
internal class GsonDecoder : Decoder { | ||
private val gson = Gson() | ||
|
||
override fun decode(response: Response, type: Type): Any? { | ||
if (Void.TYPE == type || response.body() == null) { | ||
return null | ||
} | ||
val reader = response.body().asReader(Util.UTF_8) | ||
return try { | ||
gson.fromJson<Any>(reader, type) | ||
} catch (e: JsonIOException) { | ||
if (e.cause != null && e.cause is IOException) { | ||
throw IOException::class.java.cast(e.cause) | ||
} | ||
throw e | ||
} finally { | ||
Util.ensureClosed(reader) | ||
} | ||
} | ||
} | ||
|
||
internal class TestInterfaceAsyncBuilder { | ||
private val delegate = AsyncFeign.asyncBuilder<Void>() | ||
.decoder(Decoder.Default()).encoder { `object`, bodyType, template -> | ||
if (`object` is Map<*, *>) { | ||
template.body(Gson().toJson(`object`)) | ||
} else { | ||
template.body(`object`.toString()) | ||
} | ||
} | ||
|
||
fun requestInterceptor(requestInterceptor: RequestInterceptor?): TestInterfaceAsyncBuilder { | ||
delegate.requestInterceptor(requestInterceptor) | ||
return this | ||
} | ||
|
||
fun encoder(encoder: Encoder?): TestInterfaceAsyncBuilder { | ||
delegate.encoder(encoder) | ||
return this | ||
} | ||
|
||
fun decoder(decoder: Decoder?): TestInterfaceAsyncBuilder { | ||
delegate.decoder(decoder) | ||
return this | ||
} | ||
|
||
fun errorDecoder(errorDecoder: ErrorDecoder?): TestInterfaceAsyncBuilder { | ||
delegate.errorDecoder(errorDecoder) | ||
return this | ||
} | ||
|
||
fun dismiss404(): TestInterfaceAsyncBuilder { | ||
delegate.dismiss404() | ||
return this | ||
} | ||
|
||
fun queryMapEndcoder(queryMapEncoder: QueryMapEncoder?): TestInterfaceAsyncBuilder { | ||
delegate.queryMapEncoder(queryMapEncoder) | ||
return this | ||
} | ||
|
||
fun target(url: String?): TestInterfaceAsync { | ||
return delegate.target(TestInterfaceAsync::class.java, url) | ||
} | ||
} | ||
|
||
internal interface TestInterfaceAsync { | ||
@RequestLine("GET /icecream/orders/{orderId}") | ||
suspend fun findOrder1(@Param("orderId") orderId: Int): String | ||
|
||
@RequestLine("GET /icecream/orders/{orderId}") | ||
suspend fun findOrder2(@Param("orderId") orderId: Int): IceCreamOrder | ||
|
||
@RequestLine("GET /icecream/orders/{orderId}") | ||
suspend fun findOrder3(@Param("orderId") orderId: Int): Void | ||
|
||
@RequestLine("GET /icecream/orders/{orderId}") | ||
suspend fun findOrder4(@Param("orderId") orderId: Int): Unit | ||
} | ||
|
||
data class IceCreamOrder( | ||
val id: String, | ||
val no: Long, | ||
) | ||
} |