Skip to content

Commit

Permalink
refactor(retrofit/test): revert previous commit for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SheetalAtre committed Aug 11, 2023
1 parent ea5a629 commit 00fc79f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,11 @@ public String getMessage() {
}

if (retrofit2Response != null) {
// If there's no message derived from a response, get the specified message.
// It feels a little backwards to do it this way, but super.getMessage()
// always returns something whether there's a specified message or not, so
// look at getRawMessage instead.
if (getRawMessage() == null) {
return super.getMessage();
}
return String.format(
"Status: %s, URL: %s, Message: %s",
retrofit2Response.code(),
retrofit2Response.raw().request().url().toString(),
retrofit2Response.code() + " " + retrofit2Response.message());
getRawMessage());
} else {
return String.format(
"Status: %s, URL: %s, Message: %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,32 @@
* 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 testSpinnakerHttpException() {
public void testSpinnakerHttpExceptionFromRetrofitException() {
final String validJsonResponseBodyString = "{\"name\":\"test\"}";
ResponseBody responseBody =
ResponseBody.create(
Expand All @@ -49,25 +56,23 @@ public void testSpinnakerHttpException() {
Map<String, Object> 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);

// Note:
// * If error response body has property "message" then return its value:
// eg: if errorResponseBody = {"message":"test"}, then expectedMessage = "Status: 404, URL:
// http://localhost/, Message: test"
// * If error response body does not have any property "message", then return default error
// message:
// eg: if errorResponseBody = {"name":"test"}, then expectedMessage = "Status: 404, URL:
// http://localhost/, Message: 404 Response.error()"
// * TODO: If error response body is an invalid json then the converter fails to convert it and
// returns error message:
// eg: if errorResponseBody = {"name":"test",(no closing bracket at end), then expectedMessage
// ="Failed to parse response".
String expectedMessage =
String.format(
"Status: %s, URL: %s, Message: %s",
HttpStatus.NOT_FOUND.value(),
"http://localhost/",
HttpStatus.NOT_FOUND.value() + " " + "Response.error()");
assertEquals(expectedMessage, notFoundException.getMessage());
assertTrue(newException instanceof SpinnakerHttpException);
assertEquals(CUSTOM_MESSAGE, newException.getMessage());
assertEquals(e, newException.getCause());
assertEquals(response.getStatus(), ((SpinnakerHttpException) newException).getResponseCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,12 @@

import com.netflix.spinnaker.kork.exceptions.SpinnakerException;
import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.Test;
import retrofit.RetrofitError;
import retrofit.client.Response;

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");
Expand Down

0 comments on commit 00fc79f

Please sign in to comment.