Remove use of TestHttpClientAdapter

Now that HttpClientAdapter is deprecated and replaced by HttpExchangeAdapter
and ReactorHttpExchangeAdapter, our tests should use the new contracts.

See gh-30117
This commit is contained in:
Rossen Stoyanchev
2023-07-10 10:36:16 +01:00
committed by rstoyanchev
parent 3be4c0a893
commit 068dc7db28
22 changed files with 222 additions and 362 deletions

View File

@@ -36,79 +36,78 @@ import org.springframework.web.service.annotation.GetExchange
@Suppress("DEPRECATION")
class KotlinHttpServiceMethodTests {
private val webClientAdapter = TestHttpClientAdapter()
private val httpExchangeAdapter = TestHttpExchangeAdapter()
private val proxyFactory = HttpServiceProxyFactory.builder(webClientAdapter).build()
private val blockingProxyFactory = HttpServiceProxyFactory.builder()
.exchangeAdapter(httpExchangeAdapter).build()
private val exchangeAdapter = TestExchangeAdapter()
private val reactorExchangeAdapter = TestReactorExchangeAdapter()
private val proxyFactory = HttpServiceProxyFactory.builderFor(this.exchangeAdapter).build()
private val reactorProxyFactory = HttpServiceProxyFactory.builderFor(this.reactorExchangeAdapter).build()
@Test
fun coroutinesService(): Unit = runBlocking {
val service = proxyFactory.createClient(FunctionsService::class.java)
val service = reactorProxyFactory.createClient(FunctionsService::class.java)
val stringBody = service.stringBody()
assertThat(stringBody).isEqualTo("body")
verifyClientInvocation("body", object : ParameterizedTypeReference<String>() {})
assertThat(stringBody).isEqualTo("exchangeForBodyMono")
verifyClientInvocation("exchangeForBodyMono", object : ParameterizedTypeReference<String>() {})
service.listBody()
verifyClientInvocation("body", object : ParameterizedTypeReference<MutableList<String>>() {})
verifyClientInvocation("exchangeForBodyMono", object : ParameterizedTypeReference<MutableList<String>>() {})
val flowBody = service.flowBody()
assertThat(flowBody.toList()).containsExactly("request", "To", "Body", "Flux")
verifyClientInvocation("bodyFlux", object : ParameterizedTypeReference<String>() {})
assertThat(flowBody.toList()).containsExactly("exchange", "For", "Body", "Flux")
verifyClientInvocation("exchangeForBodyFlux", object : ParameterizedTypeReference<String>() {})
val stringEntity = service.stringEntity()
assertThat(stringEntity).isEqualTo(ResponseEntity.ok<String>("entity"))
verifyClientInvocation("entity", object : ParameterizedTypeReference<String>() {})
assertThat(stringEntity).isEqualTo(ResponseEntity.ok<String>("exchangeForEntityMono"))
verifyClientInvocation("exchangeForEntityMono", object : ParameterizedTypeReference<String>() {})
service.listEntity()
verifyClientInvocation("entity", object : ParameterizedTypeReference<MutableList<String>>() {})
verifyClientInvocation("exchangeForEntityMono", object : ParameterizedTypeReference<MutableList<String>>() {})
val flowEntity = service.flowEntity()
assertThat(flowEntity.statusCode).isEqualTo(HttpStatus.OK)
assertThat(flowEntity.body!!.toList()).containsExactly("request", "To", "Entity", "Flux")
verifyClientInvocation("entityFlux", object : ParameterizedTypeReference<String>() {})
assertThat(flowEntity.body!!.toList()).containsExactly("exchange", "For", "Entity", "Flux")
verifyClientInvocation("exchangeForEntityFlux", object : ParameterizedTypeReference<String>() {})
}
@Test
fun blockingServiceWithExchangeResponseFunction() {
val service = blockingProxyFactory.createClient(BlockingFunctionsService::class.java)
val service = proxyFactory.createClient(BlockingFunctionsService::class.java)
val stringBody = service.stringBodyBlocking()
assertThat(stringBody).isEqualTo("body")
verifyTemplateInvocation("body", object : ParameterizedTypeReference<String>() {})
assertThat(stringBody).isEqualTo("exchangeForBody")
verifyTemplateInvocation("exchangeForBody", object : ParameterizedTypeReference<String>() {})
val listBody = service.listBodyBlocking()
assertThat(listBody.size).isEqualTo(1)
verifyTemplateInvocation("body", object : ParameterizedTypeReference<MutableList<String>>() {})
verifyTemplateInvocation("exchangeForBody", object : ParameterizedTypeReference<MutableList<String>>() {})
val stringEntity = service.stringEntityBlocking()
assertThat(stringEntity).isEqualTo(ResponseEntity.ok<String>("entity"))
verifyTemplateInvocation("entity", object : ParameterizedTypeReference<String>() {})
assertThat(stringEntity).isEqualTo(ResponseEntity.ok<String>("exchangeForEntity"))
verifyTemplateInvocation("exchangeForEntity", object : ParameterizedTypeReference<String>() {})
service.listEntityBlocking()
verifyTemplateInvocation("entity", object : ParameterizedTypeReference<MutableList<String>>() {})
verifyTemplateInvocation("exchangeForEntity", object : ParameterizedTypeReference<MutableList<String>>() {})
}
@Test
fun coroutineServiceWithExchangeResponseFunction() {
assertThatIllegalStateException().isThrownBy {
blockingProxyFactory.createClient(FunctionsService::class.java)
proxyFactory.createClient(FunctionsService::class.java)
}
assertThatIllegalStateException().isThrownBy {
blockingProxyFactory.createClient(SuspendingFunctionsService::class.java)
proxyFactory.createClient(SuspendingFunctionsService::class.java)
}
}
private fun verifyTemplateInvocation(methodReference: String, expectedBodyType: ParameterizedTypeReference<*>) {
assertThat(httpExchangeAdapter.invokedMethodReference).isEqualTo(methodReference)
assertThat(httpExchangeAdapter.bodyType).isEqualTo(expectedBodyType)
assertThat(exchangeAdapter.invokedMethodName).isEqualTo(methodReference)
assertThat(exchangeAdapter.bodyType).isEqualTo(expectedBodyType)
}
private fun verifyClientInvocation(methodReference: String, expectedBodyType: ParameterizedTypeReference<*>) {
assertThat(webClientAdapter.invokedMethodReference).isEqualTo(methodReference)
assertThat(webClientAdapter.bodyType).isEqualTo(expectedBodyType)
assertThat(reactorExchangeAdapter.invokedMethodName).isEqualTo(methodReference)
assertThat(reactorExchangeAdapter.bodyType).isEqualTo(expectedBodyType)
}
private interface FunctionsService : SuspendingFunctionsService {