Allow using multiple qualifiers for feign clients. (#485)

* Allow using multiple qualifiers for feign clients. Fixes gh-471.
This commit is contained in:
Olga Maciaszek-Sharma
2021-02-18 13:28:18 +01:00
committed by GitHub
parent a205afb88d
commit c92849e28e
7 changed files with 194 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* 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.
@@ -84,9 +84,9 @@ public class FeignClientBuilderTests {
for (final Method method : FeignClient.class.getMethods()) {
methodNames.add(method.getName());
}
methodNames.removeAll(
Arrays.asList("annotationType", "value", "serviceId", "qualifier",
"configuration", "primary", "equals", "hashCode", "toString"));
methodNames.removeAll(Arrays.asList("annotationType", "value", "serviceId",
"qualifier", "qualifiers", "configuration", "primary", "equals",
"hashCode", "toString"));
Collections.sort(methodNames);
// If this safety check fails the Builder has to be updated.
// (1) Either a field was removed from the FeignClient annotation and so it has to

View File

@@ -0,0 +1,135 @@
/*
* 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 org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.openfeign.test.NoSecurityConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link FeignClientsRegistrar}.
*
* @author Olga Maciaszek-Sharma
*/
@SpringBootTest(
classes = FeignClientsRegistrarIntegrationTests.QualifiersTestConfig.class)
class FeignClientsRegistrarIntegrationTests {
@Autowired
ConfigurableApplicationContext context;
@Test
void shouldUseQualifiersIfPresent() {
assertThat(context.getBean("qualifier1")).isNotNull();
assertThat(context.getBean("qualifier2")).isNotNull();
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> context.getBean("qualifier3"));
}
@Test
void shouldUseQualifierIfQualifiersArrayNotPresent() {
assertThat(context.getBean("qualifier4")).isNotNull();
}
@Test
void shouldUseDefaultQualifierWhenNonePresent() {
assertThat(context.getBean("noQualifiersFeignClient")).isNotNull();
}
@Test
void shouldUseQualifierWhenEmptyQualifiers() {
assertThat(context.getBean("test1")).isNotNull();
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> context.getBean("emptyQualifiersFeignClient"));
}
@Test
void shouldUseQualifierWhenWhitespaceQualifiers() {
assertThat(context.getBean("test2")).isNotNull();
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> context.getBean("whitespaceQualifiersFeignClient"));
}
@Test
void shouldUseDefaultQualifierWhenEmptyQualifiers() {
assertThat(context.getBean("emptyQualifiersNoQualifierFeignClient")).isNotNull();
}
@Test
void shouldUseDefaultQualifierWhenWhitespaceQualifiers() {
assertThat(context.getBean("whitespaceQualifiersNoQualifierFeignClient"))
.isNotNull();
}
@FeignClient(name = "qualifiersClient", qualifiers = { "qualifier1", "qualifier2" },
qualifier = "qualifier3")
protected interface QualifiersClient {
}
@FeignClient(name = "qualifierClient", qualifier = "qualifier4")
protected interface QualifierClient {
}
@FeignClient(name = "noQualifiers")
protected interface NoQualifiersClient {
}
@FeignClient(name = "emptyQualifiers", qualifier = "test1", qualifiers = {})
protected interface EmptyQualifiersClient {
}
@FeignClient(name = "whitespaceQualifiers", qualifier = "test2", qualifiers = { " " })
protected interface WhitespaceQualifiersClient {
}
@FeignClient(name = "emptyQualifiersNoQualifier", qualifiers = {})
protected interface EmptyQualifiersNoQualifierClient {
}
@FeignClient(name = "whitespaceQualifiersNoQualifier", qualifiers = { " " })
protected interface WhitespaceQualifiersNoQualifierClient {
}
@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
@Import(NoSecurityConfiguration.class)
@EnableFeignClients(clients = { QualifiersClient.class, QualifierClient.class,
NoQualifiersClient.class, EmptyQualifiersClient.class,
WhitespaceQualifiersClient.class, EmptyQualifiersNoQualifierClient.class,
WhitespaceQualifiersNoQualifierClient.class })
protected static class QualifiersTestConfig {
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* 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.
@@ -80,7 +80,7 @@ public class FeignClientsRegistrarTests {
private String testGetName(String name) {
FeignClientsRegistrar registrar = new FeignClientsRegistrar();
registrar.setEnvironment(new MockEnvironment());
return registrar.getName(Collections.<String, Object>singletonMap("name", name));
return registrar.getName(Collections.singletonMap("name", name));
}
@Test(expected = IllegalArgumentException.class)
@@ -102,7 +102,6 @@ public class FeignClientsRegistrarTests {
assertThatCode(() -> config.refresh()).as(
"Case https://github.com/spring-cloud/spring-cloud-openfeign/issues/331 should be solved")
.doesNotThrowAnyException();
}
@FeignClient(name = "fallbackTestClient", url = "http://localhost:8080/",

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* 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.
@@ -101,7 +101,8 @@ public class FeignPageableEncodingTests {
Pageable pageable = PageRequest.of(0, 10, Sort.Direction.DESC, "sortProperty");
// when
final ResponseEntity<Page<Invoice>> response = this.invoiceClient.getInvoicesPaged(pageable);
final ResponseEntity<Page<Invoice>> response = this.invoiceClient
.getInvoicesPaged(pageable);
// then
assertThat(response).isNotNull();
@@ -125,11 +126,12 @@ public class FeignPageableEncodingTests {
@Test
public void testPageableWithMultipleSort() {
// given
Pageable pageable = PageRequest.of(0, 10,
Sort.by(Sort.Order.desc("sortProperty1"), Sort.Order.asc("sortProperty2")));
Pageable pageable = PageRequest.of(0, 10, Sort
.by(Sort.Order.desc("sortProperty1"), Sort.Order.asc("sortProperty2")));
// when
final ResponseEntity<Page<Invoice>> response = this.invoiceClient.getInvoicesPaged(pageable);
final ResponseEntity<Page<Invoice>> response = this.invoiceClient
.getInvoicesPaged(pageable);
// then
assertThat(response).isNotNull();