Skip to content

Commit

Permalink
refactor(retrofit/test): moved http tests to new SpinnakerHttpExcepti…
Browse files Browse the repository at this point in the history
…onTest class (#1085)

* test(retrofit): moved http tests to new SpinnakerHttpExceptionTest class

* refactor(retrofit/test): moved testSpinnakerHttpException_NewInstance to SpinnakerHttpException class
  • Loading branch information
SheetalAtre authored Aug 9, 2023
1 parent 032fc2d commit 6d1a84b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -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<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);

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 @@ -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");
Expand Down Expand Up @@ -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<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())));
}
}

0 comments on commit 6d1a84b

Please sign in to comment.