Skip to content

Commit

Permalink
Revert "kork(retrofit): reverted tests to original, to be fixed in fu…
Browse files Browse the repository at this point in the history
…rther changes"

This reverts commit bf4c066.
  • Loading branch information
SheetalAtre committed Aug 11, 2023
1 parent ab9230c commit ea5a629
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 java.util.Map;
import okhttp3.MediaType;
import okhttp3.ResponseBody;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

public class SpinnakerHttpExceptionTest {

@Test
public void testSpinnakerHttpException() {
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();
SpinnakerHttpException notFoundException =
new SpinnakerHttpException(response, retrofit2Service);
assertNotNull(notFoundException.getResponseBody());
Map<String, Object> errorResponseBody = notFoundException.getResponseBody();
assertEquals(errorResponseBody.get("name"), "test");
assertEquals(HttpStatus.NOT_FOUND.value(), notFoundException.getResponseCode());

// 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,14 @@
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.assertNull;
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";
Expand Down Expand Up @@ -82,30 +74,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();
SpinnakerHttpException notFoundException =
new SpinnakerHttpException(response, retrofit2Service);
assertNotNull(notFoundException.getResponseBody());
Map<String, Object> errorResponseBody = notFoundException.getResponseBody();
assertEquals(errorResponseBody.get("name"), "test");
assertEquals(HttpStatus.NOT_FOUND.value(), notFoundException.getResponseCode());
/*
TODO : handle message property in the error response body
*/
assertNull(notFoundException.getMessage());
}
}

0 comments on commit ea5a629

Please sign in to comment.