Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(retrofit/test): moved http tests to new SpinnakerHttpExceptionTest class #1085

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}

dbyron-sf marked this conversation as resolved.
Show resolved Hide resolved
@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())));
}
}