Use ParameterizedTypeReference instead of Class in Kotlin extensions

This commit also removes WebFlux non-extension functions in favor of
regular Kotlin extensions leveraging ParameterizedTypeReference parameter.

Issue: SPR-15818
This commit is contained in:
Sebastien Deleuze
2017-07-28 00:22:00 +02:00
parent 1d86c9c3d1
commit 6583f9f754
16 changed files with 118 additions and 211 deletions

View File

@@ -1,44 +0,0 @@
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.function
import org.junit.Assert.assertNotNull
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.junit.MockitoJUnitRunner
/**
* Tests for [BodyExtractors] Kotlin extensions
*
* @author Sebastien Deleuze
*/
@RunWith(MockitoJUnitRunner::class)
class BodyExtractorsExtensionsTests {
@Test
fun `bodyToMono with reified type parameter`() {
assertNotNull(bodyToMono<Foo>())
}
@Test
fun `bodyToFlux with reified type parameter`() {
assertNotNull(bodyToFlux<Foo>())
}
class Foo
}

View File

@@ -1,48 +0,0 @@
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.function
import com.nhaarman.mockito_kotlin.mock
import org.junit.Assert.assertNotNull
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.junit.MockitoJUnitRunner
import org.reactivestreams.Publisher
/**
* Tests for [BodyExtractors] Kotlin extensions
*
* @author Sebastien Deleuze
*/
@RunWith(MockitoJUnitRunner::class)
class BodyInsertersExtensionsTests {
@Test
fun `bodyFromPublisher with reified type parameters`() {
val publisher = mock<Publisher<Foo>>()
assertNotNull(bodyFromPublisher(publisher))
}
@Test
fun `bodyFromServerSentEvents with reified type parameters`() {
val publisher = mock<Publisher<Foo>>()
assertNotNull(bodyFromServerSentEvents(publisher))
}
class Foo
}

View File

@@ -23,6 +23,7 @@ import org.mockito.Mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.junit.MockitoJUnitRunner
import org.springframework.core.ParameterizedTypeReference
/**
* Mock object based tests for [ClientResponse] Kotlin extensions
@@ -37,26 +38,26 @@ class ClientResponseExtensionsTests {
@Test
fun `bodyToMono with reified type parameters`() {
response.bodyToMono<Foo>()
verify(response, times(1)).bodyToMono(Foo::class.java)
response.bodyToMono<List<Foo>>()
verify(response, times(1)).bodyToMono(object : ParameterizedTypeReference<List<Foo>>() {})
}
@Test
fun `bodyToFlux with reified type parameters`() {
response.bodyToFlux<Foo>()
verify(response, times(1)).bodyToFlux(Foo::class.java)
response.bodyToFlux<List<Foo>>()
verify(response, times(1)).bodyToFlux(object : ParameterizedTypeReference<List<Foo>>() {})
}
@Test
fun `toEntity with reified type parameters`() {
response.toEntity<Foo>()
verify(response, times(1)).toEntity(Foo::class.java)
response.toEntity<List<Foo>>()
verify(response, times(1)).toEntity(object : ParameterizedTypeReference<List<Foo>>() {})
}
@Test
fun `ResponseSpec#toEntityList with reified type parameters`() {
response.toEntityList<Foo>()
verify(response, times(1)).toEntityList(Foo::class.java)
response.toEntityList<List<Foo>>()
verify(response, times(1)).toEntityList(object : ParameterizedTypeReference<List<Foo>>() {})
}
class Foo

View File

@@ -25,6 +25,7 @@ import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.junit.MockitoJUnitRunner
import org.reactivestreams.Publisher
import org.springframework.core.ParameterizedTypeReference
/**
* Mock object based tests for [WebClient] Kotlin extensions
@@ -43,21 +44,21 @@ class WebClientExtensionsTests {
@Test
fun `RequestBodySpec#body with Publisher and reified type parameters`() {
val body = mock<Publisher<Foo>>()
val body = mock<Publisher<List<Foo>>>()
requestBodySpec.body(body)
verify(requestBodySpec, times(1)).body(body, Foo::class.java)
verify(requestBodySpec, times(1)).body(body, object : ParameterizedTypeReference<List<Foo>>() {})
}
@Test
fun `ResponseSpec#bodyToMono with reified type parameters`() {
responseSpec.bodyToMono<Foo>()
verify(responseSpec, times(1)).bodyToMono(Foo::class.java)
responseSpec.bodyToMono<List<Foo>>()
verify(responseSpec, times(1)).bodyToMono(object : ParameterizedTypeReference<List<Foo>>() {})
}
@Test
fun `ResponseSpec#bodyToFlux with reified type parameters`() {
responseSpec.bodyToFlux<Foo>()
verify(responseSpec, times(1)).bodyToFlux(Foo::class.java)
responseSpec.bodyToFlux<List<Foo>>()
verify(responseSpec, times(1)).bodyToFlux(object : ParameterizedTypeReference<List<Foo>>() {})
}
class Foo

View File

@@ -22,6 +22,7 @@ import org.mockito.Answers
import org.mockito.Mock
import org.mockito.Mockito.*
import org.mockito.junit.MockitoJUnitRunner
import org.springframework.core.ParameterizedTypeReference
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.bodyToFlux
import org.springframework.web.reactive.function.server.bodyToMono
@@ -39,14 +40,14 @@ class ServerRequestExtensionsTests {
@Test
fun `bodyToMono with reified type parameters`() {
request.bodyToMono<Foo>()
verify(request, times(1)).bodyToMono(Foo::class.java)
request.bodyToMono<List<Foo>>()
verify(request, times(1)).bodyToMono(object : ParameterizedTypeReference<List<Foo>>() {})
}
@Test
fun `bodyToFlux with reified type parameters`() {
request.bodyToFlux<Foo>()
verify(request, times(1)).bodyToFlux(Foo::class.java)
request.bodyToFlux<List<Foo>>()
verify(request, times(1)).bodyToFlux(object : ParameterizedTypeReference<List<Foo>>() {})
}
class Foo

View File

@@ -24,6 +24,8 @@ import org.mockito.Mock
import org.mockito.Mockito.*
import org.mockito.junit.MockitoJUnitRunner
import org.reactivestreams.Publisher
import org.springframework.core.ParameterizedTypeReference
import org.springframework.http.MediaType.*
/**
* Mock object based tests for [ServerResponse] Kotlin extensions
@@ -39,9 +41,16 @@ class ServerResponseExtensionsTests {
@Test
fun `BodyBuilder#body with Publisher and reified type parameters`() {
val body = mock<Publisher<Foo>>()
val body = mock<Publisher<List<Foo>>>()
bodyBuilder.body(body)
verify(bodyBuilder, times(1)).body(body, Foo::class.java)
verify(bodyBuilder, times(1)).body(body, object : ParameterizedTypeReference<List<Foo>>() {})
}
@Test
fun `BodyBuilder#bodyToServerSentEvents with Publisher and reified type parameters`() {
val body = mock<Publisher<List<Foo>>>()
bodyBuilder.bodyToServerSentEvents(body)
verify(bodyBuilder, times(1)).contentType(TEXT_EVENT_STREAM)
}
class Foo