Refactor.

This commit is contained in:
Olga MaciaszekSharma
2023-12-04 17:30:35 +01:00
parent 4465795642
commit c99923bec2
3 changed files with 18 additions and 24 deletions

View File

@@ -72,6 +72,7 @@ import org.springframework.util.StringUtils;
* @author Hyeonmin Park
* @author Felix Dittrich
* @author Dominique Villard
* @athor Can Bezmen
*/
public class FeignClientFactoryBean
implements FactoryBean<Object>, InitializingBean, ApplicationContextAware, BeanFactoryAware {
@@ -495,7 +496,7 @@ public class FeignClientFactoryBean
RefreshableUrl refreshableUrl = context.getInstance(contextId,
RefreshableUrl.class.getCanonicalName() + "-" + contextId, RefreshableUrl.class);
if (Objects.nonNull(refreshableUrl) && StringUtils.hasText(refreshableUrl.getUrl())) {
return new RefreshableHardCodedTarget<>(type, name, refreshableUrl);
return new RefreshableHardCodedTarget<>(type, name, refreshableUrl, cleanPath());
}
}
FeignClientProperties.FeignClientConfiguration config = findConfigByKey(contextId);

View File

@@ -25,6 +25,7 @@ import feign.Target;
* `spring.cloud.openfeign.client.config.[clientId].url`.
*
* @author Olga Maciaszek-Sharma
* @author Can Bezmen
* @see FeignClientProperties.FeignClientConfiguration#getUrl()
*/
public class PropertyBasedTarget<T> extends Target.HardCodedTarget<T> {
@@ -42,6 +43,12 @@ public class PropertyBasedTarget<T> extends Target.HardCodedTarget<T> {
this.path = path;
}
public PropertyBasedTarget(Class<T> type, String name, FeignClientProperties.FeignClientConfiguration config) {
super(type, name, config.getUrl());
this.config = config;
path = "";
}
@Override
public String url() {
if (url == null) {

View File

@@ -57,12 +57,6 @@ class NonRefreshableFeignClientUrlTests {
assertThat(response.getTargetType()).isEqualTo(Target.HardCodedTarget.class);
}
@Test
void shouldInstantiateFeignClientWhenUrlFromFeignClientUrlGivenPreferenceOverProperties() {
UrlTestClient.UrlResponseForTests response = feignClientWithFixUrl.fixPath();
assertThat(response.getUrl()).isEqualTo("http://localhost:8081/fixPath");
}
@Test
public void shouldInstantiateFeignClientWhenUrlFromProperties() {
UrlTestClient.UrlResponseForTests response = configBasedClient.test();
@@ -79,34 +73,26 @@ class NonRefreshableFeignClientUrlTests {
@Test
void shouldInstantiateFeignClientWhenUrlAndPathAreInTheFeignClientAnnotation(
@Autowired Application.WithPathAndFixUrlClient withPathAndFixUrlClient
) {
UrlTestClient.UrlResponseForTests response = withPathAndFixUrlClient.test();
@Autowired Application.WithPathAndFixedUrlClient client) {
UrlTestClient.UrlResponseForTests response = client.test();
assertThat(response.getUrl()).isEqualTo("http://localhost:7777/common/test");
assertThat(response.getTargetType()).isEqualTo(Target.HardCodedTarget.class);
}
@Test
void shouldInstantiateFeignClientWhenUrlFromPropertiesAndPathInTheFeignClientAnnotation(
@Autowired Application.WithPathAndUrlFromConfigClient withPathAndUrlFromConfigClient
) {
UrlTestClient.UrlResponseForTests response = withPathAndUrlFromConfigClient.test();
@Autowired Application.WithPathAndUrlFromConfigClient client) {
UrlTestClient.UrlResponseForTests response = client.test();
assertThat(response.getUrl()).isEqualTo("http://localhost:7777/common/test");
assertThat(response.getTargetType()).isEqualTo(Target.HardCodedTarget.class);
assertThat(response.getTargetType()).isEqualTo(PropertyBasedTarget.class);
}
@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties(FeignClientProperties.class)
@EnableFeignClients(
clients = {
Application.FeignClientWithFixUrl.class,
Application.ConfigBasedClient.class,
Application.NameBasedUrlClient.class,
Application.WithPathAndUrlFromConfigClient.class,
Application.WithPathAndFixUrlClient.class
}
)
@EnableFeignClients(clients = { Application.FeignClientWithFixUrl.class, Application.ConfigBasedClient.class,
Application.NameBasedUrlClient.class, Application.WithPathAndUrlFromConfigClient.class,
Application.WithPathAndFixedUrlClient.class })
protected static class Application {
@Bean
@@ -139,7 +125,7 @@ class NonRefreshableFeignClientUrlTests {
}
@FeignClient(name = "withPathAndFixUrlClient", path = "/common", url = "http://localhost:7777")
protected interface WithPathAndFixUrlClient {
protected interface WithPathAndFixedUrlClient {
@GetMapping("/test")
UrlTestClient.UrlResponseForTests test();