|
24 | 24 | import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
25 | 25 | import static com.github.tomakehurst.wiremock.client.WireMock.verify; |
26 | 26 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
27 | 28 |
|
28 | 29 | import com.fasterxml.jackson.databind.ObjectMapper; |
29 | 30 | import com.github.tomakehurst.wiremock.WireMockServer; |
|
36 | 37 | import com.netflix.spinnaker.config.okhttp3.OkHttpClientProvider; |
37 | 38 | import com.netflix.spinnaker.kork.client.ServiceClientFactory; |
38 | 39 | import com.netflix.spinnaker.kork.client.ServiceClientProvider; |
| 40 | +import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerConversionException; |
| 41 | +import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerHttpException; |
| 42 | +import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerServerException; |
39 | 43 | import com.netflix.spinnaker.okhttp.OkHttpClientConfigurationProperties; |
40 | 44 | import java.util.List; |
41 | 45 | import java.util.Map; |
@@ -108,15 +112,16 @@ void testRetrofit2ClientWithResponse() { |
108 | 112 | .willReturn( |
109 | 113 | aResponse() |
110 | 114 | .withHeader("Content-Type", "application/json") |
111 | | - .withBody("{\"message\": \"success\", \"code\": 200}"))); |
| 115 | + .withBody("{\"message\": \"success\"}"))); |
112 | 116 |
|
113 | 117 | ServiceEndpoint serviceEndpoint = |
114 | 118 | new DefaultServiceEndpoint("retrofit2service", "http://localhost:" + port); |
115 | 119 | Retrofit2TestService retrofit2TestService = |
116 | 120 | serviceClientProvider.getService(Retrofit2TestService.class, serviceEndpoint); |
117 | 121 | Response<Map<String, String>> response = |
118 | 122 | Retrofit2SyncCall.executeCall(retrofit2TestService.getSomething()); |
119 | | - |
| 123 | + assertEquals(response.code(), 200); |
| 124 | + assertEquals(response.headers().get("Content-Type"), "application/json"); |
120 | 125 | assertEquals(response.body().get("message"), "success"); |
121 | 126 | } |
122 | 127 |
|
@@ -150,6 +155,54 @@ void testRetrofit2Client_withInterceptor() { |
150 | 155 | .withHeader("Authorization", equalTo("Bearer my-token"))); |
151 | 156 | } |
152 | 157 |
|
| 158 | + @Test |
| 159 | + void testRetrofit2Client_withHttpException() { |
| 160 | + stubFor( |
| 161 | + get(urlEqualTo("/test")) |
| 162 | + .willReturn( |
| 163 | + aResponse() |
| 164 | + .withHeader("Content-Type", "application/json") |
| 165 | + .withStatus(400) |
| 166 | + .withBody("{\"message\": \"error\"}"))); |
| 167 | + |
| 168 | + ServiceEndpoint serviceEndpoint = |
| 169 | + new DefaultServiceEndpoint("retrofit2service", "http://localhost:" + port); |
| 170 | + |
| 171 | + Retrofit2TestService retrofit2TestService = |
| 172 | + serviceClientProvider.getService(Retrofit2TestService.class, serviceEndpoint); |
| 173 | + |
| 174 | + SpinnakerHttpException exception = |
| 175 | + assertThrows( |
| 176 | + SpinnakerHttpException.class, |
| 177 | + () -> Retrofit2SyncCall.executeCall(retrofit2TestService.getSomething())); |
| 178 | + assertEquals(exception.getResponseCode(), 400); |
| 179 | + assertEquals( |
| 180 | + exception.getMessage(), |
| 181 | + "Status: 400, Method: GET, URL: http://localhost:" + port + "/test, Message: error"); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + void testRetrofit2Client_withConversionException() { |
| 186 | + stubFor( |
| 187 | + get(urlEqualTo("/test")) |
| 188 | + .willReturn( |
| 189 | + aResponse() |
| 190 | + .withHeader("Content-Type", "application/json") |
| 191 | + .withBody("{\"message\": \"incorrect json}"))); |
| 192 | + |
| 193 | + ServiceEndpoint serviceEndpoint = |
| 194 | + new DefaultServiceEndpoint("retrofit2service", "http://localhost:" + port); |
| 195 | + |
| 196 | + Retrofit2TestService retrofit2TestService = |
| 197 | + serviceClientProvider.getService(Retrofit2TestService.class, serviceEndpoint); |
| 198 | + |
| 199 | + SpinnakerServerException exception = |
| 200 | + assertThrows( |
| 201 | + SpinnakerConversionException.class, |
| 202 | + () -> Retrofit2SyncCall.executeCall(retrofit2TestService.getSomething())); |
| 203 | + assertEquals(exception.getMessage(), "Failed to process response body"); |
| 204 | + } |
| 205 | + |
153 | 206 | @Configuration |
154 | 207 | public static class Retrofit2TestConfig { |
155 | 208 |
|
|
0 commit comments