-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "kork(retrofit): reverted tests to original, to be fixed in fu…
…rther changes" This reverts commit bf4c066.
- Loading branch information
1 parent
ab9230c
commit ea5a629
Showing
2 changed files
with
73 additions
and
34 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
.../test/java/com/netflix/spinnaker/kork/retrofit/exceptions/SpinnakerHttpExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters