Commit aa87c45b authored by Stephane Nicoll's avatar Stephane Nicoll

Polish "Introduce TestRestTemplate Kotlin extensions"

Closes gh-11039
parent cd98da5e
...@@ -75,6 +75,16 @@ ...@@ -75,6 +75,16 @@
<artifactId>hamcrest-library</artifactId> <artifactId>hamcrest-library</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
...@@ -115,16 +125,6 @@ ...@@ -115,16 +125,6 @@
<artifactId>htmlunit</artifactId> <artifactId>htmlunit</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<optional>true</optional>
</dependency>
<!-- Test --> <!-- Test -->
<dependency> <dependency>
<groupId>javax.json</groupId> <groupId>javax.json</groupId>
......
...@@ -32,7 +32,7 @@ import java.net.URI ...@@ -32,7 +32,7 @@ import java.net.URI
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.getForObject(url: String, vararg uriVariables: Any): T? = inline fun <reified T : Any> TestRestTemplate.getForObject(url: String, vararg uriVariables: Any): T? =
getForObject(url, T::class.java, *uriVariables) getForObject(url, T::class.java, *uriVariables)
/** /**
...@@ -43,7 +43,7 @@ inline fun <reified T: Any> TestRestTemplate.getForObject(url: String, vararg ur ...@@ -43,7 +43,7 @@ inline fun <reified T: Any> TestRestTemplate.getForObject(url: String, vararg ur
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.getForObject(url: String, uriVariables: Map<String, Any?>): T? = inline fun <reified T : Any> TestRestTemplate.getForObject(url: String, uriVariables: Map<String, Any?>): T? =
getForObject(url, T::class.java, uriVariables) getForObject(url, T::class.java, uriVariables)
/** /**
...@@ -54,7 +54,7 @@ inline fun <reified T: Any> TestRestTemplate.getForObject(url: String, uriVariab ...@@ -54,7 +54,7 @@ inline fun <reified T: Any> TestRestTemplate.getForObject(url: String, uriVariab
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.getForObject(url: URI): T? = inline fun <reified T : Any> TestRestTemplate.getForObject(url: URI): T? =
getForObject(url, T::class.java) getForObject(url, T::class.java)
/** /**
...@@ -65,7 +65,7 @@ inline fun <reified T: Any> TestRestTemplate.getForObject(url: URI): T? = ...@@ -65,7 +65,7 @@ inline fun <reified T: Any> TestRestTemplate.getForObject(url: URI): T? =
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.getForEntity(url: URI): ResponseEntity<T> = inline fun <reified T : Any> TestRestTemplate.getForEntity(url: URI): ResponseEntity<T> =
getForEntity(url, T::class.java) getForEntity(url, T::class.java)
/** /**
...@@ -76,7 +76,7 @@ inline fun <reified T: Any> TestRestTemplate.getForEntity(url: URI): ResponseEnt ...@@ -76,7 +76,7 @@ inline fun <reified T: Any> TestRestTemplate.getForEntity(url: URI): ResponseEnt
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.getForEntity(url: String, vararg uriVariables: Any): ResponseEntity<T> = inline fun <reified T : Any> TestRestTemplate.getForEntity(url: String, vararg uriVariables: Any): ResponseEntity<T> =
getForEntity(url, T::class.java, *uriVariables) getForEntity(url, T::class.java, *uriVariables)
/** /**
...@@ -87,7 +87,7 @@ inline fun <reified T: Any> TestRestTemplate.getForEntity(url: String, vararg ur ...@@ -87,7 +87,7 @@ inline fun <reified T: Any> TestRestTemplate.getForEntity(url: String, vararg ur
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.getForEntity(url: String, uriVariables: Map<String, *>): ResponseEntity<T> = inline fun <reified T : Any> TestRestTemplate.getForEntity(url: String, uriVariables: Map<String, *>): ResponseEntity<T> =
getForEntity(url, T::class.java, uriVariables) getForEntity(url, T::class.java, uriVariables)
/** /**
...@@ -98,7 +98,7 @@ inline fun <reified T: Any> TestRestTemplate.getForEntity(url: String, uriVariab ...@@ -98,7 +98,7 @@ inline fun <reified T: Any> TestRestTemplate.getForEntity(url: String, uriVariab
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.patchForObject(url: String, request: Any, vararg uriVariables: Any): T? = inline fun <reified T : Any> TestRestTemplate.patchForObject(url: String, request: Any, vararg uriVariables: Any): T? =
patchForObject(url, request, T::class.java, *uriVariables) patchForObject(url, request, T::class.java, *uriVariables)
/** /**
...@@ -109,7 +109,7 @@ inline fun <reified T: Any> TestRestTemplate.patchForObject(url: String, request ...@@ -109,7 +109,7 @@ inline fun <reified T: Any> TestRestTemplate.patchForObject(url: String, request
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.patchForObject(url: String, request: Any, uriVariables: Map<String, *>): T? = inline fun <reified T : Any> TestRestTemplate.patchForObject(url: String, request: Any, uriVariables: Map<String, *>): T? =
patchForObject(url, request, T::class.java, uriVariables) patchForObject(url, request, T::class.java, uriVariables)
/** /**
...@@ -120,7 +120,7 @@ inline fun <reified T: Any> TestRestTemplate.patchForObject(url: String, request ...@@ -120,7 +120,7 @@ inline fun <reified T: Any> TestRestTemplate.patchForObject(url: String, request
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.patchForObject(url: URI, request: Any): T? = inline fun <reified T : Any> TestRestTemplate.patchForObject(url: URI, request: Any): T? =
patchForObject(url, request, T::class.java) patchForObject(url, request, T::class.java)
/** /**
...@@ -131,7 +131,7 @@ inline fun <reified T: Any> TestRestTemplate.patchForObject(url: URI, request: A ...@@ -131,7 +131,7 @@ inline fun <reified T: Any> TestRestTemplate.patchForObject(url: URI, request: A
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.postForObject(url: String, request: Any, vararg uriVariables: Any): T? = inline fun <reified T : Any> TestRestTemplate.postForObject(url: String, request: Any, vararg uriVariables: Any): T? =
postForObject(url, request, T::class.java, *uriVariables) postForObject(url, request, T::class.java, *uriVariables)
/** /**
...@@ -142,7 +142,7 @@ inline fun <reified T: Any> TestRestTemplate.postForObject(url: String, request: ...@@ -142,7 +142,7 @@ inline fun <reified T: Any> TestRestTemplate.postForObject(url: String, request:
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.postForObject(url: String, request: Any, uriVariables: Map<String, *>): T? = inline fun <reified T : Any> TestRestTemplate.postForObject(url: String, request: Any, uriVariables: Map<String, *>): T? =
postForObject(url, request, T::class.java, uriVariables) postForObject(url, request, T::class.java, uriVariables)
/** /**
...@@ -153,7 +153,7 @@ inline fun <reified T: Any> TestRestTemplate.postForObject(url: String, request: ...@@ -153,7 +153,7 @@ inline fun <reified T: Any> TestRestTemplate.postForObject(url: String, request:
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.postForObject(url: URI, request: Any): T? = inline fun <reified T : Any> TestRestTemplate.postForObject(url: URI, request: Any): T? =
postForObject(url, request, T::class.java) postForObject(url, request, T::class.java)
/** /**
...@@ -164,7 +164,7 @@ inline fun <reified T: Any> TestRestTemplate.postForObject(url: URI, request: An ...@@ -164,7 +164,7 @@ inline fun <reified T: Any> TestRestTemplate.postForObject(url: URI, request: An
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.postForEntity(url: String, request: Any, vararg uriVariables: Any): ResponseEntity<T> = inline fun <reified T : Any> TestRestTemplate.postForEntity(url: String, request: Any, vararg uriVariables: Any): ResponseEntity<T> =
postForEntity(url, request, T::class.java, *uriVariables) postForEntity(url, request, T::class.java, *uriVariables)
/** /**
...@@ -175,7 +175,7 @@ inline fun <reified T: Any> TestRestTemplate.postForEntity(url: String, request: ...@@ -175,7 +175,7 @@ inline fun <reified T: Any> TestRestTemplate.postForEntity(url: String, request:
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.postForEntity(url: String, request: Any, uriVariables: Map<String, *>): ResponseEntity<T> = inline fun <reified T : Any> TestRestTemplate.postForEntity(url: String, request: Any, uriVariables: Map<String, *>): ResponseEntity<T> =
postForEntity(url, request, T::class.java, uriVariables) postForEntity(url, request, T::class.java, uriVariables)
/** /**
...@@ -186,7 +186,7 @@ inline fun <reified T: Any> TestRestTemplate.postForEntity(url: String, request: ...@@ -186,7 +186,7 @@ inline fun <reified T: Any> TestRestTemplate.postForEntity(url: String, request:
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.postForEntity(url: URI, request: Any): ResponseEntity<T> = inline fun <reified T : Any> TestRestTemplate.postForEntity(url: URI, request: Any): ResponseEntity<T> =
postForEntity(url, request, T::class.java) postForEntity(url, request, T::class.java)
/** /**
...@@ -197,7 +197,7 @@ inline fun <reified T: Any> TestRestTemplate.postForEntity(url: URI, request: An ...@@ -197,7 +197,7 @@ inline fun <reified T: Any> TestRestTemplate.postForEntity(url: URI, request: An
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>, vararg uriVariables: Any): ResponseEntity<T> = inline fun <reified T : Any> TestRestTemplate.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>, vararg uriVariables: Any): ResponseEntity<T> =
exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}, *uriVariables) exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}, *uriVariables)
/** /**
...@@ -208,7 +208,7 @@ inline fun <reified T: Any> TestRestTemplate.exchange(url: String, method: HttpM ...@@ -208,7 +208,7 @@ inline fun <reified T: Any> TestRestTemplate.exchange(url: String, method: HttpM
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>, uriVariables: Map<String, *>): ResponseEntity<T> = inline fun <reified T : Any> TestRestTemplate.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>, uriVariables: Map<String, *>): ResponseEntity<T> =
exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}, uriVariables) exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}, uriVariables)
/** /**
...@@ -219,7 +219,7 @@ inline fun <reified T: Any> TestRestTemplate.exchange(url: String, method: HttpM ...@@ -219,7 +219,7 @@ inline fun <reified T: Any> TestRestTemplate.exchange(url: String, method: HttpM
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.exchange(url: URI, method: HttpMethod, requestEntity: HttpEntity<*>): ResponseEntity<T> = inline fun <reified T : Any> TestRestTemplate.exchange(url: URI, method: HttpMethod, requestEntity: HttpEntity<*>): ResponseEntity<T> =
exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}) exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {})
/** /**
...@@ -230,5 +230,5 @@ inline fun <reified T: Any> TestRestTemplate.exchange(url: URI, method: HttpMeth ...@@ -230,5 +230,5 @@ inline fun <reified T: Any> TestRestTemplate.exchange(url: URI, method: HttpMeth
* @since 2.0.0 * @since 2.0.0
*/ */
@Throws(RestClientException::class) @Throws(RestClientException::class)
inline fun <reified T: Any> TestRestTemplate.exchange(requestEntity: RequestEntity<*>): ResponseEntity<T> = inline fun <reified T : Any> TestRestTemplate.exchange(requestEntity: RequestEntity<*>): ResponseEntity<T> =
exchange(requestEntity, object : ParameterizedTypeReference<T>() {}) exchange(requestEntity, object : ParameterizedTypeReference<T>() {})
\ No newline at end of file
...@@ -22,7 +22,8 @@ import org.junit.Test ...@@ -22,7 +22,8 @@ import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.mockito.Answers import org.mockito.Answers
import org.mockito.Mock import org.mockito.Mock
import org.mockito.Mockito.* import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.junit.MockitoJUnitRunner import org.mockito.junit.MockitoJUnitRunner
import org.springframework.core.ParameterizedTypeReference import org.springframework.core.ParameterizedTypeReference
import org.springframework.http.HttpEntity import org.springframework.http.HttpEntity
...@@ -183,7 +184,8 @@ class TestRestTemplateExtensionsTests { ...@@ -183,7 +184,8 @@ class TestRestTemplateExtensionsTests {
val var1 = "var1" val var1 = "var1"
val var2 = "var2" val var2 = "var2"
template.exchange<List<Foo>>(url, method, entity, var1, var2) template.exchange<List<Foo>>(url, method, entity, var1, var2)
verify(template, times(1)).exchange(url, method, entity, object : ParameterizedTypeReference<List<Foo>>() {}, var1, var2) verify(template, times(1)).exchange(url, method, entity,
object : ParameterizedTypeReference<List<Foo>>() {}, var1, var2)
} }
@Test @Test
...@@ -193,7 +195,8 @@ class TestRestTemplateExtensionsTests { ...@@ -193,7 +195,8 @@ class TestRestTemplateExtensionsTests {
val entity = mock<HttpEntity<Foo>>() val entity = mock<HttpEntity<Foo>>()
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.exchange<List<Foo>>(url, method, entity, vars) template.exchange<List<Foo>>(url, method, entity, vars)
verify(template, times(1)).exchange(url, method, entity, object : ParameterizedTypeReference<List<Foo>>() {}, vars) verify(template, times(1)).exchange(url, method, entity,
object : ParameterizedTypeReference<List<Foo>>() {}, vars)
} }
@Test @Test
...@@ -202,26 +205,32 @@ class TestRestTemplateExtensionsTests { ...@@ -202,26 +205,32 @@ class TestRestTemplateExtensionsTests {
val method = HttpMethod.GET val method = HttpMethod.GET
val entity = mock<HttpEntity<Foo>>() val entity = mock<HttpEntity<Foo>>()
template.exchange<List<Foo>>(url, method, entity) template.exchange<List<Foo>>(url, method, entity)
verify(template, times(1)).exchange(url, method, entity, object : ParameterizedTypeReference<List<Foo>>() {}) verify(template, times(1)).exchange(url, method, entity,
object : ParameterizedTypeReference<List<Foo>>() {})
} }
@Test @Test
fun `exchange with reified type parameters, String, HttpEntity`() { fun `exchange with reified type parameters, String, HttpEntity`() {
val entity = mock<RequestEntity<Foo>>() val entity = mock<RequestEntity<Foo>>()
template.exchange<List<Foo>>(entity) template.exchange<List<Foo>>(entity)
verify(template, times(1)).exchange(entity, object : ParameterizedTypeReference<List<Foo>>() {}) verify(template, times(1)).exchange(entity,
object : ParameterizedTypeReference<List<Foo>>() {})
} }
@Test @Test
fun `RestOperations are available`() { fun `RestOperations are available`() {
val extensions = Class.forName("org.springframework.boot.test.web.client.TestRestTemplateExtensionsKt") val extensions = Class.forName(
"org.springframework.boot.test.web.client.TestRestTemplateExtensionsKt")
ReflectionUtils.doWithMethods(RestOperations::class.java) { method -> ReflectionUtils.doWithMethods(RestOperations::class.java) { method ->
arrayOf(ParameterizedTypeReference::class, Class::class).forEach { kClass -> arrayOf(ParameterizedTypeReference::class, Class::class).forEach { kClass ->
if (method.parameterTypes.contains(kClass.java)) { if (method.parameterTypes.contains(kClass.java)) {
val parameters = mutableListOf<Class<*>>(TestRestTemplate::class.java).apply { addAll(method.parameterTypes.filter { it != kClass.java }) } val parameters = mutableListOf<Class<*>>(TestRestTemplate::class.java)
val f = extensions.getDeclaredMethod(method.name, *parameters.toTypedArray()).kotlinFunction!! .apply { addAll(method.parameterTypes.filter { it != kClass.java }) }
val f = extensions.getDeclaredMethod(method.name,
*parameters.toTypedArray()).kotlinFunction!!
Assert.assertEquals(1, f.typeParameters.size) Assert.assertEquals(1, f.typeParameters.size)
Assert.assertEquals(listOf(Any::class.createType()), f.typeParameters[0].upperBounds) Assert.assertEquals(listOf(Any::class.createType()),
f.typeParameters[0].upperBounds)
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment