Skip to content

Commit

Permalink
test(retrofit): added testSpinnakerHttpExceptionFromRetrofitError for…
Browse files Browse the repository at this point in the history
… more test coverage
  • Loading branch information
SheetalAtre committed Aug 22, 2023
1 parent 3a76eb1 commit 8e9f599
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

package com.netflix.spinnaker.kork.retrofit.exceptions;

import static org.assertj.core.api.Assertions.assertThat;
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;
import java.util.List;
import java.util.Map;
Expand All @@ -30,12 +32,36 @@
import org.springframework.http.HttpStatus;
import retrofit.RetrofitError;
import retrofit.client.Response;
import retrofit.converter.GsonConverter;
import retrofit.mime.TypedString;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

public class SpinnakerHttpExceptionTest {
private static final String CUSTOM_MESSAGE = "custom message";

public void testSpinnakerHttpExceptionFromRetrofitError() {
String url = "http://localhost";
int statusCode = 200;
String message = "arbitrary message";
Response response =
new Response(
url,
statusCode,
"reason",
List.of(),
new TypedString("{ message: \"" + message + "\", name: \"test\" }"));
RetrofitError retrofitError =
RetrofitError.httpError(url, response, new GsonConverter(new Gson()), String.class);
SpinnakerHttpException spinnakerHttpException = new SpinnakerHttpException(retrofitError);
assertThat(spinnakerHttpException.getResponseBody()).isNotNull();
Map<String, Object> errorResponseBody = spinnakerHttpException.getResponseBody();
assertThat(errorResponseBody.get("name")).isEqualTo("test");
assertThat(spinnakerHttpException.getResponseCode()).isEqualTo(statusCode);
assertThat(spinnakerHttpException.getMessage())
.isEqualTo("Status: " + statusCode + ", URL: " + url + ", Message: " + message);
}

@Test
public void testSpinnakerHttpExceptionFromRetrofitException() {
final String validJsonResponseBodyString = "{\"name\":\"test\"}";
Expand Down

0 comments on commit 8e9f599

Please sign in to comment.