diff --git a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/ErrorHandlingExecutorCallAdapterFactory.java b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/ErrorHandlingExecutorCallAdapterFactory.java index 81ebacb99..ec3aec12a 100644 --- a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/ErrorHandlingExecutorCallAdapterFactory.java +++ b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/ErrorHandlingExecutorCallAdapterFactory.java @@ -152,11 +152,12 @@ public Response execute() { return syncResp; } } catch (JsonProcessingException jpe) { - throw new SpinnakerConversionException("Failed to process response body", jpe); + throw new SpinnakerConversionException( + "Failed to process response body", jpe, delegate.request()); } catch (IOException e) { - throw new SpinnakerNetworkException(e); + throw new SpinnakerNetworkException(e, delegate.request()); } catch (Exception e) { - throw new SpinnakerServerException(e); + throw new SpinnakerServerException(e, delegate.request()); } throw new SpinnakerHttpException(syncResp, retrofit); } @@ -250,11 +251,11 @@ public void onFailure(Call call, final Throwable t) { SpinnakerServerException exception; if (t instanceof IOException) { - exception = new SpinnakerNetworkException(t); + exception = new SpinnakerNetworkException(t, call.request()); } else if (t instanceof SpinnakerHttpException) { exception = (SpinnakerHttpException) t; } else { - exception = new SpinnakerServerException(t); + exception = new SpinnakerServerException(t, call.request()); } final SpinnakerServerException finalException = exception; callbackExecutor.execute( diff --git a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/Retrofit2SyncCall.java b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/Retrofit2SyncCall.java index 44d69bcc2..189ef1018 100644 --- a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/Retrofit2SyncCall.java +++ b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/Retrofit2SyncCall.java @@ -33,7 +33,7 @@ public static T execute(Call call) { try { return call.execute().body(); } catch (IOException e) { - throw new SpinnakerNetworkException(e); + throw new SpinnakerNetworkException(e, call.request()); } } } diff --git a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerConversionException.java b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerConversionException.java index cfd2e3a6b..a8a5bebfa 100644 --- a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerConversionException.java +++ b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerConversionException.java @@ -16,14 +16,36 @@ package com.netflix.spinnaker.kork.retrofit.exceptions; +import okhttp3.Request; +import retrofit.RetrofitError; + /** Wraps an exception converting a successful retrofit http response body to its indicated type */ public class SpinnakerConversionException extends SpinnakerServerException { - public SpinnakerConversionException(String message, Throwable cause) { + /** + * Construct a SpinnakerServerException from retrofit2 with a message and cause (e.g. an exception + * converting a response to the specified type). + */ + public SpinnakerConversionException(String message, Throwable cause, Request request) { + super(message, cause, request); + setRetryable(false); + } + + /** + * Construct a SpinnakerConversionException from another SpinnakerConversionException (e.g. via + * newInstance). + */ + public SpinnakerConversionException(String message, SpinnakerConversionException cause) { super(message, cause); setRetryable(false); } + /** Construct a SpinnakerConversionException corresponding to a RetrofitError. */ + public SpinnakerConversionException(RetrofitError e) { + super(e); + setRetryable(false); + } + @Override public SpinnakerConversionException newInstance(String message) { return new SpinnakerConversionException(message, this); diff --git a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerHttpException.java b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerHttpException.java index 355aea70e..7a705797f 100644 --- a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerHttpException.java +++ b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerHttpException.java @@ -56,8 +56,6 @@ public class SpinnakerHttpException extends SpinnakerServerException { private final Map responseBody; - private final String url; - private final int responseCode; /** @@ -66,6 +64,7 @@ public class SpinnakerHttpException extends SpinnakerServerException { */ private final String reason; + /** Construct a SpinnakerHttpException corresponding to a RetrofitError. */ public SpinnakerHttpException(RetrofitError e) { super(e); @@ -101,7 +100,6 @@ public SpinnakerHttpException(RetrofitError e) { if (responseBody != null) { tmpMessage = (String) responseBody.get("message"); } - url = e.getUrl(); responseCode = response.getStatus(); reason = response.getReason(); rawMessage = tmpMessage != null ? tmpMessage : reason; @@ -113,6 +111,7 @@ public SpinnakerHttpException(RetrofitError e) { */ public SpinnakerHttpException( retrofit2.Response retrofit2Response, retrofit2.Retrofit retrofit) { + super(retrofit2Response.raw().request()); this.response = null; this.retrofit2Response = retrofit2Response; if ((retrofit2Response.code() == HttpStatus.NOT_FOUND.value()) @@ -120,7 +119,6 @@ public SpinnakerHttpException( setRetryable(false); } responseBody = this.getErrorBodyAs(retrofit); - url = retrofit2Response.raw().request().url().toString(); responseCode = retrofit2Response.code(); reason = retrofit2Response.message(); this.rawMessage = @@ -159,7 +157,6 @@ public SpinnakerHttpException(String message, SpinnakerHttpException cause) { this.retrofit2Response = cause.retrofit2Response; rawMessage = null; this.responseBody = cause.responseBody; - this.url = cause.url; this.responseCode = cause.responseCode; this.reason = cause.reason; } @@ -197,7 +194,8 @@ public String getMessage() { return super.getMessage(); } - return String.format("Status: %s, URL: %s, Message: %s", responseCode, url, getRawMessage()); + return String.format( + "Status: %s, URL: %s, Message: %s", responseCode, this.getUrl(), getRawMessage()); } @Override @@ -209,10 +207,6 @@ public Map getResponseBody() { return this.responseBody; } - public String getUrl() { - return this.url; - } - public String getReason() { return this.reason; } diff --git a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerNetworkException.java b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerNetworkException.java index a51843d82..febf466e0 100644 --- a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerNetworkException.java +++ b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerNetworkException.java @@ -17,19 +17,34 @@ package com.netflix.spinnaker.kork.retrofit.exceptions; import com.netflix.spinnaker.kork.annotations.NonnullByDefault; +import okhttp3.Request; import retrofit.RetrofitError; -/** Wraps an exception of kind {@link RetrofitError.Kind} NETWORK. */ +/** Represents a network error while attempting to execute a retrofit http client request. */ @NonnullByDefault public final class SpinnakerNetworkException extends SpinnakerServerException { - public SpinnakerNetworkException(Throwable cause) { - super(cause); + + /** + * Construct a SpinnakerNetworkException from retrofit2 with a cause (e.g. an exception sending a + * request or processing a response). + */ + public SpinnakerNetworkException(Throwable cause, Request request) { + super(cause, request); } - public SpinnakerNetworkException(String message, Throwable cause) { + /** + * Construct a SpinnakerNetworkException from another SpinnakerNetworkException (e.g. via + * newInstance). + */ + public SpinnakerNetworkException(String message, SpinnakerNetworkException cause) { super(message, cause); } + /** Construct a SpinnakerNetworkException corresponding to a RetrofitError. */ + public SpinnakerNetworkException(RetrofitError e) { + super(e); + } + @Override public SpinnakerNetworkException newInstance(String message) { return new SpinnakerNetworkException(message, this); diff --git a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofitErrorHandler.java b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofitErrorHandler.java index 8dace58aa..ca03cb643 100644 --- a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofitErrorHandler.java +++ b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofitErrorHandler.java @@ -59,11 +59,11 @@ public Throwable handleError(RetrofitError e) { } return retval; case NETWORK: - return new SpinnakerNetworkException(e.getMessage(), e.getCause()); + return new SpinnakerNetworkException(e); case CONVERSION: - return new SpinnakerConversionException(e.getMessage(), e.getCause()); + return new SpinnakerConversionException(e); default: - return new SpinnakerServerException(e.getMessage(), e.getCause()); + return new SpinnakerServerException(e); } } diff --git a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerServerException.java b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerServerException.java index 654c164aa..0b2e6ebca 100644 --- a/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerServerException.java +++ b/kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerServerException.java @@ -18,20 +18,57 @@ import com.netflix.spinnaker.kork.annotations.NonnullByDefault; import com.netflix.spinnaker.kork.exceptions.SpinnakerException; +import lombok.Getter; +import okhttp3.Request; +import retrofit.RetrofitError; /** Represents an error while attempting to execute a retrofit http client request. */ @NonnullByDefault public class SpinnakerServerException extends SpinnakerException { - public SpinnakerServerException(String message, Throwable cause) { - super(message, cause); + @Getter private final String url; + + /** Construct a SpinnakerServerException corresponding to a RetrofitError. */ + public SpinnakerServerException(RetrofitError e) { + super(e.getMessage(), e.getCause()); + url = e.getUrl(); } - public SpinnakerServerException(Throwable cause) { + /** + * Construct a SpinnakerServerException from retrofit2 with no cause (e.g. a non-200 http + * response). + */ + public SpinnakerServerException(Request request) { + super(); + url = request.url().toString(); + } + + /** + * Construct a SpinnakerServerException from retrofit2 with a cause (e.g. an exception sending a + * request or processing a response). + */ + public SpinnakerServerException(Throwable cause, Request request) { super(cause); + this.url = request.url().toString(); } - public SpinnakerServerException() {} + /** + * Construct a SpinnakerServerException from retrofit2 with a message and cause (e.g. an exception + * converting a response to the specified type). + */ + public SpinnakerServerException(String message, Throwable cause, Request request) { + super(message, cause); + this.url = request.url().toString(); + } + + /** + * Construct a SpinnakerServerException from another SpinnakerServerException (e.g. via + * newInstance). + */ + public SpinnakerServerException(String message, SpinnakerServerException cause) { + super(message, cause); + this.url = cause.getUrl(); + } @Override public SpinnakerServerException newInstance(String message) { diff --git a/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/Retrofit2SyncCallTest.java b/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/Retrofit2SyncCallTest.java index c114ea97d..b622734d0 100644 --- a/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/Retrofit2SyncCallTest.java +++ b/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/Retrofit2SyncCallTest.java @@ -16,38 +16,45 @@ package com.netflix.spinnaker.kork.retrofit.exceptions; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowableOfType; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.netflix.spinnaker.kork.retrofit.Retrofit2SyncCall; import java.io.IOException; +import okhttp3.HttpUrl; +import okhttp3.Request; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import retrofit2.Call; import retrofit2.Response; -public class Retrofit2SyncCallTest { +class Retrofit2SyncCallTest { @Test - public void testExecuteSuccss() throws IOException { - Call mockcall = Mockito.mock(Call.class); - when(mockcall.execute()).thenReturn(Response.success("testing")); - String execute = Retrofit2SyncCall.execute(mockcall); - assertEquals("testing", execute); + void testExecuteSuccess() throws IOException { + Call mockCall = mock(Call.class); + String responseBody = "testing"; + when(mockCall.execute()).thenReturn(Response.success(responseBody)); + String execute = Retrofit2SyncCall.execute(mockCall); + assertThat(execute).isEqualTo(responseBody); } @Test - public void testExecuteThrowException() throws IOException { - Call mockcall = Mockito.mock(Call.class); + void testExecuteThrowException() throws IOException { + Call mockCall = mock(Call.class); IOException ioException = new IOException("exception test"); - when(mockcall.execute()).thenThrow(ioException); - SpinnakerNetworkException networkEx = - assertThrows( - SpinnakerNetworkException.class, - () -> { - Retrofit2SyncCall.execute(mockcall); - }); - assertEquals(ioException, networkEx.getCause()); + when(mockCall.execute()).thenThrow(ioException); + + HttpUrl url = HttpUrl.parse("http://arbitrary-url"); + Request mockRequest = mock(Request.class); + when(mockCall.request()).thenReturn(mockRequest); + when(mockRequest.url()).thenReturn(url); + + SpinnakerNetworkException thrown = + catchThrowableOfType( + () -> Retrofit2SyncCall.execute(mockCall), SpinnakerNetworkException.class); + assertThat(thrown).hasCause(ioException); + assertThat(thrown.getUrl()).isEqualTo(url.toString()); } } diff --git a/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerHttpExceptionTest.java b/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerHttpExceptionTest.java index 9d2c4774b..bfe9c4b7b 100644 --- a/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerHttpExceptionTest.java +++ b/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerHttpExceptionTest.java @@ -19,9 +19,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; import com.google.gson.Gson; import com.netflix.spinnaker.kork.exceptions.SpinnakerException; @@ -39,11 +36,11 @@ import retrofit2.Retrofit; import retrofit2.converter.jackson.JacksonConverterFactory; -public class SpinnakerHttpExceptionTest { +class SpinnakerHttpExceptionTest { private static final String CUSTOM_MESSAGE = "custom message"; @Test - public void testSpinnakerHttpExceptionFromRetrofitError() { + void testSpinnakerHttpExceptionFromRetrofitError() { String url = "http://localhost"; int statusCode = 200; String message = "arbitrary message"; @@ -69,7 +66,7 @@ public void testSpinnakerHttpExceptionFromRetrofitError() { } @Test - public void testSpinnakerHttpExceptionFromRetrofitException() { + void testSpinnakerHttpExceptionFromRetrofitException() { final String validJsonResponseBodyString = "{\"name\":\"test\"}"; ResponseBody responseBody = ResponseBody.create( @@ -86,19 +83,19 @@ public void testSpinnakerHttpExceptionFromRetrofitException() { assertThat(retrofit2Service.baseUrl().toString()).isEqualTo(url); SpinnakerHttpException notFoundException = new SpinnakerHttpException(response, retrofit2Service); - assertNotNull(notFoundException.getResponseBody()); + assertThat(notFoundException.getResponseBody()).isNotNull(); assertThat(notFoundException.getUrl()).isEqualTo(url); assertThat(notFoundException.getReason()) .isEqualTo("Response.error()"); // set by Response.error Map errorResponseBody = notFoundException.getResponseBody(); - assertEquals(errorResponseBody.get("name"), "test"); - assertEquals(HttpStatus.NOT_FOUND.value(), notFoundException.getResponseCode()); - assertTrue( - notFoundException.getMessage().contains(String.valueOf(HttpStatus.NOT_FOUND.value()))); + assertThat(errorResponseBody.get("name")).isEqualTo("test"); + assertThat(notFoundException.getResponseCode()).isEqualTo(HttpStatus.NOT_FOUND.value()); + assertThat(notFoundException) + .hasMessageContaining(String.valueOf(HttpStatus.NOT_FOUND.value())); } @Test - public void testSpinnakerHttpException_NewInstance() { + void testSpinnakerHttpException_NewInstance() { String url = "http://localhost"; String reason = "reason"; Response response = new Response(url, 200, reason, List.of(), null); @@ -108,10 +105,11 @@ public void testSpinnakerHttpException_NewInstance() { } catch (SpinnakerException e) { SpinnakerException newException = e.newInstance(CUSTOM_MESSAGE); - assertTrue(newException instanceof SpinnakerHttpException); - assertEquals(CUSTOM_MESSAGE, newException.getMessage()); - assertEquals(e, newException.getCause()); - assertEquals(response.getStatus(), ((SpinnakerHttpException) newException).getResponseCode()); + assertThat(newException).isInstanceOf(SpinnakerHttpException.class); + assertThat(newException).hasMessage(CUSTOM_MESSAGE); + assertThat(newException).hasCause(e); + assertThat(((SpinnakerHttpException) newException).getResponseCode()) + .isEqualTo(response.getStatus()); SpinnakerHttpException spinnakerHttpException = (SpinnakerHttpException) newException; assertThat(spinnakerHttpException.getUrl()).isEqualTo(url); assertThat(spinnakerHttpException.getReason()).isEqualTo(reason); diff --git a/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofit2ErrorHandleTest.java b/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofit2ErrorHandleTest.java index 2da42fb8b..eb2101e1e 100644 --- a/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofit2ErrorHandleTest.java +++ b/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofit2ErrorHandleTest.java @@ -16,12 +16,8 @@ package com.netflix.spinnaker.kork.retrofit.exceptions; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowableOfType; import com.fasterxml.jackson.databind.ObjectMapper; import com.netflix.spinnaker.kork.retrofit.ErrorHandlingExecutorCallAdapterFactory; @@ -40,7 +36,7 @@ import retrofit2.Retrofit; import retrofit2.converter.jackson.JacksonConverterFactory; -public class SpinnakerRetrofit2ErrorHandleTest { +class SpinnakerRetrofit2ErrorHandleTest { private static Retrofit2Service retrofit2Service; @@ -51,7 +47,7 @@ public class SpinnakerRetrofit2ErrorHandleTest { private static String baseUrl = mockWebServer.url("/").toString(); @BeforeAll - public static void setupOnce() throws Exception { + static void setupOnce() throws Exception { Map responseBodyMap = new HashMap<>(); responseBodyMap.put("timestamp", "123123123123"); @@ -73,61 +69,66 @@ public static void setupOnce() throws Exception { } @AfterAll - public static void shutdownOnce() throws Exception { + static void shutdownOnce() throws Exception { mockWebServer.shutdown(); } @Test - public void testRetrofitNotFoundIsNotRetryable() { + void testRetrofitNotFoundIsNotRetryable() { mockWebServer.enqueue( new MockResponse() .setResponseCode(HttpStatus.NOT_FOUND.value()) .setBody(responseBodyString)); SpinnakerHttpException notFoundException = - assertThrows(SpinnakerHttpException.class, () -> retrofit2Service.getRetrofit2().execute()); - assertNotNull(notFoundException.getRetryable()); - assertFalse(notFoundException.getRetryable()); + catchThrowableOfType( + () -> retrofit2Service.getRetrofit2().execute(), SpinnakerHttpException.class); + assertThat(notFoundException.getRetryable()).isNotNull(); + assertThat(notFoundException.getRetryable()).isFalse(); + assertThat(notFoundException.getUrl()).isEqualTo(mockWebServer.url("/retrofit2").toString()); } @Test - public void testRetrofitBadRequestIsNotRetryable() { + void testRetrofitBadRequestIsNotRetryable() { mockWebServer.enqueue( new MockResponse() .setResponseCode(HttpStatus.NOT_FOUND.value()) .setBody(responseBodyString)); SpinnakerHttpException spinnakerHttpException = - assertThrows(SpinnakerHttpException.class, () -> retrofit2Service.getRetrofit2().execute()); - assertNotNull(spinnakerHttpException.getRetryable()); - assertFalse(spinnakerHttpException.getRetryable()); + catchThrowableOfType( + () -> retrofit2Service.getRetrofit2().execute(), SpinnakerHttpException.class); + assertThat(spinnakerHttpException.getRetryable()).isNotNull(); + assertThat(spinnakerHttpException.getRetryable()).isFalse(); + assertThat(spinnakerHttpException.getUrl()) + .isEqualTo(mockWebServer.url("/retrofit2").toString()); } @Test - public void testRetrofitOtherClientErrorHasNullRetryable() { + void testRetrofitOtherClientErrorHasNullRetryable() { mockWebServer.enqueue( new MockResponse().setResponseCode(HttpStatus.GONE.value()).setBody(responseBodyString)); SpinnakerHttpException spinnakerHttpException = - assertThrows(SpinnakerHttpException.class, () -> retrofit2Service.getRetrofit2().execute()); - assertNull(spinnakerHttpException.getRetryable()); + catchThrowableOfType( + () -> retrofit2Service.getRetrofit2().execute(), SpinnakerHttpException.class); + assertThat(spinnakerHttpException.getRetryable()).isNull(); + assertThat(spinnakerHttpException.getUrl()) + .isEqualTo(mockWebServer.url("/retrofit2").toString()); } @Test - public void testRetrofitSimpleSpinnakerNetworkException() { + void testRetrofitSimpleSpinnakerNetworkException() { mockWebServer.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE)); - - assertThrows(SpinnakerNetworkException.class, () -> retrofit2Service.getRetrofit2().execute()); - } - - @Test - public void testRetrofitSimpleSpinnakerServerException() { - mockWebServer.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START)); - assertThrows(SpinnakerServerException.class, () -> retrofit2Service.getRetrofit2().execute()); + SpinnakerNetworkException spinnakerNetworkException = + catchThrowableOfType( + () -> retrofit2Service.getRetrofit2().execute(), SpinnakerNetworkException.class); + assertThat(spinnakerNetworkException.getUrl()) + .isEqualTo(mockWebServer.url("/retrofit2").toString()); } @Test - public void testResponseHeadersInException() { + void testResponseHeadersInException() { // Check response headers are retrievable from a SpinnakerHttpException mockWebServer.enqueue( @@ -136,35 +137,35 @@ public void testResponseHeadersInException() { .setBody(responseBodyString) .setHeader("Test", "true")); SpinnakerHttpException spinnakerHttpException = - assertThrows(SpinnakerHttpException.class, () -> retrofit2Service.getRetrofit2().execute()); - assertTrue(spinnakerHttpException.getHeaders().containsKey("Test")); - assertTrue(spinnakerHttpException.getHeaders().get("Test").contains("true")); + catchThrowableOfType( + () -> retrofit2Service.getRetrofit2().execute(), SpinnakerHttpException.class); + assertThat(spinnakerHttpException.getHeaders().containsKey("Test")).isTrue(); + assertThat(spinnakerHttpException.getHeaders().get("Test").contains("true")).isTrue(); + assertThat(spinnakerHttpException.getUrl()) + .isEqualTo(mockWebServer.url("/retrofit2").toString()); } @Test - public void testNotParameterizedException() { - + void testNotParameterizedException() { IllegalArgumentException illegalArgumentException = - assertThrows( - IllegalArgumentException.class, - () -> retrofit2Service.testNotParameterized().execute()); - - assertEquals( - "Call return type must be parameterized as Call or Call", - illegalArgumentException.getCause().getMessage()); + catchThrowableOfType( + () -> retrofit2Service.testNotParameterized().execute(), + IllegalArgumentException.class); + assertThat(illegalArgumentException.getCause().getMessage()) + .isEqualTo("Call return type must be parameterized as Call or Call"); } @Test - public void testWrongReturnTypeException() { + void testWrongReturnTypeException() { IllegalArgumentException illegalArgumentException = - assertThrows( - IllegalArgumentException.class, () -> retrofit2Service.testWrongReturnType().execute()); + catchThrowableOfType( + () -> retrofit2Service.testWrongReturnType().execute(), IllegalArgumentException.class); - assertEquals( - "Unable to create call adapter for interface com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerRetrofit2ErrorHandleTest$DummyWithExecute\n" - + " for method Retrofit2Service.testWrongReturnType", - illegalArgumentException.getMessage()); + assertThat(illegalArgumentException) + .hasMessage( + "Unable to create call adapter for interface com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerRetrofit2ErrorHandleTest$DummyWithExecute\n" + + " for method Retrofit2Service.testWrongReturnType"); } interface Retrofit2Service { @@ -189,14 +190,17 @@ void testSpinnakerConversionException() { .setBody(invalidJsonTypeResponseBody)); SpinnakerConversionException spinnakerConversionException = - assertThrows( - SpinnakerConversionException.class, () -> retrofit2Service.getRetrofit2().execute()); - - assertEquals("Failed to process response body", spinnakerConversionException.getMessage()); + catchThrowableOfType( + () -> retrofit2Service.getRetrofit2().execute(), SpinnakerConversionException.class); + assertThat(spinnakerConversionException.getRetryable()).isNotNull(); + assertThat(spinnakerConversionException.getRetryable()).isFalse(); + assertThat(spinnakerConversionException).hasMessage("Failed to process response body"); + assertThat(spinnakerConversionException.getUrl()) + .isEqualTo(mockWebServer.url("/retrofit2").toString()); } @Test - public void testNonJsonHttpErrorResponse() { + void testNonJsonHttpErrorResponse() { String invalidJsonTypeResponseBody = "{'errorResponse': 'Failure'"; int responseCode = HttpStatus.INTERNAL_SERVER_ERROR.value(); @@ -206,14 +210,14 @@ public void testNonJsonHttpErrorResponse() { mockWebServer.enqueue( new MockResponse().setResponseCode(responseCode).setBody(invalidJsonTypeResponseBody)); SpinnakerHttpException spinnakerHttpException = - assertThrows(SpinnakerHttpException.class, () -> retrofit2Service.getRetrofit2().execute()); - assertNull(spinnakerHttpException.getResponseBody()); - assertEquals(responseCode, spinnakerHttpException.getResponseCode()); - assertEquals( - "Status: " + responseCode + ", URL: " + url + ", Message: " + reason, - spinnakerHttpException.getMessage()); - assertEquals(url, spinnakerHttpException.getUrl()); - assertEquals(reason, spinnakerHttpException.getReason()); + catchThrowableOfType( + () -> retrofit2Service.getRetrofit2().execute(), SpinnakerHttpException.class); + assertThat(spinnakerHttpException.getResponseBody()).isNull(); + assertThat(spinnakerHttpException.getResponseCode()).isEqualTo(responseCode); + assertThat(spinnakerHttpException) + .hasMessage("Status: " + responseCode + ", URL: " + url + ", Message: " + reason); + assertThat(spinnakerHttpException.getUrl()).isEqualTo(url); + assertThat(spinnakerHttpException.getReason()).isEqualTo(reason); } interface DummyWithExecute { diff --git a/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofitErrorHandlerTest.java b/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofitErrorHandlerTest.java index 405c010b4..2f5556183 100644 --- a/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofitErrorHandlerTest.java +++ b/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofitErrorHandlerTest.java @@ -16,12 +16,9 @@ package com.netflix.spinnaker.kork.retrofit.exceptions; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.catchThrowableOfType; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; @@ -30,6 +27,7 @@ import java.util.Map; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.SocketPolicy; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -42,14 +40,14 @@ import retrofit.converter.JacksonConverter; import retrofit.http.GET; -public class SpinnakerRetrofitErrorHandlerTest { +class SpinnakerRetrofitErrorHandlerTest { private static RetrofitService retrofitService; private static final MockWebServer mockWebServer = new MockWebServer(); @BeforeAll - public static void setupOnce() throws Exception { + static void setupOnce() throws Exception { mockWebServer.start(); retrofitService = @@ -61,17 +59,24 @@ public static void setupOnce() throws Exception { } @AfterAll - public static void shutdownOnce() throws Exception { + static void shutdownOnce() throws Exception { mockWebServer.shutdown(); } @Test - public void testNotFoundIsNotRetryable() { + void testNotFoundIsNotRetryable() { mockWebServer.enqueue(new MockResponse().setResponseCode(HttpStatus.NOT_FOUND.value())); SpinnakerHttpException notFoundException = - assertThrows(SpinnakerHttpException.class, () -> retrofitService.getFoo()); - assertNotNull(notFoundException.getRetryable()); - assertFalse(notFoundException.getRetryable()); + catchThrowableOfType(() -> retrofitService.getFoo(), SpinnakerHttpException.class); + assertThat(notFoundException.getRetryable()).isNotNull(); + assertThat(notFoundException.getRetryable()).isFalse(); + } + + @Test + void testSpinnakerNetworkException() { + mockWebServer.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE)); + assertThatExceptionOfType(SpinnakerNetworkException.class) + .isThrownBy(() -> retrofitService.getFoo()); } @ParameterizedTest(name = "Deserialize response using {0}") @@ -88,7 +93,7 @@ public void testNotFoundIsNotRetryable() { // is set when building out the RestAdapter @ValueSource( strings = {"Default_GSONConverter", "JacksonConverter", "JacksonConverterWithObjectMapper"}) - public void testResponseWithExtraField(String retrofitConverter) throws Exception { + void testResponseWithExtraField(String retrofitConverter) throws Exception { Map responseBodyMap = new HashMap<>(); responseBodyMap.put("timestamp", "123123123123"); responseBodyMap.put("message", "Not Found error Message"); @@ -131,82 +136,97 @@ public void testResponseWithExtraField(String retrofitConverter) throws Exceptio // "..." // // so make sure we get a SpinnakerHttpException from calling getFoo - assertThrows(SpinnakerHttpException.class, retrofitServiceTestConverter::getFoo); + SpinnakerHttpException spinnakerHttpException = + catchThrowableOfType(retrofitServiceTestConverter::getFoo, SpinnakerHttpException.class); + assertThat(spinnakerHttpException.getUrl()).isEqualTo(mockWebServer.url("/foo").toString()); } @Test - public void testBadRequestIsNotRetryable() { + void testBadRequestIsNotRetryable() { mockWebServer.enqueue(new MockResponse().setResponseCode(HttpStatus.BAD_REQUEST.value())); SpinnakerHttpException spinnakerHttpException = - assertThrows(SpinnakerHttpException.class, () -> retrofitService.getFoo()); - assertNotNull(spinnakerHttpException.getRetryable()); - assertFalse(spinnakerHttpException.getRetryable()); + catchThrowableOfType(() -> retrofitService.getFoo(), SpinnakerHttpException.class); + assertThat(spinnakerHttpException.getRetryable()).isNotNull(); + assertThat(spinnakerHttpException.getRetryable()).isFalse(); + assertThat(spinnakerHttpException.getUrl()).isEqualTo(mockWebServer.url("/foo").toString()); } @Test - public void testOtherClientErrorHasNullRetryable() { + void testOtherClientErrorHasNullRetryable() { // Arbitrarily choose GONE as an example of a client (e.g. 4xx) error that // we expect to have null retryable mockWebServer.enqueue(new MockResponse().setResponseCode(HttpStatus.GONE.value())); SpinnakerHttpException spinnakerHttpException = - assertThrows(SpinnakerHttpException.class, () -> retrofitService.getFoo()); - assertNull(spinnakerHttpException.getRetryable()); + catchThrowableOfType(() -> retrofitService.getFoo(), SpinnakerHttpException.class); + assertThat(spinnakerHttpException.getRetryable()).isNull(); + assertThat(spinnakerHttpException.getUrl()).isEqualTo(mockWebServer.url("/foo").toString()); } @Test - public void testResponseHeadersInException() { + void testResponseHeadersInException() { // Check response headers are retrievable from a SpinnakerHttpException mockWebServer.enqueue( new MockResponse() .setResponseCode(HttpStatus.BAD_REQUEST.value()) .setHeader("Test", "true")); SpinnakerHttpException spinnakerHttpException = - assertThrows(SpinnakerHttpException.class, () -> retrofitService.getFoo()); - assertTrue(spinnakerHttpException.getHeaders().containsKey("Test")); - assertTrue(spinnakerHttpException.getHeaders().get("Test").contains("true")); + catchThrowableOfType(() -> retrofitService.getFoo(), SpinnakerHttpException.class); + assertThat(spinnakerHttpException.getHeaders().containsKey("Test")).isTrue(); + assertThat(spinnakerHttpException.getHeaders().get("Test").contains("true")).isTrue(); + assertThat(spinnakerHttpException.getUrl()).isEqualTo(mockWebServer.url("/foo").toString()); } @Test - public void testSimpleSpinnakerNetworkException() { + void testSimpleSpinnakerNetworkException() { String message = "my custom message"; IOException e = new IOException(message); - RetrofitError retrofitError = RetrofitError.networkError("http://localhost", e); + String url = "http://localhost"; + RetrofitError retrofitError = RetrofitError.networkError(url, e); SpinnakerRetrofitErrorHandler handler = SpinnakerRetrofitErrorHandler.getInstance(); Throwable throwable = handler.handleError(retrofitError); - assertEquals(message, throwable.getMessage()); + assertThat(throwable).hasMessage(message); + assertThat(throwable).isInstanceOf(SpinnakerNetworkException.class); + SpinnakerNetworkException spinnakerNetworkException = (SpinnakerNetworkException) throwable; + assertThat(spinnakerNetworkException.getUrl()).isEqualTo(url); } @Test - public void testSpinnakerConversionException() { + void testSpinnakerConversionException() { mockWebServer.enqueue( new MockResponse().setBody("Invalid JSON response").setResponseCode(HttpStatus.OK.value())); SpinnakerConversionException spinnakerConversionException = - assertThrows(SpinnakerConversionException.class, () -> retrofitService.getData()); - assertTrue( - spinnakerConversionException - .getMessage() - .contains("Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $")); + catchThrowableOfType(() -> retrofitService.getData(), SpinnakerConversionException.class); + assertThat(spinnakerConversionException.getRetryable()).isNotNull(); + assertThat(spinnakerConversionException.getRetryable()).isFalse(); + assertThat(spinnakerConversionException) + .hasMessageContaining("Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $"); + assertThat(spinnakerConversionException.getUrl()) + .isEqualTo(mockWebServer.url("/data").toString()); } @Test - public void testChainSpinnakerException_SpinnakerNetworkException() { + void testChainSpinnakerException_SpinnakerNetworkException() { SpinnakerRetrofitErrorHandler handler = SpinnakerRetrofitErrorHandler.getInstance(); String originalMessage = "original message"; String newMessage = "new message"; IOException originalException = new IOException(originalMessage); - RetrofitError retrofitError = RetrofitError.networkError("http://localhost", originalException); + + String url = "http://localhost"; + RetrofitError retrofitError = RetrofitError.networkError(url, originalException); Throwable newException = handler.handleError( retrofitError, (exception) -> String.format("%s: %s", newMessage, exception.getMessage())); - assertTrue(newException instanceof SpinnakerNetworkException); - assertEquals("new message: original message", newException.getMessage()); - assertEquals(originalMessage, newException.getCause().getMessage()); + assertThat(newException).isInstanceOf(SpinnakerNetworkException.class); + assertThat(newException).hasMessage("new message: original message"); + assertThat(newException.getCause()).hasMessage(originalMessage); + SpinnakerNetworkException spinnakerNetworkException = (SpinnakerNetworkException) newException; + assertThat(spinnakerNetworkException.getUrl()).isEqualTo(url); } interface RetrofitService { diff --git a/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofitExceptionHandlersTest.java b/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofitExceptionHandlersTest.java index cb342b20e..93bbc2ad5 100644 --- a/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofitExceptionHandlersTest.java +++ b/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerRetrofitExceptionHandlersTest.java @@ -16,7 +16,7 @@ package com.netflix.spinnaker.kork.retrofit.exceptions; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -74,7 +74,7 @@ class SpinnakerRetrofitExceptionHandlersTest { private MemoryAppender memoryAppender; @BeforeEach - private void setup(TestInfo testInfo) { + void setup(TestInfo testInfo) { System.out.println("--------------- Test " + testInfo.getDisplayName()); memoryAppender = new MemoryAppender(SpinnakerRetrofitExceptionHandlers.class); } @@ -84,8 +84,8 @@ void testSpinnakerServerException() throws Exception { URI uri = getUri("/spinnakerServerException"); ResponseEntity entity = restTemplate.getForEntity(uri, String.class); - assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); - assertEquals(1, memoryAppender.countEventsForLevel(Level.ERROR)); + assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); + assertThat(memoryAppender.countEventsForLevel(Level.ERROR)).isEqualTo(1); } @Test @@ -93,11 +93,11 @@ void testChainedSpinnakerServerException() throws Exception { URI uri = getUri("/chainedSpinnakerServerException"); ResponseEntity entity = restTemplate.getForEntity(uri, String.class); - assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); - assertEquals(1, memoryAppender.countEventsForLevel(Level.ERROR)); + assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); + assertThat(memoryAppender.countEventsForLevel(Level.ERROR)).isEqualTo(1); // Make sure the message is what we expect. - assertEquals(1, memoryAppender.search(CUSTOM_MESSAGE, Level.ERROR).size()); + assertThat(memoryAppender.search(CUSTOM_MESSAGE, Level.ERROR)).hasSize(1); } @ParameterizedTest(name = "testSpinnakerHttpException status = {0}") @@ -106,15 +106,15 @@ void testSpinnakerHttpException(int status) throws Exception { URI uri = getUri("/spinnakerHttpException/" + String.valueOf(status)); ResponseEntity entity = restTemplate.getForEntity(uri, String.class); - assertEquals(status, entity.getStatusCode().value()); + assertThat(entity.getStatusCode().value()).isEqualTo(status); // Only expect error logging for a server error, debug otherwise. No need // to fill up logs with client errors assuming the server is doing the best // it can. - assertEquals( - 1, - memoryAppender.countEventsForLevel( - HttpStatus.resolve(status).is5xxServerError() ? Level.ERROR : Level.DEBUG)); + assertThat( + memoryAppender.countEventsForLevel( + HttpStatus.resolve(status).is5xxServerError() ? Level.ERROR : Level.DEBUG)) + .isEqualTo(1); } @ParameterizedTest(name = "testChainedSpinnakerHttpException status = {0}") @@ -123,24 +123,22 @@ void testChainedSpinnakerHttpException(int status) throws Exception { URI uri = getUri("/chainedSpinnakerHttpException/" + String.valueOf(status)); ResponseEntity entity = restTemplate.getForEntity(uri, String.class); - assertEquals(status, entity.getStatusCode().value()); + assertThat(entity.getStatusCode().value()).isEqualTo(status); // Only expect error logging for a server error, debug otherwise. No need // to fill up logs with client errors assuming the server is doing the best // it can. - assertEquals( - 1, - memoryAppender.countEventsForLevel( - HttpStatus.resolve(status).is5xxServerError() ? Level.ERROR : Level.DEBUG)); + assertThat( + memoryAppender.countEventsForLevel( + HttpStatus.resolve(status).is5xxServerError() ? Level.ERROR : Level.DEBUG)) + .isEqualTo(1); // Make sure the message is what we expect. - assertEquals( - 1, - memoryAppender - .search( + assertThat( + memoryAppender.search( CUSTOM_MESSAGE, - HttpStatus.resolve(status).is5xxServerError() ? Level.ERROR : Level.DEBUG) - .size()); + HttpStatus.resolve(status).is5xxServerError() ? Level.ERROR : Level.DEBUG)) + .hasSize(1); } private URI getUri(String path) { diff --git a/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerServerExceptionTest.java b/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerServerExceptionTest.java index 1292eed0a..6c5680ff1 100644 --- a/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerServerExceptionTest.java +++ b/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerServerExceptionTest.java @@ -16,13 +16,13 @@ package com.netflix.spinnaker.kork.retrofit.exceptions; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import com.google.gson.Gson; import com.netflix.spinnaker.kork.exceptions.SpinnakerException; import java.io.IOException; import java.util.List; +import okhttp3.Request; import org.junit.jupiter.api.Test; import retrofit.RetrofitError; import retrofit.client.Response; @@ -30,41 +30,68 @@ import retrofit.converter.GsonConverter; import retrofit.mime.TypedByteArray; -public class SpinnakerServerExceptionTest { +class SpinnakerServerExceptionTest { private static final String CUSTOM_MESSAGE = "custom message"; @Test - public void testSpinnakerNetworkException_NewInstance() { + void testSpinnakerNetworkExceptionWithUrl() { + Throwable cause = new Throwable("arbitrary message"); + String url = "http://some-url/"; + Request request = new Request.Builder().url(url).build(); + SpinnakerNetworkException spinnakerNetworkException = + new SpinnakerNetworkException(cause, request); + assertThat(spinnakerNetworkException.getUrl()).isEqualTo(url); + } + + @Test + void testSpinnakerNetworkException_NewInstance() { IOException initialException = new IOException("message"); + String url = "http://localhost"; try { - RetrofitError error = RetrofitError.networkError("http://localhost", initialException); + RetrofitError error = RetrofitError.networkError(url, initialException); throw new SpinnakerNetworkException(error); } catch (SpinnakerException e) { SpinnakerException newException = e.newInstance(CUSTOM_MESSAGE); - assertTrue(newException instanceof SpinnakerNetworkException); - assertEquals(CUSTOM_MESSAGE, newException.getMessage()); - assertEquals(e, newException.getCause()); + assertThat(newException).isInstanceOf(SpinnakerNetworkException.class); + assertThat(newException).hasMessage(CUSTOM_MESSAGE); + assertThat(newException).hasCause(e); + SpinnakerNetworkException spinnakerNetworkException = + (SpinnakerNetworkException) newException; + assertThat(spinnakerNetworkException.getUrl()).isEqualTo(url); } } @Test - public void testSpinnakerServerException_NewInstance() { + void testSpinnakerServerExceptionWithUrl() { + Throwable cause = new Throwable("arbitrary message"); + String url = "http://some-url/"; + Request request = new Request.Builder().url(url).build(); + SpinnakerServerException spinnakerServerException = + new SpinnakerServerException(cause, request); + assertThat(spinnakerServerException.getUrl()).isEqualTo(url); + } + + @Test + void testSpinnakerServerException_NewInstance() { Throwable cause = new Throwable("message"); + String url = "http://localhost"; try { - RetrofitError error = RetrofitError.unexpectedError("http://localhost", cause); + RetrofitError error = RetrofitError.unexpectedError(url, cause); throw new SpinnakerServerException(error); } catch (SpinnakerException e) { SpinnakerException newException = e.newInstance(CUSTOM_MESSAGE); - assertTrue(newException instanceof SpinnakerServerException); - assertEquals(CUSTOM_MESSAGE, newException.getMessage()); - assertEquals(e, newException.getCause()); + assertThat(newException).isInstanceOf(SpinnakerServerException.class); + assertThat(newException).hasMessage(CUSTOM_MESSAGE); + assertThat(newException).hasCause(e); + SpinnakerServerException spinnakerServerException = (SpinnakerServerException) newException; + assertThat(spinnakerServerException.getUrl()).isEqualTo(url); } } @Test - public void testSpinnakerConversionException_NewInstance() { + void testSpinnakerConversionException_NewInstance() { String url = "http://localhost"; String reason = "reason"; @@ -82,13 +109,18 @@ public void testSpinnakerConversionException_NewInstance() { RetrofitError retrofitError = RetrofitError.conversionError( url, response, new GsonConverter(new Gson()), null, conversionException); - throw new SpinnakerConversionException(retrofitError.getMessage(), retrofitError.getCause()); + throw new SpinnakerConversionException(retrofitError); } catch (SpinnakerException e) { SpinnakerException newException = e.newInstance(CUSTOM_MESSAGE); - assertTrue(newException instanceof SpinnakerConversionException); - assertEquals(CUSTOM_MESSAGE, newException.getMessage()); - assertEquals(e, newException.getCause()); + assertThat(newException).isInstanceOf(SpinnakerConversionException.class); + assertThat(newException).hasMessage(CUSTOM_MESSAGE); + assertThat(newException).hasCause(e); + SpinnakerConversionException spinnakerConversionException = + (SpinnakerConversionException) newException; + assertThat(spinnakerConversionException.getRetryable()).isNotNull(); + assertThat(spinnakerConversionException.getRetryable()).isFalse(); + assertThat(spinnakerConversionException.getUrl()).isEqualTo(url); } } }