18
18
import static com .github .tomakehurst .wiremock .client .WireMock .urlPathEqualTo ;
19
19
import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .options ;
20
20
import static org .junit .Assert .assertEquals ;
21
+ import static org .junit .Assert .fail ;
21
22
22
23
import com .github .tomakehurst .wiremock .junit .WireMockRule ;
23
24
import io .kubernetes .client .Discovery .APIResource ;
24
25
import io .kubernetes .client .openapi .ApiClient ;
25
26
import io .kubernetes .client .openapi .ApiException ;
27
+ import io .kubernetes .client .openapi .models .V1APIGroup ;
28
+ import io .kubernetes .client .openapi .models .V1APIGroupList ;
26
29
import io .kubernetes .client .openapi .models .V1APIResource ;
27
30
import io .kubernetes .client .openapi .models .V1APIResourceList ;
28
31
import io .kubernetes .client .openapi .models .V1APIVersions ;
32
+ import io .kubernetes .client .openapi .models .V1GroupVersionForDiscovery ;
29
33
import io .kubernetes .client .util .ClientBuilder ;
34
+ import io .kubernetes .client .util .exception .IncompleteDiscoveryException ;
30
35
import java .io .IOException ;
31
36
import java .util .ArrayList ;
32
37
import java .util .Arrays ;
@@ -106,13 +111,40 @@ public void testGroupResourcesByName() {
106
111
}
107
112
108
113
@ Test
109
- public void findAllShouldReturnAllApiResourceWhenApiResponseIsSuccess () {
114
+ public void findAllShouldReturnAllApiResourceWhenAllResourceDiscoveryApiResponseAreSuccess () throws ApiException {
110
115
Discovery discovery = new Discovery (apiClient );
111
116
117
+ wireMockRule .stubFor (
118
+ get ("/api" )
119
+ .willReturn (
120
+ aResponse ()
121
+ .withStatus (200 )
122
+ .withHeader ("Content-Type" , "application/json" )
123
+ .withBody (
124
+ apiClient
125
+ .getJSON ()
126
+ .serialize (new V1APIVersions ()))));
127
+
112
128
String group = "test.com" ;
113
129
String version = "v1" ;
114
130
String path ="/apis/" +group +'/' +version ;
115
131
132
+ wireMockRule .stubFor (
133
+ get ("/apis" )
134
+ .willReturn (
135
+ aResponse ()
136
+ .withStatus (200 )
137
+ .withHeader ("Content-Type" , "application/json" )
138
+ .withBody (
139
+ apiClient
140
+ .getJSON ()
141
+ .serialize (new V1APIGroupList ()
142
+ .addGroupsItem (new V1APIGroup ()
143
+ .name (group )
144
+ .preferredVersion (new V1GroupVersionForDiscovery ().version (version ))
145
+ .versions (Arrays .asList (
146
+ new V1GroupVersionForDiscovery ().version (version ))))))));
147
+
116
148
wireMockRule .stubFor (
117
149
get (urlPathEqualTo (path ))
118
150
.willReturn (
@@ -132,29 +164,88 @@ public void findAllShouldReturnAllApiResourceWhenApiResponseIsSuccess() {
132
164
133
165
List <String > versions = new ArrayList <>();
134
166
versions .add (version );
135
- Set <APIResource > apiResources = discovery .findAll (group , versions , version );
167
+ Set <APIResource > apiResources = discovery .findAll ();
136
168
wireMockRule .verify (1 , getRequestedFor (urlPathEqualTo (path )));
137
169
assertEquals (2 , apiResources .size ());
138
170
}
139
171
140
172
@ Test
141
- public void findAllShouldReturnEmptyListWhenApiResponseIsNotSuccess () {
173
+ public void findAllShouldThrowImcompleteDiscoveryExceptionWhenAtLeastOneResourceDiscoveryApiResponseIsNotSuccess () throws ApiException {
142
174
Discovery discovery = new Discovery (apiClient );
143
175
144
- String group = "test.com" ;
176
+ wireMockRule .stubFor (
177
+ get ("/api" )
178
+ .willReturn (
179
+ aResponse ()
180
+ .withStatus (200 )
181
+ .withHeader ("Content-Type" , "application/json" )
182
+ .withBody (
183
+ apiClient
184
+ .getJSON ()
185
+ .serialize (new V1APIVersions ()))));
186
+
187
+ String groupSuccess = "test.com" ;
145
188
String version = "v1" ;
146
- String path ="/apis/" +group +'/' +version ;
189
+ String pathSuccess ="/apis/" +groupSuccess +'/' +version ;
190
+
191
+ String groupError = "testError.com" ;
192
+ String pathError ="/apis/" +groupError +'/' +version ;
147
193
148
194
wireMockRule .stubFor (
149
- get (urlPathEqualTo (path ))
195
+ get ("/apis" )
196
+ .willReturn (
197
+ aResponse ()
198
+ .withStatus (200 )
199
+ .withHeader ("Content-Type" , "application/json" )
200
+ .withBody (
201
+ apiClient
202
+ .getJSON ()
203
+ .serialize (new V1APIGroupList ()
204
+ .addGroupsItem (new V1APIGroup ()
205
+ .name (groupError )
206
+ .preferredVersion (new V1GroupVersionForDiscovery ().version (version ))
207
+ .versions (Arrays .asList (
208
+ new V1GroupVersionForDiscovery ().version (version ))))
209
+ .addGroupsItem (new V1APIGroup ()
210
+ .name (groupSuccess )
211
+ .preferredVersion (new V1GroupVersionForDiscovery ().version (version ))
212
+ .versions (Arrays .asList (
213
+ new V1GroupVersionForDiscovery ().version (version ))))))));
214
+
215
+ wireMockRule .stubFor (
216
+ get (urlPathEqualTo (pathError ))
150
217
.willReturn (
151
218
aResponse ()
152
219
.withStatus (503 )));
153
220
221
+ wireMockRule .stubFor (
222
+ get (urlPathEqualTo (pathSuccess ))
223
+ .willReturn (
224
+ aResponse ()
225
+ .withStatus (200 )
226
+ .withHeader ("Content-Type" , "application/json" )
227
+ .withBody (
228
+ apiClient
229
+ .getJSON ()
230
+ .serialize (new V1APIResourceList ()
231
+ .resources (
232
+ Arrays .asList (
233
+ new V1APIResource ()
234
+ .name ("first" ),
235
+ new V1APIResource ()
236
+ .name ("second" )))))));
237
+
154
238
List <String > versions = new ArrayList <>();
155
239
versions .add (version );
156
- Set <APIResource > apiResources = discovery .findAll (group , versions , version );
157
- wireMockRule .verify (1 , getRequestedFor (urlPathEqualTo (path )));
158
- assertEquals (0 , apiResources .size ());
240
+ Set <APIResource > apiResources = null ;
241
+ try {
242
+ discovery .findAll ();
243
+ fail ("Should have throw ImcompleteDiscoveryException" );
244
+ } catch (IncompleteDiscoveryException e ) {
245
+ apiResources = e .getDiscoveredResources ();
246
+ }
247
+ wireMockRule .verify (1 , getRequestedFor (urlPathEqualTo (pathError )));
248
+ wireMockRule .verify (1 , getRequestedFor (urlPathEqualTo (pathSuccess )));
249
+ assertEquals (2 , apiResources .size ());
159
250
}
160
251
}
0 commit comments