|
20 | 20 | import static org.junit.Assert.assertEquals;
|
21 | 21 |
|
22 | 22 | import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
| 23 | +import io.kubernetes.client.Discovery.APIResource; |
23 | 24 | import io.kubernetes.client.openapi.ApiClient;
|
24 | 25 | import io.kubernetes.client.openapi.ApiException;
|
25 | 26 | import io.kubernetes.client.openapi.models.V1APIResource;
|
|
28 | 29 | import io.kubernetes.client.util.ClientBuilder;
|
29 | 30 | import java.io.IOException;
|
30 | 31 | import java.util.Arrays;
|
| 32 | +import java.util.List; |
31 | 33 | import java.util.Set;
|
32 | 34 | import org.junit.Before;
|
33 | 35 | import org.junit.Rule;
|
@@ -101,4 +103,53 @@ public void testGroupResourcesByName() {
|
101 | 103 | assertEquals(true, meow.getNamespaced());
|
102 | 104 | assertEquals("mouse", meow.getSubResources().get(0));
|
103 | 105 | }
|
| 106 | + |
| 107 | + @Test |
| 108 | + public void findAllShouldReturnAllApiResourceWhenApiResponseIsSuccess() { |
| 109 | + Discovery discovery = new Discovery(apiClient); |
| 110 | + |
| 111 | + String group = "test.com"; |
| 112 | + String version = "v1"; |
| 113 | + String path="/apis/"+group+'/'+version; |
| 114 | + |
| 115 | + wireMockRule.stubFor( |
| 116 | + get(urlPathEqualTo(path)) |
| 117 | + .willReturn( |
| 118 | + aResponse() |
| 119 | + .withStatus(200) |
| 120 | + .withHeader("Content-Type", "application/json") |
| 121 | + .withBody( |
| 122 | + apiClient |
| 123 | + .getJSON() |
| 124 | + .serialize(new V1APIResourceList() |
| 125 | + .resources( |
| 126 | + Arrays.asList( |
| 127 | + new V1APIResource() |
| 128 | + .name("first"), |
| 129 | + new V1APIResource() |
| 130 | + .name("second"))))))); |
| 131 | + |
| 132 | + Set<APIResource> apiResources = discovery.findAll(group, List.of(version), version); |
| 133 | + wireMockRule.verify(1, getRequestedFor(urlPathEqualTo(path))); |
| 134 | + assertEquals(2, apiResources.size()); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void findAllShouldReturnEmptyListWhenApiResponseIsNotSuccess() { |
| 139 | + Discovery discovery = new Discovery(apiClient); |
| 140 | + |
| 141 | + String group = "test.com"; |
| 142 | + String version = "v1"; |
| 143 | + String path="/apis/"+group+'/'+version; |
| 144 | + |
| 145 | + wireMockRule.stubFor( |
| 146 | + get(urlPathEqualTo(path)) |
| 147 | + .willReturn( |
| 148 | + aResponse() |
| 149 | + .withStatus(503))); |
| 150 | + |
| 151 | + Set<APIResource> apiResources = discovery.findAll(group, List.of(version), version); |
| 152 | + wireMockRule.verify(1, getRequestedFor(urlPathEqualTo(path))); |
| 153 | + assertEquals(0, apiResources.size()); |
| 154 | + } |
104 | 155 | }
|
0 commit comments