1
1
/*
2
- * Copyright 2013-2022 the original author or authors.
2
+ * Copyright 2013-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
24
24
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
25
25
import org .springframework .boot .test .context .SpringBootTest ;
26
26
import org .springframework .cloud .context .scope .refresh .RefreshScope ;
27
- import org .springframework .context .ApplicationContext ;
28
27
import org .springframework .context .annotation .Bean ;
29
28
import org .springframework .context .annotation .Configuration ;
30
29
import org .springframework .test .annotation .DirtiesContext ;
35
34
36
35
/**
37
36
* @author Jasbir Singh
37
+ * @author Olga Maciaszek-Sharma
38
38
*/
39
39
@ SpringBootTest
40
40
@ TestPropertySource ("classpath:feign-refreshable-properties.properties" )
41
41
@ DirtiesContext
42
42
class RefreshableFeignClientUrlTests {
43
43
44
- @ Autowired
45
- private ApplicationContext applicationContext ;
46
-
47
44
@ Autowired
48
45
private RefreshScope refreshScope ;
49
46
50
47
@ Autowired
51
- private RefreshableFeignClientUrlTests .Application .RefreshableClientWithFixUrl refreshableClientWithFixUrl ;
48
+ private RefreshableFeignClientUrlTests .Application .RefreshableClientWithFixedUrl refreshableClientWithFixedUrl ;
52
49
53
50
@ Autowired
54
51
private RefreshableFeignClientUrlTests .Application .RefreshableUrlClient refreshableUrlClient ;
55
52
56
53
@ Autowired
57
- private Application .RefreshableUrlClientForContextRefreshCase refreshableUrlClientForContextRefreshCase ;
54
+ private Application .RefreshableUrlClientForContextRefreshCase refreshableClientForContextRefreshCase ;
58
55
59
56
@ Autowired
60
57
private Application .NameBasedUrlClient nameBasedUrlClient ;
@@ -63,16 +60,18 @@ class RefreshableFeignClientUrlTests {
63
60
private FeignClientProperties clientProperties ;
64
61
65
62
@ Test
66
- void shouldInstantiateFeignClientWhenUrlFromFeignClientUrl () {
67
- UrlTestClient .UrlResponseForTests response = refreshableClientWithFixUrl . fixPath ();
68
- assertThat (response .getUrl ()).isEqualTo ("http://localhost:8081/fixPath " );
63
+ void shouldInstantiateFeignClientWhenUrlFromAnnotation () {
64
+ UrlTestClient .UrlResponseForTests response = refreshableClientWithFixedUrl . fixedPath ();
65
+ assertThat (response .getUrl ()).isEqualTo ("http://localhost:8081/fixedPath " );
69
66
assertThat (response .getTargetType ()).isEqualTo (Target .HardCodedTarget .class );
70
67
}
71
68
72
69
@ Test
73
- void shouldInstantiateFeignClientWhenUrlFromFeignClientUrlGivenPreferenceOverProperties () {
74
- UrlTestClient .UrlResponseForTests response = refreshableClientWithFixUrl .fixPath ();
75
- assertThat (response .getUrl ()).isEqualTo ("http://localhost:8081/fixPath" );
70
+ void shouldInstantiateFeignClientWhenUrlAndPathFromAnnotation (
71
+ @ Autowired Application .WithPathAndFixedUrlClient client ) {
72
+ UrlTestClient .UrlResponseForTests response = client .test ();
73
+ assertThat (response .getUrl ()).isEqualTo ("http://localhost:7777/common/test" );
74
+ assertThat (response .getTargetType ()).isEqualTo (Target .HardCodedTarget .class );
76
75
}
77
76
78
77
@ Test
@@ -84,15 +83,28 @@ public void shouldInstantiateFeignClientWhenUrlFromProperties() {
84
83
85
84
@ Test
86
85
void shouldInstantiateFeignClientWhenUrlFromPropertiesAndThenUpdateUrlWhenContextRefresh () {
87
- UrlTestClient .UrlResponseForTests response = refreshableUrlClientForContextRefreshCase .refreshable ();
86
+ UrlTestClient .UrlResponseForTests response = refreshableClientForContextRefreshCase .refreshable ();
88
87
assertThat (response .getUrl ()).isEqualTo ("http://localhost:8080/refreshable" );
89
88
90
- clientProperties .getConfig ().get ("refreshableClient " ).setUrl ("http://localhost:8888/ " );
89
+ clientProperties .getConfig ().get ("refreshableClientForContextRefreshCase " ).setUrl ("http://localhost:8888" );
91
90
refreshScope .refreshAll ();
92
- response = refreshableUrlClient .refreshable ();
91
+ response = refreshableClientForContextRefreshCase .refreshable ();
93
92
assertThat (response .getUrl ()).isEqualTo ("http://localhost:8888/refreshable" );
94
93
}
95
94
95
+ @ Test
96
+ void shouldInstantiateFeignClientWhenUrlFromPropertiesAndPathFromAnnotationThenUpdateUrlWhenContextRefresh (
97
+ @ Autowired Application .RefreshableUrlClientForContextRefreshCaseWithPath client ) {
98
+ UrlTestClient .UrlResponseForTests response = client .refreshable ();
99
+ assertThat (response .getUrl ()).isEqualTo ("http://localhost:8080/common/refreshable" );
100
+
101
+ clientProperties .getConfig ().get ("refreshableClientForContextRefreshCaseWithPath" )
102
+ .setUrl ("http://localhost:8888" );
103
+ refreshScope .refreshAll ();
104
+ response = client .refreshable ();
105
+ assertThat (response .getUrl ()).isEqualTo ("http://localhost:8888/common/refreshable" );
106
+ }
107
+
96
108
@ Test
97
109
void shouldInstantiateFeignClientWhenUrlFromFeignClientName () {
98
110
UrlTestClient .UrlResponseForTests response = nameBasedUrlClient .nonRefreshable ();
@@ -104,20 +116,21 @@ void shouldInstantiateFeignClientWhenUrlFromFeignClientName() {
104
116
@ EnableAutoConfiguration
105
117
@ EnableConfigurationProperties (FeignClientProperties .class )
106
118
@ EnableFeignClients (clients = { Application .RefreshableUrlClient .class , Application .NameBasedUrlClient .class ,
107
- Application .RefreshableClientWithFixUrl .class ,
108
- Application .RefreshableUrlClientForContextRefreshCase .class })
119
+ Application .RefreshableClientWithFixedUrl .class ,
120
+ Application .RefreshableUrlClientForContextRefreshCase .class , Application .WithPathAndFixedUrlClient .class ,
121
+ Application .RefreshableUrlClientForContextRefreshCaseWithPath .class })
109
122
protected static class Application {
110
123
111
124
@ Bean
112
125
UrlTestClient client () {
113
126
return new UrlTestClient ();
114
127
}
115
128
116
- @ FeignClient (name = "refreshableClientWithFixUrl " , url = "http://localhost:8081" )
117
- protected interface RefreshableClientWithFixUrl {
129
+ @ FeignClient (name = "refreshableClientWithFixedUrl " , url = "http://localhost:8081" )
130
+ protected interface RefreshableClientWithFixedUrl {
118
131
119
- @ GetMapping ("/fixPath " )
120
- UrlTestClient .UrlResponseForTests fixPath ();
132
+ @ GetMapping ("/fixedPath " )
133
+ UrlTestClient .UrlResponseForTests fixedPath ();
121
134
122
135
}
123
136
@@ -137,6 +150,14 @@ protected interface RefreshableUrlClientForContextRefreshCase {
137
150
138
151
}
139
152
153
+ @ FeignClient (name = "refreshableClientForContextRefreshCaseWithPath" , path = "/common" )
154
+ protected interface RefreshableUrlClientForContextRefreshCaseWithPath {
155
+
156
+ @ GetMapping ("/refreshable" )
157
+ UrlTestClient .UrlResponseForTests refreshable ();
158
+
159
+ }
160
+
140
161
@ FeignClient (name = "nameBasedClient" )
141
162
protected interface NameBasedUrlClient {
142
163
@@ -145,6 +166,14 @@ protected interface NameBasedUrlClient {
145
166
146
167
}
147
168
169
+ @ FeignClient (name = "withPathAndFixUrlClient" , path = "/common" , url = "http://localhost:7777" )
170
+ protected interface WithPathAndFixedUrlClient {
171
+
172
+ @ GetMapping ("/test" )
173
+ UrlTestClient .UrlResponseForTests test ();
174
+
175
+ }
176
+
148
177
}
149
178
150
179
}
0 commit comments