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 new file mode 100644 index 000000000..76b0582ec --- /dev/null +++ b/kork-retrofit/src/test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerHttpExceptionTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2023 OpsMx, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.netflix.spinnaker.kork.retrofit.exceptions; + +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.netflix.spinnaker.kork.exceptions.SpinnakerException; +import java.util.List; +import java.util.Map; +import okhttp3.MediaType; +import okhttp3.ResponseBody; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpStatus; +import retrofit.RetrofitError; +import retrofit.client.Response; +import retrofit2.Retrofit; +import retrofit2.converter.jackson.JacksonConverterFactory; + +public class SpinnakerHttpExceptionTest { + private static final String CUSTOM_MESSAGE = "custom message"; + + @Test + public void testSpinnakerHttpExceptionFromRetrofitException() { + final String validJsonResponseBodyString = "{\"name\":\"test\"}"; + ResponseBody responseBody = + ResponseBody.create( + MediaType.parse("application/json" + "; charset=utf-8"), validJsonResponseBodyString); + retrofit2.Response response = + retrofit2.Response.error(HttpStatus.NOT_FOUND.value(), responseBody); + + Retrofit retrofit2Service = + new Retrofit.Builder() + .baseUrl("http://localhost") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + RetrofitException retrofitException = RetrofitException.httpError(response, retrofit2Service); + SpinnakerHttpException notFoundException = new SpinnakerHttpException(retrofitException); + assertNotNull(notFoundException.getResponseBody()); + 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()))); + } + + @Test + public void testSpinnakerHttpException_NewInstance() { + Response response = new Response("http://localhost", 200, "reason", List.of(), null); + try { + RetrofitError error = RetrofitError.httpError("http://localhost", response, null, null); + throw new SpinnakerHttpException(error); + } 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()); + } + } +} 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 eb5386a2a..a9b764a93 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 @@ -17,41 +17,16 @@ package com.netflix.spinnaker.kork.retrofit.exceptions; 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.netflix.spinnaker.kork.exceptions.SpinnakerException; import java.io.IOException; -import java.util.List; -import java.util.Map; -import okhttp3.MediaType; -import okhttp3.ResponseBody; import org.junit.jupiter.api.Test; -import org.springframework.http.HttpStatus; import retrofit.RetrofitError; -import retrofit.client.Response; -import retrofit2.Retrofit; -import retrofit2.converter.jackson.JacksonConverterFactory; public class SpinnakerServerExceptionTest { private static final String CUSTOM_MESSAGE = "custom message"; - @Test - public void testSpinnakerHttpException_NewInstance() { - Response response = new Response("http://localhost", 200, "reason", List.of(), null); - try { - RetrofitError error = RetrofitError.httpError("http://localhost", response, null, null); - throw new SpinnakerHttpException(error); - } 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()); - } - } - @Test public void testSpinnakerNetworkException_NewInstance() { IOException initialException = new IOException("message"); @@ -81,28 +56,4 @@ public void testSpinnakerServerException_NewInstance() { assertEquals(e, newException.getCause()); } } - - @Test - public void testSpinnakerHttpExceptionFromRetrofitException() { - final String validJsonResponseBodyString = "{\"name\":\"test\"}"; - ResponseBody responseBody = - ResponseBody.create( - MediaType.parse("application/json" + "; charset=utf-8"), validJsonResponseBodyString); - retrofit2.Response response = - retrofit2.Response.error(HttpStatus.NOT_FOUND.value(), responseBody); - - Retrofit retrofit2Service = - new Retrofit.Builder() - .baseUrl("http://localhost") - .addConverterFactory(JacksonConverterFactory.create()) - .build(); - RetrofitException retrofitException = RetrofitException.httpError(response, retrofit2Service); - SpinnakerHttpException notFoundException = new SpinnakerHttpException(retrofitException); - assertNotNull(notFoundException.getResponseBody()); - 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()))); - } }