40 changed files with 822 additions and 376 deletions
@ -1 +0,0 @@
@@ -1 +0,0 @@
|
||||
spring-cloud-openfeign.adoc |
@ -0,0 +1 @@
@@ -0,0 +1 @@
|
||||
include::spring-cloud-openfeign.adoc[] |
@ -0,0 +1,34 @@
@@ -0,0 +1,34 @@
|
||||
/* |
||||
* Copyright 2013-2021 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 |
||||
* |
||||
* https://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.cloud.openfeign; |
||||
|
||||
import java.lang.reflect.Method; |
||||
|
||||
import feign.Target; |
||||
|
||||
/** |
||||
* Used to resolve a circuitbreaker name which will be used in |
||||
* {@link org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory}. |
||||
* |
||||
* @author Kwangyong Kim |
||||
* @since 2020.0.4 |
||||
*/ |
||||
public interface CircuitBreakerNameResolver { |
||||
|
||||
String resolveCircuitBreakerName(String feignClientName, Target<?> target, Method method); |
||||
|
||||
} |
@ -0,0 +1,67 @@
@@ -0,0 +1,67 @@
|
||||
/* |
||||
* Copyright 2013-2021 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 |
||||
* |
||||
* https://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.cloud.openfeign.annotation; |
||||
|
||||
import java.lang.annotation.Annotation; |
||||
import java.lang.reflect.Method; |
||||
import java.util.Arrays; |
||||
|
||||
import feign.MethodMetadata; |
||||
|
||||
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor; |
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.web.bind.annotation.CookieValue; |
||||
|
||||
import static feign.Util.checkState; |
||||
import static feign.Util.emptyToNull; |
||||
|
||||
/** |
||||
* @{link CookieValue} annotation processor. |
||||
* @author Gong Yi |
||||
* |
||||
*/ |
||||
public class CookieValueParameterProcessor implements AnnotatedParameterProcessor { |
||||
|
||||
private static final Class<CookieValue> ANNOTATION = CookieValue.class; |
||||
|
||||
@Override |
||||
public Class<? extends Annotation> getAnnotationType() { |
||||
return ANNOTATION; |
||||
} |
||||
|
||||
@Override |
||||
public boolean processArgument(AnnotatedParameterContext context, Annotation annotation, Method method) { |
||||
int parameterIndex = context.getParameterIndex(); |
||||
MethodMetadata data = context.getMethodMetadata(); |
||||
CookieValue cookie = ANNOTATION.cast(annotation); |
||||
String name = cookie.value().trim(); |
||||
checkState(emptyToNull(name) != null, "Cookie.name() was empty on parameter %s", parameterIndex); |
||||
context.setParameterName(name); |
||||
String cookieExpression = data.template().headers().getOrDefault(HttpHeaders.COOKIE, Arrays.asList("")).stream() |
||||
.findFirst().orElse(""); |
||||
if (cookieExpression.length() == 0) { |
||||
cookieExpression = String.format("%s={%s}", name, name); |
||||
} |
||||
else { |
||||
cookieExpression += String.format("; %s={%s}", name, name); |
||||
} |
||||
data.template().removeHeader(HttpHeaders.COOKIE); |
||||
data.template().header(HttpHeaders.COOKIE, cookieExpression); |
||||
return true; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
/* |
||||
* Copyright 2016-2021 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 |
||||
* |
||||
* https://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.cloud.openfeign.hateoas; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.springframework.cloud.openfeign.support.HttpMessageConverterCustomizer; |
||||
import org.springframework.hateoas.config.WebConverters; |
||||
import org.springframework.http.converter.HttpMessageConverter; |
||||
|
||||
/** |
||||
* @author Olga Maciaszek-Sharma |
||||
*/ |
||||
public class WebConvertersCustomizer implements HttpMessageConverterCustomizer { |
||||
|
||||
private final WebConverters webConverters; |
||||
|
||||
public WebConvertersCustomizer(WebConverters webConverters) { |
||||
this.webConverters = webConverters; |
||||
} |
||||
|
||||
@Override |
||||
public void accept(List<HttpMessageConverter<?>> httpMessageConverters) { |
||||
webConverters.augmentClient(httpMessageConverters); |
||||
} |
||||
|
||||
} |
@ -1,80 +0,0 @@
@@ -1,80 +0,0 @@
|
||||
/* |
||||
* Copyright 2013-2020 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 |
||||
* |
||||
* https://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.cloud.openfeign.support; |
||||
|
||||
import java.io.BufferedReader; |
||||
import java.io.IOException; |
||||
import java.io.InputStreamReader; |
||||
import java.lang.reflect.Type; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.Collection; |
||||
import java.util.zip.GZIPInputStream; |
||||
|
||||
import feign.FeignException; |
||||
import feign.Response; |
||||
import feign.codec.Decoder; |
||||
|
||||
import org.springframework.cloud.openfeign.encoding.HttpEncoding; |
||||
|
||||
/** |
||||
* When response is compressed as gzip, this decompresses and uses {@link SpringDecoder} |
||||
* to decode. |
||||
* |
||||
* @author Jaesik Kim |
||||
*/ |
||||
public class DefaultGzipDecoder implements Decoder { |
||||
|
||||
private Decoder decoder; |
||||
|
||||
public DefaultGzipDecoder(Decoder decoder) { |
||||
this.decoder = decoder; |
||||
} |
||||
|
||||
@Override |
||||
public Object decode(final Response response, Type type) throws IOException, FeignException { |
||||
Collection<String> encoding = response.headers().containsKey(HttpEncoding.CONTENT_ENCODING_HEADER) |
||||
? response.headers().get(HttpEncoding.CONTENT_ENCODING_HEADER) : null; |
||||
|
||||
if (encoding != null) { |
||||
if (encoding.contains(HttpEncoding.GZIP_ENCODING)) { |
||||
String decompressedBody = decompress(response); |
||||
if (decompressedBody != null) { |
||||
Response decompressedResponse = response.toBuilder().body(decompressedBody.getBytes()).build(); |
||||
return decoder.decode(decompressedResponse, type); |
||||
} |
||||
} |
||||
} |
||||
return decoder.decode(response, type); |
||||
} |
||||
|
||||
private String decompress(Response response) throws IOException { |
||||
if (response.body() == null) { |
||||
return null; |
||||
} |
||||
try (GZIPInputStream gzipInputStream = new GZIPInputStream(response.body().asInputStream()); |
||||
BufferedReader reader = new BufferedReader( |
||||
new InputStreamReader(gzipInputStream, StandardCharsets.UTF_8))) { |
||||
String outputString = ""; |
||||
String line; |
||||
while ((line = reader.readLine()) != null) { |
||||
outputString += line; |
||||
} |
||||
return outputString; |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,58 +0,0 @@
@@ -1,58 +0,0 @@
|
||||
/* |
||||
* Copyright 2013-2020 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 |
||||
* |
||||
* https://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.cloud.openfeign.support; |
||||
|
||||
import feign.codec.Decoder; |
||||
import feign.optionals.OptionalDecoder; |
||||
|
||||
import org.springframework.beans.factory.ObjectFactory; |
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters; |
||||
import org.springframework.cloud.openfeign.FeignAutoConfiguration; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
/** |
||||
* Configures Default Gzip Decoder. |
||||
* |
||||
* @author Jaesik Kim |
||||
*/ |
||||
@Configuration(proxyBeanMethods = false) |
||||
@ConditionalOnProperty("feign.compression.response.enabled") |
||||
// The OK HTTP client uses "transparent" compression.
|
||||
// If the accept-encoding header is present, it disables transparent compression
|
||||
@ConditionalOnMissingBean(type = "okhttp3.OkHttpClient") |
||||
@AutoConfigureAfter(FeignAutoConfiguration.class) |
||||
public class DefaultGzipDecoderConfiguration { |
||||
|
||||
private ObjectFactory<HttpMessageConverters> messageConverters; |
||||
|
||||
public DefaultGzipDecoderConfiguration(ObjectFactory<HttpMessageConverters> messageConverters) { |
||||
this.messageConverters = messageConverters; |
||||
} |
||||
|
||||
@Bean |
||||
@ConditionalOnMissingBean |
||||
@ConditionalOnProperty("feign.compression.response.useGzipDecoder") |
||||
public Decoder defaultGzipDecoder() { |
||||
return new OptionalDecoder( |
||||
new ResponseEntityDecoder(new DefaultGzipDecoder(new SpringDecoder(messageConverters)))); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,54 @@
@@ -0,0 +1,54 @@
|
||||
/* |
||||
* Copyright 2013-2021 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 |
||||
* |
||||
* https://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.cloud.openfeign.support; |
||||
|
||||
import java.util.function.Consumer; |
||||
|
||||
import org.springframework.beans.BeansException; |
||||
import org.springframework.beans.factory.ObjectProvider; |
||||
|
||||
/** |
||||
* @author Olga Maciaszek-Sharma |
||||
*/ |
||||
class EmptyObjectProvider<T> implements ObjectProvider<T> { |
||||
|
||||
@Override |
||||
public T getObject(Object... args) throws BeansException { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public T getIfAvailable() throws BeansException { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public T getIfUnique() throws BeansException { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public T getObject() throws BeansException { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public void forEach(Consumer action) { |
||||
// do nothing
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,33 @@
@@ -0,0 +1,33 @@
|
||||
/* |
||||
* Copyright 2016-2021 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 |
||||
* |
||||
* https://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.cloud.openfeign.support; |
||||
|
||||
import java.util.List; |
||||
import java.util.function.Consumer; |
||||
|
||||
import org.springframework.http.converter.HttpMessageConverter; |
||||
|
||||
/** |
||||
* Allows customising {@link HttpMessageConverter} objects passed via {@link Consumer} |
||||
* parameter. |
||||
* |
||||
* @author Olga Maciaszek-Sharma |
||||
* @since 3.1.0 |
||||
*/ |
||||
public interface HttpMessageConverterCustomizer extends Consumer<List<HttpMessageConverter<?>>> { |
||||
|
||||
} |
@ -1,90 +0,0 @@
@@ -1,90 +0,0 @@
|
||||
/* |
||||
* Copyright 2016-2020 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 |
||||
* |
||||
* https://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.cloud.openfeign.hateoas; |
||||
|
||||
import java.util.Collections; |
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
|
||||
import org.springframework.beans.factory.ObjectProvider; |
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
||||
import org.springframework.hateoas.mediatype.MessageResolver; |
||||
import org.springframework.hateoas.mediatype.hal.CurieProvider; |
||||
import org.springframework.hateoas.mediatype.hal.HalConfiguration; |
||||
import org.springframework.hateoas.mediatype.hal.HalMediaTypeConfiguration; |
||||
import org.springframework.hateoas.mediatype.hal.Jackson2HalModule; |
||||
import org.springframework.hateoas.server.LinkRelationProvider; |
||||
import org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2HttpMessageConverter; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.ArgumentMatchers.any; |
||||
import static org.mockito.Mockito.mock; |
||||
import static org.mockito.Mockito.when; |
||||
import static org.springframework.hateoas.MediaTypes.HAL_JSON; |
||||
|
||||
/** |
||||
* @author Hector Espert |
||||
* @author Olga Maciaszek-Sharma |
||||
*/ |
||||
@ExtendWith(MockitoExtension.class) |
||||
public class FeignHalAutoConfigurationTests { |
||||
|
||||
@Mock |
||||
private ObjectProvider<HalConfiguration> halConfiguration; |
||||
|
||||
@Mock |
||||
private ObjectProvider<ObjectMapper> objectMapper; |
||||
|
||||
@Mock |
||||
private LinkRelationProvider relProvider; |
||||
|
||||
@Mock |
||||
private ObjectProvider<CurieProvider> curieProvider; |
||||
|
||||
@Mock |
||||
private MessageResolver messageResolver; |
||||
|
||||
@InjectMocks |
||||
private FeignHalAutoConfiguration feignHalAutoConfiguration; |
||||
|
||||
@Test |
||||
public void halJacksonHttpMessageConverter() { |
||||
ObjectMapper mapper = new ObjectMapper(); |
||||
when(objectMapper.getIfAvailable(any())).thenReturn(mapper); |
||||
|
||||
when(halConfiguration.getIfAvailable(any())).thenReturn(mock(HalConfiguration.class)); |
||||
when(curieProvider.getIfAvailable(any())).thenReturn(mock(CurieProvider.class)); |
||||
|
||||
HalMediaTypeConfiguration halMediaTypeConfiguration = new HalMediaTypeConfiguration(relProvider, curieProvider, |
||||
halConfiguration, messageResolver, new DefaultListableBeanFactory()); |
||||
|
||||
TypeConstrainedMappingJackson2HttpMessageConverter converter = feignHalAutoConfiguration |
||||
.halJacksonHttpMessageConverter(objectMapper, halMediaTypeConfiguration); |
||||
|
||||
assertThat(converter).isNotNull(); |
||||
assertThat(converter.getObjectMapper()).isNotNull(); |
||||
assertThat(converter.getSupportedMediaTypes()).isEqualTo(Collections.singletonList(HAL_JSON)); |
||||
|
||||
assertThat(Jackson2HalModule.isAlreadyRegisteredIn(converter.getObjectMapper())).isTrue(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
/* |
||||
* Copyright 2013-2020 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 |
||||
* |
||||
* https://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.cloud.openfeign.support; |
||||
|
||||
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties; |
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; |
||||
|
||||
/** |
||||
* Tests the pagination encoding and sorting. |
||||
* |
||||
* @author Yanming Zhou |
||||
*/ |
||||
@EnableConfigurationProperties(SpringDataWebProperties.class) |
||||
@SpringBootTest(classes = SpringEncoderTests.Application.class, webEnvironment = RANDOM_PORT, |
||||
value = { "spring.application.name=springencodertest", "spring.jmx.enabled=false", |
||||
"spring.data.web.pageable.pageParameter=pageNo", "spring.data.web.pageable.sizeParameter=pageSize", |
||||
"spring.data.web.sort.sortParameter=orderBy" }) |
||||
public class PageableEncoderWithSpringDataWebTests extends PageableEncoderTests { |
||||
|
||||
@Override |
||||
protected String getPageParameter() { |
||||
return "pageNo"; |
||||
} |
||||
|
||||
@Override |
||||
protected String getSizeParameter() { |
||||
return "pageSize"; |
||||
} |
||||
|
||||
@Override |
||||
protected String getSortParameter() { |
||||
return "orderBy"; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,124 @@
@@ -0,0 +1,124 @@
|
||||
/* |
||||
* Copyright 2013-2020 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 |
||||
* |
||||
* https://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.cloud.openfeign.support; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import feign.QueryMapEncoder; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.cloud.openfeign.FeignContext; |
||||
import org.springframework.data.domain.PageRequest; |
||||
import org.springframework.data.domain.Pageable; |
||||
import org.springframework.data.domain.Sort; |
||||
import org.springframework.test.annotation.DirtiesContext; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; |
||||
|
||||
/** |
||||
* Tests the pagination encoding and sorting. |
||||
* |
||||
* @author Yanming Zhou |
||||
*/ |
||||
@SpringBootTest(classes = SpringEncoderTests.Application.class, webEnvironment = RANDOM_PORT, |
||||
value = { "spring.application.name=springencodertest", "spring.jmx.enabled=false" }) |
||||
@DirtiesContext |
||||
public class PageableSpringQueryMapEncoderTests { |
||||
|
||||
public static final int PAGE = 1; |
||||
|
||||
public static final int SIZE = 10; |
||||
|
||||
public static final String SORT_2 = "sort2"; |
||||
|
||||
public static final String SORT_1 = "sort1"; |
||||
|
||||
@Autowired |
||||
private FeignContext context; |
||||
|
||||
protected String getPageParameter() { |
||||
return "page"; |
||||
} |
||||
|
||||
protected String getSizeParameter() { |
||||
return "size"; |
||||
} |
||||
|
||||
protected String getSortParameter() { |
||||
return "sort"; |
||||
} |
||||
|
||||
@Test |
||||
public void testPaginationAndSortingRequest() { |
||||
QueryMapEncoder encoder = this.context.getInstance("foo", QueryMapEncoder.class); |
||||
assertThat(encoder).isNotNull(); |
||||
|
||||
Map<String, Object> map = encoder.encode(createPageAndSortRequest()); |
||||
assertThat(map).hasSize(3); |
||||
assertThat((Integer) map.get(getPageParameter())).isEqualTo(PAGE); |
||||
assertThat((Integer) map.get(getSizeParameter())).isEqualTo(SIZE); |
||||
assertThat((List<?>) map.get(getSortParameter())).hasSize(2); |
||||
} |
||||
|
||||
private Pageable createPageAndSortRequest() { |
||||
return PageRequest.of(PAGE, SIZE, Sort.Direction.ASC, SORT_1, SORT_2); |
||||
} |
||||
|
||||
@Test |
||||
public void testPaginationRequest() { |
||||
QueryMapEncoder encoder = this.context.getInstance("foo", QueryMapEncoder.class); |
||||
assertThat(encoder).isNotNull(); |
||||
|
||||
Map<String, Object> map = encoder.encode(createPageAndRequest()); |
||||
assertThat(map).hasSize(2); |
||||
assertThat((Integer) map.get(getPageParameter())).isEqualTo(PAGE); |
||||
assertThat((Integer) map.get(getSizeParameter())).isEqualTo(SIZE); |
||||
assertThat(map).doesNotContainKey(getSortParameter()); |
||||
} |
||||
|
||||
private Pageable createPageAndRequest() { |
||||
return PageRequest.of(PAGE, SIZE); |
||||
} |
||||
|
||||
@Test |
||||
public void testSortingRequest() { |
||||
QueryMapEncoder encoder = this.context.getInstance("foo", QueryMapEncoder.class); |
||||
assertThat(encoder).isNotNull(); |
||||
|
||||
Map<String, Object> map = encoder.encode(createSort()); |
||||
assertThat(map).hasSize(1); |
||||
assertThat((List<?>) map.get(getSortParameter())).hasSize(2); |
||||
} |
||||
|
||||
private Sort createSort() { |
||||
return Sort.by(SORT_1, SORT_2).ascending(); |
||||
} |
||||
|
||||
@Test |
||||
public void testUnpagedRequest() { |
||||
QueryMapEncoder encoder = this.context.getInstance("foo", QueryMapEncoder.class); |
||||
assertThat(encoder).isNotNull(); |
||||
|
||||
Map<String, Object> map = encoder.encode(Pageable.unpaged()); |
||||
assertThat(map).isEmpty(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
/* |
||||
* Copyright 2013-2020 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 |
||||
* |
||||
* https://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.cloud.openfeign.support; |
||||
|
||||
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties; |
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; |
||||
|
||||
/** |
||||
* Tests the pagination encoding and sorting. |
||||
* |
||||
* @author Yanming Zhou |
||||
*/ |
||||
@EnableConfigurationProperties(SpringDataWebProperties.class) |
||||
@SpringBootTest(classes = SpringEncoderTests.Application.class, webEnvironment = RANDOM_PORT, |
||||
value = { "spring.application.name=springencodertest", "spring.jmx.enabled=false", |
||||
"spring.data.web.pageable.pageParameter=pageNo", "spring.data.web.pageable.sizeParameter=pageSize", |
||||
"spring.data.web.sort.sortParameter=orderBy" }) |
||||
public class PageableSpringQueryMapEncoderWithSpringDataWebTests extends PageableSpringQueryMapEncoderTests { |
||||
|
||||
@Override |
||||
protected String getPageParameter() { |
||||
return "pageNo"; |
||||
} |
||||
|
||||
@Override |
||||
protected String getSizeParameter() { |
||||
return "pageSize"; |
||||
} |
||||
|
||||
@Override |
||||
protected String getSortParameter() { |
||||
return "orderBy"; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue